Skip to content

Commit

Permalink
RTTI: Permit undefined enum values
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Oct 15, 2023
1 parent 1f17a1c commit a10bc86
Showing 1 changed file with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Constant valueOf(int value) {
final Set<Constant> values = new HashSet<>();

for (Constant constant : constants) {
final int other = toUnsignedValue(constant);
final int other = toUnsignedValue(constant.value());

if ((value & other) != 0) {
values.add(constant);
Expand All @@ -48,20 +48,21 @@ public Constant valueOf(int value) {
}

if (value != 0) {
throw new IllegalArgumentException("No constants found that match value " + value);
throw new IllegalArgumentException("No constants found that match value " + value + " in enum " + this);
}

return new MyConstantSet(this, values);
} else {
for (Constant constant : constants) {
final int other = toUnsignedValue(constant);
final int other = toUnsignedValue(constant.value());

if (value == other) {
return constant;
}
}

throw new IllegalArgumentException("No constant found that matches value " + value);
// Some core files contain unlisted enum values. This lets parse these files successfully
return new MyConstant(this, String.valueOf(value), value);
}
}

Expand All @@ -78,7 +79,7 @@ public Constant valueOf(@NotNull String value) {
}
}

throw new IllegalArgumentException("No constant found that matches value " + value);
throw new IllegalArgumentException("No constant found that matches value " + value + " in enum " + this);
}

@Override
Expand Down Expand Up @@ -148,11 +149,11 @@ public Class<Constant> getInstanceType() {
return Constant.class;
}

private int toUnsignedValue(@NotNull Constant constant) {
private int toUnsignedValue(int value) {
return switch (size) {
case 1 -> constant.value() & 0xff;
case 2 -> constant.value() & 0xffff;
case 4 -> constant.value();
case 1 -> value & 0xff;
case 2 -> value & 0xffff;
case 4 -> value;
default -> throw new IllegalArgumentException("Unexpected enum size: " + size);
};
}
Expand Down

0 comments on commit a10bc86

Please sign in to comment.