Ask for Visitor

I wrote the visitor to be used in the class as below.
and I overridden the visitAnyAttribute and visitAnyInstruction.

public class ControlFlowObfuscator implements InstructionVisitor,
        ConstantVisitor,
        AttributeVisitor
{
    private static final Logger logger = LogManager.getLogger(ControlFlowObfuscator.class);

    public ControlFlowObfuscator()
    {

    }

    @Override
    public void visitAnyAttribute(Clazz clazz, Attribute attribute)
    {
        logger.info("hi");
    }

    @Override
    public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) {

        logger.info("visitCodeAttribute {} {}", clazz.getName(), method.getName(clazz));
    }

    @Override
    public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction)
    {
        logger.info("visit {} {} {} {}", instruction.toString(clazz, offset), clazz.getName(), method.getName(clazz), method.getDescriptor(clazz));
    }
}

And then, the pass called as follows.

        appView.programClassPool.classesAccept(
                new AllMethodVisitor(
                 new AllAttributeVisitor(
                  new AllInstructionVisitor( new ControlFlowObfuscator()
                        )));

This code ignores the visitCodeAttribute. I want to visit all Override functions.
How do I modify classesAccept code?

1 Like

Hi @ollydbg20 ,

You’ll need to pass the ControlFlowObfuscator as an AttributeVisitor:

        appView.programClassPool.classesAccept(
                new AllMethodVisitor(
                new AllAttributeVisitor(new ControlFlowObfuscator()));

In visitCodeAttribute you can visit all the instructions:

    @Override
    public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) {

        logger.info("visitCodeAttribute {} {}", clazz.getName(), method.getName(clazz));
        codeAttribute.accept(clazz, method, new AllInstructionVisitor(this));
    }
2 Likes