Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the value of a field if it is not a constant field? #489

Open
2017398956 opened this issue Sep 9, 2024 · 1 comment
Open

Comments

@2017398956
Copy link

public class TestBean {
    public static final int[] test = new int[]{1,2,3};
}

How to get the value of test? CtField.getConstantValue() return null if the field type is a primitive type.

@chibash
Copy link
Member

chibash commented Sep 29, 2024

Getting the value of test at compile time would be difficult because the above code is compiled into the bytecode equivalent to:

public class TestBean {
    public static final int[] test;
    static {
        test = new int[] { 1, 2, 3 };
    }
}

To obtain the value of test, you must statically analyze the body of this static initializer. It would be possible but Javassist does not help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants