-
Notifications
You must be signed in to change notification settings - Fork 311
Access to Enum in SwitchCase #1433
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
Comments
Enum constants in a switch statement are difficult to recognize in the byte code:
using a synthetic field in a synthetic class:
So what my compiler (I was using OpenJDK 21) generates is essentially equivalent to static final int[] SWITCH_MAP = new int[ExampleEnum.values().length];
static {
SWITCH_MAP[ExampleEnum.A.ordinal()] = 1;
SWITCH_MAP[ExampleEnum.B.ordinal()] = 2;
}
public String mapMyEnum(ExampleEnum exampleEnum) {
if (exampleEnum == ExampleEnum.C) {
return "C";
}
switch (SWITCH_MAP[exampleEnum.ordinal()]) {
case 1:
return "A";
case 2:
return "B";
default:
return "unknown";
}
} ArchUnit currently doesn't reconstruct dependencies from such a pattern in the byte code. I think it would require a quite sophisticated reverse-engineering, as I noticed that the byte code for another switch like switch (exampleEnum) {
case A:
return "A";
default:
return "unknown";
} may also use the same synthetic |
After reading your post I noticed there are several more similar issues regarding synthetic classes. @hankem thanks for pointing this out |
I am trying to analyze my codebase and find usages of an Enum
I am trying to collect all of the
ExampleEnum
usages in my classes:however
javaClass.getAccessesFromSelf()
only gives me the access ofExampleEnum.C
and ignores the switch-case access to the enumShould I be retrieving the access information in a different way in order to expose
A
andB
usage in the switch-case?The text was updated successfully, but these errors were encountered: