8341028: Do not use lambdas or method refs for verifyConstantPool

Reviewed-by: adinn, liach
This commit is contained in:
David M. Lloyd 2024-11-27 01:03:33 +00:00 committed by Chen Liang
parent 28c8729019
commit 08c1f44eef

View File

@ -34,10 +34,9 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.stream.Collectors;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.ToIntFunction; import java.util.function.ToIntFunction;
import java.util.stream.Collectors;
import jdk.internal.classfile.impl.BoundAttribute; import jdk.internal.classfile.impl.BoundAttribute;
import jdk.internal.classfile.impl.Util; import jdk.internal.classfile.impl.Util;
@ -65,50 +64,79 @@ public record ParserVerifier(ClassModel classModel) {
private void verifyConstantPool(List<VerifyError> errors) { private void verifyConstantPool(List<VerifyError> errors) {
for (var cpe : classModel.constantPool()) { for (var cpe : classModel.constantPool()) {
Consumer<Runnable> check = c -> { try {
try { switch (cpe) {
c.run(); case DoubleEntry de -> de.doubleValue();
} catch (VerifyError|Exception e) { case FloatEntry fe -> fe.floatValue();
errors.add(new VerifyError("%s at constant pool index %d in %s".formatted(e.getMessage(), cpe.index(), toString(classModel)))); case IntegerEntry ie -> ie.intValue();
case LongEntry le -> le.longValue();
case Utf8Entry ue -> ue.stringValue();
case ConstantDynamicEntry cde -> cde.asSymbol();
case InvokeDynamicEntry ide -> ide.asSymbol();
case ClassEntry ce -> ce.asSymbol();
case StringEntry se -> se.stringValue();
case MethodHandleEntry mhe -> mhe.asSymbol();
case MethodTypeEntry mte -> mte.asSymbol();
case FieldRefEntry fre -> {
try {
fre.owner().asSymbol();
} catch (VerifyError|Exception e) {
errors.add(cpeVerifyError(cpe, e));
}
try {
fre.typeSymbol();
} catch (VerifyError|Exception e) {
errors.add(cpeVerifyError(cpe, e));
}
verifyFieldName(fre.name().stringValue());
}
case InterfaceMethodRefEntry imre -> {
try {
imre.owner().asSymbol();
} catch (VerifyError|Exception e) {
errors.add(cpeVerifyError(cpe, e));
}
try {
imre.typeSymbol();
} catch (VerifyError|Exception e) {
errors.add(cpeVerifyError(cpe, e));
}
verifyMethodName(imre.name().stringValue());
}
case MethodRefEntry mre -> {
try {
mre.owner().asSymbol();
} catch (VerifyError|Exception e) {
errors.add(cpeVerifyError(cpe, e));
}
try {
mre.typeSymbol();
} catch (VerifyError|Exception e) {
errors.add(cpeVerifyError(cpe, e));
}
verifyMethodName(mre.name().stringValue());
}
case ModuleEntry me -> me.asSymbol();
case NameAndTypeEntry nate -> {
try {
nate.name().stringValue();
} catch (VerifyError|Exception e) {
errors.add(cpeVerifyError(cpe, e));
}
nate.type().stringValue();
}
case PackageEntry pe -> pe.asSymbol();
} }
}; } catch (VerifyError|Exception e) {
check.accept(switch (cpe) { errors.add(cpeVerifyError(cpe, e));
case DoubleEntry de -> de::doubleValue; }
case FloatEntry fe -> fe::floatValue;
case IntegerEntry ie -> ie::intValue;
case LongEntry le -> le::longValue;
case Utf8Entry ue -> ue::stringValue;
case ConstantDynamicEntry cde -> cde::asSymbol;
case InvokeDynamicEntry ide -> ide::asSymbol;
case ClassEntry ce -> ce::asSymbol;
case StringEntry se -> se::stringValue;
case MethodHandleEntry mhe -> mhe::asSymbol;
case MethodTypeEntry mte -> mte::asSymbol;
case FieldRefEntry fre -> {
check.accept(fre.owner()::asSymbol);
check.accept(fre::typeSymbol);
yield () -> verifyFieldName(fre.name().stringValue());
}
case InterfaceMethodRefEntry imre -> {
check.accept(imre.owner()::asSymbol);
check.accept(imre::typeSymbol);
yield () -> verifyMethodName(imre.name().stringValue());
}
case MethodRefEntry mre -> {
check.accept(mre.owner()::asSymbol);
check.accept(mre::typeSymbol);
yield () -> verifyMethodName(mre.name().stringValue());
}
case ModuleEntry me -> me::asSymbol;
case NameAndTypeEntry nate -> {
check.accept(nate.name()::stringValue);
yield () -> nate.type().stringValue();
}
case PackageEntry pe -> pe::asSymbol;
});
} }
} }
private VerifyError cpeVerifyError(final PoolEntry cpe, final Throwable e) {
return new VerifyError("%s at constant pool index %d in %s".formatted(e.getMessage(), cpe.index(), toString(classModel)));
}
private void verifyFieldName(String name) { private void verifyFieldName(String name) {
if (name.length() == 0 || name.chars().anyMatch(ch -> switch(ch) { if (name.length() == 0 || name.chars().anyMatch(ch -> switch(ch) {
case '.', ';', '[', '/' -> true; case '.', ';', '[', '/' -> true;