[ProGuard-Core] MemberVisitor.visitProgramField. -> How get the initial Value of a String field?

Hello everybody,

we use reflection at different points in our legacy application. Therefore I would like to search for classname string values in our jar with proguard-core.

For method variables the partialEvaluator in your VisualizeControlFlow example is helpful and delivers a satisfactory result.

But I don’t find out, how I get the String values of fields and constants.
Is it possible the find out the default value? e.g. from proguard.classfile.ProgramField or proguard.classfile.ConstantValueAttribute

Kind Regards
André

Hi André!

If I understand correctly, you have something like this:

public class Foo {
     public static final String = "string";
}

You can construct a chain of visitors to get to the string in the constant value attribute like this:

public class MyVisitor implements MemberVisitor, AttributeVisitor, ConstantVisitor {
    public void visitAnyMember(Clazz clazz, Member member) {}
    public void visitProgramField(ProgramClass clazz, ProgramField field) {
         field.attributesAccept(clazz, this);
    }

    public void visitAnyAttribute(Clazz clazz, Attribute attribute) { }
    public void visitConstantValueAttribute(Clazz clazz, Attribute attribute, ConstantValueAttribute attribute) {
        clazz.constantPoolEntryAccept(attribute.u2constantValueIndex, this);
    }

    public void visitAnyConstant(Clazz clazz, Constant constant) { }
    public void visitStringConstant(Clazz clazz, StringConstant stringConstant) {
        clazz.constantPoolEntryAccept(stringConstant.u2stringIndex, this);
    }

    public void visitUtf8Constant(Clazz clazz, Utf8Constant utf8Constant) {
         System.out.println(utf8Constant.getString());
    }
}

You can use it like this, for example, and it will print all the field constant strings:

clazz.fieldsAccept(new MyVisitor());
1 Like