From 380418fad07c0526bb698b4bfcbacbd65a8615be Mon Sep 17 00:00:00 2001 From: Qing Xiao Date: Mon, 7 Aug 2023 15:49:11 +0000 Subject: [PATCH] 8295058: test/langtools/tools/javac 116 test classes uses com.sun.tools.classfile library Reviewed-by: asotona --- .../classfile/ClassfileInspector.java | 566 ++++++------------ .../8009170/RedundantByteCodeInArrayTest.java | 38 +- .../javac/StringConcat/WellKnownTypes.java | 9 +- ...rClassAttrMustNotHaveStrictFPFlagTest.java | 23 +- .../EmptyUTF8ForInnerClassNameTest.java | 27 +- .../DontGenerateLVTForGNoneOpTest.java | 20 +- ...oLocalsMustBeReservedForDCEedVarsTest.java | 20 +- .../annotations/SyntheticParameters.java | 11 +- .../classfile/AnonymousClassTest.java | 193 +++--- .../classfile/ClassfileTestHelper.java | 231 +++---- .../classfile/CombinationsTargetTest1.java | 21 +- .../classfile/CombinationsTargetTest2.java | 25 +- .../classfile/CombinationsTargetTest3.java | 145 ++++- .../typeAnnotations/classfile/DeadCode.java | 22 +- .../classfile/InstanceInitializer.java | 22 +- .../classfile/NewTypeArguments.java | 21 +- .../classfile/NoTargetAnnotations.java | 124 ++-- .../typeAnnotations/classfile/Scopes.java | 15 +- .../classfile/StaticInitializer.java | 21 +- .../classfile/SyntheticParameters.java | 14 +- .../typeAnnotations/classfile/T8008762.java | 21 +- .../typeAnnotations/classfile/T8008769.java | 15 +- .../typeAnnotations/classfile/T8010015.java | 15 +- .../classfile/TestAnonInnerClasses.java | 192 ++---- .../classfile/TestNewCastArray.java | 134 ++--- .../TypeAnnotationPropagationTest.java | 55 +- .../typeAnnotations/classfile/TypeCasts.java | 21 +- .../typeAnnotations/classfile/Wildcards.java | 20 +- .../AnnotationDefaultTest.java | 43 +- .../AnnotationDefaultVerifier.java | 194 +++--- .../EnclosingMethod/EnclosingMethodTest.java | 40 +- .../LineNumberTable/LineNumberTest.java | 7 +- .../LineNumberTable/LineNumberTestBase.java | 56 +- .../MultipleRecordPatterns.java | 7 +- .../LineNumberTable/RuleSwitchBreaks.java | 7 +- .../LineNumberTable/StringSwitchBreaks.java | 7 +- .../attributes/LineNumberTable/T8050993.java | 34 +- .../LocalVariableTableTest.java | 45 +- .../LocalVariableTestBase.java | 50 +- .../LocalVariableTypeTableTest.java | 43 +- .../attributes/Module/ModuleFlagTest.java | 21 +- .../attributes/Module/ModuleTest.java | 7 +- .../attributes/Module/ModuleTestBase.java | 118 ++-- .../attributes/Signature/ConstructorTest.java | 7 +- .../attributes/Signature/Driver.java | 80 +-- .../attributes/Signature/EnumTest.java | 7 +- .../attributes/Signature/ExceptionTest.java | 7 +- .../attributes/Signature/FieldTest.java | 7 +- .../attributes/Signature/InnerClassTest.java | 7 +- .../Signature/MethodParameterTest.java | 7 +- .../Signature/MethodTypeBoundTest.java | 7 +- .../attributes/Signature/ReturnTypeTest.java | 7 +- .../SourceFile/AnonymousClassTest.java | 7 +- .../attributes/SourceFile/InnerClassTest.java | 7 +- .../attributes/SourceFile/LocalClassTest.java | 7 +- .../attributes/SourceFile/MixTest.java | 7 +- .../attributes/SourceFile/ModuleInfoTest.java | 7 +- .../SourceFile/NoSourceFileAttribute.java | 16 +- .../SourceFile/SourceFileTestBase.java | 35 +- .../SourceFile/SyntheticClassTest.java | 7 +- .../TopLevelClassesOneFileTest.java | 7 +- ...ssToPrivateInnerClassConstructorsTest.java | 7 +- .../AccessToPrivateInnerClassMembersTest.java | 7 +- .../AccessToPrivateSiblingsTest.java | 7 +- .../attributes/Synthetic/AssertFieldTest.java | 7 +- .../BridgeMethodForGenericMethodTest.java | 7 +- ...geMethodsForLambdaTargetRelease14Test.java | 7 +- .../Synthetic/BridgeMethodsForLambdaTest.java | 7 +- .../attributes/Synthetic/EnumTest.java | 7 +- .../attributes/Synthetic/PackageInfoTest.java | 7 +- .../Synthetic/SyntheticTestDriver.java | 76 +-- .../attributes/Synthetic/ThisFieldTest.java | 7 +- .../annotations/AnnotationsTestBase.java | 14 +- ...untimeAnnotationsForGenericMethodTest.java | 11 +- ...timeAnnotationsForInnerAnnotationTest.java | 7 +- .../RuntimeAnnotationsForInnerClassTest.java | 7 +- .../RuntimeAnnotationsForInnerEnumTest.java | 7 +- ...ntimeAnnotationsForInnerInterfaceTest.java | 7 +- ...untimeAnnotationsForTopLevelClassTest.java | 7 +- .../RuntimeAnnotationsTestBase.java | 88 +-- ...ameterAnnotationsForGenericMethodTest.java | 11 +- ...timeParameterAnnotationsForLambdaTest.java | 40 +- .../RuntimeParameterAnnotationsTest.java | 9 +- .../RuntimeParameterAnnotationsTestBase.java | 72 ++- .../annotations/TestAnnotationInfo.java | 195 +++--- .../deprecated/DeprecatedPackageTest.java | 18 +- .../attributes/deprecated/DeprecatedTest.java | 76 ++- ...InnerAnnotationsInInnerAnnotationTest.java | 7 +- .../InnerAnnotationsInInnerClassTest.java | 7 +- .../InnerAnnotationsInInnerEnumTest.java | 7 +- .../InnerAnnotationsInInnerInterfaceTest.java | 7 +- .../InnerClassesHierarchyTest.java | 37 +- .../InnerClassesInAnonymousClassTest.java | 7 +- .../InnerClassesInInnerAnnotationTest.java | 7 +- .../InnerClassesInInnerClassTest.java | 7 +- .../InnerClassesInInnerEnumTest.java | 7 +- .../InnerClassesInInnerInterfaceTest.java | 7 +- .../InnerClassesInLocalClassTest.java | 7 +- .../innerclasses/InnerClassesIndexTest.java | 35 +- .../innerclasses/InnerClassesTest.java | 7 +- .../innerclasses/InnerClassesTestBase.java | 65 +- .../InnerEnumInInnerAnnotationTest.java | 7 +- .../InnerEnumInInnerEnumTest.java | 7 +- .../InnerEnumInInnerInterfaceTest.java | 7 +- .../InnerEnumsInInnerClassTest.java | 7 +- .../InnerInterfacesInInnerAnnotationTest.java | 7 +- .../InnerInterfacesInInnerClassTest.java | 7 +- .../InnerInterfacesInInnerEnumTest.java | 7 +- .../InnerInterfacesInInnerInterfaceTest.java | 7 +- .../innerclasses/NoInnerClassesTest.java | 20 +- .../classfiles/attributes/lib/TestBase.java | 20 +- .../tools/javac/classreader/BadClass.java | 11 +- .../javac/constDebug/ConstDebugTest.java | 15 +- .../javac/lambda/LambdaTestStrictFPFlag.java | 28 +- .../ConditionalLineNumberTest.java | 45 +- .../tools/javac/resolve/NoObjectToString.java | 28 +- .../sealed/CheckSubtypesOfSealedTest.java | 47 +- 117 files changed, 2144 insertions(+), 2011 deletions(-) diff --git a/test/langtools/lib/annotations/annotations/classfile/ClassfileInspector.java b/test/langtools/lib/annotations/annotations/classfile/ClassfileInspector.java index 5327e869b1c..8707d48e311 100644 --- a/test/langtools/lib/annotations/annotations/classfile/ClassfileInspector.java +++ b/test/langtools/lib/annotations/annotations/classfile/ClassfileInspector.java @@ -25,10 +25,11 @@ package annotations.classfile; import java.io.*; import java.net.URL; +import java.util.ArrayList; +import java.util.List; -import com.sun.tools.classfile.*; -import com.sun.tools.classfile.ConstantPool.InvalidIndex; -import com.sun.tools.classfile.ConstantPool.UnexpectedEntry; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; /** * A class providing utilities for writing tests that inspect class @@ -326,8 +327,8 @@ public class ClassfileInspector { /** * See if this template matches the given visibility. * - * @param Whether or not the annotation is visible at runtime. - * @return Whether or not this template matches the visibility. + * @param visibility Whether the annotation is visible at runtime. + * @return Whether this template matches the visibility. */ public boolean matchVisibility(boolean visibility) { return this.visibility == visibility; @@ -340,9 +341,8 @@ public class ClassfileInspector { * * @param anno The annotation to attempt to match. */ - public void matchAnnotation(ConstantPool cpool, - Annotation anno) { - if (checkMatch(cpool, anno)) { + public void matchAnnotation(Annotation anno) { + if (checkMatch(anno)) { count++; } } @@ -351,17 +351,11 @@ public class ClassfileInspector { * Indicate whether an annotation matches this expected * annotation. * - * @param ConstantPool The constant pool to use. * @param anno The annotation to check. * @return Whether the annotation matches. */ - protected boolean checkMatch(ConstantPool cpool, - Annotation anno) { - try { - return cpool.getUTF8Info(anno.type_index).value.equals("L" + expectedName + ";"); - } catch (InvalidIndex | UnexpectedEntry e) { - return false; - } + protected boolean checkMatch(Annotation anno) { + return anno.classSymbol().descriptorString().equals("L" + expectedName + ";"); } /** @@ -532,7 +526,7 @@ public class ClassfileInspector { protected final int parameter_index; protected final int type_index; protected final int exception_index; - protected final TypeAnnotation.Position.TypePathEntry[] typePath; + protected final List typePath; /** * Create an {@code ExpectedTypeAnnotation} from its @@ -561,7 +555,7 @@ public class ClassfileInspector { int parameter_index, int type_index, int exception_index, - TypeAnnotation.Position.TypePathEntry... typePath) { + List typePath) { super(expectedName, visibility, expectedCount); this.targetType = targetType; this.bound_index = bound_index; @@ -589,19 +583,18 @@ public class ClassfileInspector { sb.append(", exception_index = "); sb.append(exception_index); sb.append(", type_path = ["); - for(int i = 0; i < typePath.length; i++) { + for(int i = 0; i < typePath.size(); i++) { if (i != 0) { sb.append(", "); } - sb.append(typePath[i]); + sb.append(typePath.get(i)); } sb.append("]"); return sb.toString(); } @Override - public void matchAnnotation(ConstantPool cpool, - Annotation anno) {} + public void matchAnnotation(Annotation anno) {} public void matchAnnotation(TypeAnnotation anno) { if (checkMatch(anno)) { @@ -610,20 +603,39 @@ public class ClassfileInspector { } public boolean checkMatch(TypeAnnotation anno) { - boolean matches = checkMatch(anno.constant_pool, anno.annotation); - - matches = matches && anno.position.type == targetType; - matches = matches && anno.position.bound_index == bound_index; - matches = matches && anno.position.parameter_index == parameter_index; - matches = matches && anno.position.type_index == type_index; - matches = matches && anno.position.exception_index == exception_index; - matches = matches && anno.position.location.size() == typePath.length; + boolean matches = checkMatch((Annotation) anno); + int boundIdx = Integer.MIN_VALUE, paraIdx = Integer.MIN_VALUE, tIdx = Integer.MIN_VALUE, exIdx = Integer.MIN_VALUE; + switch (anno.targetInfo()) { + case TypeAnnotation.TypeParameterBoundTarget binfo -> { + boundIdx = binfo.boundIndex(); + paraIdx = binfo.typeParameterIndex(); + } + case TypeAnnotation.FormalParameterTarget fpinfo -> { + paraIdx = fpinfo.formalParameterIndex(); + } + case TypeAnnotation.TypeParameterTarget pinfo -> { + paraIdx = pinfo.typeParameterIndex(); + } + case TypeAnnotation.TypeArgumentTarget ainfo -> { + tIdx = ainfo.typeArgumentIndex(); + } + case TypeAnnotation.CatchTarget cinfo -> { + exIdx = cinfo.exceptionTableIndex(); + } + default -> {} + } + matches = matches && anno.targetInfo().targetType() == targetType; + matches = matches && boundIdx == bound_index; + matches = matches && paraIdx == parameter_index; + matches = matches && tIdx == type_index; + matches = matches && exIdx == exception_index; + matches = matches && anno.targetPath().size() == typePath.size(); if (matches) { int i = 0; - for(TypeAnnotation.Position.TypePathEntry entry : - anno.position.location) { - matches = matches && typePath[i++].equals(entry); + for(TypeAnnotation.TypePathComponent entry : + anno.targetPath()) { + matches = matches && typePath.get(i++).equals(entry); } } @@ -647,8 +659,8 @@ public class ClassfileInspector { protected int parameter_index = Integer.MIN_VALUE; protected int type_index = Integer.MIN_VALUE; protected int exception_index = Integer.MIN_VALUE; - protected TypeAnnotation.Position.TypePathEntry[] typePath = - new TypeAnnotation.Position.TypePathEntry[0]; + protected List typePath = + new ArrayList(); /** * Create a {@code Builder} from the mandatory parameters. @@ -696,7 +708,7 @@ public class ClassfileInspector { /** * Provide a parameter index parameter. * - * @param bound_index The parameter_index value. + * @param parameter_index The parameter_index value. */ public Builder setParameterIndex(int parameter_index) { this.parameter_index = parameter_index; @@ -728,7 +740,7 @@ public class ClassfileInspector { * * @param typePath The type path value. */ - public Builder setTypePath(TypeAnnotation.Position.TypePathEntry[] typePath) { + public Builder setTypePath(List typePath) { this.typePath = typePath; return this; } @@ -768,7 +780,7 @@ public class ClassfileInspector { int parameter_index, int type_index, int exception_index, - TypeAnnotation.Position.TypePathEntry... typePath) { + List typePath) { super(expectedName, visibility, expectedCount, targetType, bound_index, parameter_index, type_index, exception_index, typePath); this.methodname = methodname; @@ -792,11 +804,11 @@ public class ClassfileInspector { sb.append(", exception_index = "); sb.append(exception_index); sb.append(", type_path = ["); - for(int i = 0; i < typePath.length; i++) { + for(int i = 0; i < typePath.size(); i++) { if (i != 0) { sb.append(", "); } - sb.append(typePath[i]); + sb.append(typePath.get(i)); } sb.append("]"); sb.append(" on method "); @@ -894,7 +906,7 @@ public class ClassfileInspector { int parameter_index, int type_index, int exception_index, - TypeAnnotation.Position.TypePathEntry... typePath) { + List typePath) { super(expectedName, visibility, expectedCount, targetType, bound_index, parameter_index, type_index, exception_index, typePath); this.fieldname = fieldname; @@ -913,11 +925,11 @@ public class ClassfileInspector { .append(", exception_index = ").append(exception_index) .append(", type_path = ["); - for(int i = 0; i < typePath.length; i++) { + for(int i = 0; i < typePath.size(); i++) { if (i != 0) { sb.append(", "); } - sb.append(typePath[i]); + sb.append(typePath.get(i)); } sb.append("]") .append(" on field ").append(fieldname); @@ -981,162 +993,146 @@ public class ClassfileInspector { } } - private void matchClassAnnotation(ClassFile classfile, - ExpectedAnnotation expected) - throws ConstantPoolException { - for(Attribute attr : classfile.attributes) { - attr.accept(annoMatcher(classfile.constant_pool), expected); + private void matchClassAnnotation(ClassModel classfile, + ExpectedAnnotation expected) { + for(Attribute attr : classfile.attributes()) { + annoMatcher(attr, expected); } } - private void matchMethodAnnotation(ClassFile classfile, - ExpectedMethodAnnotation expected) - throws ConstantPoolException { - for(Method meth : classfile.methods) { - if (expected.matchMethodName(meth.getName(classfile.constant_pool))) { - for(Attribute attr : meth.attributes) { - attr.accept(annoMatcher(classfile.constant_pool), expected); + private void matchMethodAnnotation(ClassModel classfile, + ExpectedMethodAnnotation expected) { + for(MethodModel meth : classfile.methods()) { + if (expected.matchMethodName(meth.methodName().stringValue())) { + for(Attribute attr : meth.attributes()) { + annoMatcher(attr, expected); } } } } - private void matchParameterAnnotation(ClassFile classfile, - ExpectedParameterAnnotation expected) - throws ConstantPoolException { - for(Method meth : classfile.methods) { - if (expected.matchMethodName(meth.getName(classfile.constant_pool))) { - for(Attribute attr : meth.attributes) { - attr.accept(paramMatcher(classfile.constant_pool), expected); + private void matchParameterAnnotation(ClassModel classfile, + ExpectedParameterAnnotation expected) { + for(MethodModel meth : classfile.methods()) { + if (expected.matchMethodName(meth.methodName().stringValue())) { + for(Attribute attr : meth.attributes()) { + paramMatcher(attr, expected); } } } } - private void matchFieldAnnotation(ClassFile classfile, - ExpectedFieldAnnotation expected) - throws ConstantPoolException { - for(Field field : classfile.fields) { - if (expected.matchFieldName(field.getName(classfile.constant_pool))) { - for(Attribute attr : field.attributes) { - attr.accept(annoMatcher(classfile.constant_pool), expected); + private void matchFieldAnnotation(ClassModel classfile, + ExpectedFieldAnnotation expected) { + for(FieldModel field : classfile.fields()) { + if (expected.matchFieldName(field.fieldName().stringValue())) { + for(Attribute attr : field.attributes()) { + annoMatcher(attr, expected); } } } } - private void matchClassTypeAnnotation(ClassFile classfile, - ExpectedTypeAnnotation expected) - throws ConstantPoolException { - for(Attribute attr : classfile.attributes) { - attr.accept(typeAnnoMatcher, expected); + private void matchClassTypeAnnotation(ClassModel classfile, + ExpectedTypeAnnotation expected) { + for(Attribute attr : classfile.attributes()) { + typeAnnoMatcher(attr, expected); } } - private void matchMethodTypeAnnotation(ClassFile classfile, - ExpectedMethodTypeAnnotation expected) - throws ConstantPoolException { - for(Method meth : classfile.methods) { - if (expected.matchMethodName(meth.getName(classfile.constant_pool))) { - for(Attribute attr : meth.attributes) { - attr.accept(typeAnnoMatcher, expected); + private void matchMethodTypeAnnotation(ClassModel classfile, + ExpectedMethodTypeAnnotation expected) { + for(MethodModel meth : classfile.methods()) { + if (expected.matchMethodName(meth.methodName().stringValue())) { + for(Attribute attr : meth.attributes()) { + typeAnnoMatcher(attr, expected); } } } } - private void matchFieldTypeAnnotation(ClassFile classfile, - ExpectedFieldTypeAnnotation expected) - throws ConstantPoolException { - for(Field field : classfile.fields) { - if (expected.matchFieldName(field.getName(classfile.constant_pool))) { - for(Attribute attr : field.attributes) { - attr.accept(typeAnnoMatcher, expected); + private void matchFieldTypeAnnotation(ClassModel classfile, + ExpectedFieldTypeAnnotation expected) { + for(FieldModel field : classfile.fields()) { + if (expected.matchFieldName(field.fieldName().stringValue())) { + for(Attribute attr : field.attributes()) { + typeAnnoMatcher(attr, expected); } } } } - private void matchClassAnnotations(ClassFile classfile, - ExpectedAnnotation[] expected) - throws ConstantPoolException { + private void matchClassAnnotations(ClassModel classfile, + ExpectedAnnotation[] expected) { for(ExpectedAnnotation one : expected) { matchClassAnnotation(classfile, one); } } - private void matchMethodAnnotations(ClassFile classfile, - ExpectedMethodAnnotation[] expected) - throws ConstantPoolException { + private void matchMethodAnnotations(ClassModel classfile, + ExpectedMethodAnnotation[] expected) { for(ExpectedMethodAnnotation one : expected) { matchMethodAnnotation(classfile, one); } } - private void matchParameterAnnotations(ClassFile classfile, - ExpectedParameterAnnotation[] expected) - throws ConstantPoolException { + private void matchParameterAnnotations(ClassModel classfile, + ExpectedParameterAnnotation[] expected) { for(ExpectedParameterAnnotation one : expected) { matchParameterAnnotation(classfile, one); } } - private void matchFieldAnnotations(ClassFile classfile, - ExpectedFieldAnnotation[] expected) - throws ConstantPoolException { + private void matchFieldAnnotations(ClassModel classfile, + ExpectedFieldAnnotation[] expected) { for(ExpectedFieldAnnotation one : expected) { matchFieldAnnotation(classfile, one); } } - private void matchClassTypeAnnotations(ClassFile classfile, - ExpectedTypeAnnotation[] expected) - throws ConstantPoolException { + private void matchClassTypeAnnotations(ClassModel classfile, + ExpectedTypeAnnotation[] expected) { for(ExpectedTypeAnnotation one : expected) { matchClassTypeAnnotation(classfile, one); } } - private void matchMethodTypeAnnotations(ClassFile classfile, - ExpectedMethodTypeAnnotation[] expected) - throws ConstantPoolException { + private void matchMethodTypeAnnotations(ClassModel classfile, + ExpectedMethodTypeAnnotation[] expected) { for(ExpectedMethodTypeAnnotation one : expected) { matchMethodTypeAnnotation(classfile, one); } } - private void matchFieldTypeAnnotations(ClassFile classfile, - ExpectedFieldTypeAnnotation[] expected) - throws ConstantPoolException { + private void matchFieldTypeAnnotations(ClassModel classfile, + ExpectedFieldTypeAnnotation[] expected) { for(ExpectedFieldTypeAnnotation one : expected) { matchFieldTypeAnnotation(classfile, one); } } /** - * Run a template on a single {@code ClassFile}. + * Run a template on a single {@code ClassModel}. * - * @param classfile The {@code ClassFile} on which to run tests. + * @param classfile The {@code ClassModel} on which to run tests. * @param expected The expected annotation template. */ - public void run(ClassFile classfile, - Expected... expected) - throws ConstantPoolException { - run(new ClassFile[] { classfile }, expected); + public void run(ClassModel classfile, + Expected... expected) { + run(new ClassModel[] { classfile }, expected); } /** - * Run a template on multiple {@code ClassFile}s. + * Run a template on multiple {@code ClassModel}s. * - * @param classfile The {@code ClassFile}s on which to run tests. + * @param classfiles The {@code ClassModel}s on which to run tests. * @param expected The expected annotation template. */ - public void run(ClassFile[] classfiles, - Expected... expected) - throws ConstantPoolException { - for(ClassFile classfile : classfiles) { + public void run(ClassModel[] classfiles, + Expected... expected) { + for(ClassModel classfile : classfiles) { for(Expected one : expected) { - if (one.matchClassName(classfile.getName())) { + if (one.matchClassName(classfile.thisClass().name().stringValue())) { if (one.classAnnos != null) matchClassAnnotations(classfile, one.classAnnos); if (one.methodAnnos != null) @@ -1165,303 +1161,81 @@ public class ClassfileInspector { } /** - * Get a {@code ClassFile} from its file name. + * Get a {@code ClassModel} from its file name. * * @param name The class' file name. * @param host A class in the same package. - * @return The {@code ClassFile} + * @return The {@code ClassModel} */ - public static ClassFile getClassFile(String name, + public static ClassModel getClassFile(String name, Class host) - throws IOException, ConstantPoolException { + throws IOException { final URL url = host.getResource(name); + assert url != null; try (InputStream in = url.openStream()) { - return ClassFile.read(in); + return Classfile.of().parse(in.readAllBytes()); } } - private static class AbstractAttributeVisitor implements Attribute.Visitor { - - @Override - public Void visitDefault(DefaultAttribute attr, T p) { - return null; + public void typeAnnoMatcher(Attribute attr, ExpectedTypeAnnotation expected) { + switch (attr) { + case RuntimeVisibleTypeAnnotationsAttribute vattr -> { + if (expected.matchVisibility(true)) { + for (TypeAnnotation anno : vattr.annotations()) expected.matchAnnotation(anno); + } + } + case RuntimeInvisibleTypeAnnotationsAttribute ivattr -> { + if (expected.matchVisibility(false)) { + ivattr.annotations().forEach(expected::matchAnnotation); + } + } + default -> {} } + }; - @Override - public Void visitAnnotationDefault(AnnotationDefault_attribute attr, T p) { - return null; - } - - @Override - public Void visitBootstrapMethods(BootstrapMethods_attribute attr, T p) { - return null; - } - - @Override - public Void visitCharacterRangeTable(CharacterRangeTable_attribute attr, T p) { - return null; - } - - @Override - public Void visitCode(Code_attribute attr, T p) { - return null; - } - - @Override - public Void visitCompilationID(CompilationID_attribute attr, T p) { - return null; - } - - @Override - public Void visitConstantValue(ConstantValue_attribute attr, T p) { - return null; - } - - @Override - public Void visitDeprecated(Deprecated_attribute attr, T p) { - return null; - } - - @Override - public Void visitEnclosingMethod(EnclosingMethod_attribute attr, T p) { - return null; - } - - @Override - public Void visitExceptions(Exceptions_attribute attr, T p) { - return null; - } - - @Override - public Void visitInnerClasses(InnerClasses_attribute attr, T p) { - return null; - } - - @Override - public Void visitLineNumberTable(LineNumberTable_attribute attr, T p) { - return null; - } - - @Override - public Void visitLocalVariableTable(LocalVariableTable_attribute attr, T p) { - return null; - } - - @Override - public Void visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, T p) { - return null; - } - - @Override - public Void visitNestHost(NestHost_attribute attr, T p) { - return null; - } - - @Override - public Void visitMethodParameters(MethodParameters_attribute attr, T p) { - return null; - } - - @Override - public Void visitModule(Module_attribute attr, T p) { - return null; - } - - @Override - public Void visitModuleHashes(ModuleHashes_attribute attr, T p) { - return null; - } - - @Override - public Void visitModuleMainClass(ModuleMainClass_attribute attr, T p) { - return null; - } - - @Override - public Void visitModulePackages(ModulePackages_attribute attr, T p) { - return null; - } - - @Override - public Void visitModuleResolution(ModuleResolution_attribute attr, T p) { - return null; - } - - @Override - public Void visitModuleTarget(ModuleTarget_attribute attr, T p) { - return null; - } - - @Override - public Void visitNestMembers(NestMembers_attribute attr, T p) { - return null; - } - - @Override - public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, T p) { - return null; - } - - @Override - public Void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, T p) { - return null; - } - - @Override - public Void visitRuntimeInvisibleTypeAnnotations(RuntimeInvisibleTypeAnnotations_attribute attr, T p) { - return null; - } - - @Override - public Void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, T p) { - return null; - } - - @Override - public Void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, T p) { - return null; - } - - @Override - public Void visitRuntimeVisibleTypeAnnotations(RuntimeVisibleTypeAnnotations_attribute attr, T p) { - return null; - } - - @Override - public Void visitSignature(Signature_attribute attr, T p) { - return null; - } - - @Override - public Void visitSourceDebugExtension(SourceDebugExtension_attribute attr, T p) { - return null; - } - - @Override - public Void visitSourceFile(SourceFile_attribute attr, T p) { - return null; - } - - @Override - public Void visitSourceID(SourceID_attribute attr, T p) { - return null; - } - - @Override - public Void visitStackMap(StackMap_attribute attr, T p) { - return null; - } - - @Override - public Void visitStackMapTable(StackMapTable_attribute attr, T p) { - return null; - } - - @Override - public Void visitSynthetic(Synthetic_attribute attr, T p) { - return null; - } - - @Override - public Void visitPermittedSubclasses(PermittedSubclasses_attribute attr, T p) { - return null; - } - - @Override - public Void visitRecord(Record_attribute attr, T p) { - return null; + public void annoMatcher(Attribute attr, ExpectedAnnotation expected) { + switch (attr) { + case RuntimeVisibleTypeAnnotationsAttribute rvattr -> { + if (expected.matchVisibility(true)) { + for(Annotation anno : rvattr.annotations()) { + expected.matchAnnotation(anno); + } + } + } + case RuntimeInvisibleAnnotationsAttribute rivattr -> { + if (expected.matchVisibility(false)) { + for(Annotation anno : rivattr.annotations()) { + expected.matchAnnotation(anno); + } + } + } + default -> {} } } - private static final Attribute.Visitor typeAnnoMatcher - = new AbstractAttributeVisitor() { - - @Override - public Void visitRuntimeVisibleTypeAnnotations(RuntimeVisibleTypeAnnotations_attribute attr, - ExpectedTypeAnnotation expected) { - if (expected.matchVisibility(true)) { - for (TypeAnnotation anno : attr.annotations) { + private void paramMatcher(Attribute attr, ExpectedParameterAnnotation expected) { + switch (attr) { + case RuntimeVisibleParameterAnnotationsAttribute vattr -> { + if (expected.matchVisibility(true)) { + if (expected.index < vattr.parameterAnnotations().size()) { + for(Annotation anno : + vattr.parameterAnnotations().get(expected.index)) { expected.matchAnnotation(anno); } } - - return null; } - - @Override - public Void visitRuntimeInvisibleTypeAnnotations(RuntimeInvisibleTypeAnnotations_attribute attr, - ExpectedTypeAnnotation expected) { - if (expected.matchVisibility(false)) { - for (TypeAnnotation anno : attr.annotations) { + } + case RuntimeInvisibleParameterAnnotationsAttribute ivattr -> { + if (expected.matchVisibility(false)) { + if (expected.index < ivattr.parameterAnnotations().size()) { + for(Annotation anno : + ivattr.parameterAnnotations().get(expected.index)) { expected.matchAnnotation(anno); } } - - return null; } - }; - - private static Attribute.Visitor annoMatcher(ConstantPool cpool) { - return new AbstractAttributeVisitor() { - - @Override - public Void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, - ExpectedAnnotation expected) { - if (expected.matchVisibility(true)) { - for(Annotation anno : attr.annotations) { - expected.matchAnnotation(cpool, anno); - } - } - - return null; } - - @Override - public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, - ExpectedAnnotation expected) { - if (expected.matchVisibility(false)) { - for(Annotation anno : attr.annotations) { - expected.matchAnnotation(cpool, anno); - } - } - - return null; - } - }; - } - - private static Attribute.Visitor paramMatcher(ConstantPool cpool) { - return new AbstractAttributeVisitor() { - - @Override - public Void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, - ExpectedParameterAnnotation expected) { - if (expected.matchVisibility(true)) { - if (expected.index < attr.parameter_annotations.length) { - for(Annotation anno : - attr.parameter_annotations[expected.index]) { - expected.matchAnnotation(cpool, anno); - } - } - } - - return null; - } - - @Override - public Void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, - ExpectedParameterAnnotation expected) { - if (expected.matchVisibility(false)) { - if (expected.index < attr.parameter_annotations.length) { - for(Annotation anno : - attr.parameter_annotations[expected.index]) { - expected.matchAnnotation(cpool, anno); - } - } - } - - return null; - } - }; + default -> {} + } } } diff --git a/test/langtools/tools/javac/8009170/RedundantByteCodeInArrayTest.java b/test/langtools/tools/javac/8009170/RedundantByteCodeInArrayTest.java index 6176bc33270..1d672785fce 100644 --- a/test/langtools/tools/javac/8009170/RedundantByteCodeInArrayTest.java +++ b/test/langtools/tools/javac/8009170/RedundantByteCodeInArrayTest.java @@ -26,25 +26,23 @@ * @bug 8009170 * @summary Regression: javac generates redundant bytecode in assignop involving * arrays - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components * @run main RedundantByteCodeInArrayTest */ +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.CodeAttribute; +import jdk.internal.classfile.constantpool.ConstantPool; import java.io.File; import java.io.IOException; -import com.sun.tools.classfile.Attribute; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.Code_attribute; -import com.sun.tools.classfile.Code_attribute.InvalidIndex; -import com.sun.tools.classfile.ConstantPool; -import com.sun.tools.classfile.ConstantPoolException; -import com.sun.tools.classfile.Descriptor.InvalidDescriptor; -import com.sun.tools.classfile.Method; - public class RedundantByteCodeInArrayTest { public static void main(String[] args) - throws IOException, ConstantPoolException, InvalidDescriptor, InvalidIndex { + throws IOException { new RedundantByteCodeInArrayTest() .checkClassFile(new File(System.getProperty("test.classes", "."), RedundantByteCodeInArrayTest.class.getName() + ".class")); @@ -55,18 +53,18 @@ public class RedundantByteCodeInArrayTest { } void checkClassFile(File file) - throws IOException, ConstantPoolException, InvalidDescriptor, InvalidIndex { - ClassFile classFile = ClassFile.read(file); - ConstantPool constantPool = classFile.constant_pool; + throws IOException { + ClassModel classFile = Classfile.of().parse(file.toPath()); + ConstantPool constantPool = classFile.constantPool(); //lets get all the methods in the class file. - for (Method method : classFile.methods) { - if (method.getName(constantPool).equals("arrMethod")) { - Code_attribute code = (Code_attribute) method.attributes - .get(Attribute.Code); - if (code.max_locals > 4) + for (MethodModel method : classFile.methods()) { + if (method.methodName().equalsString("arrMethod")) { + CodeAttribute code = method.findAttribute(Attributes.CODE).orElse(null); + assert code != null; + if (code.maxLocals() > 4) throw new AssertionError("Too many locals for method arrMethod"); } } } -} +} \ No newline at end of file diff --git a/test/langtools/tools/javac/StringConcat/WellKnownTypes.java b/test/langtools/tools/javac/StringConcat/WellKnownTypes.java index 39150cfc4da..2da4dd70564 100644 --- a/test/langtools/tools/javac/StringConcat/WellKnownTypes.java +++ b/test/langtools/tools/javac/StringConcat/WellKnownTypes.java @@ -21,14 +21,15 @@ * questions. */ -import com.sun.tools.classfile.*; -import com.sun.tools.classfile.ConstantPool.*; - /* * @test * @bug 8273914 * @summary Indy string concat changes order of operations - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components * * @compile -XDstringConcat=indy WellKnownTypes.java * @run main WellKnownTypes diff --git a/test/langtools/tools/javac/T7165659/InnerClassAttrMustNotHaveStrictFPFlagTest.java b/test/langtools/tools/javac/T7165659/InnerClassAttrMustNotHaveStrictFPFlagTest.java index d31c7e1fa81..061d1a1d441 100644 --- a/test/langtools/tools/javac/T7165659/InnerClassAttrMustNotHaveStrictFPFlagTest.java +++ b/test/langtools/tools/javac/T7165659/InnerClassAttrMustNotHaveStrictFPFlagTest.java @@ -25,17 +25,18 @@ * @test * @bug 7165659 * @summary javac incorrectly sets strictfp access flag on inner-classes - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components * jdk.compiler/com.sun.tools.javac.util */ import java.io.File; -import com.sun.tools.classfile.AccessFlags; -import com.sun.tools.classfile.Attribute; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.InnerClasses_attribute; -import com.sun.tools.classfile.InnerClasses_attribute.Info; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; import com.sun.tools.javac.util.Assert; public class InnerClassAttrMustNotHaveStrictFPFlagTest { @@ -50,11 +51,11 @@ public class InnerClassAttrMustNotHaveStrictFPFlagTest { } void analyzeClassFile(File path) throws Exception { - ClassFile classFile = ClassFile.read(path); - InnerClasses_attribute innerClasses = - (InnerClasses_attribute) classFile.attributes.get(Attribute.InnerClasses); - for (Info classInfo : innerClasses.classes) { - Assert.check(!classInfo.inner_class_access_flags.is(AccessFlags.ACC_STRICT), + ClassModel classFile = Classfile.of().parse(path.toPath()); + InnerClassesAttribute innerClasses = classFile.findAttribute(Attributes.INNER_CLASSES).orElse(null); + assert innerClasses != null; + for (InnerClassInfo classInfo : innerClasses.classes()) { + Assert.check(classInfo.flagsMask() != Classfile.ACC_STRICT, "Inner classes attribute must not have the ACC_STRICT flag set"); } } diff --git a/test/langtools/tools/javac/T8011181/EmptyUTF8ForInnerClassNameTest.java b/test/langtools/tools/javac/T8011181/EmptyUTF8ForInnerClassNameTest.java index 40013725200..30cc6d455d5 100644 --- a/test/langtools/tools/javac/T8011181/EmptyUTF8ForInnerClassNameTest.java +++ b/test/langtools/tools/javac/T8011181/EmptyUTF8ForInnerClassNameTest.java @@ -25,7 +25,11 @@ * @test * @bug 8011181 * @summary javac, empty UTF8 entry generated for inner class - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components * jdk.compiler/com.sun.tools.javac.util */ @@ -35,11 +39,8 @@ import java.nio.file.Path; import java.nio.file.Paths; import com.sun.tools.javac.util.Assert; -import com.sun.tools.classfile.ClassFile; - -import static com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8; -import static com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8_info; -import static com.sun.tools.classfile.ConstantPool.CPInfo; +import jdk.internal.classfile.*; +import jdk.internal.classfile.constantpool.*; public class EmptyUTF8ForInnerClassNameTest { @@ -55,13 +56,13 @@ public class EmptyUTF8ForInnerClassNameTest { } void checkClassFile(final Path path) throws Exception { - ClassFile classFile = ClassFile.read( - new BufferedInputStream(Files.newInputStream(path))); - for (CPInfo cpInfo : classFile.constant_pool.entries()) { - if (cpInfo.getTag() == CONSTANT_Utf8) { - CONSTANT_Utf8_info utf8Info = (CONSTANT_Utf8_info)cpInfo; - Assert.check(utf8Info.value.length() > 0, - "UTF8 with length 0 found at class " + classFile.getName()); + ClassModel classFile = Classfile.of().parse( + new BufferedInputStream(Files.newInputStream(path)).readAllBytes()); + for (int i = 1; i < classFile.constantPool().entryCount(); ++i) { + PoolEntry pe = classFile.constantPool().entryByIndex(i); + if (pe instanceof Utf8Entry utf8Info) { + Assert.check(utf8Info.stringValue().length() > 0, + "UTF8 with length 0 found at class " + classFile.thisClass().name()); } } } diff --git a/test/langtools/tools/javac/T8028504/DontGenerateLVTForGNoneOpTest.java b/test/langtools/tools/javac/T8028504/DontGenerateLVTForGNoneOpTest.java index fcdd822046c..0320e3ba5e5 100644 --- a/test/langtools/tools/javac/T8028504/DontGenerateLVTForGNoneOpTest.java +++ b/test/langtools/tools/javac/T8028504/DontGenerateLVTForGNoneOpTest.java @@ -25,7 +25,11 @@ * @test * @bug 8028504 * @summary javac generates LocalVariableTable even with -g:none - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components * @compile -g:none DontGenerateLVTForGNoneOpTest.java * @run main DontGenerateLVTForGNoneOpTest */ @@ -35,10 +39,8 @@ import java.lang.annotation.ElementType; import java.lang.annotation.Target; import java.nio.file.Paths; -import com.sun.tools.classfile.Attribute; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.Code_attribute; -import com.sun.tools.classfile.Method; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.CodeAttribute; public class DontGenerateLVTForGNoneOpTest { @@ -52,11 +54,11 @@ public class DontGenerateLVTForGNoneOpTest { } void checkClassFile(final File cfile) throws Exception { - ClassFile classFile = ClassFile.read(cfile); - for (Method method : classFile.methods) { - Code_attribute code = (Code_attribute)method.attributes.get(Attribute.Code); + ClassModel classFile = Classfile.of().parse(cfile.toPath()); + for (MethodModel method : classFile.methods()) { + CodeAttribute code = method.findAttribute(Attributes.CODE).orElse(null); if (code != null) { - if (code.attributes.get(Attribute.LocalVariableTable) != null) { + if (code.findAttribute(Attributes.LOCAL_VARIABLE_TABLE).orElse(null) != null) { throw new AssertionError("LVT shouldn't be generated for g:none"); } } diff --git a/test/langtools/tools/javac/T8210435/NoLocalsMustBeReservedForDCEedVarsTest.java b/test/langtools/tools/javac/T8210435/NoLocalsMustBeReservedForDCEedVarsTest.java index 14ff679ab60..7850d1ec307 100644 --- a/test/langtools/tools/javac/T8210435/NoLocalsMustBeReservedForDCEedVarsTest.java +++ b/test/langtools/tools/javac/T8210435/NoLocalsMustBeReservedForDCEedVarsTest.java @@ -25,7 +25,11 @@ * @test * @summary * @library /tools/lib - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components * jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * jdk.compiler/com.sun.tools.javac.util @@ -37,7 +41,8 @@ import java.io.File; import java.nio.file.Paths; -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; import com.sun.tools.javac.util.Assert; import toolbox.JavacTask; @@ -64,11 +69,12 @@ public class NoLocalsMustBeReservedForDCEedVarsTest { .run(); File cfile = new File(Paths.get(System.getProperty("user.dir"), "Test.class").toUri()); - ClassFile classFile = ClassFile.read(cfile); - for (Method method: classFile.methods) { - if (method.getName(classFile.constant_pool).equals("foo")) { - Code_attribute codeAttr = (Code_attribute)method.attributes.get("Code"); - Assert.check(codeAttr.max_locals == 0, "max locals found " + codeAttr.max_locals); + ClassModel classFile = Classfile.of().parse(cfile.toPath()); + for (MethodModel method: classFile.methods()) { + if (method.methodName().stringValue().equals("foo")) { + CodeAttribute codeAttr = method.findAttribute(Attributes.CODE).orElse(null); + assert codeAttr != null; + Assert.check(codeAttr.maxLocals() == 0, "max locals found " + codeAttr.maxLocals()); } } } diff --git a/test/langtools/tools/javac/annotations/SyntheticParameters.java b/test/langtools/tools/javac/annotations/SyntheticParameters.java index 9670f768690..322394369f6 100644 --- a/test/langtools/tools/javac/annotations/SyntheticParameters.java +++ b/test/langtools/tools/javac/annotations/SyntheticParameters.java @@ -26,7 +26,12 @@ * @bug 8065132 * @summary Test generation of annotations on inner class parameters. * @library /lib/annotations/ - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build annotations.classfile.ClassfileInspector SyntheticParameters * @run main SyntheticParameters */ @@ -36,7 +41,7 @@ import annotations.classfile.ClassfileInspector; import java.io.*; import java.lang.annotation.*; -import com.sun.tools.classfile.*; +import jdk.internal.classfile.ClassModel; public class SyntheticParameters extends ClassfileInspector { @@ -116,7 +121,7 @@ public class SyntheticParameters extends ClassfileInspector { public static void main(String... args) throws Exception { new SyntheticParameters().run( - new ClassFile[] { getClassFile(Inner_class, Inner.class), + new ClassModel[] { getClassFile(Inner_class, Inner.class), getClassFile(Foo_class, Foo.class) }, new Expected[] { Inner_expected, Foo_expected }); } diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/AnonymousClassTest.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/AnonymousClassTest.java index 68867cb7ac0..99b910d0c4e 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/AnonymousClassTest.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/AnonymousClassTest.java @@ -28,7 +28,12 @@ * @library /tools/lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * jdk.jdeps/com.sun.tools.javap * @build toolbox.ToolBox toolbox.JavapTask * @run compile -g AnonymousClassTest.java @@ -37,31 +42,15 @@ import static java.util.stream.Collectors.toSet; -import com.sun.tools.classfile.Annotation; -import com.sun.tools.classfile.Annotation.Annotation_element_value; -import com.sun.tools.classfile.Annotation.Array_element_value; -import com.sun.tools.classfile.Annotation.Class_element_value; -import com.sun.tools.classfile.Annotation.Enum_element_value; -import com.sun.tools.classfile.Annotation.Primitive_element_value; -import com.sun.tools.classfile.Annotation.element_value; -import com.sun.tools.classfile.Annotation.element_value.Visitor; -import com.sun.tools.classfile.Attribute; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.Code_attribute; -import com.sun.tools.classfile.ConstantPool.CONSTANT_Integer_info; -import com.sun.tools.classfile.ConstantPool.InvalidIndex; -import com.sun.tools.classfile.ConstantPoolException; -import com.sun.tools.classfile.Method; -import com.sun.tools.classfile.RuntimeVisibleTypeAnnotations_attribute; -import com.sun.tools.classfile.TypeAnnotation; -import com.sun.tools.classfile.TypeAnnotation.Position; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.CodeAttribute; +import jdk.internal.classfile.attribute.RuntimeVisibleTypeAnnotationsAttribute; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.nio.file.Paths; -import java.util.Arrays; -import java.util.Set; +import java.util.*; import java.util.concurrent.Callable; import toolbox.ToolBox; @@ -113,108 +102,108 @@ public class AnonymousClassTest { } static void testAnonymousClassDeclaration() throws Exception { - ClassFile cf = ClassFile.read(Paths.get(ToolBox.testClasses, "AnonymousClassTest$1.class")); - RuntimeVisibleTypeAnnotations_attribute rvta = - (RuntimeVisibleTypeAnnotations_attribute) - cf.attributes.get(Attribute.RuntimeVisibleTypeAnnotations); + ClassModel cm = Classfile.of().parse(Paths.get(ToolBox.testClasses, "AnonymousClassTest$1.class")); + RuntimeVisibleTypeAnnotationsAttribute rvta = + cm.findAttribute(Attributes.RUNTIME_VISIBLE_TYPE_ANNOTATIONS).orElse(null); + assert rvta != null; assertEquals( Set.of( "@LAnonymousClassTest$TA;(1) CLASS_EXTENDS, offset=-1, location=[TYPE_ARGUMENT(0)]", "@LAnonymousClassTest$TA;(0) CLASS_EXTENDS, offset=-1, location=[]"), - Arrays.stream(rvta.annotations) - .map(a -> annotationDebugString(cf, a)) + rvta.annotations().stream() + .map(a -> annotationDebugString(cm, null, a)) .collect(toSet())); } static void testTopLevelMethod() throws Exception { - ClassFile cf = ClassFile.read(Paths.get(ToolBox.testClasses, "AnonymousClassTest.class")); - Method method = findMethod(cf, "f"); + ClassModel cm = Classfile.of().parse(Paths.get(ToolBox.testClasses, "AnonymousClassTest.class")); + MethodModel method = findMethod(cm, "f"); Set annotations = getRuntimeVisibleTypeAnnotations(method); + CodeAttribute cAttr = method.findAttribute(Attributes.CODE).orElse(null); assertEquals( Set.of("@LAnonymousClassTest$TA;(0) NEW, offset=0, location=[INNER_TYPE]"), - annotations.stream().map(a -> annotationDebugString(cf, a)).collect(toSet())); + annotations.stream().map(a -> annotationDebugString(cm, cAttr, a)).collect(toSet())); } static void testInnerClassMethod() throws Exception { - ClassFile cf = - ClassFile.read(Paths.get(ToolBox.testClasses, "AnonymousClassTest$Inner.class")); - Method method = findMethod(cf, "g"); + ClassModel cm = + Classfile.of().parse(Paths.get(ToolBox.testClasses, "AnonymousClassTest$Inner.class")); + MethodModel method = findMethod(cm, "g"); Set annotations = getRuntimeVisibleTypeAnnotations(method); + CodeAttribute cAttr = method.findAttribute(Attributes.CODE).orElse(null); // The annotation needs two INNER_TYPE type path entries to apply to // AnonymousClassTest$Inner$1. assertEquals( Set.of( "@LAnonymousClassTest$TA;(2) NEW, offset=0, location=[INNER_TYPE, INNER_TYPE]"), - annotations.stream().map(a -> annotationDebugString(cf, a)).collect(toSet())); + annotations.stream().map(a -> annotationDebugString(cm, cAttr, a)).collect(toSet())); } static void testQualifiedSuperType() throws Exception { { - ClassFile cf = - ClassFile.read(Paths.get(ToolBox.testClasses, "AnonymousClassTest.class")); - Method method = findMethod(cf, "g"); + ClassModel cm = + Classfile.of().parse(Paths.get(ToolBox.testClasses, "AnonymousClassTest.class")); + MethodModel method = findMethod(cm, "g"); Set annotations = getRuntimeVisibleTypeAnnotations(method); + CodeAttribute cAttr = method.findAttribute(Attributes.CODE).orElse(null); // Only @TA(4) is propagated to the anonymous class declaration. assertEquals( Set.of("@LAnonymousClassTest$TA;(4) NEW, offset=0, location=[INNER_TYPE]"), - annotations.stream().map(a -> annotationDebugString(cf, a)).collect(toSet())); + annotations.stream().map(a -> annotationDebugString(cm, cAttr, a)).collect(toSet())); } { - ClassFile cf = - ClassFile.read(Paths.get(ToolBox.testClasses, "AnonymousClassTest$2.class")); - RuntimeVisibleTypeAnnotations_attribute rvta = - (RuntimeVisibleTypeAnnotations_attribute) - cf.attributes.get(Attribute.RuntimeVisibleTypeAnnotations); + ClassModel cm = + Classfile.of().parse(Paths.get(ToolBox.testClasses, "AnonymousClassTest$2.class")); + RuntimeVisibleTypeAnnotationsAttribute rvta = + cm.findAttribute(Attributes.RUNTIME_VISIBLE_TYPE_ANNOTATIONS).orElse(null); + assert rvta != null; assertEquals( Set.of( "@LAnonymousClassTest$TA;(3) CLASS_EXTENDS, offset=-1, location=[]", "@LAnonymousClassTest$TA;(4) CLASS_EXTENDS, offset=-1, location=[INNER_TYPE]"), - Arrays.stream(rvta.annotations) - .map(a -> annotationDebugString(cf, a)) + rvta.annotations().stream() + .map(a -> annotationDebugString(cm, null, a)) .collect(toSet())); } } static void testInstanceAndClassInit() throws Exception { - ClassFile cf = ClassFile.read(Paths.get(ToolBox.testClasses, "AnonymousClassTest.class")); - Method method = findMethod(cf, ""); + ClassModel cm = Classfile.of().parse(Paths.get(ToolBox.testClasses, "AnonymousClassTest.class")); + MethodModel method = findMethod(cm, ""); Set annotations = getRuntimeVisibleTypeAnnotations(method); + CodeAttribute cAttr1 = method.findAttribute(Attributes.CODE).orElse(null); assertEquals( Set.of("@LAnonymousClassTest$TA;(5) NEW, offset=4, location=[INNER_TYPE]"), - annotations.stream().map(a -> annotationDebugString(cf, a)).collect(toSet()) ); + annotations.stream().map(a -> annotationDebugString(cm, cAttr1, a)).collect(toSet()) ); - method = findMethod(cf, ""); + method = findMethod(cm, ""); annotations = getRuntimeVisibleTypeAnnotations(method); + CodeAttribute cAttr2 = method.findAttribute(Attributes.CODE).orElse(null); assertEquals( - Set.of("@LAnonymousClassTest$TA;(6) NEW, offset=0, location=[INNER_TYPE]"), - annotations.stream().map(a -> annotationDebugString(cf, a)).collect(toSet()) ); + Set.of("@LAnonymousClassTest$TA;(6) NEW, offset=16, location=[INNER_TYPE]"), + annotations.stream().map(a -> annotationDebugString(cm, cAttr2, a)).collect(toSet()) ); } // Returns the Method's RuntimeVisibleTypeAnnotations, and asserts that there are no RVTIs // erroneously associated with the Method instead of its Code attribute. - private static Set getRuntimeVisibleTypeAnnotations(Method method) { - if (method.attributes.get(Attribute.RuntimeVisibleTypeAnnotations) != null) { + private static Set getRuntimeVisibleTypeAnnotations(MethodModel method) { + if (method.findAttribute(Attributes.RUNTIME_VISIBLE_TYPE_ANNOTATIONS).orElse(null) != null) { throw new AssertionError( "expected no RuntimeVisibleTypeAnnotations attribute on enclosing method"); } - Code_attribute code = (Code_attribute) method.attributes.get(Attribute.Code); - RuntimeVisibleTypeAnnotations_attribute rvta = - (RuntimeVisibleTypeAnnotations_attribute) - code.attributes.get(Attribute.RuntimeVisibleTypeAnnotations); - return Set.of(rvta.annotations); + CodeAttribute code = method.findAttribute(Attributes.CODE).orElse(null); + assert code != null; + RuntimeVisibleTypeAnnotationsAttribute rvta = + code.findAttribute(Attributes.RUNTIME_VISIBLE_TYPE_ANNOTATIONS).orElse(null); + assert rvta != null; + return new HashSet<>(rvta.annotations()); } - private static Method findMethod(ClassFile cf, String name) { - return Arrays.stream(cf.methods) + private static MethodModel findMethod(ClassModel cm, String name) { + return cm.methods().stream() .filter( - m -> { - try { - return m.getName(cf.constant_pool).contentEquals(name); - } catch (ConstantPoolException e) { - throw new AssertionError(e); - } - }) + m -> m.methodName().stringValue().contentEquals(name)) .findFirst() .get(); } @@ -225,73 +214,45 @@ public class AnonymousClassTest { } } - private static String annotationDebugString(ClassFile cf, TypeAnnotation annotation) { - Position pos = annotation.position; + private static String annotationDebugString(ClassModel cm, CodeAttribute cAttr, TypeAnnotation annotation) { + TypeAnnotation.TargetInfo info = annotation.targetInfo(); + int offset = info instanceof TypeAnnotation.OffsetTarget offsetInfo? cAttr.labelToBci(offsetInfo.target()): -1; String name; try { - name = cf.constant_pool.getUTF8Info(annotation.annotation.type_index).value; + name = annotation.classSymbol().descriptorString(); } catch (Exception e) { throw new AssertionError(e); } + List location = new ArrayList<>(); + for (TypeAnnotation.TypePathComponent path: annotation.targetPath()) { + if (path.typePathKind() == TypeAnnotation.TypePathComponent.Kind.INNER_TYPE)location.add(path.typePathKind().name()); + else location.add(path.typePathKind() + "(" + path.typeArgumentIndex() + ")"); + } return String.format( "@%s(%s) %s, offset=%d, location=%s", name, - annotationValueoDebugString(cf, annotation.annotation), - pos.type, - pos.offset, - pos.location); + annotationValueDebugString(cm, annotation), + info.targetType(), + offset, + location); } - private static String annotationValueoDebugString(ClassFile cf, Annotation annotation) { - if (annotation.element_value_pairs.length != 1) { + private static String annotationValueDebugString(ClassModel cm, Annotation annotation) { + if (annotation.elements().size() != 1) { throw new UnsupportedOperationException(); } try { - return elementValueDebugString(cf, annotation.element_value_pairs[0].value); + return elementValueDebugString(annotation.elements().get(0).value()); } catch (Exception e) { throw new AssertionError(e); } } - private static String elementValueDebugString(ClassFile cf, element_value value) { - class Viz implements Visitor { - @Override - public String visitPrimitive(Primitive_element_value ev, Void aVoid) { - try { - switch (ev.tag) { - case 'I': - return Integer.toString( - ((CONSTANT_Integer_info) - cf.constant_pool.get(ev.const_value_index)) - .value); - default: - throw new UnsupportedOperationException(String.format("%c", ev.tag)); - } - } catch (InvalidIndex e) { - throw new AssertionError(e); - } - } - - @Override - public String visitEnum(Enum_element_value ev, Void aVoid) { - throw new UnsupportedOperationException(); - } - - @Override - public String visitClass(Class_element_value ev, Void aVoid) { - throw new UnsupportedOperationException(); - } - - @Override - public String visitAnnotation(Annotation_element_value ev, Void aVoid) { - throw new UnsupportedOperationException(); - } - - @Override - public String visitArray(Array_element_value ev, Void aVoid) { - throw new UnsupportedOperationException(); - } + private static String elementValueDebugString(AnnotationValue value) { + if (value.tag() == 'I') { + return Integer.toString(((AnnotationValue.OfInteger) value).intValue()); + } else { + throw new UnsupportedOperationException(String.format("%c", value.tag())); } - return value.accept(new Viz(), null); } } diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/ClassfileTestHelper.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/ClassfileTestHelper.java index e68b47e3739..856ce1fb6e2 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/ClassfileTestHelper.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/ClassfileTestHelper.java @@ -21,12 +21,12 @@ * questions. */ -import java.lang.annotation.*; import java.io.*; import java.net.URL; import java.util.List; -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; import java.util.ArrayList; public class ClassfileTestHelper { @@ -59,158 +59,119 @@ public class ClassfileTestHelper { return new File(path.substring(0, path.length() - 5) + ".class"); } - ClassFile getClassFile(String name) throws IOException, ConstantPoolException { + ClassModel getClassFile(String name) throws IOException { URL url = getClass().getResource(name); - InputStream in = url.openStream(); - try { - return ClassFile.read(in); - } finally { - in.close(); + assert url != null; + try (InputStream in = url.openStream()) { + return Classfile.of().parse(in.readAllBytes()); } } - ClassFile getClassFile(URL url) throws IOException, ConstantPoolException { - InputStream in = url.openStream(); - try { - return ClassFile.read(in); - } finally { - in.close(); + ClassModel getClassFile(URL url) throws IOException { + try (InputStream in = url.openStream()) { + return Classfile.of().parse(in.readAllBytes()); } } /************ Helper annotations counting methods ******************/ - void test(ClassFile cf) { - test("CLASS",cf, null, null, Attribute.RuntimeVisibleTypeAnnotations, true); - test("CLASS",cf, null, null, Attribute.RuntimeInvisibleTypeAnnotations, false); - //RuntimeAnnotations since one annotation can result in two attributes. - test("CLASS",cf, null, null, Attribute.RuntimeVisibleAnnotations, true); - test("CLASS",cf, null, null, Attribute.RuntimeInvisibleAnnotations, false); + void test(ClassModel cm) { + test(cm, false); //For ClassModel, not look for annotations in code attr + } + // default to not looking in code attribute + void test(FieldModel fm) { + test(fm, false); } - void test(ClassFile cf, Field f, Boolean local) { - if (!local) { - test("FIELD",cf, f, null, Attribute.RuntimeVisibleTypeAnnotations, true); - test("FIELD",cf, f, null, Attribute.RuntimeInvisibleTypeAnnotations, false); - test("FIELD",cf, f, null, Attribute.RuntimeVisibleAnnotations, true); - test("FIELD",cf, f, null, Attribute.RuntimeInvisibleAnnotations, false); - } else { - test("CODE",cf, f, null, Attribute.RuntimeVisibleTypeAnnotations, true); - test("CODE",cf, f, null, Attribute.RuntimeInvisibleTypeAnnotations, false); - test("CODE",cf, f, null, Attribute.RuntimeVisibleAnnotations, true); - test("CODE",cf, f, null, Attribute.RuntimeInvisibleAnnotations, false); - } - } - - void test(ClassFile cf, Field f) { - test(cf, f, false); + void test(MethodModel mm ) { + test(mm, false); } // 'local' determines whether to look for annotations in code attribute or not. - void test(ClassFile cf, Method m, Boolean local) { - if (!local) { - test("METHOD",cf, null, m, Attribute.RuntimeVisibleTypeAnnotations, true); - test("METHOD",cf, null, m, Attribute.RuntimeInvisibleTypeAnnotations, false); - test("METHOD",cf, null, m, Attribute.RuntimeVisibleAnnotations, true); - test("METHOD",cf, null, m, Attribute.RuntimeInvisibleAnnotations, false); - } else { - test("MCODE",cf, null, m, Attribute.RuntimeVisibleTypeAnnotations, true); - test("MCODE",cf, null, m, Attribute.RuntimeInvisibleTypeAnnotations, false); - test("MCODE",cf, null, m, Attribute.RuntimeVisibleAnnotations, true); - test("MCODE",cf, null, m, Attribute.RuntimeInvisibleAnnotations, false); - } + void test(AttributedElement m, Boolean local) { + test(m, Attributes.RUNTIME_VISIBLE_TYPE_ANNOTATIONS, local); + test(m, Attributes.RUNTIME_INVISIBLE_TYPE_ANNOTATIONS, local); + test(m, Attributes.RUNTIME_VISIBLE_ANNOTATIONS, local); + test(m, Attributes.RUNTIME_INVISIBLE_ANNOTATIONS, local); } - // default to not looking in code attribute - void test(ClassFile cf, Method m ) { - test(cf, m, false); - } - - // Test the result of Attributes.getIndex according to expectations + // Test the result of MethodModel.findAttribute according to expectations // encoded in the class/field/method name; increment annotations counts. - void test(String ttype, ClassFile cf, Field f, Method m, String annName, boolean visible) { - String testtype = ttype; - String name = null; - int index = -1; - Attribute attr = null; - Code_attribute cAttr = null; - boolean isTAattr = annName.contains("TypeAnnotations"); - try { - switch(testtype) { - case "FIELD": - name = f.getName(cf.constant_pool); - index = f.attributes.getIndex(cf.constant_pool, annName); - if(index!= -1) - attr = f.attributes.get(index); - break; - case "CODE": - name = f.getName(cf.constant_pool); - //fetch index of and code attribute and annotations from code attribute. - index = cf.attributes.getIndex(cf.constant_pool, Attribute.Code); - if(index!= -1) { - attr = cf.attributes.get(index); - assert attr instanceof Code_attribute; - cAttr = (Code_attribute)attr; - index = cAttr.attributes.getIndex(cf.constant_pool, annName); - if(index!= -1) - attr = cAttr.attributes.get(index); - } - break; - case "METHOD": - name = m.getName(cf.constant_pool); - index = m.attributes.getIndex(cf.constant_pool, annName); - if(index!= -1) - attr = m.attributes.get(index); - break; - case "MCODE": - name = m.getName(cf.constant_pool); - //fetch index of and code attribute and annotations from code attribute. - index = m.attributes.getIndex(cf.constant_pool, Attribute.Code); - if(index!= -1) { - attr = m.attributes.get(index); - assert attr instanceof Code_attribute; - cAttr = (Code_attribute)attr; - index = cAttr.attributes.getIndex(cf.constant_pool, annName); - if(index!= -1) - attr = cAttr.attributes.get(index); - } - break; - default: - name = cf.getName(); - index = cf.attributes.getIndex(cf.constant_pool, annName); - if(index!= -1) attr = cf.attributes.get(index); + >void test(AttributedElement m, AttributeMapper annName, Boolean local) { + String name; + Attribute attr; + boolean isTAattr = annName.name().contains("Type"); + switch(m) { + case FieldModel fm -> { + name = fm.fieldName().stringValue(); + attr = extractAnnotation(m, annName, local); } - } catch(ConstantPoolException cpe) { cpe.printStackTrace(); } - - if (index != -1) { - if(isTAattr) { //count RuntimeTypeAnnotations - RuntimeTypeAnnotations_attribute tAttr = - (RuntimeTypeAnnotations_attribute)attr; - println(testtype + ": " + name + ", " + annName + ": " + - tAttr.annotations.length ); - if (tAttr.annotations.length > 0) { - for (int i = 0; i < tAttr.annotations.length; i++) { - println(" types:" + tAttr.annotations[i].position.type); - } - } else { - println(""); - } - allt += tAttr.annotations.length; - if (visible) - tvisibles += tAttr.annotations.length; - else - tinvisibles += tAttr.annotations.length; - } else { - RuntimeAnnotations_attribute tAttr = - (RuntimeAnnotations_attribute)attr; - println(testtype + ": " + name + ", " + annName + ": " + - tAttr.annotations.length ); - all += tAttr.annotations.length; - if (visible) - visibles += tAttr.annotations.length; - else - invisibles += tAttr.annotations.length; + case MethodModel mm -> { + name = mm.methodName().stringValue(); + attr = extractAnnotation(m, annName, local); + } + default -> { + ClassModel cm = (ClassModel) m; + name = cm.thisClass().asInternalName(); + attr = extractAnnotation(cm, annName, local); } } + + if (attr != null) { + if(isTAattr) { //count RuntimeTypeAnnotations +// List tAnnots = new ArrayList(); + switch (attr) { + case RuntimeVisibleTypeAnnotationsAttribute vtAttr -> { + List tAnnots = vtAttr.annotations(); + tvisibles += tAnnots.size(); + allt += tAnnots.size(); + } + case RuntimeInvisibleTypeAnnotationsAttribute invtAttr -> { + System.err.println(invtAttr.annotations()); + List tAnnots = invtAttr.annotations(); + tinvisibles += tAnnots.size(); + allt += tAnnots.size(); + } + default -> throw new AssertionError(); + } + // This snippet is simply for printlin. which are duplicated in two cases. Therefore, I want to drop it. +// if (!tAnnots.isEmpty()) { +//// for (TypeAnnotation tAnnot : tAnnots) +//// println(" types:" + tAnnot.targetInfo().targetType()); +//// println("Local: " + local + ", " + name + ", " + annName + ": " + tAnnots.size()); +// allt += tAnnots.size(); +// } + } else { + List annots; + switch (attr) { + case RuntimeVisibleAnnotationsAttribute tAttr -> { + annots = tAttr.annotations(); + visibles += annots.size(); + } + case RuntimeInvisibleAnnotationsAttribute tAttr -> { + annots = tAttr.annotations(); + invisibles += annots.size(); + } + default -> throw new AssertionError(); + } + if (!annots.isEmpty()) { + println("Local: " + local + ", " + name + ", " + annName + ": " + annots.size()); + all += annots.size(); + } + } + } + } + > Attribute extractAnnotation(AttributedElement m, AttributeMapper annName, Boolean local) { + CodeAttribute cAttr; + Attribute attr = null; + if (local) { + cAttr = m.findAttribute(Attributes.CODE).orElse(null); + if (cAttr != null) { + attr = cAttr.findAttribute(annName).orElse(null); + } + } else { + attr = m.findAttribute(annName).orElse(null); + } + return attr; } void countAnnotations() { diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest1.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest1.java index 3614992158b..0f855d7d426 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest1.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest1.java @@ -25,10 +25,15 @@ * @test * @bug 8005085 8005877 8004829 8005681 8006734 8006775 * @summary Combinations of Target ElementTypes on (repeated)type annotations. - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl */ -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; import java.io.File; public class CombinationsTargetTest1 extends ClassfileTestHelper { @@ -142,16 +147,16 @@ public class CombinationsTargetTest1 extends ClassfileTestHelper { classFile=new File(sb.insert(sb.lastIndexOf(".class"), innerClassname).toString()); } - ClassFile cf = ClassFile.read(classFile); + ClassModel cm = Classfile.of().parse(classFile.toPath()); //Test class,fields and method counts. - test(cf); + test(cm); - for (Field f : cf.fields) { - test(cf, f); + for (FieldModel fm : cm.fields()) { + test(fm); } - for (Method m: cf.methods) { - test(cf, m); + for (MethodModel mm: cm.methods()) { + test(mm); } countAnnotations(); if (errors > 0) { diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest2.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest2.java index 2cd93907a1a..3ce48df24ba 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest2.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest2.java @@ -25,10 +25,15 @@ * @test * @bug 8005085 8005877 8004829 8005681 8006734 8006775 8006507 * @summary Combinations of Target ElementTypes on (repeated)type annotations. - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl */ -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; import java.io.File; import java.util.List; @@ -161,22 +166,22 @@ public class CombinationsTargetTest2 extends ClassfileTestHelper { classFile=new File(sb.insert(sb.lastIndexOf(".class"),innerClassname).toString()); println("classfile: " + classFile.getAbsolutePath()); } - ClassFile cf = ClassFile.read(classFile); + ClassModel cm = Classfile.of().parse(classFile.toPath()); //Test class,fields and method counts. - test(cf); + test(cm); - for (Field f : cf.fields) { + for (FieldModel fm : cm.fields()) { if(source.local) - test(cf, f, true); + test(fm, true); else - test(cf,f); + test(fm); } - for (Method m: cf.methods) { + for (MethodModel mm: cm.methods()) { if(source.local) - test(cf, m, true); + test(mm, true); else - test(cf, m); + test(mm); } countAnnotations(); if (errors > 0) { diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest3.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest3.java index a63a285fb02..6b026f16295 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest3.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest3.java @@ -26,11 +26,19 @@ * @bug 8005085 8005681 8008769 8010015 * @summary Check (repeating)type annotations on lambda usage. * @modules jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @run main CombinationsTargetTest3 */ import com.sun.tools.classfile.*; -import java.io.File; +import java.io.*; +import java.util.ArrayList; +import java.util.List; import java.util.Vector; public class CombinationsTargetTest3 extends ClassfileTestHelper { @@ -537,4 +545,139 @@ public class CombinationsTargetTest3 extends ClassfileTestHelper { } return imports + source; } + + /************ Helper annotations counting methods ******************/ + void test(ClassFile cf) { + test("CLASS",cf, null, null, Attribute.RuntimeVisibleTypeAnnotations, true); + test("CLASS",cf, null, null, Attribute.RuntimeInvisibleTypeAnnotations, false); + //RuntimeAnnotations since one annotation can result in two attributes. + test("CLASS",cf, null, null, Attribute.RuntimeVisibleAnnotations, true); + test("CLASS",cf, null, null, Attribute.RuntimeInvisibleAnnotations, false); + } + + void test(ClassFile cf, Field f, Boolean local) { + if (!local) { + test("FIELD",cf, f, null, Attribute.RuntimeVisibleTypeAnnotations, true); + test("FIELD",cf, f, null, Attribute.RuntimeInvisibleTypeAnnotations, false); + test("FIELD",cf, f, null, Attribute.RuntimeVisibleAnnotations, true); + test("FIELD",cf, f, null, Attribute.RuntimeInvisibleAnnotations, false); + } else { + test("CODE",cf, f, null, Attribute.RuntimeVisibleTypeAnnotations, true); + test("CODE",cf, f, null, Attribute.RuntimeInvisibleTypeAnnotations, false); + test("CODE",cf, f, null, Attribute.RuntimeVisibleAnnotations, true); + test("CODE",cf, f, null, Attribute.RuntimeInvisibleAnnotations, false); + } + } + + void test(ClassFile cf, Field f) { + test(cf, f, false); + } + + // 'local' determines whether to look for annotations in code attribute or not. + void test(ClassFile cf, Method m, Boolean local) { + if (!local) { + test("METHOD",cf, null, m, Attribute.RuntimeVisibleTypeAnnotations, true); + test("METHOD",cf, null, m, Attribute.RuntimeInvisibleTypeAnnotations, false); + test("METHOD",cf, null, m, Attribute.RuntimeVisibleAnnotations, true); + test("METHOD",cf, null, m, Attribute.RuntimeInvisibleAnnotations, false); + } else { + test("MCODE",cf, null, m, Attribute.RuntimeVisibleTypeAnnotations, true); + test("MCODE",cf, null, m, Attribute.RuntimeInvisibleTypeAnnotations, false); + test("MCODE",cf, null, m, Attribute.RuntimeVisibleAnnotations, true); + test("MCODE",cf, null, m, Attribute.RuntimeInvisibleAnnotations, false); + } + } + + // default to not looking in code attribute + void test(ClassFile cf, Method m ) { + test(cf, m, false); + } + + // Test the result of Attributes.getIndex according to expectations + // encoded in the class/field/method name; increment annotations counts. + void test(String ttype, ClassFile cf, Field f, Method m, String annName, boolean visible) { + String testtype = ttype; + String name = null; + int index = -1; + Attribute attr = null; + Code_attribute cAttr = null; + boolean isTAattr = annName.contains("TypeAnnotations"); + try { + switch(testtype) { + case "FIELD": + name = f.getName(cf.constant_pool); + index = f.attributes.getIndex(cf.constant_pool, annName); + if(index!= -1) + attr = f.attributes.get(index); + break; + case "CODE": + name = f.getName(cf.constant_pool); + //fetch index of and code attribute and annotations from code attribute. + index = cf.attributes.getIndex(cf.constant_pool, Attribute.Code); + if(index!= -1) { + attr = cf.attributes.get(index); + assert attr instanceof Code_attribute; + cAttr = (Code_attribute)attr; + index = cAttr.attributes.getIndex(cf.constant_pool, annName); + if(index!= -1) + attr = cAttr.attributes.get(index); + } + break; + case "METHOD": + name = m.getName(cf.constant_pool); + index = m.attributes.getIndex(cf.constant_pool, annName); + if(index!= -1) + attr = m.attributes.get(index); + break; + case "MCODE": + name = m.getName(cf.constant_pool); + //fetch index of and code attribute and annotations from code attribute. + index = m.attributes.getIndex(cf.constant_pool, Attribute.Code); + if(index!= -1) { + attr = m.attributes.get(index); + assert attr instanceof Code_attribute; + cAttr = (Code_attribute)attr; + index = cAttr.attributes.getIndex(cf.constant_pool, annName); + if(index!= -1) + attr = cAttr.attributes.get(index); + } + break; + default: + name = cf.getName(); + index = cf.attributes.getIndex(cf.constant_pool, annName); + if(index!= -1) attr = cf.attributes.get(index); + } + } catch(ConstantPoolException cpe) { cpe.printStackTrace(); } + + if (index != -1) { + if(isTAattr) { //count RuntimeTypeAnnotations + RuntimeTypeAnnotations_attribute tAttr = + (RuntimeTypeAnnotations_attribute)attr; + System.out.println(testtype + ": " + name + ", " + annName + ": " + + tAttr.annotations.length ); + if (tAttr.annotations.length > 0) { + for (int i = 0; i < tAttr.annotations.length; i++) { + System.out.println(" types:" + tAttr.annotations[i].position.type); + } + } else { + System.out.println(""); + } + allt += tAttr.annotations.length; + if (visible) + tvisibles += tAttr.annotations.length; + else + tinvisibles += tAttr.annotations.length; + } else { + RuntimeAnnotations_attribute tAttr = + (RuntimeAnnotations_attribute)attr; + System.out.println(testtype + ": " + name + ", " + annName + ": " + + tAttr.annotations.length ); + all += tAttr.annotations.length; + if (visible) + visibles += tAttr.annotations.length; + else + invisibles += tAttr.annotations.length; + } + } + } } diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/DeadCode.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/DeadCode.java index f4b6921230c..893283a9450 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/DeadCode.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/DeadCode.java @@ -26,13 +26,18 @@ import java.io.*; import java.net.URL; import java.util.List; -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; /* * @test * @bug 6917130 8006775 * @summary test that optimized away annotations are not emited to classfile - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl */ public class DeadCode extends ClassfileTestHelper { @@ -43,14 +48,13 @@ public class DeadCode extends ClassfileTestHelper { public void run() throws Exception { expected_tinvisibles = 1; expected_tvisibles = 0; - - ClassFile cf = getClassFile("DeadCode$Test.class"); - test(cf); - for (Field f : cf.fields) { - test(cf, f); + ClassModel cm = getClassFile("DeadCode$Test.class"); + test(cm); + for (FieldModel fm : cm.fields()) { + test(fm); } - for (Method m: cf.methods) { - test(cf, m, true); + for (MethodModel mm: cm.methods()) { + test(mm, true); } countAnnotations(); diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/InstanceInitializer.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/InstanceInitializer.java index b5e24136c50..cdf9f78a935 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/InstanceInitializer.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/InstanceInitializer.java @@ -21,16 +21,20 @@ * questions. */ +import jdk.internal.classfile.*; import java.lang.annotation.*; import java.util.ArrayList; -import com.sun.tools.classfile.*; - /* * @test * @bug 8136419 8200301 * @summary test that type annotations on entities in initializers are emitted to classfile - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @compile -XDdeduplicateLambdas=false InstanceInitializer.java * @run main InstanceInitializer */ @@ -44,13 +48,13 @@ public class InstanceInitializer extends ClassfileTestHelper { expected_tinvisibles = 4; expected_tvisibles = 0; - ClassFile cf = getClassFile("InstanceInitializer$Test.class"); - test(cf); - for (Field f : cf.fields) { - test(cf, f); + ClassModel cm = getClassFile("InstanceInitializer$Test.class"); + test(cm); + for (FieldModel fm : cm.fields()) { + test(fm); } - for (Method m: cf.methods) { - test(cf, m, true); + for (MethodModel mm: cm.methods()) { + test(mm, true); } countAnnotations(); diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/NewTypeArguments.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/NewTypeArguments.java index 26c488e7061..8dff27a2108 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/NewTypeArguments.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/NewTypeArguments.java @@ -21,17 +21,22 @@ * questions. */ +import jdk.internal.classfile.*; import java.lang.annotation.*; import java.io.*; import java.net.URL; import java.util.List; -import com.sun.tools.classfile.*; /* * @test ClassLiterals * @summary test that new type arguments are emitted to classfile - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl */ public class NewTypeArguments extends ClassfileTestHelper{ @@ -43,13 +48,13 @@ public class NewTypeArguments extends ClassfileTestHelper{ expected_tinvisibles = 3; expected_tvisibles = 0; - ClassFile cf = getClassFile("NewTypeArguments$Test.class"); - test(cf); - for (Field f : cf.fields) { - test(cf, f); + ClassModel cm = getClassFile("NewTypeArguments$Test.class"); + test(cm); + for (FieldModel fm : cm.fields()) { + test(fm); } - for (Method m: cf.methods) { - test(cf, m, true); + for (MethodModel m: cm.methods()) { + test(m, true); } countAnnotations(); diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/NoTargetAnnotations.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/NoTargetAnnotations.java index adc37e7cf23..d38bee259e6 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/NoTargetAnnotations.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/NoTargetAnnotations.java @@ -27,29 +27,35 @@ import java.lang.annotation.RetentionPolicy; import java.net.URL; import java.util.List; -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; /* * @test NoTargetAnnotations * @summary test that annotations with no Target meta type is emitted * only once as declaration annotation - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl */ -public class NoTargetAnnotations { +public class NoTargetAnnotations extends ClassfileTestHelper { public static void main(String[] args) throws Exception { new NoTargetAnnotations().run(); } public void run() throws Exception { - ClassFile cf = getClassFile("NoTargetAnnotations$Test.class"); - for (Field f : cf.fields) { - test(cf, f); - testDeclaration(cf, f); + ClassModel cm = getClassFile("NoTargetAnnotations$Test.class"); + for (FieldModel fm : cm.fields()) { + test(fm); + testDeclaration(fm); } - for (Method m: cf.methods) { - test(cf, m); - testDeclaration(cf, m); + for (MethodModel mm: cm.methods()) { + test(mm); + testDeclaration(mm); } countAnnotations(); @@ -59,90 +65,34 @@ public class NoTargetAnnotations { System.out.println("PASSED"); } - ClassFile getClassFile(String name) throws IOException, ConstantPoolException { + ClassModel getClassFile(String name) throws IOException { URL url = getClass().getResource(name); - InputStream in = url.openStream(); - try { - return ClassFile.read(in); - } finally { - in.close(); + assert url != null; + try (InputStream in = url.openStream()) { + return Classfile.of().parse(in.readAllBytes()); } } - /************ Helper annotations counting methods ******************/ - void test(ClassFile cf, Method m) { - test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); - test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); + + void testDeclaration(AttributedElement m) { + testDecl(m, Attributes.RUNTIME_VISIBLE_ANNOTATIONS); + testDecl(m, Attributes.RUNTIME_INVISIBLE_ANNOTATIONS); } - void test(ClassFile cf, Field m) { - test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); - test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); - } - - void testDeclaration(ClassFile cf, Method m) { - testDecl(cf, m, Attribute.RuntimeVisibleAnnotations, true); - testDecl(cf, m, Attribute.RuntimeInvisibleAnnotations, false); - } - - void testDeclaration(ClassFile cf, Field m) { - testDecl(cf, m, Attribute.RuntimeVisibleAnnotations, true); - testDecl(cf, m, Attribute.RuntimeInvisibleAnnotations, false); - } - - // test the result of Attributes.getIndex according to expectations + // test the result of AttributedElement.findAttribute according to expectations // encoded in the method's name - void test(ClassFile cf, Method m, String name, boolean visible) { - int index = m.attributes.getIndex(cf.constant_pool, name); - if (index != -1) { - Attribute attr = m.attributes.get(index); - assert attr instanceof RuntimeTypeAnnotations_attribute; - RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; - all += tAttr.annotations.length; - if (visible) - visibles += tAttr.annotations.length; - else - invisibles += tAttr.annotations.length; - } - } - - // test the result of Attributes.getIndex according to expectations - // encoded in the method's name - void test(ClassFile cf, Field m, String name, boolean visible) { - int index = m.attributes.getIndex(cf.constant_pool, name); - if (index != -1) { - Attribute attr = m.attributes.get(index); - assert attr instanceof RuntimeTypeAnnotations_attribute; - RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; - all += tAttr.annotations.length; - if (visible) - visibles += tAttr.annotations.length; - else - invisibles += tAttr.annotations.length; - } - } - - // test the result of Attributes.getIndex according to expectations - // encoded in the method's name - void testDecl(ClassFile cf, Method m, String name, boolean visible) { - int index = m.attributes.getIndex(cf.constant_pool, name); - if (index != -1) { - Attribute attr = m.attributes.get(index); - assert attr instanceof RuntimeAnnotations_attribute; - RuntimeAnnotations_attribute tAttr = (RuntimeAnnotations_attribute)attr; - this.declAnnotations += tAttr.annotations.length; - } - } - - // test the result of Attributes.getIndex according to expectations - // encoded in the method's name - void testDecl(ClassFile cf, Field m, String name, boolean visible) { - int index = m.attributes.getIndex(cf.constant_pool, name); - if (index != -1) { - Attribute attr = m.attributes.get(index); - assert attr instanceof RuntimeAnnotations_attribute; - RuntimeAnnotations_attribute tAttr = (RuntimeAnnotations_attribute)attr; - this.declAnnotations += tAttr.annotations.length; + > void testDecl(AttributedElement m, AttributeMapper name) { + Attribute attr = m.findAttribute(name).orElse(null); + if (attr != null) { + switch (attr) { + case RuntimeVisibleAnnotationsAttribute tAttr -> { + this.declAnnotations += tAttr.annotations().size(); + } + case RuntimeInvisibleAnnotationsAttribute tAttr -> { + this.declAnnotations += tAttr.annotations().size(); + } + default -> throw new AssertionError(); + } } } diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/Scopes.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/Scopes.java index 12749be088d..baf913f88a1 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/Scopes.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/Scopes.java @@ -25,14 +25,19 @@ import java.lang.annotation.*; import java.io.*; import java.net.URL; import java.util.List; - -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; /* * @test * @bug 6843077 8006775 * @summary Qualified inner type annotation accessible to the class. - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl */ @Scopes.UniqueInner @@ -45,8 +50,8 @@ public class Scopes extends ClassfileTestH expected_tinvisibles = 1; expected_invisibles = 1; - ClassFile cf = getClassFile("Scopes.class"); - test(cf); + ClassModel cm = getClassFile("Scopes.class"); + test(cm); countAnnotations(); diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/StaticInitializer.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/StaticInitializer.java index e38387fca06..fe84a617d8d 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/StaticInitializer.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/StaticInitializer.java @@ -24,13 +24,18 @@ import java.lang.annotation.*; import java.util.ArrayList; -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; /* * @test * @bug 8136419 8200301 * @summary test that type annotations on entities in static initializers are emitted to classfile - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @compile -XDdeduplicateLambdas=false StaticInitializer.java * @run main StaticInitializer */ @@ -44,13 +49,13 @@ public class StaticInitializer extends ClassfileTestHelper { expected_tinvisibles = 4; expected_tvisibles = 0; - ClassFile cf = getClassFile("StaticInitializer$Test.class"); - test(cf); - for (Field f : cf.fields) { - test(cf, f); + ClassModel cm = getClassFile("StaticInitializer$Test.class"); + test(cm); + for (FieldModel fm : cm.fields()) { + test(fm); } - for (Method m: cf.methods) { - test(cf, m, true); + for (MethodModel mm: cm.methods()) { + test(mm, true); } countAnnotations(); diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/SyntheticParameters.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/SyntheticParameters.java index 44dd96d133e..817808d3577 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/SyntheticParameters.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/SyntheticParameters.java @@ -25,16 +25,22 @@ * @test SyntheticParameters * @summary Test generation of annotations on inner class parameters. * @library /lib/annotations/ - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @run main SyntheticParameters */ import annotations.classfile.ClassfileInspector; +import jdk.internal.classfile.ClassModel; +import jdk.internal.classfile.TypeAnnotation; import java.io.*; import java.lang.annotation.*; -import com.sun.tools.classfile.*; public class SyntheticParameters extends ClassfileInspector { @@ -72,8 +78,8 @@ public class SyntheticParameters extends ClassfileInspector { public static void main(String... args) throws Exception { new SyntheticParameters().run( - new ClassFile[] { getClassFile(Inner_class, Inner.class), - getClassFile(Foo_class, Foo.class) }, + new ClassModel[] { getClassFile(Inner_class, Inner.class), + getClassFile(Foo_class, Foo.class) }, new Expected[] { Inner_expected, Foo_expected }); } diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/T8008762.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/T8008762.java index 2a1a678a82c..e80a07b91b7 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/T8008762.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/T8008762.java @@ -26,13 +26,18 @@ * @bug 8008762 * @summary Type annotation on inner class in anonymous class * shows up as regular annotation - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl */ import java.lang.annotation.*; import static java.lang.annotation.RetentionPolicy.*; import static java.lang.annotation.ElementType.*; -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; public class T8008762 extends ClassfileTestHelper{ public static void main(String[] args) throws Exception { @@ -43,13 +48,13 @@ public class T8008762 extends ClassfileTestHelper{ expected_tinvisibles = 0; expected_tvisibles = 4; - ClassFile cf = getClassFile("T8008762$Test$1$InnerAnon.class"); - test(cf); - for (Field f : cf.fields) { - test(cf, f, false); + ClassModel cm = getClassFile("T8008762$Test$1$InnerAnon.class"); + test(cm); + for (FieldModel fm : cm.fields()) { + test(fm, false); } - for (Method m : cf.methods) { - test(cf, m, false); + for (MethodModel mm: cm.methods()) { + test(mm, false); } countAnnotations(); diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/T8008769.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/T8008769.java index d5598cdb593..18c5f90e0b3 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/T8008769.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/T8008769.java @@ -26,12 +26,17 @@ * @summary Repeated type-annotations on type parm of local variable * are not written to classfile. * @bug 8008769 - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl */ import java.lang.annotation.*; import static java.lang.annotation.RetentionPolicy.*; import static java.lang.annotation.ElementType.*; -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; public class T8008769 extends ClassfileTestHelper{ public static void main(String[] args) throws Exception { @@ -40,9 +45,9 @@ public class T8008769 extends ClassfileTestHelper{ public void run() throws Exception { expected_tvisibles = 4; - ClassFile cf = getClassFile("T8008769$Test.class"); - for (Method m : cf.methods) { - test(cf, m, true); + ClassModel cm = getClassFile("T8008769$Test.class"); + for (MethodModel mm : cm.methods()) { + test(mm, true); } countAnnotations(); diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/T8010015.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/T8010015.java index 725b9c7e2f3..9b8d5e6a4ce 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/T8010015.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/T8010015.java @@ -25,13 +25,18 @@ * @test * @summary Wrong classfile attribution in inner class of lambda expression. * @bug 8010015 - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl */ import java.lang.annotation.*; import static java.lang.annotation.RetentionPolicy.*; import static java.lang.annotation.ElementType.*; -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; /* * A type-annotations on a field in an inner class not in a lambda expression @@ -47,9 +52,9 @@ public class T8010015 extends ClassfileTestHelper{ public void run() throws Exception { expected_tvisibles = 1; expected_visibles = 1; - ClassFile cf = getClassFile("T8010015$Test$1innerClass.class"); - for (Field f : cf.fields) { - test(cf, f); + ClassModel cm = getClassFile("T8010015$Test$1innerClass.class"); + for (FieldModel fm : cm.fields()) { + test(fm); } countAnnotations(); diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/TestAnonInnerClasses.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/TestAnonInnerClasses.java index fbcfe67bf81..9e96b91d7b5 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/TestAnonInnerClasses.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/TestAnonInnerClasses.java @@ -33,13 +33,17 @@ * results in 2). Elements with no annotations expect 0. * Source template is read in from testanoninner.template * - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl */ -import java.lang.annotation.*; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; import java.io.*; -import java.util.List; -import java.util.LinkedList; -import com.sun.tools.classfile.*; +import java.util.*; import java.nio.file.Files; import java.nio.charset.*; import java.io.File; @@ -62,11 +66,11 @@ public class TestAnonInnerClasses extends ClassfileTestHelper { int tc = 0, xtc = 180; // 45 x 4 variations of repeated annotations. File testSrc = new File(System.getProperty("test.src")); - String[] AnnoAttributes = { - Attribute.RuntimeVisibleTypeAnnotations, - Attribute.RuntimeInvisibleTypeAnnotations, - Attribute.RuntimeVisibleAnnotations, - Attribute.RuntimeInvisibleAnnotations + AttributeMapper [] AnnoAttributes = new AttributeMapper[]{ + Attributes.RUNTIME_VISIBLE_TYPE_ANNOTATIONS, + Attributes.RUNTIME_INVISIBLE_TYPE_ANNOTATIONS, + Attributes.RUNTIME_VISIBLE_ANNOTATIONS, + Attributes.RUNTIME_INVISIBLE_ANNOTATIONS }; // template for source files @@ -146,7 +150,7 @@ public class TestAnonInnerClasses extends ClassfileTestHelper { System.out.println("PASSED all tests."); } - void test(String ttype, ClassFile cf, Method m, Field f, boolean visible) { + > void test(AttributedElement m) { int vtaActual = 0, itaActual = 0, vaActual = 0, @@ -160,122 +164,55 @@ public class TestAnonInnerClasses extends ClassfileTestHelper { String memberName = null, testcase = "undefined", testClassName = null; - Attribute attr = null, + Attribute attr = null, cattr = null; - Code_attribute CAttr = null; + CodeAttribute CAttr = null; // Get counts of 4 annotation Attributes on element being checked. - for (String AnnoType : AnnoAttributes) { - try { - switch (ttype) { - case "METHOD": - index = m.attributes.getIndex(cf.constant_pool, - AnnoType); - memberName = m.getName(cf.constant_pool); - if (index != -1) - attr = m.attributes.get(index); - //fetch index annotations from code attribute. - index2 = m.attributes.getIndex(cf.constant_pool, - Attribute.Code); - if (index2 != -1) { - cattr = m.attributes.get(index2); - assert cattr instanceof Code_attribute; - CAttr = (Code_attribute)cattr; - index2 = CAttr.attributes.getIndex(cf.constant_pool, - AnnoType); - if (index2 != -1) - cattr = CAttr.attributes.get(index2); - } - break; - case "FIELD": - index = f.attributes.getIndex(cf.constant_pool, - AnnoType); - memberName = f.getName(cf.constant_pool); - if (index != -1) - attr = f.attributes.get(index); - //fetch index annotations from code attribute. - index2 = cf.attributes.getIndex(cf.constant_pool, - Attribute.Code); - if (index2!= -1) { - cattr = cf.attributes.get(index2); - assert cattr instanceof Code_attribute; - CAttr = (Code_attribute)cattr; - index2 = CAttr.attributes.getIndex(cf.constant_pool, - AnnoType); - if (index2!= -1) - cattr = CAttr.attributes.get(index2); - } - break; - - default: - memberName = cf.getName(); - index = cf.attributes.getIndex(cf.constant_pool, - AnnoType); - if (index!= -1) attr = cf.attributes.get(index); - break; + for (AttributeMapper Anno : AnnoAttributes) { + AttributeMapper AnnoType = (AttributeMapper) Anno; + if (Objects.requireNonNull(m) instanceof ClassModel) { + ClassModel cm = (ClassModel) m; + memberName = cm.thisClass().name().stringValue(); + attr = m.findAttribute(AnnoType).orElse(null); + } else { + memberName = m instanceof MethodModel ? + ((MethodModel) m).methodName().stringValue() : ((FieldModel) m).fieldName().stringValue(); + attr = m.findAttribute(AnnoType).orElse(null); + //fetch index annotations from code attribute. + CAttr = m.findAttribute(Attributes.CODE).orElse(null); + if (CAttr != null) { + cattr = CAttr.findAttribute(AnnoType).orElse(null); } } - catch (ConstantPoolException cpe) { cpe.printStackTrace(); } - try { - testClassName=cf.getName(); - testcase = ttype + ": " + testClassName + ": " + - memberName + ", "; - } - catch (ConstantPoolException cpe) { cpe.printStackTrace(); } - if (index != -1) { - switch (AnnoType) { - case Attribute.RuntimeVisibleTypeAnnotations: - //count RuntimeVisibleTypeAnnotations - RuntimeVisibleTypeAnnotations_attribute RVTAa = - (RuntimeVisibleTypeAnnotations_attribute)attr; - vtaActual += RVTAa.annotations.length; - break; - case Attribute.RuntimeVisibleAnnotations: - //count RuntimeVisibleAnnotations - RuntimeVisibleAnnotations_attribute RVAa = - (RuntimeVisibleAnnotations_attribute)attr; - vaActual += RVAa.annotations.length; - break; - case Attribute.RuntimeInvisibleTypeAnnotations: - //count RuntimeInvisibleTypeAnnotations - RuntimeInvisibleTypeAnnotations_attribute RITAa = - (RuntimeInvisibleTypeAnnotations_attribute)attr; - itaActual += RITAa.annotations.length; - break; - case Attribute.RuntimeInvisibleAnnotations: - //count RuntimeInvisibleAnnotations - RuntimeInvisibleAnnotations_attribute RIAa = - (RuntimeInvisibleAnnotations_attribute)attr; - iaActual += RIAa.annotations.length; - break; + ; +// testClassName=cm.getName(); +// testcase = ttype + ": " + testClassName + ": " + +// memberName + ", "; + if (attr != null) { + switch (attr) { + case RuntimeVisibleTypeAnnotationsAttribute RVTAa -> //count RuntimeVisibleTypeAnnotations + vtaActual += RVTAa.annotations().size(); + case RuntimeVisibleAnnotationsAttribute RVAa -> //count RuntimeVisibleAnnotations + vaActual += RVAa.annotations().size(); + case RuntimeInvisibleTypeAnnotationsAttribute RITAa -> //count RuntimeInvisibleTypeAnnotations + itaActual += RITAa.annotations().size(); + case RuntimeInvisibleAnnotationsAttribute RIAa -> //count RuntimeInvisibleAnnotations + iaActual += RIAa.annotations().size(); + default -> throw new AssertionError(); } } // annotations from code attribute. - if (index2 != -1) { - switch (AnnoType) { - case Attribute.RuntimeVisibleTypeAnnotations: - //count RuntimeVisibleTypeAnnotations - RuntimeVisibleTypeAnnotations_attribute RVTAa = - (RuntimeVisibleTypeAnnotations_attribute)cattr; - vtaActual += RVTAa.annotations.length; - break; - case Attribute.RuntimeVisibleAnnotations: - //count RuntimeVisibleAnnotations - RuntimeVisibleAnnotations_attribute RVAa = - (RuntimeVisibleAnnotations_attribute)cattr; - vaActual += RVAa.annotations.length; - break; - case Attribute.RuntimeInvisibleTypeAnnotations: - //count RuntimeInvisibleTypeAnnotations - RuntimeInvisibleTypeAnnotations_attribute RITAa = - (RuntimeInvisibleTypeAnnotations_attribute)cattr; - itaActual += RITAa.annotations.length; - break; - case Attribute.RuntimeInvisibleAnnotations: - //count RuntimeInvisibleAnnotations - RuntimeInvisibleAnnotations_attribute RIAa = - (RuntimeInvisibleAnnotations_attribute)cattr; - iaActual += RIAa.annotations.length; - break; + if (cattr != null) { + switch (cattr) { + case RuntimeVisibleTypeAnnotationsAttribute RVTAa -> //count RuntimeVisibleTypeAnnotations + vtaActual += RVTAa.annotations().size(); + case RuntimeVisibleAnnotationsAttribute RVAa -> //count RuntimeVisibleAnnotations + vaActual += RVAa.annotations().size(); + case RuntimeInvisibleTypeAnnotationsAttribute RITAa -> //count RuntimeInvisibleTypeAnnotations + itaActual += RITAa.annotations().size(); + case RuntimeInvisibleAnnotationsAttribute RIAa -> //count RuntimeInvisibleAnnotations + iaActual += RIAa.annotations().size(); + default -> throw new AssertionError(); } } } @@ -343,8 +280,8 @@ public class TestAnonInnerClasses extends ClassfileTestHelper { vtaActual,itaActual,vaActual,iaActual); } - public void run() { - ClassFile cf = null; + public >void run() { + ClassModel cm = null; InputStream in = null; int testcount = 1; File testFile = null; @@ -384,15 +321,16 @@ public class TestAnonInnerClasses extends ClassfileTestHelper { 0,classFile.getAbsolutePath().indexOf(classFile.getPath())); for (String clazz : classes) { try { - cf = ClassFile.read(new File(testloc+clazz)); + cm = Classfile.of().parse(new File(testloc+clazz).toPath()); } catch (Exception e) { e.printStackTrace(); } // Test for all methods and fields - for (Method m: cf.methods) { - test("METHOD", cf, m, null, true); + assert cm != null; + for (MethodModel m: cm.methods()) { + test(m); } - for (Field f: cf.fields) { - test("FIELD", cf, null, f, true); + for (FieldModel f: cm.fields()) { + test(f); } } } diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/TestNewCastArray.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/TestNewCastArray.java index 2ee2673ad0a..e5f0622f42f 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/TestNewCastArray.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/TestNewCastArray.java @@ -25,14 +25,21 @@ * @test * @bug 8005681 * @summary Repeated annotations on new,array,cast. - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl */ +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; import java.lang.annotation.*; import java.io.*; import java.util.List; -import com.sun.tools.classfile.*; import java.lang.annotation.*; +import java.util.Objects; import static java.lang.annotation.RetentionPolicy.*; import static java.lang.annotation.ElementType.*; @@ -73,67 +80,53 @@ public class TestNewCastArray { System.out.println("PASS"); } - void test(String clazz, String ttype, ClassFile cf, Method m, Field f, - String name, boolean codeattr) { + > void test(String clazz, AttributedElement m, AttributeMapper name, Boolean codeattr) { int actual = 0; int expected = 0, cexpected = 0; - int index = 0; - String memberName = null; - Attribute attr = null; - Code_attribute cAttr = null; - String testcase = "undefined"; - try { - switch(ttype) { - case "METHOD": - index = m.attributes.getIndex(cf.constant_pool, name); - memberName = m.getName(cf.constant_pool); - if(index != -1) - attr = m.attributes.get(index); - break; - case "MCODE": - memberName = m.getName(cf.constant_pool); - //fetch index of and code attribute and annotations from code attribute. - index = m.attributes.getIndex(cf.constant_pool, Attribute.Code); - if(index!= -1) { - attr = m.attributes.get(index); - assert attr instanceof Code_attribute; - cAttr = (Code_attribute)attr; - index = cAttr.attributes.getIndex(cf.constant_pool, name); - if(index!= -1) - attr = cAttr.attributes.get(index); + String memberName; + Attribute attr = null; + CodeAttribute cAttr; + String testcase; + switch (m) { + case MethodModel mm -> { + memberName = mm.methodName().stringValue(); + if(codeattr) { + //fetch index of and code attribute and annotations from code attribute. + cAttr = mm.findAttribute(Attributes.CODE).orElse(null); + if(cAttr != null) { + attr = cAttr.findAttribute(name).orElse(null); + } + } else { + attr = mm.findAttribute(name).orElse(null); } - break; - case "FIELD": - index = f.attributes.getIndex(cf.constant_pool, name); - memberName = f.getName(cf.constant_pool); - if(index != -1) - attr = f.attributes.get(index); - break; - case "CODE": - memberName = f.getName(cf.constant_pool); - //fetch index of and code attribute and annotations from code attribute. - index = cf.attributes.getIndex(cf.constant_pool, Attribute.Code); - if(index!= -1) { - attr = cf.attributes.get(index); - assert attr instanceof Code_attribute; - cAttr = (Code_attribute)attr; - index = cAttr.attributes.getIndex(cf.constant_pool, name); - if(index!= -1) - attr = cAttr.attributes.get(index); + } + case FieldModel fm -> { + memberName = fm.fieldName().stringValue(); + if(codeattr) { + cAttr = fm.findAttribute(Attributes.CODE).orElse(null); + if(cAttr != null) { + attr = cAttr.findAttribute(name).orElse(null); + } + } else { + attr = fm.findAttribute(name).orElse(null); } - break; - default: - break; + } + default -> throw new AssertionError(); } - } catch(ConstantPoolException cpe) { cpe.printStackTrace(); } - testcase = clazz+" "+ttype + ": " + memberName + ", " + name; - if(index != -1) { + testcase = clazz+" , Local: "+ codeattr + ": " + memberName + ", " + name; + if(attr != null) { //count RuntimeTypeAnnotations - assert attr instanceof RuntimeTypeAnnotations_attribute; - RuntimeTypeAnnotations_attribute tAttr = - (RuntimeTypeAnnotations_attribute)attr; - actual += tAttr.annotations.length; + switch (attr) { + case RuntimeVisibleTypeAnnotationsAttribute tAttr -> { + actual += tAttr.annotations().size(); + } + case RuntimeInvisibleTypeAnnotationsAttribute tAttr -> { + actual += tAttr.annotations().size(); + } + default -> throw new AssertionError(); + } } + assert memberName != null; if(memberName.compareTo("")==0) memberName=clazz+memberName; switch ( memberName ) { //METHOD: @@ -204,27 +197,28 @@ public class TestNewCastArray { } public void run() { - ClassFile cf = null; - InputStream in = null; + ClassModel cm = null; + InputStream in; for( String clazz : testclasses) { String testclazz = "TestNewCastArray$" + clazz + ".class"; System.out.println("Testing " + testclazz); try { - in = getClass().getResource(testclazz).openStream(); - cf = ClassFile.read(in); + in = Objects.requireNonNull(getClass().getResource(testclazz)).openStream(); + cm = Classfile.of().parse(in.readAllBytes()); in.close(); } catch(Exception e) { e.printStackTrace(); } + assert cm != null; if(clazz.startsWith("Test1")) { - for (Field f: cf.fields) - test(clazz, "FIELD", cf, null, f, Attribute.RuntimeVisibleTypeAnnotations, false); - for (Method m: cf.methods) - test(clazz, "METHOD", cf, m, null, Attribute.RuntimeVisibleTypeAnnotations, false); + for (FieldModel fm: cm.fields()) + test(clazz, fm, Attributes.RUNTIME_VISIBLE_TYPE_ANNOTATIONS, false); + for (MethodModel mm: cm.methods()) + test(clazz, mm, Attributes.RUNTIME_VISIBLE_TYPE_ANNOTATIONS, false); } else { - for (Field f: cf.fields) - test(clazz, "CODE", cf, null, f, Attribute.RuntimeVisibleTypeAnnotations, true); - for (Method m: cf.methods) - test(clazz, "MCODE", cf, m, null, Attribute.RuntimeVisibleTypeAnnotations, true); + for (FieldModel fm: cm.fields()) + test(clazz, fm, Attributes.RUNTIME_VISIBLE_TYPE_ANNOTATIONS, true); + for (MethodModel mm: cm.methods()) + test(clazz, mm, Attributes.RUNTIME_VISIBLE_TYPE_ANNOTATIONS, true); } } report(); @@ -350,7 +344,7 @@ public class TestNewCastArray { // Cast expressions static class Test5a { Test5a(){} - Object o = new Integer(1); + Object o = 1; // expect 2+2=4 Integer ci11 = (@A @B Integer)o; // OK expect 3, got 3 Integer ci21 = (@A @A @B Integer)o; // OK expect 3, got 3 @@ -358,7 +352,7 @@ public class TestNewCastArray { static class Test5b { Test5b(){} - Object o = new Integer(1); + Object o = 1; // Cast expressions // expect 1+2=3 Integer ci2 = (@A @A Integer)o; // FAIL expect 2, got 1 diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/TypeAnnotationPropagationTest.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/TypeAnnotationPropagationTest.java index d58e55111d2..9f8622386eb 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/TypeAnnotationPropagationTest.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/TypeAnnotationPropagationTest.java @@ -25,21 +25,24 @@ * @test * @bug 8144185 * @summary javac produces incorrect RuntimeInvisibleTypeAnnotations length attribute - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl */ import static java.lang.annotation.ElementType.TYPE_USE; import static java.lang.annotation.RetentionPolicy.RUNTIME; -import com.sun.tools.classfile.Attribute; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.Code_attribute; -import com.sun.tools.classfile.Method; -import com.sun.tools.classfile.RuntimeVisibleTypeAnnotations_attribute; -import com.sun.tools.classfile.TypeAnnotation; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.CodeAttribute; +import jdk.internal.classfile.attribute.RuntimeVisibleTypeAnnotationsAttribute; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.util.Arrays; +import java.util.List; import java.util.Objects; public class TypeAnnotationPropagationTest extends ClassfileTestHelper { @@ -48,26 +51,36 @@ public class TypeAnnotationPropagationTest extends ClassfileTestHelper { } public void run() throws Exception { - ClassFile cf = getClassFile("TypeAnnotationPropagationTest$Test.class"); + ClassModel cm = getClassFile("TypeAnnotationPropagationTest$Test.class"); - Method f = null; - for (Method m : cf.methods) { - if (m.getName(cf.constant_pool).contains("f")) { - f = m; + MethodModel f = null; + for (MethodModel mm : cm.methods()) { + if (mm.methodName().stringValue().contains("f")) { + f = mm; break; } } - int idx = f.attributes.getIndex(cf.constant_pool, Attribute.Code); - Code_attribute cattr = (Code_attribute) f.attributes.get(idx); - idx = cattr.attributes.getIndex(cf.constant_pool, Attribute.RuntimeVisibleTypeAnnotations); - RuntimeVisibleTypeAnnotations_attribute attr = - (RuntimeVisibleTypeAnnotations_attribute) cattr.attributes.get(idx); + assert f != null; + CodeAttribute cattr = f.findAttribute(Attributes.CODE).orElse(null); + assert cattr != null; + RuntimeVisibleTypeAnnotationsAttribute attr = cattr.findAttribute(Attributes.RUNTIME_VISIBLE_TYPE_ANNOTATIONS).orElse(null); - TypeAnnotation anno = attr.annotations[0]; - assertEquals(anno.position.lvarOffset, new int[] {3}, "start_pc"); - assertEquals(anno.position.lvarLength, new int[] {8}, "length"); - assertEquals(anno.position.lvarIndex, new int[] {1}, "index"); + assert attr != null; + List annosPosition = ((TypeAnnotation.LocalVarTarget) attr.annotations().get(0).targetInfo()).table(); + int[] lvarOffset = annosPosition.stream() + .map(e -> cattr.labelToBci(e.startLabel())) + .mapToInt(t -> t).toArray(); + int[] lvarLength = annosPosition.stream() + .map(e -> cattr.labelToBci(e.endLabel()) - cattr.labelToBci(e.startLabel())) + .mapToInt(t -> t).toArray(); + int[] lvarIndex = annosPosition.stream() + .map(TypeAnnotation.LocalVarTargetInfo::index) + .mapToInt(t -> t).toArray(); + + assertEquals(lvarOffset, new int[] {3}, "start_pc"); + assertEquals(lvarLength, new int[] {8}, "length"); + assertEquals(lvarIndex, new int[] {1}, "index"); } void assertEquals(int[] actual, int[] expected, String message) { diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/TypeCasts.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/TypeCasts.java index dcf92853187..cca0af32abc 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/TypeCasts.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/TypeCasts.java @@ -26,14 +26,18 @@ import java.io.*; import java.net.URL; import java.util.List; -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; /* * @test * @bug 6843077 8006775 * @summary test that typecasts annotation are emitted if only the cast * expression is optimized away - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components */ public class TypeCasts extends ClassfileTestHelper{ @@ -44,14 +48,13 @@ public class TypeCasts extends ClassfileTestHelper{ public void run() throws Exception { expected_tinvisibles = 4; expected_tvisibles = 0; - - ClassFile cf = getClassFile("TypeCasts$Test.class"); - test(cf); - for (Field f : cf.fields) { - test(cf, f); + ClassModel cm = getClassFile("TypeCasts$Test.class"); + test(cm); + for (FieldModel fm : cm.fields()) { + test(fm); } - for (Method m: cf.methods) { - test(cf, m, true); + for (MethodModel mm: cm.methods()) { + test(mm, true); } countAnnotations(); diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/Wildcards.java b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/Wildcards.java index 9dec88b5813..0b54fdbe58c 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/classfile/Wildcards.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/classfile/Wildcards.java @@ -26,13 +26,17 @@ import java.io.*; import java.net.URL; import java.util.List; -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; /* * @test Wildcards * @bug 6843077 8006775 * @summary test that annotations target wildcards get emitted to classfile - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components */ public class Wildcards extends ClassfileTestHelper { public static void main(String[] args) throws Exception { @@ -43,13 +47,13 @@ public class Wildcards extends ClassfileTestHelper { expected_tinvisibles = 3; expected_tvisibles = 0; - ClassFile cf = getClassFile("Wildcards$Test.class"); - test(cf); - for (Field f : cf.fields) { - test(cf, f); + ClassModel cm = getClassFile("Wildcards$Test.class"); + test(cm); + for (FieldModel fm : cm.fields()) { + test(fm); } - for (Method m: cf.methods) { - test(cf, m,false); + for (MethodModel mm: cm.methods()) { + test(mm,false); } countAnnotations(); diff --git a/test/langtools/tools/javac/classfiles/attributes/AnnotationDefault/AnnotationDefaultTest.java b/test/langtools/tools/javac/classfiles/attributes/AnnotationDefault/AnnotationDefaultTest.java index bfced7f5a01..5aafc65fb36 100644 --- a/test/langtools/tools/javac/classfiles/attributes/AnnotationDefault/AnnotationDefaultTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/AnnotationDefault/AnnotationDefaultTest.java @@ -28,24 +28,31 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build AnnotationDefaultTest AnnotationDefaultVerifier * @run main AnnotationDefaultTest */ +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; import java.io.File; import java.io.IOException; import java.lang.annotation.RetentionPolicy; import java.nio.file.Files; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Objects; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; -import com.sun.tools.classfile.*; public class AnnotationDefaultTest extends TestResult { @@ -68,14 +75,13 @@ public class AnnotationDefaultTest extends TestResult { // Map Map expectedValues = getExpectedValues(forName(className, fileManager)); - ClassFile classFile = readClassFile(fileManager.getClasses().get(className)); + ClassModel classFile = readClassFile(fileManager.getClasses().get(className)); - for (Method method : classFile.methods) { - String methodName = method.getName(classFile.constant_pool); + for (MethodModel method : classFile.methods()) { + String methodName = method.methodName().stringValue(); printf("Testing method : %s\n", methodName); - AnnotationDefault_attribute attr = - (AnnotationDefault_attribute) method.attributes - .get(Attribute.AnnotationDefault); + AnnotationDefaultAttribute attr = + method.findAttribute(Attributes.ANNOTATION_DEFAULT).orElse(null); if (hasDefault && !checkNotNull(attr, "Attribute is not null") || !hasDefault && checkNull(attr, "Attribute is null")) { @@ -83,20 +89,19 @@ public class AnnotationDefaultTest extends TestResult { continue; } - checkEquals(countNumberOfAttributes(method.attributes.attrs), - 1l, + checkEquals(countNumberOfAttributes(method.attributes()), + 1L, "Number of AnnotationDefault attribute"); - checkEquals(classFile.constant_pool - .getUTF8Value(attr.attribute_name_index), + checkEquals(attr.attributeName(), "AnnotationDefault", "attribute_name_index"); ExpectedValues expectedValue = expectedValues.get(methodName); - checkEquals((char) attr.default_value.tag, expectedValue.tag(), + checkEquals(attr.defaultValue().tag(), expectedValue.tag(), String.format("check tag : %c %s", expectedValue.tag(), expectedValue.name())); - verifier.testElementValue(attr.default_value.tag, - this, classFile, attr.default_value, + verifier.testElementValue((int)attr.defaultValue().tag(), + this, classFile, attr.defaultValue(), expectedValue.values()); - verifier.testLength(attr.default_value.tag, this, attr); + verifier.testLength((int)attr.defaultValue().tag(), this, attr); } } catch (Exception e) { addFailure(e); @@ -124,9 +129,9 @@ public class AnnotationDefaultTest extends TestResult { return ans; } - private long countNumberOfAttributes(Attribute[] attrs) { - return Stream.of(attrs) - .filter(x -> x instanceof AnnotationDefault_attribute) + private long countNumberOfAttributes(List> attrs) { + return attrs.stream() + .filter(x -> x instanceof AnnotationDefaultAttribute) .count(); } diff --git a/test/langtools/tools/javac/classfiles/attributes/AnnotationDefault/AnnotationDefaultVerifier.java b/test/langtools/tools/javac/classfiles/attributes/AnnotationDefault/AnnotationDefaultVerifier.java index f9384d26d9b..0a10e710c9a 100644 --- a/test/langtools/tools/javac/classfiles/attributes/AnnotationDefault/AnnotationDefaultVerifier.java +++ b/test/langtools/tools/javac/classfiles/attributes/AnnotationDefault/AnnotationDefaultVerifier.java @@ -21,10 +21,10 @@ * questions. */ -import com.sun.tools.classfile.Annotation; -import com.sun.tools.classfile.AnnotationDefault_attribute; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.ConstantPool; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; +import jdk.internal.classfile.constantpool.ConstantPoolBuilder; +import jdk.internal.classfile.impl.*; import java.util.Arrays; import java.util.HashMap; @@ -51,13 +51,12 @@ public class AnnotationDefaultVerifier { verifiers.put((int) '@', new TestAnnotationElementValue()); } - public void testLength(int tag, TestResult testResult, AnnotationDefault_attribute attr) { + public void testLength(int tag, TestResult testResult, AnnotationDefaultAttribute attr) { verifiers.get(tag).testLength(testResult, attr); } - public void testElementValue(int tag, TestResult testResult, ClassFile classFile, - Annotation.element_value element_value, String[] values) - throws ConstantPool.UnexpectedEntry, ConstantPool.InvalidIndex { + public void testElementValue(int tag, TestResult testResult, ClassModel classFile, + AnnotationValue element_value, String[] values) { get(tag).testElementValue(testResult, classFile, element_value, values); } @@ -70,8 +69,10 @@ public class AnnotationDefaultVerifier { } private abstract class TestElementValue { - public void testLength(TestResult testCase, AnnotationDefault_attribute attr) { - testCase.checkEquals(attr.attribute_length, 1 + attr.default_value.length(), + public void testLength(TestResult testCase, AnnotationDefaultAttribute attr) { + BufWriter buf = new BufWriterImpl(ConstantPoolBuilder.of(), (ClassfileImpl) Classfile.of()); + attr.defaultValue().writeTo(buf); + testCase.checkEquals(((BoundAttribute)attr).payloadLen(), buf.size(), "attribute_length"); } @@ -85,10 +86,9 @@ public class AnnotationDefaultVerifier { public abstract void testElementValue( TestResult testCase, - ClassFile classFile, - Annotation.element_value element_value, - String[] values) - throws ConstantPool.InvalidIndex, ConstantPool.UnexpectedEntry; + ClassModel classFile, + AnnotationValue element_value, + String[] values); } private class TestIntegerElementValue extends TestElementValue { @@ -96,15 +96,26 @@ public class AnnotationDefaultVerifier { @Override public void testElementValue( TestResult testCase, - ClassFile classFile, - Annotation.element_value element_value, - String[] values) throws ConstantPool.InvalidIndex { - Annotation.Primitive_element_value ev = - (Annotation.Primitive_element_value) element_value; - ConstantPool.CONSTANT_Integer_info info = - (ConstantPool.CONSTANT_Integer_info) - classFile.constant_pool.get(ev.const_value_index); - testCase.checkEquals(info.value, Integer.parseInt(values[0]), "const_value_index"); + ClassModel classFile, + AnnotationValue element_value, + String[] values) { + switch (element_value) { + case AnnotationValue.OfByte ev -> { + testCase.checkEquals((int)ev.byteValue(), Integer.parseInt(values[0]), "const_value_index"); + } + case AnnotationValue.OfCharacter ev -> { + testCase.checkEquals((int)ev.charValue(), Integer.parseInt(values[0]), "const_value_index"); + } + case AnnotationValue.OfShort ev -> { + testCase.checkEquals((int)ev.shortValue(), Integer.parseInt(values[0]), "const_value_index"); + } + case AnnotationValue.OfBoolean ev -> { + testCase.checkEquals(ev.booleanValue()? 1: 0, Integer.parseInt(values[0]), "const_value_index"); + } + default -> { + testCase.checkEquals(((AnnotationValue.OfInteger) element_value).intValue(), Integer.parseInt(values[0]), "const_value_index"); + } + } } } @@ -112,15 +123,12 @@ public class AnnotationDefaultVerifier { @Override public void testElementValue( TestResult testCase, - ClassFile classFile, - Annotation.element_value element_value, - String[] values) throws ConstantPool.InvalidIndex { - Annotation.Primitive_element_value ev = - (Annotation.Primitive_element_value) element_value; - ConstantPool.CONSTANT_Long_info info = - (ConstantPool.CONSTANT_Long_info) - classFile.constant_pool.get(ev.const_value_index); - testCase.checkEquals(info.value, Long.parseLong(values[0]), "const_value_index"); + ClassModel classFile, + AnnotationValue element_value, + String[] values) { + AnnotationValue.OfLong ev = + (AnnotationValue.OfLong) element_value; + testCase.checkEquals(ev.longValue(), Long.parseLong(values[0]), "const_value_index"); } } @@ -128,15 +136,12 @@ public class AnnotationDefaultVerifier { @Override public void testElementValue( TestResult testCase, - ClassFile classFile, - Annotation.element_value element_value, - String[] values) throws ConstantPool.InvalidIndex { - Annotation.Primitive_element_value ev = - (Annotation.Primitive_element_value) element_value; - ConstantPool.CONSTANT_Float_info info = - (ConstantPool.CONSTANT_Float_info) - classFile.constant_pool.get(ev.const_value_index); - testCase.checkEquals(info.value, Float.parseFloat(values[0]), "const_value_index"); + ClassModel classFile, + AnnotationValue element_value, + String[] values) { + AnnotationValue.OfFloat ev = + (AnnotationValue.OfFloat) element_value; + testCase.checkEquals(ev.floatValue(), Float.parseFloat(values[0]), "const_value_index"); } } @@ -144,15 +149,12 @@ public class AnnotationDefaultVerifier { @Override public void testElementValue( TestResult testCase, - ClassFile classFile, - Annotation.element_value element_value, - String[] values) throws ConstantPool.InvalidIndex { - Annotation.Primitive_element_value ev = - (Annotation.Primitive_element_value) element_value; - ConstantPool.CONSTANT_Double_info info = - (ConstantPool.CONSTANT_Double_info) - classFile.constant_pool.get(ev.const_value_index); - testCase.checkEquals(info.value, Double.parseDouble(values[0]), "const_value_index"); + ClassModel classFile, + AnnotationValue element_value, + String[] values) { + AnnotationValue.OfDouble ev = + (AnnotationValue.OfDouble) element_value; + testCase.checkEquals(ev.doubleValue(), Double.parseDouble(values[0]), "const_value_index"); } } @@ -160,15 +162,12 @@ public class AnnotationDefaultVerifier { @Override public void testElementValue( TestResult testCase, - ClassFile classFile, - Annotation.element_value element_value, - String[] values) throws ConstantPool.InvalidIndex { - Annotation.Primitive_element_value ev = - (Annotation.Primitive_element_value) element_value; - ConstantPool.CONSTANT_Utf8_info info = - (ConstantPool.CONSTANT_Utf8_info) - classFile.constant_pool.get(ev.const_value_index); - testCase.checkEquals(info.value, values[0], "const_value_index"); + ClassModel classFile, + AnnotationValue element_value, + String[] values) { + AnnotationValue.OfString ev = + (AnnotationValue.OfString) element_value; + testCase.checkEquals(ev.stringValue(), values[0], "const_value_index"); } } @@ -182,14 +181,13 @@ public class AnnotationDefaultVerifier { @Override public void testElementValue( TestResult testCase, - ClassFile classFile, - Annotation.element_value element_value, - String[] values) - throws ConstantPool.InvalidIndex, ConstantPool.UnexpectedEntry { - Annotation.Enum_element_value ev = (Annotation.Enum_element_value) element_value; - testCase.checkEquals(classFile.constant_pool.getUTF8Info(ev.type_name_index).value, + ClassModel classFile, + AnnotationValue element_value, + String[] values) { + AnnotationValue.OfEnum ev = (AnnotationValue.OfEnum) element_value; + testCase.checkEquals(ev.classSymbol().descriptorString(), values[0], "type_name_index"); - testCase.checkEquals(classFile.constant_pool.getUTF8Info(ev.const_name_index).value, + testCase.checkEquals(ev.constantName().stringValue(), values[1], "const_name_index"); } } @@ -198,20 +196,19 @@ public class AnnotationDefaultVerifier { @Override public void testElementValue( TestResult testCase, - ClassFile classFile, - Annotation.element_value element_value, - String[] values) - throws ConstantPool.InvalidIndex, ConstantPool.UnexpectedEntry { - Annotation.Class_element_value ev = (Annotation.Class_element_value) element_value; + ClassModel classFile, + AnnotationValue element_value, + String[] values) { + AnnotationValue.OfClass ev = (AnnotationValue.OfClass) element_value; testCase.checkEquals( - classFile.constant_pool.getUTF8Info(ev.class_info_index).value, + ev.classSymbol().descriptorString(), values[0], "class_info_index"); } } private class TestAnnotationElementValue extends TestElementValue { @Override - public void testLength(TestResult testCase, AnnotationDefault_attribute attr) { + public void testLength(TestResult testCase, AnnotationDefaultAttribute attr) { // Suppress, since it is hard to test the length of this kind of element values. } @@ -225,60 +222,49 @@ public class AnnotationDefaultVerifier { @Override public void testElementValue( TestResult testCase, - ClassFile classFile, - Annotation.element_value element_value, - String[] values) - throws ConstantPool.InvalidIndex, ConstantPool.UnexpectedEntry { - Annotation ev = ((Annotation.Annotation_element_value) element_value) - .annotation_value; + ClassModel classFile, + AnnotationValue element_value, + String[] values) { + Annotation ev = ((AnnotationValue.OfAnnotation) element_value) + .annotation(); testCase.checkEquals( - classFile.constant_pool.getUTF8Info(ev.type_index).value, + ev.classSymbol().descriptorString(), values[0], "type_index"); - for (int i = 0; i < ev.num_element_value_pairs; ++i) { - Annotation.element_value_pair pair = ev.element_value_pairs[i]; + for (int i = 0; i < ev.elements().size(); ++i) { + AnnotationElement pair = ev.elements().get(i); testCase.checkEquals( - classFile.constant_pool.getUTF8Info(pair.element_name_index).value, + pair.name().stringValue(), values[2 * i + 1], "element_name_index"); - TestElementValue testElementValue = verifiers.get(pair.value.tag); + TestElementValue testElementValue = verifiers.get((int)pair.value().tag()); testElementValue.testElementValue( testCase, classFile, - pair.value, + pair.value(), new String[]{values[2 * i + 2]}); } } } private class TestArrayElementValue extends TestElementValue { - @Override - public void testLength(TestResult testCase, AnnotationDefault_attribute attr) { - Annotation.Array_element_value ev = - (Annotation.Array_element_value) attr.default_value; - int sizeOfTag = ev.values[0].tag == 'e' ? 0 : 1; - // tag (1 byte) + array header (2 byte) + length of entries - testCase.checkEquals(attr.attribute_length, 1 + 2 + - (sizeOfTag + ev.length() / ev.num_values) * ev.num_values, "attribute_length"); - } - + // testLength method is the same as in TestElementValue class @Override public void testElementValue( TestResult testCase, - ClassFile classFile, - Annotation.element_value element_value, - String[] values) - throws ConstantPool.InvalidIndex, ConstantPool.UnexpectedEntry { - Annotation.Array_element_value ev = - (Annotation.Array_element_value) element_value; + ClassModel classFile, + AnnotationValue element_value, + String[] values) { + AnnotationValue.OfArray ev = + (AnnotationValue.OfArray) element_value; int index = 0; - for (int i = 0; i < ev.num_values; ++i) { - TestElementValue testElementValue = verifiers.get(ev.values[i].tag); + for (int i = 0; i < ev.values().size(); ++i) { + TestElementValue testElementValue = verifiers.get((int)ev.values().get(i).tag()); int length = testElementValue.getLength(); testElementValue.testElementValue( testCase, classFile, - ev.values[i], + ev.values().get(i), testElementValue.getValues(values, index, length)); index += length; } diff --git a/test/langtools/tools/javac/classfiles/attributes/EnclosingMethod/EnclosingMethodTest.java b/test/langtools/tools/javac/classfiles/attributes/EnclosingMethod/EnclosingMethodTest.java index 24887dd800c..837fb64287b 100644 --- a/test/langtools/tools/javac/classfiles/attributes/EnclosingMethod/EnclosingMethodTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/EnclosingMethod/EnclosingMethodTest.java @@ -28,14 +28,19 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @run main EnclosingMethodTest */ -import com.sun.tools.classfile.Attribute; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.EnclosingMethod_attribute; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.EnclosingMethodAttribute; +import jdk.internal.classfile.impl.BoundAttribute; import java.io.File; import java.io.FilenameFilter; @@ -140,10 +145,10 @@ public class EnclosingMethodTest extends TestResult { for (Class clazz : noEnclosingMethod) { try { addTestCase("Class should not have EnclosingMethod attribute : " + clazz); - ClassFile classFile = readClassFile(clazz); + ClassModel classFile = readClassFile(clazz); checkEquals(countEnclosingMethodAttributes(classFile), - 0l, "number of the EnclosingMethod attribute in the class is zero : " - + classFile.getName()); + 0L, "number of the EnclosingMethod attribute in the class is zero : " + + classFile.thisClass().name()); } catch (Exception e) { addFailure(e); } @@ -157,25 +162,24 @@ public class EnclosingMethodTest extends TestResult { + (clazz.isAnonymousClass() ? "anonymous" : "local"); addTestCase(info); printf("Testing test case : %s\n", info); - ClassFile classFile = readClassFile(clazz); + ClassModel classFile = readClassFile(clazz); String className = clazz.getName(); checkEquals(countEnclosingMethodAttributes(classFile), 1l, "number of the EnclosingMethod attribute in the class is one : " + clazz); - EnclosingMethod_attribute attr = (EnclosingMethod_attribute) - classFile.getAttribute(Attribute.EnclosingMethod); + EnclosingMethodAttribute attr = classFile.findAttribute(Attributes.ENCLOSING_METHOD).orElse(null); if (!checkNotNull(attr, "the EnclosingMethod attribute is not null : " + className)) { // stop checking, attr is null. test case failed return; } - checkEquals(classFile.constant_pool.getUTF8Value(attr.attribute_name_index), + checkEquals(attr.attributeName(), "EnclosingMethod", "attribute_name_index of EnclosingMethod attribute in the class : " + className); - checkEquals(attr.attribute_length, 4, + checkEquals(((BoundAttribute)attr).payloadLen(), 4, "attribute_length of EnclosingMethod attribute in the class : " + className); String expectedClassName = enclosingMethod.enclosingClazz().getName(); - checkEquals(classFile.constant_pool.getClassInfo(attr.class_index).getName(), + checkEquals(attr.enclosingClass().name().stringValue(), expectedClassName, String.format( "enclosing class of EnclosingMethod attribute in the class %s is %s", className, expectedClassName)); @@ -183,10 +187,10 @@ public class EnclosingMethodTest extends TestResult { String expectedMethodName = enclosingMethod.enclosingMethod(); if (expectedMethodName.isEmpty()) { // class does not have an enclosing method - checkEquals(attr.method_index, 0, String.format( + checkEquals(attr.enclosingMethod().isPresent()? attr.enclosingMethod().get().index(): 0, 0, String.format( "enclosing method of EnclosingMethod attribute in the class %s is null", className)); } else { - String methodName = classFile.constant_pool.getNameAndTypeInfo(attr.method_index).getName(); + String methodName = attr.enclosingMethodName().get().stringValue() + attr.enclosingMethodType().get().stringValue(); checkTrue(methodName.startsWith(expectedMethodName), String.format( "enclosing method of EnclosingMethod attribute in the class %s" + " is method name %s" + @@ -199,9 +203,9 @@ public class EnclosingMethodTest extends TestResult { }); } - private long countEnclosingMethodAttributes(ClassFile classFile) { - return Stream.of(classFile.attributes.attrs) - .filter(x -> x instanceof EnclosingMethod_attribute) + private long countEnclosingMethodAttributes(ClassModel classFile) { + return classFile.attributes().stream() + .filter(x -> x instanceof EnclosingMethodAttribute) .count(); } diff --git a/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/LineNumberTest.java b/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/LineNumberTest.java index 332b2bf5160..6a2faa34b9c 100644 --- a/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/LineNumberTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/LineNumberTest.java @@ -29,7 +29,12 @@ * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * jdk.compiler/com.sun.tools.javac.util - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestBase * @build LineNumberTestBase Container TestCase * @run main LineNumberTest diff --git a/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/LineNumberTestBase.java b/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/LineNumberTestBase.java index f52e46455e7..867a67057e9 100644 --- a/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/LineNumberTestBase.java +++ b/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/LineNumberTestBase.java @@ -21,7 +21,9 @@ * questions. */ -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; +import jdk.internal.classfile.impl.BoundAttribute; import java.io.InputStream; import java.nio.file.Paths; @@ -32,8 +34,6 @@ import java.util.stream.IntStream; import java.util.stream.Stream; import javax.tools.JavaFileObject; -import static com.sun.tools.classfile.Attribute.Code; -import static com.sun.tools.classfile.Attribute.LineNumberTable; import static java.lang.String.format; import static java.util.stream.Collectors.toSet; @@ -67,27 +67,28 @@ public class LineNumberTestBase extends TestBase { writeToFileIfEnabled(Paths.get(testCase.getName() + ".java"), testCase.src); Set coveredLines = new HashSet<>(); for (JavaFileObject file : compile(testCase.extraCompilerOptions, testCase.src).getClasses().values()) { - ClassFile classFile; + ClassModel classFile; try (InputStream input = file.openInputStream()) { - classFile = ClassFile.read(input); + classFile = Classfile.of().parse(input.readAllBytes()); } - for (Method m : classFile.methods) { - Code_attribute code_attribute = (Code_attribute) m.attributes.get(Code); + for (MethodModel m : classFile.methods()) { + CodeAttribute code_attribute = m.findAttribute(Attributes.CODE).orElse(null); + assert code_attribute != null; assertEquals( - countAttributes(LineNumberTable, code_attribute.attributes.attrs, classFile.constant_pool), + countAttributes(Attributes.LINE_NUMBER_TABLE, code_attribute), 1, "Can be more than one LNT attribute, but javac should generate only one."); - LineNumberTable_attribute tableAttribute = - (LineNumberTable_attribute) code_attribute.attributes.get(LineNumberTable); - checkAttribute(testCase, tableAttribute, code_attribute.code_length); + LineNumberTableAttribute tableAttribute = code_attribute.findAttribute(Attributes.LINE_NUMBER_TABLE).orElse(null); + assert tableAttribute != null; + checkAttribute(testCase, tableAttribute, code_attribute.codeLength()); Set methodCoveredLines = - Stream.of(tableAttribute.line_number_table) - .map(e -> e.line_number) + tableAttribute.lineNumbers().stream() + .map(LineNumberInfo::lineNumber) .collect(toSet()); - TestCase.MethodData expected = testCase.findData(m.getName(classFile.constant_pool)); + TestCase.MethodData expected = testCase.findData(m.methodName().stringValue()); if (expected != null) { verifyCoveredLines(methodCoveredLines, expected); @@ -134,26 +135,27 @@ public class LineNumberTestBase extends TestBase { } } - private int countAttributes(String name, Attribute[] attrs, ConstantPool constant_pool) throws ConstantPoolException { + private > int countAttributes(AttributeMapper attr, AttributedElement attributedElement) { int i = 0; - for (Attribute attribute : attrs) { - if (name.equals(attribute.getName(constant_pool))) { + for (Attribute attribute : attributedElement.attributes()) { + if (attribute.attributeName().equals(attr.name())) { i++; } } return i; } - private void checkAttribute(TestCase testCase, LineNumberTable_attribute tableAttribute, int code_length) { - assertEquals(tableAttribute.line_number_table_length, tableAttribute.line_number_table.length, - "Incorrect line number table length."); + private void checkAttribute(TestCase testCase, LineNumberTableAttribute tableAttribute, int code_length) { + // This test is unnecessary +// assertEquals(tableAttribute.line_number_table_length, tableAttribute.line_number_table.length, +// "Incorrect line number table length."); //attribute length is offset(line_number_table_length) + element_size*element_count - assertEquals(tableAttribute.attribute_length, 2 + 4 * tableAttribute.line_number_table_length, + assertEquals(((BoundAttribute)tableAttribute).payloadLen(), 2 + 4 * tableAttribute.lineNumbers().size(), "Incorrect attribute length"); testNonEmptyLine(testCase.src.split("\n"), tableAttribute); assertEquals( - Stream.of(tableAttribute.line_number_table) - .filter(e -> e.start_pc >= code_length) + tableAttribute.lineNumbers().stream() + .filter(e -> e.startPc() >= code_length) .count() , 0L, "StartPC is out of bounds."); } @@ -162,11 +164,11 @@ public class LineNumberTestBase extends TestBase { * Expects line number table point to non empty lines. * The method can't recognize commented lines as empty(insensible) in case of multiline comment. */ - private void testNonEmptyLine(String[] source, LineNumberTable_attribute attribute) { - for (LineNumberTable_attribute.Entry e : attribute.line_number_table) { - String line = source[e.line_number - 1].trim(); + private void testNonEmptyLine(String[] source, LineNumberTableAttribute attribute) { + for (LineNumberInfo e : attribute.lineNumbers()) { + String line = source[e.lineNumber() - 1].trim(); assertTrue(!("".equals(line) || line.startsWith("//") || line.startsWith("/*")), - format("Expect that line #%d is not empty.%n", e.line_number)); + format("Expect that line #%d is not empty.%n", e.lineNumber())); } } diff --git a/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/MultipleRecordPatterns.java b/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/MultipleRecordPatterns.java index 32ab561f2ce..b567c05151d 100644 --- a/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/MultipleRecordPatterns.java +++ b/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/MultipleRecordPatterns.java @@ -29,7 +29,12 @@ * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * jdk.compiler/com.sun.tools.javac.util - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestBase * @build LineNumberTestBase TestCase * @run main MultipleRecordPatterns diff --git a/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/RuleSwitchBreaks.java b/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/RuleSwitchBreaks.java index 35278522ddf..f6dca5ed6b0 100644 --- a/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/RuleSwitchBreaks.java +++ b/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/RuleSwitchBreaks.java @@ -29,7 +29,12 @@ * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * jdk.compiler/com.sun.tools.javac.util - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestBase * @build LineNumberTestBase TestCase * @run main RuleSwitchBreaks diff --git a/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/StringSwitchBreaks.java b/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/StringSwitchBreaks.java index f121122f14d..977fec346c4 100644 --- a/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/StringSwitchBreaks.java +++ b/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/StringSwitchBreaks.java @@ -29,7 +29,12 @@ * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * jdk.compiler/com.sun.tools.javac.util - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestBase * @build LineNumberTestBase TestCase * @run main StringSwitchBreaks diff --git a/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/T8050993.java b/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/T8050993.java index 2c2e8fcd6a3..5ec7ba973c2 100644 --- a/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/T8050993.java +++ b/test/langtools/tools/javac/classfiles/attributes/LineNumberTable/T8050993.java @@ -2,7 +2,12 @@ * @test /nodynamiccopyright/ * @bug 8050993 * @summary Verify that the condition in the conditional lexpression gets a LineNumberTable entry - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @compile -g T8050993.java * @run main T8050993 */ @@ -14,21 +19,24 @@ import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; public class T8050993 { - public static void main(String[] args) throws IOException, ConstantPoolException { - ClassFile someTestIn = ClassFile.read(T8050993.class.getResourceAsStream("T8050993.class")); + public static void main(String[] args) throws IOException { + ClassModel someTestIn = Classfile.of().parse(T8050993.class.getResourceAsStream("T8050993.class").readAllBytes()); Set expectedLineNumbers = new HashSet<>(Arrays.asList(49, 50, 47, 48)); - for (Method m : someTestIn.methods) { - if ("method".equals(m.getName(someTestIn.constant_pool))) { - Code_attribute code_attribute = (Code_attribute) m.attributes.get(Attribute.Code); - for (Attribute at : code_attribute.attributes) { - if (Attribute.LineNumberTable.equals(at.getName(someTestIn.constant_pool))) { - LineNumberTable_attribute att = (LineNumberTable_attribute) at; - Set actualLinesNumbers = Arrays.stream(att.line_number_table) - .map(e -> e.line_number) - .collect(Collectors.toSet()); + for (MethodModel m : someTestIn.methods()) { + if (m.methodName().equalsString("method")) { + CodeAttribute code_attribute = m.findAttribute(Attributes.CODE).orElse(null); + assert code_attribute != null; + for (Attribute at : code_attribute.attributes()) { + if (Attributes.LINE_NUMBER_TABLE.equals(at)) { + assert at instanceof LineNumberTableAttribute; + LineNumberTableAttribute att = (LineNumberTableAttribute) at; + Set actualLinesNumbers = Arrays.stream(att.lineNumbers().toArray(new LineNumberInfo[0])) + .map(LineNumberInfo::lineNumber) + .collect(Collectors.toSet()); if (!Objects.equals(expectedLineNumbers, actualLinesNumbers)) { throw new AssertionError("Expected LineNumber entries not found;" + "actual=" + actualLinesNumbers + ";" + diff --git a/test/langtools/tools/javac/classfiles/attributes/LocalVariableTable/LocalVariableTableTest.java b/test/langtools/tools/javac/classfiles/attributes/LocalVariableTable/LocalVariableTableTest.java index f7def32e25e..7dd7a3e706b 100644 --- a/test/langtools/tools/javac/classfiles/attributes/LocalVariableTable/LocalVariableTableTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/LocalVariableTable/LocalVariableTableTest.java @@ -29,15 +29,20 @@ * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * jdk.compiler/com.sun.tools.javac.util - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestBase * @build LocalVariableTestBase * @compile -g LocalVariableTableTest.java * @run main LocalVariableTableTest */ -import com.sun.tools.classfile.Code_attribute; -import com.sun.tools.classfile.LocalVariableTable_attribute; +import jdk.internal.classfile.attribute.*; +import jdk.internal.classfile.impl.BoundAttribute; import java.io.IOException; import java.util.Arrays; @@ -74,11 +79,11 @@ public class LocalVariableTableTest extends LocalVariableTestBase { } @Override - protected List getVariableTables(Code_attribute codeAttribute) { - return Stream.of(codeAttribute.attributes.attrs) - .filter(at -> at instanceof LocalVariableTable_attribute) - .map(at -> (LocalVariableTable_attribute) at) - .map((t) -> new LocalVariableTable(t)).collect(toList()); + protected List getVariableTables(CodeAttribute codeAttribute) { + return codeAttribute.attributes().stream() + .filter(at -> at instanceof LocalVariableTableAttribute) + .map(at -> (LocalVariableTableAttribute) at) + .map(LocalVariableTable::new).collect(toList()); } @ExpectedLocals(name = "l", type = "J") @@ -185,58 +190,58 @@ public class LocalVariableTableTest extends LocalVariableTestBase { class LocalVariableTable implements VariableTable { - final LocalVariableTable_attribute att; + final LocalVariableTableAttribute att; - public LocalVariableTable(LocalVariableTable_attribute att) { + public LocalVariableTable(LocalVariableTableAttribute att) { this.att = att; } @Override public int localVariableTableLength() { - return att.local_variable_table_length; + return att.localVariables().size(); } @Override public List entries() { - return Stream.of(att.local_variable_table).map(LocalVariableTableEntry::new).collect(toList()); + return att.localVariables().stream().map(LocalVariableTableEntry::new).collect(toList()); } @Override public int attributeLength() { - return att.attribute_length; + return ((BoundAttribute) att).payloadLen(); } private class LocalVariableTableEntry implements Entry { - final LocalVariableTable_attribute.Entry entry; + final LocalVariableInfo entry; - private LocalVariableTableEntry(LocalVariableTable_attribute.Entry entry) { + private LocalVariableTableEntry(LocalVariableInfo entry) { this.entry = entry; } @Override public int index() { - return entry.index; + return entry.slot(); } @Override public int startPC() { - return entry.start_pc; + return entry.startPc(); } @Override public int length() { - return entry.length; + return entry.length(); } @Override public String name() { - return getString(entry.name_index); + return entry.name().stringValue(); } @Override public String type() { - return getString(entry.descriptor_index); + return entry.type().stringValue(); } @Override diff --git a/test/langtools/tools/javac/classfiles/attributes/LocalVariableTable/LocalVariableTestBase.java b/test/langtools/tools/javac/classfiles/attributes/LocalVariableTable/LocalVariableTestBase.java index 097d8c491bf..7fe81fe7f1d 100644 --- a/test/langtools/tools/javac/classfiles/attributes/LocalVariableTable/LocalVariableTestBase.java +++ b/test/langtools/tools/javac/classfiles/attributes/LocalVariableTable/LocalVariableTestBase.java @@ -21,7 +21,8 @@ * questions. */ -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; import java.io.IOException; import java.lang.annotation.Repeatable; @@ -50,7 +51,7 @@ import static java.util.stream.Collectors.*; */ public abstract class LocalVariableTestBase extends TestBase { public static final int DEFAULT_SCOPE = 0; - private final ClassFile classFile; + private final ClassModel classFile; private final Class clazz; /** @@ -59,13 +60,13 @@ public abstract class LocalVariableTestBase extends TestBase { public LocalVariableTestBase(Class clazz) { this.clazz = clazz; try { - this.classFile = ClassFile.read(getClassFile(clazz)); - } catch (IOException | ConstantPoolException e) { + this.classFile = Classfile.of().parse(getClassFile(clazz).toPath()); + } catch (IOException e) { throw new IllegalArgumentException("Can't read classfile for specified class", e); } } - protected abstract List getVariableTables(Code_attribute codeAttribute); + protected abstract List getVariableTables(CodeAttribute codeAttribute); /** * Finds expected variables with their type in VariableTable. @@ -74,7 +75,7 @@ public abstract class LocalVariableTestBase extends TestBase { public void test() throws IOException { List testMethods = Stream.of(clazz.getDeclaredMethods()) .filter(m -> m.getAnnotationsByType(ExpectedLocals.class).length > 0) - .collect(toList()); + .toList(); int failed = 0; for (java.lang.reflect.Method method : testMethods) { try { @@ -99,12 +100,12 @@ public abstract class LocalVariableTestBase extends TestBase { public void test(String methodName, Map expectedLocals2Types, Map sig2scope) throws IOException { - for (Method m : classFile.methods) { - String mName = getString(m.name_index); + for (MethodModel m : classFile.methods()) { + String mName = m.methodName().stringValue(); if (methodName.equals(mName)) { System.out.println("Testing local variable table in method " + mName); - Code_attribute code_attribute = (Code_attribute) m.attributes.get(Attribute.Code); - + CodeAttribute code_attribute = m.findAttribute(Attributes.CODE).orElse(null); + assert code_attribute != null; List variableTables = getVariableTables(code_attribute); generalLocalVariableTableCheck(variableTables); @@ -115,7 +116,7 @@ public abstract class LocalVariableTestBase extends TestBase { generalEntriesCheck(entries, code_attribute); assertIndexesAreUnique(entries, sig2scope); checkNamesAndTypes(entries, expectedLocals2Types); - checkDoubleAndLongIndexes(entries, sig2scope, code_attribute.max_locals); + checkDoubleAndLongIndexes(entries, sig2scope, code_attribute.maxLocals()); } } } @@ -131,17 +132,17 @@ public abstract class LocalVariableTestBase extends TestBase { } } - private void generalEntriesCheck(List entries, Code_attribute code_attribute) { + private void generalEntriesCheck(List entries, CodeAttribute code_attribute) { for (VariableTable.Entry e : entries) { - assertTrue(e.index() >= 0 && e.index() < code_attribute.max_locals, - "Index " + e.index() + " out of variable array. Size of array is " + code_attribute.max_locals); + assertTrue(e.index() >= 0 && e.index() < code_attribute.maxLocals(), + "Index " + e.index() + " out of variable array. Size of array is " + code_attribute.maxLocals()); assertTrue(e.startPC() >= 0, "StartPC is less then 0. StartPC = " + e.startPC()); assertTrue(e.length() >= 0, "Length is less then 0. Length = " + e.length()); - assertTrue(e.startPC() + e.length() <= code_attribute.code_length, + assertTrue(e.startPC() + e.length() <= code_attribute.codeLength(), format("StartPC+Length > code length.%n" + "%s%n" + "code_length = %s" - , e, code_attribute.code_length)); + , e, code_attribute.codeLength())); } } @@ -205,14 +206,15 @@ public abstract class LocalVariableTestBase extends TestBase { return entries.stream().collect(groupingBy(e -> scopes.getOrDefault(e.name() + "&" + e.type(), DEFAULT_SCOPE))); } - protected String getString(int i) { - try { - return classFile.constant_pool.getUTF8Info(i).value; - } catch (ConstantPool.InvalidIndex | ConstantPool.UnexpectedEntry ex) { - ex.printStackTrace(); - throw new AssertionFailedException("Issue while reading constant pool"); - } - } + // Don't need the getString method now +// protected String getString(int i) { +// try { +// return classFile.constant_pool.getUTF8Info(i).value; +// } catch (ConstantPool.InvalidIndex | ConstantPool.UnexpectedEntry ex) { +// ex.printStackTrace(); +// throw new AssertionFailedException("Issue while reading constant pool"); +// } +// } /** * LocalVariableTable and LocalVariableTypeTable are similar. diff --git a/test/langtools/tools/javac/classfiles/attributes/LocalVariableTable/LocalVariableTypeTableTest.java b/test/langtools/tools/javac/classfiles/attributes/LocalVariableTable/LocalVariableTypeTableTest.java index 2bb4038875f..38fb687b429 100644 --- a/test/langtools/tools/javac/classfiles/attributes/LocalVariableTable/LocalVariableTypeTableTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/LocalVariableTable/LocalVariableTypeTableTest.java @@ -29,14 +29,19 @@ * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * jdk.compiler/com.sun.tools.javac.util - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestBase LocalVariableTestBase * @compile -g LocalVariableTypeTableTest.java * @run main LocalVariableTypeTableTest */ -import com.sun.tools.classfile.Code_attribute; -import com.sun.tools.classfile.LocalVariableTypeTable_attribute; +import jdk.internal.classfile.attribute.*; +import jdk.internal.classfile.impl.BoundAttribute; import java.io.IOException; import java.util.Arrays; @@ -58,10 +63,10 @@ public class LocalVariableTypeTableTest extends LocalVariableTestBase { } @Override - protected List getVariableTables(Code_attribute codeAttribute) { - return Stream.of(codeAttribute.attributes.attrs) - .filter(at -> at instanceof LocalVariableTypeTable_attribute) - .map(at -> (LocalVariableTypeTable_attribute) at) + protected List getVariableTables(CodeAttribute codeAttribute) { + return codeAttribute.attributes().stream() + .filter(at -> at instanceof LocalVariableTypeTableAttribute) + .map(at -> (LocalVariableTypeTableAttribute) at) .map(LocalVariableTypeTable::new).collect(toList()); } @@ -135,59 +140,59 @@ public class LocalVariableTypeTableTest extends LocalVariableTestBase { class LocalVariableTypeTable implements VariableTable { - final LocalVariableTypeTable_attribute att; + final LocalVariableTypeTableAttribute att; - public LocalVariableTypeTable(LocalVariableTypeTable_attribute att) { + public LocalVariableTypeTable(LocalVariableTypeTableAttribute att) { this.att = att; } @Override public int localVariableTableLength() { - return att.local_variable_table_length; + return att.localVariableTypes().size(); } @Override public List entries() { - return Stream.of(att.local_variable_table).map(LocalVariableTypeTableEntry::new).collect(toList()); + return att.localVariableTypes().stream().map(LocalVariableTypeTableEntry::new).collect(toList()); } @Override public int attributeLength() { - return att.attribute_length; + return ((BoundAttribute)att).payloadLen(); } private class LocalVariableTypeTableEntry implements Entry { - final LocalVariableTypeTable_attribute.Entry entry; + final LocalVariableTypeInfo entry; - private LocalVariableTypeTableEntry(LocalVariableTypeTable_attribute.Entry entry) { + private LocalVariableTypeTableEntry(LocalVariableTypeInfo entry) { this.entry = entry; } @Override public int index() { - return entry.index; + return entry.slot(); } @Override public int startPC() { - return entry.start_pc; + return entry.startPc(); } @Override public int length() { - return entry.length; + return entry.length(); } @Override public String name() { - return getString(entry.name_index); + return entry.name().stringValue(); } @Override public String type() { - return getString(entry.signature_index); + return entry.signature().stringValue(); } @Override diff --git a/test/langtools/tools/javac/classfiles/attributes/Module/ModuleFlagTest.java b/test/langtools/tools/javac/classfiles/attributes/Module/ModuleFlagTest.java index 2ac745fb0c8..42be9f74e82 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Module/ModuleFlagTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Module/ModuleFlagTest.java @@ -29,14 +29,19 @@ * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * jdk.compiler/com.sun.tools.javac.util - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox toolbox.JavacTask toolbox.ToolBox * @run main ModuleFlagTest */ -import com.sun.tools.classfile.AccessFlags; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.ConstantPoolException; +import jdk.internal.classfile.AccessFlags; +import jdk.internal.classfile.Classfile; +import java.lang.reflect.AccessFlag; import java.io.IOException; import java.nio.file.Path; @@ -46,7 +51,7 @@ import toolbox.JavacTask; import toolbox.ToolBox; public class ModuleFlagTest { - public static void main(String[] args) throws IOException, ConstantPoolException { + public static void main(String[] args) throws IOException { Path outdir = Paths.get("."); ToolBox tb = new ToolBox(); final Path moduleInfo = Paths.get("module-info.java"); @@ -56,9 +61,9 @@ public class ModuleFlagTest { .files(moduleInfo) .run(); - AccessFlags accessFlags = ClassFile.read(outdir.resolve("module-info.class")) - .access_flags; - if (!accessFlags.is(AccessFlags.ACC_MODULE)) { + AccessFlags accessFlags = Classfile.of().parse(outdir.resolve("module-info.class")) + .flags(); + if (!accessFlags.has(AccessFlag.MODULE)) { throw new RuntimeException("Classfile doesn't have module access flag"); } } diff --git a/test/langtools/tools/javac/classfiles/attributes/Module/ModuleTest.java b/test/langtools/tools/javac/classfiles/attributes/Module/ModuleTest.java index b19573fbc56..ca4550a44e6 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Module/ModuleTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Module/ModuleTest.java @@ -29,7 +29,12 @@ * jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * jdk.compiler/com.sun.tools.javac.util - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @library /tools/lib ../lib /tools/javac/lib * @build toolbox.ToolBox toolbox.JavacTask toolbox.ToolBox * TestBase TestResult ModuleTestBase diff --git a/test/langtools/tools/javac/classfiles/attributes/Module/ModuleTestBase.java b/test/langtools/tools/javac/classfiles/attributes/Module/ModuleTestBase.java index 6dc43614492..ce6231b6ec1 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Module/ModuleTestBase.java +++ b/test/langtools/tools/javac/classfiles/attributes/Module/ModuleTestBase.java @@ -21,10 +21,9 @@ * questions. */ -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.ConstantPool; -import com.sun.tools.classfile.ConstantPoolException; -import com.sun.tools.classfile.Module_attribute; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; +import jdk.internal.classfile.constantpool.*; import java.io.IOException; import java.lang.annotation.Retention; @@ -70,98 +69,101 @@ public class ModuleTestBase { } protected void testModuleAttribute(Path modulePath, ModuleDescriptor moduleDescriptor) throws Exception { - ClassFile classFile = ClassFile.read(modulePath.resolve("module-info.class")); - Module_attribute moduleAttribute = (Module_attribute) classFile.getAttribute("Module"); - ConstantPool constantPool = classFile.constant_pool; - testModuleName(moduleDescriptor, moduleAttribute, constantPool); + ClassModel classFile = Classfile.of().parse(modulePath.resolve("module-info.class")); + ModuleAttribute moduleAttribute = classFile.findAttribute(Attributes.MODULE).orElse(null); + assert moduleAttribute != null; + testModuleName(moduleDescriptor, moduleAttribute); testModuleFlags(moduleDescriptor, moduleAttribute); - testRequires(moduleDescriptor, moduleAttribute, constantPool); - testExports(moduleDescriptor, moduleAttribute, constantPool); - testOpens(moduleDescriptor, moduleAttribute, constantPool); - testProvides(moduleDescriptor, moduleAttribute, constantPool); - testUses(moduleDescriptor, moduleAttribute, constantPool); + testRequires(moduleDescriptor, moduleAttribute); + testExports(moduleDescriptor, moduleAttribute); + testOpens(moduleDescriptor, moduleAttribute); + testProvides(moduleDescriptor, moduleAttribute); + testUses(moduleDescriptor, moduleAttribute); } - private void testModuleName(ModuleDescriptor moduleDescriptor, Module_attribute module, ConstantPool constantPool) throws ConstantPoolException { - tr.checkEquals(constantPool.getModuleInfo(module.module_name).getName(), moduleDescriptor.name, "Unexpected module name"); + private void testModuleName(ModuleDescriptor moduleDescriptor, ModuleAttribute module) { + tr.checkEquals(module.moduleName().name().stringValue(), moduleDescriptor.name, "Unexpected module name"); } - private void testModuleFlags(ModuleDescriptor moduleDescriptor, Module_attribute module) { - tr.checkEquals(module.module_flags, moduleDescriptor.flags, "Unexpected module flags"); + private void testModuleFlags(ModuleDescriptor moduleDescriptor, ModuleAttribute module) { + tr.checkEquals(module.moduleFlagsMask(), moduleDescriptor.flags, "Unexpected module flags"); } - private void testRequires(ModuleDescriptor moduleDescriptor, Module_attribute module, ConstantPool constantPool) throws ConstantPoolException { - tr.checkEquals(module.requires_count, moduleDescriptor.requires.size(), "Wrong amount of requires."); + private void testRequires(ModuleDescriptor moduleDescriptor, ModuleAttribute module) { + tr.checkEquals(module.requires().size(), moduleDescriptor.requires.size(), "Wrong amount of requires."); List actualRequires = new ArrayList<>(); - for (Module_attribute.RequiresEntry require : module.requires) { + for (ModuleRequireInfo require : module.requires()) { actualRequires.add(new Requires( - require.getRequires(constantPool), - require.requires_flags)); + require.requires().name().stringValue(), + require.requiresFlagsMask())); } tr.checkContains(actualRequires, moduleDescriptor.requires, "Lists of requires don't match"); } - private void testExports(ModuleDescriptor moduleDescriptor, Module_attribute module, ConstantPool constantPool) throws ConstantPoolException { - tr.checkEquals(module.exports_count, moduleDescriptor.exports.size(), "Wrong amount of exports."); - for (Module_attribute.ExportsEntry export : module.exports) { - String pkg = constantPool.getPackageInfo(export.exports_index).getName(); + private void testExports(ModuleDescriptor moduleDescriptor, ModuleAttribute module) { + tr.checkEquals(module.exports().size(), moduleDescriptor.exports.size(), "Wrong amount of exports."); + for (ModuleExportInfo export : module.exports()) { + String pkg = export.exportedPackage().name().stringValue(); if (tr.checkTrue(moduleDescriptor.exports.containsKey(pkg), "Unexpected export " + pkg)) { Export expectedExport = moduleDescriptor.exports.get(pkg); - tr.checkEquals(expectedExport.mask, export.exports_flags, "Wrong export flags"); + tr.checkEquals(expectedExport.mask, export.exportsFlagsMask(), "Wrong export flags"); List expectedTo = expectedExport.to; - tr.checkEquals(export.exports_to_count, expectedTo.size(), "Wrong amount of exports to"); + tr.checkEquals(export.exportsTo().size(), expectedTo.size(), "Wrong amount of exports to"); List actualTo = new ArrayList<>(); - for (int toIdx : export.exports_to_index) { - actualTo.add(constantPool.getModuleInfo(toIdx).getName()); + for (ModuleEntry toIdx : export.exportsTo()) { + actualTo.add(toIdx.name().stringValue()); } tr.checkContains(actualTo, expectedTo, "Lists of \"exports to\" don't match."); } } } - private void testOpens(ModuleDescriptor moduleDescriptor, Module_attribute module, ConstantPool constantPool) throws ConstantPoolException { - tr.checkEquals(module.opens_count, moduleDescriptor.opens.size(), "Wrong amount of opens."); - for (Module_attribute.OpensEntry open : module.opens) { - String pkg = constantPool.getPackageInfo(open.opens_index).getName(); + private void testOpens(ModuleDescriptor moduleDescriptor, ModuleAttribute module) { + tr.checkEquals(module.opens().size(), moduleDescriptor.opens.size(), "Wrong amount of opens."); + for (ModuleOpenInfo open : module.opens()) { + String pkg = open.openedPackage().name().stringValue(); if (tr.checkTrue(moduleDescriptor.opens.containsKey(pkg), "Unexpected open " + pkg)) { Open expectedOpen = moduleDescriptor.opens.get(pkg); - tr.checkEquals(expectedOpen.mask, open.opens_flags, "Wrong open flags"); + tr.checkEquals(expectedOpen.mask, open.opensFlagsMask(), "Wrong open flags"); List expectedTo = expectedOpen.to; - tr.checkEquals(open.opens_to_count, expectedTo.size(), "Wrong amount of opens to"); + tr.checkEquals(open.opensTo().size(), expectedTo.size(), "Wrong amount of opens to"); List actualTo = new ArrayList<>(); - for (int toIdx : open.opens_to_index) { - actualTo.add(constantPool.getModuleInfo(toIdx).getName()); + for (ModuleEntry toIdx : open.opensTo()) { + actualTo.add(toIdx.name().stringValue()); } tr.checkContains(actualTo, expectedTo, "Lists of \"opens to\" don't match."); } } } - private void testUses(ModuleDescriptor moduleDescriptor, Module_attribute module, ConstantPool constantPool) throws ConstantPoolException { - tr.checkEquals(module.uses_count, moduleDescriptor.uses.size(), "Wrong amount of uses."); + private void testUses(ModuleDescriptor moduleDescriptor, ModuleAttribute module) { + tr.checkEquals(module.uses().size(), moduleDescriptor.uses.size(), "Wrong amount of uses."); List actualUses = new ArrayList<>(); - for (int usesIdx : module.uses_index) { - String uses = constantPool.getClassInfo(usesIdx).getBaseName(); + for (ClassEntry usesIdx : module.uses()) { + if (!usesIdx.asSymbol().isClassOrInterface()) continue; //get basename + String uses = usesIdx.asInternalName(); actualUses.add(uses); } tr.checkContains(actualUses, moduleDescriptor.uses, "Lists of uses don't match"); } - private void testProvides(ModuleDescriptor moduleDescriptor, Module_attribute module, ConstantPool constantPool) throws ConstantPoolException { - int moduleProvidesCount = Arrays.asList(module.provides).stream() - .mapToInt(e -> e.with_index.length) + private void testProvides(ModuleDescriptor moduleDescriptor, ModuleAttribute module) { + int moduleProvidesCount = module.provides().stream() + .mapToInt(e -> e.providesWith().size()) .sum(); int moduleDescriptorProvidesCount = moduleDescriptor.provides.values().stream() .mapToInt(impls -> impls.size()) .sum(); tr.checkEquals(moduleProvidesCount, moduleDescriptorProvidesCount, "Wrong amount of provides."); Map> actualProvides = new HashMap<>(); - for (Module_attribute.ProvidesEntry provide : module.provides) { - String provides = constantPool.getClassInfo(provide.provides_index).getBaseName(); + for (ModuleProvideInfo provide : module.provides()) { + if (!provide.provides().asSymbol().isClassOrInterface()) continue; + String provides = provide.provides().asInternalName(); List impls = new ArrayList<>(); - for (int i = 0; i < provide.with_count; i++) { - String with = constantPool.getClassInfo(provide.with_index[i]).getBaseName(); + for (ClassEntry withEntry: provide.providesWith()) { + if (!withEntry.asSymbol().isClassOrInterface()) continue; + String with = withEntry.asInternalName(); impls.add(with); } actualProvides.put(provides, impls); @@ -191,7 +193,7 @@ public class ModuleTestBase { } public enum ModuleFlag implements Mask { - OPEN("open", Module_attribute.ACC_OPEN); + OPEN("open", Classfile.ACC_OPEN); private final String token; private final int mask; @@ -208,9 +210,9 @@ public class ModuleTestBase { } public enum RequiresFlag implements Mask { - TRANSITIVE("transitive", Module_attribute.ACC_TRANSITIVE), - STATIC("static", Module_attribute.ACC_STATIC_PHASE), - MANDATED("", Module_attribute.ACC_MANDATED); + TRANSITIVE("transitive", Classfile.ACC_TRANSITIVE), + STATIC("static", Classfile.ACC_STATIC_PHASE), + MANDATED("", Classfile.ACC_MANDATED); private final String token; private final int mask; @@ -227,7 +229,7 @@ public class ModuleTestBase { } public enum ExportsFlag implements Mask { - SYNTHETIC("", Module_attribute.ACC_SYNTHETIC); + SYNTHETIC("", Classfile.ACC_SYNTHETIC); private final String token; private final int mask; @@ -244,7 +246,7 @@ public class ModuleTestBase { } public enum OpensFlag implements Mask { - SYNTHETIC("", Module_attribute.ACC_SYNTHETIC); + SYNTHETIC("", Classfile.ACC_SYNTHETIC); private final String token; private final int mask; @@ -326,7 +328,7 @@ public class ModuleTestBase { private static final String LINE_END = ";\n"; - StringBuilder content = new StringBuilder(""); + StringBuilder content = new StringBuilder(); public ModuleDescriptor(String moduleName, ModuleFlag... flags) { this.name = moduleName; @@ -370,7 +372,7 @@ public class ModuleTestBase { List tos = Pattern.compile(",") .splitAsStream(to) .map(String::trim) - .collect(Collectors.toList()); + .toList(); this.exports.compute(toInternalForm(pkg), (k,v) -> new Export(k, computeMask(flags))) .to.addAll(tos); @@ -396,7 +398,7 @@ public class ModuleTestBase { List tos = Pattern.compile(",") .splitAsStream(to) .map(String::trim) - .collect(Collectors.toList()); + .toList(); this.opens.compute(toInternalForm(pkg), (k,v) -> new Open(toInternalForm(k), computeMask(flags))) .to.addAll(tos); diff --git a/test/langtools/tools/javac/classfiles/attributes/Signature/ConstructorTest.java b/test/langtools/tools/javac/classfiles/attributes/Signature/ConstructorTest.java index 8d17ee406fb..384e8973a3d 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Signature/ConstructorTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Signature/ConstructorTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build ConstructorTest Driver ExpectedSignature ExpectedSignatureContainer * @run main Driver ConstructorTest diff --git a/test/langtools/tools/javac/classfiles/attributes/Signature/Driver.java b/test/langtools/tools/javac/classfiles/attributes/Signature/Driver.java index 800851863f4..30913ad2118 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Signature/Driver.java +++ b/test/langtools/tools/javac/classfiles/attributes/Signature/Driver.java @@ -21,12 +21,13 @@ * questions. */ -import com.sun.tools.classfile.*; -import com.sun.tools.classfile.Field; -import com.sun.tools.classfile.Method; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.SignatureAttribute; +import jdk.internal.classfile.impl.BoundAttribute; import java.io.File; import java.io.FilenameFilter; +import java.lang.constant.ClassDesc; import java.lang.reflect.*; import java.util.*; import java.util.function.Function; @@ -34,6 +35,7 @@ import java.util.function.Predicate; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; +import java.lang.reflect.AccessFlag; /** * The main class of Signature tests. @@ -49,9 +51,6 @@ import java.util.stream.Stream; * of ExpectedSignature must return true. */ public class Driver extends TestResult { - - private final static String ACC_BRIDGE = "ACC_BRIDGE"; - private final String topLevelClassName; private final File[] files; @@ -78,7 +77,7 @@ public class Driver extends TestResult { // is located in its enclosing class. boolean isAnonymous = isAnonymous(className); clazz = isAnonymous ? getEnclosingClass(className) : clazz; - return Stream.of(clazz.getAnnotationsByType(ExpectedSignature.class)) + return Arrays.stream(clazz.getAnnotationsByType(ExpectedSignature.class)) .filter(s -> s.isAnonymous() == isAnonymous) .collect(Collectors.toMap(ExpectedSignature::descriptor, Function.identity())) .get(className); @@ -97,7 +96,7 @@ public class Driver extends TestResult { private Map getExpectedExecutableSignatures(Executable[] executables, Predicate filterBridge) { - return Stream.of(executables) + return Arrays.stream(executables) .filter(filterBridge) .map(e -> e.getAnnotation(ExpectedSignature.class)) .filter(Objects::nonNull) @@ -115,7 +114,7 @@ public class Driver extends TestResult { } private Map getExpectedFieldSignatures(Class clazz) { - return Stream.of(clazz.getDeclaredFields()) + return Arrays.stream(clazz.getDeclaredFields()) .map(f -> f.getAnnotation(ExpectedSignature.class)) .filter(Objects::nonNull) .collect(Collectors.toMap(ExpectedSignature::descriptor, Function.identity())); @@ -130,13 +129,12 @@ public class Driver extends TestResult { String className = file.getName().replace(".class", ""); Class clazz = Class.forName(className); printf("Testing class %s\n", className); - ClassFile classFile = readClassFile(file); + ClassModel classFile = readClassFile(file); // test class signature testAttribute( className, - classFile, - () -> (Signature_attribute) classFile.getAttribute(Attribute.Signature), + () -> classFile.findAttribute(Attributes.SIGNATURE).orElse(null), getClassExpectedSignature(className, clazz).get(className)); testFields(getExpectedFieldSignatures(clazz), classFile); @@ -163,21 +161,19 @@ public class Driver extends TestResult { } } - private void testMethods(Map expectedSignatures, ClassFile classFile) - throws ConstantPoolException, Descriptor.InvalidDescriptor { - String className = classFile.getName(); + private void testMethods(Map expectedSignatures, ClassModel classFile) { + String className = classFile.thisClass().name().stringValue(); Set foundMethods = new HashSet<>(); - for (Method method : classFile.methods) { - String methodName = getMethodName(classFile, method); + for (MethodModel method : classFile.methods()) { + String methodName = getMethodName(method); printf("Testing method %s\n", methodName); - if (method.access_flags.getMethodFlags().contains(ACC_BRIDGE)) { + if (method.flags().has(AccessFlag.BRIDGE)) { printf("Bridge method is skipped : %s\n", methodName); continue; } testAttribute( methodName, - classFile, - () -> (Signature_attribute) method.attributes.get(Attribute.Signature), + () -> method.findAttribute(Attributes.SIGNATURE).orElse(null), expectedSignatures.get(methodName)); foundMethods.add(methodName); } @@ -185,24 +181,30 @@ public class Driver extends TestResult { "Checking that all methods of class " + className + " with Signature attribute found"); } - private String getMethodName(ClassFile classFile, Method method) - throws ConstantPoolException, Descriptor.InvalidDescriptor { - return String.format("%s%s", - method.getName(classFile.constant_pool), - method.descriptor.getParameterTypes(classFile.constant_pool)); + private String getMethodName(MethodModel method) { + StringBuilder methodName = new StringBuilder(method.methodName().stringValue() + "("); + List paras = method.methodTypeSymbol().parameterList(); + for (int i = 0; i < method.methodTypeSymbol().parameterCount(); ++i) { + if (i != 0) { + methodName.append(", "); + } + ClassDesc para = paras.get(i); + String prefix = para.componentType() == null? para.packageName(): para.componentType().packageName(); + methodName.append(prefix).append(Objects.equals(prefix, "") ? "":".").append(para.displayName()); + } + methodName.append(")"); + return methodName.toString(); } - private void testFields(Map expectedSignatures, ClassFile classFile) - throws ConstantPoolException { - String className = classFile.getName(); + private void testFields(Map expectedSignatures, ClassModel classFile) { + String className = classFile.thisClass().name().stringValue(); Set foundFields = new HashSet<>(); - for (Field field : classFile.fields) { - String fieldName = field.getName(classFile.constant_pool); + for (FieldModel field : classFile.fields()) { + String fieldName = field.fieldName().stringValue(); printf("Testing field %s\n", fieldName); testAttribute( fieldName, - classFile, - () -> (Signature_attribute) field.attributes.get(Attribute.Signature), + () -> field.findAttribute(Attributes.SIGNATURE).orElse(null), expectedSignatures.get(fieldName)); foundFields.add(fieldName); } @@ -212,17 +214,15 @@ public class Driver extends TestResult { private void testAttribute( String memberName, - ClassFile classFile, - Supplier sup, - ExpectedSignature expectedSignature) - throws ConstantPoolException { + Supplier sup, + ExpectedSignature expectedSignature) { - Signature_attribute attribute = sup.get(); + SignatureAttribute attribute = sup.get(); if (expectedSignature != null && checkNotNull(attribute, memberName + " must have attribute")) { - checkEquals(classFile.constant_pool.getUTF8Value(attribute.attribute_name_index), + checkEquals(attribute.attributeName(), "Signature", "Attribute's name : " + memberName); - checkEquals(attribute.attribute_length, 2, "Attribute's length : " + memberName); - checkEquals(attribute.getSignature(classFile.constant_pool), + checkEquals(((BoundAttribute)attribute).payloadLen(), 2, "Attribute's length : " + memberName); + checkEquals(attribute.signature().stringValue(), expectedSignature.signature(), "Testing signature of : " + memberName); } else { diff --git a/test/langtools/tools/javac/classfiles/attributes/Signature/EnumTest.java b/test/langtools/tools/javac/classfiles/attributes/Signature/EnumTest.java index 26e182a6be2..554ba11e4ad 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Signature/EnumTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Signature/EnumTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build EnumTest Driver ExpectedSignature ExpectedSignatureContainer * @run main Driver EnumTest diff --git a/test/langtools/tools/javac/classfiles/attributes/Signature/ExceptionTest.java b/test/langtools/tools/javac/classfiles/attributes/Signature/ExceptionTest.java index 03a6d7c4244..f43a58fd884 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Signature/ExceptionTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Signature/ExceptionTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build ExceptionTest Driver ExpectedSignature ExpectedSignatureContainer * @run main Driver ExceptionTest diff --git a/test/langtools/tools/javac/classfiles/attributes/Signature/FieldTest.java b/test/langtools/tools/javac/classfiles/attributes/Signature/FieldTest.java index 4eb555382c1..be604ff8fc6 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Signature/FieldTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Signature/FieldTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build FieldTest Driver ExpectedSignature ExpectedSignatureContainer * @run main Driver FieldTest diff --git a/test/langtools/tools/javac/classfiles/attributes/Signature/InnerClassTest.java b/test/langtools/tools/javac/classfiles/attributes/Signature/InnerClassTest.java index 6c9e3fadb87..c0b65f0f213 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Signature/InnerClassTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Signature/InnerClassTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassTest Driver ExpectedSignature ExpectedSignatureContainer * @run main Driver InnerClassTest diff --git a/test/langtools/tools/javac/classfiles/attributes/Signature/MethodParameterTest.java b/test/langtools/tools/javac/classfiles/attributes/Signature/MethodParameterTest.java index 84aa6e7fb0e..f9f644436c3 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Signature/MethodParameterTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Signature/MethodParameterTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build MethodParameterTest Driver ExpectedSignature ExpectedSignatureContainer * @run main Driver MethodParameterTest diff --git a/test/langtools/tools/javac/classfiles/attributes/Signature/MethodTypeBoundTest.java b/test/langtools/tools/javac/classfiles/attributes/Signature/MethodTypeBoundTest.java index 7995a345986..5cc51558141 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Signature/MethodTypeBoundTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Signature/MethodTypeBoundTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build MethodTypeBoundTest Driver ExpectedSignature ExpectedSignatureContainer * @run main Driver MethodTypeBoundTest diff --git a/test/langtools/tools/javac/classfiles/attributes/Signature/ReturnTypeTest.java b/test/langtools/tools/javac/classfiles/attributes/Signature/ReturnTypeTest.java index a178b61f372..2855767e72e 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Signature/ReturnTypeTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Signature/ReturnTypeTest.java @@ -29,7 +29,12 @@ * @modules java.desktop * jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build ReturnTypeTest Driver ExpectedSignature ExpectedSignatureContainer * @run main Driver ReturnTypeTest diff --git a/test/langtools/tools/javac/classfiles/attributes/SourceFile/AnonymousClassTest.java b/test/langtools/tools/javac/classfiles/attributes/SourceFile/AnonymousClassTest.java index a95433ed07a..4aba5356fac 100644 --- a/test/langtools/tools/javac/classfiles/attributes/SourceFile/AnonymousClassTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/SourceFile/AnonymousClassTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestBase SourceFileTestBase * @run main AnonymousClassTest */ diff --git a/test/langtools/tools/javac/classfiles/attributes/SourceFile/InnerClassTest.java b/test/langtools/tools/javac/classfiles/attributes/SourceFile/InnerClassTest.java index eaad13785dd..99c3d0be969 100644 --- a/test/langtools/tools/javac/classfiles/attributes/SourceFile/InnerClassTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/SourceFile/InnerClassTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestBase SourceFileTestBase * @run main InnerClassTest */ diff --git a/test/langtools/tools/javac/classfiles/attributes/SourceFile/LocalClassTest.java b/test/langtools/tools/javac/classfiles/attributes/SourceFile/LocalClassTest.java index 24af8caddfe..8a25a4cf6dd 100644 --- a/test/langtools/tools/javac/classfiles/attributes/SourceFile/LocalClassTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/SourceFile/LocalClassTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestBase SourceFileTestBase * @run main LocalClassTest */ diff --git a/test/langtools/tools/javac/classfiles/attributes/SourceFile/MixTest.java b/test/langtools/tools/javac/classfiles/attributes/SourceFile/MixTest.java index 55ab63bcc2f..0bc72755910 100644 --- a/test/langtools/tools/javac/classfiles/attributes/SourceFile/MixTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/SourceFile/MixTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestBase SourceFileTestBase * @run main MixTest */ diff --git a/test/langtools/tools/javac/classfiles/attributes/SourceFile/ModuleInfoTest.java b/test/langtools/tools/javac/classfiles/attributes/SourceFile/ModuleInfoTest.java index 52c359abf91..8107017fa40 100644 --- a/test/langtools/tools/javac/classfiles/attributes/SourceFile/ModuleInfoTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/SourceFile/ModuleInfoTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox toolbox.JavacTask InMemoryFileManager TestBase SourceFileTestBase * @run main ModuleInfoTest */ diff --git a/test/langtools/tools/javac/classfiles/attributes/SourceFile/NoSourceFileAttribute.java b/test/langtools/tools/javac/classfiles/attributes/SourceFile/NoSourceFileAttribute.java index 82ca7725b92..ea925c0b7e3 100644 --- a/test/langtools/tools/javac/classfiles/attributes/SourceFile/NoSourceFileAttribute.java +++ b/test/langtools/tools/javac/classfiles/attributes/SourceFile/NoSourceFileAttribute.java @@ -28,15 +28,19 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestBase SourceFileTestBase * @compile -g:none NoSourceFileAttribute.java * @run main NoSourceFileAttribute */ -import com.sun.tools.classfile.Attribute; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.ConstantPoolException; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; import java.io.IOException; @@ -46,9 +50,9 @@ public class NoSourceFileAttribute extends TestBase { new NoSourceFileAttribute().test(); } - public void test() throws IOException, ConstantPoolException { + public void test() throws IOException { assertNull( - ClassFile.read(getClassFile(NoSourceFileAttribute.class)).getAttribute(Attribute.SourceFile), + Classfile.of().parse(getClassFile(NoSourceFileAttribute.class).toPath()).findAttribute(Attributes.SOURCE_FILE).orElse(null), "Classfile should have no SourceFile attribute when compiled without debug information."); } } diff --git a/test/langtools/tools/javac/classfiles/attributes/SourceFile/SourceFileTestBase.java b/test/langtools/tools/javac/classfiles/attributes/SourceFile/SourceFileTestBase.java index f7f27e0af4e..cf9acbee4a1 100644 --- a/test/langtools/tools/javac/classfiles/attributes/SourceFile/SourceFileTestBase.java +++ b/test/langtools/tools/javac/classfiles/attributes/SourceFile/SourceFileTestBase.java @@ -21,9 +21,9 @@ * questions. */ -import com.sun.tools.classfile.Attribute; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.SourceFile_attribute; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.SourceFileAttribute; +import jdk.internal.classfile.impl.BoundAttribute; import java.io.InputStream; import java.nio.file.Path; @@ -50,7 +50,7 @@ public class SourceFileTestBase extends TestBase { * @param fileName expected name of the file from which the test file is compiled. */ protected void test(Class classToTest, String fileName) throws Exception { - assertAttributePresent(ClassFile.read(getClassFile(classToTest)), fileName); + assertAttributePresent(Classfile.of().parse(getClassFile(classToTest).toPath()), fileName); } /** @@ -60,7 +60,7 @@ public class SourceFileTestBase extends TestBase { * @param fileName expected name of the file from which the test file is compiled. */ protected void test(String classToTest, String fileName) throws Exception { - assertAttributePresent(ClassFile.read(getClassFile(classToTest + ".class")), fileName); + assertAttributePresent(Classfile.of().parse(getClassFile(classToTest + ".class").toPath()), fileName); } /** @@ -70,7 +70,7 @@ public class SourceFileTestBase extends TestBase { * @param fileName expected name of the file from which the test file is compiled. */ protected void test(Path classToTest, String fileName) throws Exception { - assertAttributePresent(ClassFile.read(classToTest), fileName); + assertAttributePresent(Classfile.of().parse(classToTest), fileName); } /** @@ -85,33 +85,32 @@ public class SourceFileTestBase extends TestBase { Map classes = compile(sourceCode).getClasses(); String fileName = ToolBox.getJavaFileNameFromSource(sourceCode); for (String className : classesToTest) { - ClassFile classFile; + ClassModel classFile; try (InputStream input = classes.get(className).openInputStream()) { - classFile = ClassFile.read(input); + classFile = Classfile.of().parse(input.readAllBytes()); } assertAttributePresent(classFile, fileName); } } - private void assertAttributePresent(ClassFile classFile, String fileName) throws Exception { + private void assertAttributePresent(ClassModel classFile, String fileName) throws Exception { //We need to count attributes with the same names because there is no appropriate API in the ClassFile. - List sourceFileAttributes = new ArrayList<>(); - for (Attribute a : classFile.attributes.attrs) { - if (Attribute.SourceFile.equals(a.getName(classFile.constant_pool))) { - sourceFileAttributes.add((SourceFile_attribute) a); + List sourceFileAttributes = new ArrayList<>(); + for (Attribute a : classFile.attributes()) { + if (a instanceof SourceFileAttribute) { + sourceFileAttributes.add((SourceFileAttribute) a); } } assertEquals(sourceFileAttributes.size(), 1, "Should be the only SourceFile attribute"); - SourceFile_attribute attribute = sourceFileAttributes.get(0); + SourceFileAttribute attribute = sourceFileAttributes.get(0); - assertEquals(classFile.constant_pool.getUTF8Info(attribute.attribute_name_index).value, - Attribute.SourceFile, "Incorrect attribute name"); - assertEquals(classFile.constant_pool.getUTF8Info(attribute.sourcefile_index).value, fileName, + assertEquals(attribute.attributeName(), Attributes.SOURCE_FILE.name(), "Incorrect attribute name"); + assertEquals(attribute.sourceFile().stringValue(), fileName, "Incorrect source file name"); - assertEquals(attribute.attribute_length, 2, "Incorrect attribute length"); + assertEquals(((BoundAttribute)attribute).payloadLen(), 2, "Incorrect attribute length"); } } diff --git a/test/langtools/tools/javac/classfiles/attributes/SourceFile/SyntheticClassTest.java b/test/langtools/tools/javac/classfiles/attributes/SourceFile/SyntheticClassTest.java index a96da1bc77e..c08fc263da0 100644 --- a/test/langtools/tools/javac/classfiles/attributes/SourceFile/SyntheticClassTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/SourceFile/SyntheticClassTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestBase SourceFileTestBase * @compile -source 10 -target 10 SyntheticClassTest.java * @run main SyntheticClassTest true diff --git a/test/langtools/tools/javac/classfiles/attributes/SourceFile/TopLevelClassesOneFileTest.java b/test/langtools/tools/javac/classfiles/attributes/SourceFile/TopLevelClassesOneFileTest.java index 01b2f194cf3..9b4d543512b 100644 --- a/test/langtools/tools/javac/classfiles/attributes/SourceFile/TopLevelClassesOneFileTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/SourceFile/TopLevelClassesOneFileTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestBase SourceFileTestBase * @run main TopLevelClassesOneFileTest */ diff --git a/test/langtools/tools/javac/classfiles/attributes/Synthetic/AccessToPrivateInnerClassConstructorsTest.java b/test/langtools/tools/javac/classfiles/attributes/Synthetic/AccessToPrivateInnerClassConstructorsTest.java index d7152de66ba..f6ea96b8048 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Synthetic/AccessToPrivateInnerClassConstructorsTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Synthetic/AccessToPrivateInnerClassConstructorsTest.java @@ -27,7 +27,12 @@ * @summary Synthetic anonymous class used as access constructor tag conflicting with a top level class. * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @library /tools/lib /tools/javac/lib ../lib * @build toolbox.ToolBox InMemoryFileManager TestResult * @build AccessToPrivateInnerClassConstructorsTest SyntheticTestDriver ExpectedClass ExpectedClasses diff --git a/test/langtools/tools/javac/classfiles/attributes/Synthetic/AccessToPrivateInnerClassMembersTest.java b/test/langtools/tools/javac/classfiles/attributes/Synthetic/AccessToPrivateInnerClassMembersTest.java index 171a7a5627b..ed440845f03 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Synthetic/AccessToPrivateInnerClassMembersTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Synthetic/AccessToPrivateInnerClassMembersTest.java @@ -28,7 +28,12 @@ * generated to access to private methods and fields. * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @library /tools/lib /tools/javac/lib ../lib * @build toolbox.ToolBox InMemoryFileManager TestResult * @build AccessToPrivateInnerClassMembersTest SyntheticTestDriver ExpectedClass ExpectedClasses diff --git a/test/langtools/tools/javac/classfiles/attributes/Synthetic/AccessToPrivateSiblingsTest.java b/test/langtools/tools/javac/classfiles/attributes/Synthetic/AccessToPrivateSiblingsTest.java index c0e039f8344..4e3d0565ea4 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Synthetic/AccessToPrivateSiblingsTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Synthetic/AccessToPrivateSiblingsTest.java @@ -28,7 +28,12 @@ * generated to access to private methods and fields. * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @library /tools/lib /tools/javac/lib ../lib * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build AccessToPrivateSiblingsTest SyntheticTestDriver ExpectedClass ExpectedClasses diff --git a/test/langtools/tools/javac/classfiles/attributes/Synthetic/AssertFieldTest.java b/test/langtools/tools/javac/classfiles/attributes/Synthetic/AssertFieldTest.java index c2299c612de..223d60075dd 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Synthetic/AssertFieldTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Synthetic/AssertFieldTest.java @@ -27,7 +27,12 @@ * @summary Checking ACC_SYNTHETIC flag is generated for assert statement. * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @library /tools/lib /tools/javac/lib ../lib * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build AssertFieldTest SyntheticTestDriver ExpectedClass ExpectedClasses diff --git a/test/langtools/tools/javac/classfiles/attributes/Synthetic/BridgeMethodForGenericMethodTest.java b/test/langtools/tools/javac/classfiles/attributes/Synthetic/BridgeMethodForGenericMethodTest.java index d69adc9bfc5..d314c4bf12a 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Synthetic/BridgeMethodForGenericMethodTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Synthetic/BridgeMethodForGenericMethodTest.java @@ -27,7 +27,12 @@ * @summary Checking ACC_SYNTHETIC flag is generated for bridge method generated for generic method. * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @library /tools/lib /tools/javac/lib ../lib * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build BridgeMethodForGenericMethodTest SyntheticTestDriver ExpectedClass ExpectedClasses diff --git a/test/langtools/tools/javac/classfiles/attributes/Synthetic/BridgeMethodsForLambdaTargetRelease14Test.java b/test/langtools/tools/javac/classfiles/attributes/Synthetic/BridgeMethodsForLambdaTargetRelease14Test.java index fc892e2e0ca..13db7b34663 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Synthetic/BridgeMethodsForLambdaTargetRelease14Test.java +++ b/test/langtools/tools/javac/classfiles/attributes/Synthetic/BridgeMethodsForLambdaTargetRelease14Test.java @@ -29,7 +29,12 @@ * compiling with --release 14. * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @library /tools/lib /tools/javac/lib ../lib * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build SyntheticTestDriver ExpectedClass ExpectedClasses diff --git a/test/langtools/tools/javac/classfiles/attributes/Synthetic/BridgeMethodsForLambdaTest.java b/test/langtools/tools/javac/classfiles/attributes/Synthetic/BridgeMethodsForLambdaTest.java index 51d9f324e06..c1f632b2576 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Synthetic/BridgeMethodsForLambdaTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Synthetic/BridgeMethodsForLambdaTest.java @@ -28,7 +28,12 @@ * generated for lambda expressions and method references. * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @library /tools/lib /tools/javac/lib ../lib * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build SyntheticTestDriver ExpectedClass ExpectedClasses diff --git a/test/langtools/tools/javac/classfiles/attributes/Synthetic/EnumTest.java b/test/langtools/tools/javac/classfiles/attributes/Synthetic/EnumTest.java index 764a23a3990..9f99a453405 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Synthetic/EnumTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Synthetic/EnumTest.java @@ -27,7 +27,12 @@ * @summary Checking ACC_SYNTHETIC flag is generated for enum members. * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @library /tools/lib /tools/javac/lib ../lib * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build EnumTest SyntheticTestDriver ExpectedClass ExpectedClasses diff --git a/test/langtools/tools/javac/classfiles/attributes/Synthetic/PackageInfoTest.java b/test/langtools/tools/javac/classfiles/attributes/Synthetic/PackageInfoTest.java index 71bd8af7a2b..d9f9a8c8f19 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Synthetic/PackageInfoTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Synthetic/PackageInfoTest.java @@ -27,7 +27,12 @@ * @summary Checking ACC_SYNTHETIC flag is generated for package-info. * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @library /tools/lib /tools/javac/lib ../lib * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build SyntheticTestDriver ExpectedClass ExpectedClasses diff --git a/test/langtools/tools/javac/classfiles/attributes/Synthetic/SyntheticTestDriver.java b/test/langtools/tools/javac/classfiles/attributes/Synthetic/SyntheticTestDriver.java index 5c5d67792d9..bef23b4373b 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Synthetic/SyntheticTestDriver.java +++ b/test/langtools/tools/javac/classfiles/attributes/Synthetic/SyntheticTestDriver.java @@ -23,6 +23,7 @@ import java.io.File; import java.io.IOException; +import java.lang.constant.ClassDesc; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; @@ -31,8 +32,10 @@ import java.util.function.Supplier; import java.util.regex.*; import java.util.stream.Collectors; import java.util.stream.Stream; +import java.lang.reflect.AccessFlag; -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.SyntheticAttribute; /** * The tests work as follows. Firstly, it looks through the test cases @@ -48,15 +51,12 @@ import com.sun.tools.classfile.*; * See the appropriate class for more information about a test case. */ public class SyntheticTestDriver extends TestResult { - - private static final String ACC_SYNTHETIC = "ACC_SYNTHETIC"; - private final String testCaseName; - private final Map classes; + private final Map classes; private final Map expectedClasses; public static void main(String[] args) - throws TestFailedException, ConstantPoolException, IOException, ClassNotFoundException { + throws TestFailedException, IOException, ClassNotFoundException { if (args.length != 1 && args.length != 2) { throw new IllegalArgumentException("Usage: SyntheticTestDriver []"); } @@ -64,7 +64,7 @@ public class SyntheticTestDriver extends TestResult { new SyntheticTestDriver(args[0]).test(numberOfSyntheticClasses); } - public SyntheticTestDriver(String testCaseName) throws IOException, ConstantPoolException, ClassNotFoundException { + public SyntheticTestDriver(String testCaseName) throws IOException, ClassNotFoundException { Class clazz = Class.forName(testCaseName); this.testCaseName = testCaseName; this.expectedClasses = Stream.of(clazz.getAnnotationsByType(ExpectedClass.class)) @@ -75,7 +75,7 @@ public class SyntheticTestDriver extends TestResult { List paths = Files.walk(classDir) .map(p -> classDir.relativize(p.toAbsolutePath())) .filter(p -> filePattern.matcher(p.toString()).matches()) - .collect(Collectors.toList()); + .toList(); for (Path path : paths) { String className = path.toString().replace(".class", "").replace(File.separatorChar, '.'); classes.put(className, readClassFile(classDir.resolve(path).toFile())); @@ -91,11 +91,19 @@ public class SyntheticTestDriver extends TestResult { } } - private String getMethodName(ClassFile classFile, Method method) - throws ConstantPoolException, Descriptor.InvalidDescriptor { - String methodName = method.getName(classFile.constant_pool); - String parameters = method.descriptor.getParameterTypes(classFile.constant_pool); - return methodName + parameters; + private String getMethodName(MethodModel method) { + StringBuilder methodName = new StringBuilder(method.methodName().stringValue() + "("); + List paras = method.methodTypeSymbol().parameterList(); + for (int i = 0; i < method.methodTypeSymbol().parameterCount(); ++i) { + if (i != 0) { + methodName.append(", "); + } + ClassDesc para = paras.get(i); + String prefix = para.componentType() == null? para.packageName(): para.componentType().packageName(); + methodName.append(prefix).append(Objects.equals(prefix, "") ? "":".").append(para.displayName()); + } + methodName.append(")"); + return methodName.toString(); } public void test(int expectedNumberOfSyntheticClasses) throws TestFailedException { @@ -104,14 +112,14 @@ public class SyntheticTestDriver extends TestResult { Set foundClasses = new HashSet<>(); int numberOfSyntheticClasses = 0; - for (Map.Entry entry : classes.entrySet()) { + for (Map.Entry entry : classes.entrySet()) { String className = entry.getKey(); - ClassFile classFile = entry.getValue(); + ClassModel classFile = entry.getValue(); foundClasses.add(className); if (testAttribute( classFile, - () -> (Synthetic_attribute) classFile.getAttribute(Attribute.Synthetic), - classFile.access_flags::getClassFlags, + () -> classFile.findAttribute(Attributes.SYNTHETIC).orElse(null), + classFile.flags()::flags, expectedClasses.keySet(), className, "Testing class " + className)) { @@ -123,13 +131,13 @@ public class SyntheticTestDriver extends TestResult { : new HashSet<>(); int numberOfSyntheticMethods = 0; Set foundMethods = new HashSet<>(); - for (Method method : classFile.methods) { - String methodName = getMethodName(classFile, method); + for (MethodModel method : classFile.methods()) { + String methodName = getMethodName(method); foundMethods.add(methodName); if (testAttribute( classFile, - () -> (Synthetic_attribute) method.attributes.get(Attribute.Synthetic), - method.access_flags::getMethodFlags, + () -> method.findAttribute(Attributes.SYNTHETIC).orElse(null), + method.flags()::flags, expectedMethods, methodName, "Testing method " + methodName + " in class " @@ -149,13 +157,13 @@ public class SyntheticTestDriver extends TestResult { : new HashSet<>(); int numberOfSyntheticFields = 0; Set foundFields = new HashSet<>(); - for (Field field : classFile.fields) { - String fieldName = field.getName(classFile.constant_pool); + for (FieldModel field : classFile.fields()) { + String fieldName = field.fieldName().stringValue(); foundFields.add(fieldName); if (testAttribute( classFile, - () -> (Synthetic_attribute) field.attributes.get(Attribute.Synthetic), - field.access_flags::getFieldFlags, + () -> field.findAttribute(Attributes.SYNTHETIC).orElse(null), + field.flags()::flags, expectedFields, fieldName, "Testing field " + fieldName + " in class " @@ -181,25 +189,25 @@ public class SyntheticTestDriver extends TestResult { } } - private boolean testAttribute(ClassFile classFile, - Supplier getSyntheticAttribute, - Supplier> getAccessFlags, + private boolean testAttribute(ClassModel classFile, + Supplier getSyntheticAttribute, + Supplier> getAccessFlags, Set expectedMembers, String memberName, - String info) throws ConstantPoolException { + String info) { echo(info); - String className = classFile.getName(); - Synthetic_attribute attr = getSyntheticAttribute.get(); - Set flags = getAccessFlags.get(); + String className = classFile.thisClass().name().stringValue(); + SyntheticAttribute attr = getSyntheticAttribute.get(); + Set flags = getAccessFlags.get(); if (expectedMembers.contains(memberName)) { checkNull(attr, "Member must not have synthetic attribute : " + memberName); - checkFalse(flags.contains(ACC_SYNTHETIC), + checkFalse(flags.contains(AccessFlag.SYNTHETIC), "Member must not have synthetic flag : " + memberName + " in class : " + className); return false; } else { return checkNull(attr, "Synthetic attribute should not be generated") - && checkTrue(flags.contains(ACC_SYNTHETIC), "Member must have synthetic flag : " + && checkTrue(flags.contains(AccessFlag.SYNTHETIC), "Member must have synthetic flag : " + memberName + " in class : " + className); } } diff --git a/test/langtools/tools/javac/classfiles/attributes/Synthetic/ThisFieldTest.java b/test/langtools/tools/javac/classfiles/attributes/Synthetic/ThisFieldTest.java index 3ca6e65ef90..c32075207d5 100644 --- a/test/langtools/tools/javac/classfiles/attributes/Synthetic/ThisFieldTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/Synthetic/ThisFieldTest.java @@ -27,7 +27,12 @@ * @summary Checking ACC_SYNTHETIC flag is generated for "this$0" field. * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @library /tools/lib /tools/javac/lib ../lib * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build ThisFieldTest SyntheticTestDriver ExpectedClass ExpectedClasses diff --git a/test/langtools/tools/javac/classfiles/attributes/annotations/AnnotationsTestBase.java b/test/langtools/tools/javac/classfiles/attributes/annotations/AnnotationsTestBase.java index cadbd354497..25c804bd011 100644 --- a/test/langtools/tools/javac/classfiles/attributes/annotations/AnnotationsTestBase.java +++ b/test/langtools/tools/javac/classfiles/attributes/annotations/AnnotationsTestBase.java @@ -21,9 +21,7 @@ * questions. */ -import com.sun.tools.classfile.Attribute; -import com.sun.tools.classfile.ConstantPoolException; -import com.sun.tools.classfile.Descriptor; +import jdk.internal.classfile.Attribute; import javax.tools.JavaFileObject; import java.io.IOException; @@ -105,7 +103,7 @@ public abstract class AnnotationsTestBase extends TestResult { public abstract List generateTestCases(); public abstract void test(TestCase testCase, Map classes) - throws IOException, ConstantPoolException, Descriptor.InvalidDescriptor; + throws IOException; /** * The method is used to create a repeatable annotation. @@ -176,7 +174,7 @@ public abstract class AnnotationsTestBase extends TestResult { combinations.addAll(list.stream() .map(Collections::singletonList) .map(TestAnnotationInfos::new) - .collect(Collectors.toList())); + .toList()); // add cases with a repeatable annotation for (Annotations annotationName2 : Annotations.values()) { @@ -203,9 +201,9 @@ public abstract class AnnotationsTestBase extends TestResult { throw new IllegalArgumentException(name); } - protected long countNumberOfAttributes(Attribute[] attrs, - Class clazz) { - return Stream.of(attrs) + protected long countNumberOfAttributes(List> attrs, + Class> clazz) { + return attrs.stream() .filter(clazz::isInstance) .count(); } diff --git a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForGenericMethodTest.java b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForGenericMethodTest.java index e2b7ae61d4f..4aafea1b40b 100644 --- a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForGenericMethodTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForGenericMethodTest.java @@ -26,7 +26,12 @@ * @bug 8044411 * @summary Tests the RuntimeVisibleAnnotations/RuntimeInvisibleAnnotations attribute. * Checks that the attribute is generated for bridge method. - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * @library /tools/lib /tools/javac/lib ../lib @@ -70,10 +75,10 @@ public class RuntimeAnnotationsForGenericMethodTest extends RuntimeAnnotationsTe TestAnnotationInfos annotations = groupedAnnotations.get(i); // generate: public class Test extends java.util.ArrayList TestCase.TestClassInfo clazz = testCase.addClassInfo("java.util.ArrayList", ClassType.CLASS, "Test" + i); - TestCase.TestMethodInfo method = clazz.addMethodInfo("add(java.lang.Integer)", "public"); + TestCase.TestMethodInfo method = clazz.addMethodInfo("add(Integer)", "public"); method.addParameter("Integer", "i"); annotations.annotate(method); - TestCase.TestMethodInfo synMethod = clazz.addMethodInfo("add(java.lang.Object)", true, "public"); + TestCase.TestMethodInfo synMethod = clazz.addMethodInfo("add(Object)", true, "public"); annotations.annotate(synMethod); } testCases.add(testCase); diff --git a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForInnerAnnotationTest.java b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForInnerAnnotationTest.java index b32a9596cb0..f0e65c2babf 100644 --- a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForInnerAnnotationTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForInnerAnnotationTest.java @@ -25,7 +25,12 @@ * @test * @bug 8044411 * @summary Tests the RuntimeVisibleAnnotations/RuntimeInvisibleAnnotations attribute. - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * @library /tools/lib /tools/javac/lib ../lib diff --git a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForInnerClassTest.java b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForInnerClassTest.java index 3757b5e4681..139193446e4 100644 --- a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForInnerClassTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForInnerClassTest.java @@ -25,7 +25,12 @@ * @test * @bug 8044411 * @summary Tests the RuntimeVisibleAnnotations/RuntimeInvisibleAnnotations attribute. - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * @library /tools/lib /tools/javac/lib ../lib diff --git a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForInnerEnumTest.java b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForInnerEnumTest.java index b40176c08bd..87070329ebd 100644 --- a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForInnerEnumTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForInnerEnumTest.java @@ -25,7 +25,12 @@ * @test * @bug 8044411 * @summary Tests the RuntimeVisibleAnnotations/RuntimeInvisibleAnnotations attribute. - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * @library /tools/lib /tools/javac/lib ../lib diff --git a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForInnerInterfaceTest.java b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForInnerInterfaceTest.java index d4306e1c353..ae11817cabc 100644 --- a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForInnerInterfaceTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForInnerInterfaceTest.java @@ -25,7 +25,12 @@ * @test * @bug 8044411 * @summary Tests the RuntimeVisibleAnnotations/RuntimeInvisibleAnnotations attribute. - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * @library /tools/lib /tools/javac/lib ../lib diff --git a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForTopLevelClassTest.java b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForTopLevelClassTest.java index 52e2c524194..55414c261c8 100644 --- a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForTopLevelClassTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsForTopLevelClassTest.java @@ -25,7 +25,12 @@ * @test * @bug 8044411 * @summary Tests the RuntimeVisibleAnnotations/RuntimeInvisibleAnnotations attribute. - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * @library /tools/lib /tools/javac/lib ../lib diff --git a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsTestBase.java b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsTestBase.java index 37cc1c049bd..af5ac818855 100644 --- a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsTestBase.java +++ b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeAnnotationsTestBase.java @@ -21,7 +21,8 @@ * questions. */ -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; import java.io.IOException; import java.lang.annotation.RetentionPolicy; @@ -34,14 +35,14 @@ public abstract class RuntimeAnnotationsTestBase extends AnnotationsTestBase { @Override public void test(TestCase testCase, Map classes) - throws IOException, ConstantPoolException, Descriptor.InvalidDescriptor { + throws IOException { for (Map.Entry entry : classes.entrySet()) { String className = entry.getKey(); TestCase.TestClassInfo clazz = testCase.getTestClassInfo(className); echo("Testing class : " + className); - ClassFile classFile = readClassFile(entry.getValue()); + ClassModel classFile = readClassFile(entry.getValue()); - testAttributes(clazz, classFile, () -> classFile.attributes); + testAttributes(clazz, classFile, classFile); testMethods(clazz, classFile); @@ -49,13 +50,13 @@ public abstract class RuntimeAnnotationsTestBase extends AnnotationsTestBase { } } - private void testMethods(TestCase.TestClassInfo clazz, ClassFile classFile) - throws ConstantPoolException, Descriptor.InvalidDescriptor { + private void testMethods(TestCase.TestClassInfo clazz, ClassModel classFile) { String className = clazz.getName(); Set foundMethods = new HashSet<>(); - for (Method method : classFile.methods) { - String methodName = method.getName(classFile.constant_pool) + - method.descriptor.getParameterTypes(classFile.constant_pool); + for (MethodModel method : classFile.methods()) { + String methodName = method.methodName().stringValue() + + method.methodTypeSymbol().displayDescriptor(); + methodName = methodName.substring(0, methodName.indexOf(")") + 1); if (methodName.startsWith("")) { String constructorName = className.replaceAll(".*\\$", ""); methodName = methodName.replace("", constructorName); @@ -67,16 +68,15 @@ public abstract class RuntimeAnnotationsTestBase extends AnnotationsTestBase { if (testMethod == null) { continue; } - testAttributes(testMethod, classFile, () -> method.attributes); + testAttributes(testMethod, classFile, method); } checkContains(foundMethods, clazz.methods.keySet(), "Methods in class : " + className); } - private void testFields(TestCase.TestClassInfo clazz, ClassFile classFile) - throws ConstantPoolException { + private void testFields(TestCase.TestClassInfo clazz, ClassModel classFile) { Set foundFields = new HashSet<>(); - for (Field field : classFile.fields) { - String fieldName = field.getName(classFile.constant_pool); + for (FieldModel field : classFile.fields()) { + String fieldName = field.fieldName().stringValue(); echo("Testing field : " + fieldName); TestCase.TestFieldInfo testField = clazz.getTestFieldInfo(fieldName); @@ -84,26 +84,23 @@ public abstract class RuntimeAnnotationsTestBase extends AnnotationsTestBase { if (testField == null) { continue; } - testAttributes(testField, classFile, () -> field.attributes); + testAttributes(testField, classFile, field); } checkContains(foundFields, clazz.fields.keySet(), "Fields in class : " + clazz.getName()); } private void testAttributes( TestCase.TestMemberInfo member, - ClassFile classFile, - Supplier attributes) - throws ConstantPoolException { + ClassModel classFile, + AttributedElement attributedElement) { Map actualInvisible = collectAnnotations( - classFile, member, - attributes.get(), - Attribute.RuntimeInvisibleAnnotations); + attributedElement, + Attributes.RUNTIME_INVISIBLE_ANNOTATIONS); Map actualVisible = collectAnnotations( - classFile, member, - attributes.get(), - Attribute.RuntimeVisibleAnnotations); + attributedElement, + Attributes.RUNTIME_VISIBLE_ANNOTATIONS); checkEquals(actualInvisible.keySet(), member.getRuntimeInvisibleAnnotations(), "RuntimeInvisibleAnnotations"); @@ -126,32 +123,41 @@ public abstract class RuntimeAnnotationsTestBase extends AnnotationsTestBase { } } - private Map collectAnnotations( - ClassFile classFile, + private > Map collectAnnotations( TestCase.TestMemberInfo member, - Attributes attributes, - String attribute) throws ConstantPoolException { + AttributedElement attributedElement, + AttributeMapper attribute) { - RuntimeAnnotations_attribute attr = (RuntimeAnnotations_attribute) attributes.get(attribute); + Object attr = attributedElement.findAttribute(attribute).orElse(null); Map actualAnnotations = new HashMap<>(); - RetentionPolicy policy = getRetentionPolicy(attribute); + RetentionPolicy policy = getRetentionPolicy(attribute.name()); if (member.isAnnotated(policy)) { - if (!checkNotNull(attr, String.format("%s should be not null value", attribute))) { + if (!checkNotNull(attr, String.format("%s should be not null value", attribute.name()))) { // test case failed, stop checking return actualAnnotations; } - for (Annotation ann : attr.annotations) { - String name = classFile.constant_pool.getUTF8Value(ann.type_index); - actualAnnotations.put(name.substring(1, name.length() - 1), ann); + List annotationList; + switch (attr) { + case RuntimeVisibleAnnotationsAttribute annots -> { + annotationList = annots.annotations(); + } + case RuntimeInvisibleAnnotationsAttribute annots -> { + annotationList = annots.annotations(); + } + default -> throw new AssertionError(); } - checkEquals(countNumberOfAttributes(attributes.attrs, - getRetentionPolicy(attribute) == RetentionPolicy.RUNTIME - ? RuntimeVisibleAnnotations_attribute.class - : RuntimeInvisibleAnnotations_attribute.class), - 1l, - String.format("Number of %s", attribute)); + for (Annotation ann : annotationList) { + String name = ann.classSymbol().displayName(); + actualAnnotations.put(name, ann); + } + checkEquals(countNumberOfAttributes(attributedElement.attributes(), + getRetentionPolicy(attribute.name()) == RetentionPolicy.RUNTIME + ? RuntimeVisibleAnnotationsAttribute.class + : RuntimeInvisibleAnnotationsAttribute.class), + 1L, + String.format("Number of %s", attribute.name())); } else { - checkNull(attr, String.format("%s should be null", attribute)); + checkNull(attr, String.format("%s should be null", attribute.name())); } return actualAnnotations; } diff --git a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsForGenericMethodTest.java b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsForGenericMethodTest.java index a2cfcbf7512..e2b1a82d1b0 100644 --- a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsForGenericMethodTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsForGenericMethodTest.java @@ -26,7 +26,12 @@ * @bug 8044411 * @summary Tests the RuntimeParameterVisibleAnnotations/RuntimeParameterInvisibleAnnotations attribute. * Checks that the attribute is generated for bridge method. - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * @library /tools/lib /tools/javac/lib ../lib @@ -68,9 +73,9 @@ public class RuntimeParameterAnnotationsForGenericMethodTest extends RuntimePara // generate: public class Test extends java.util.ArrayList TestCase testCase = new TestCase(); TestCase.TestClassInfo clazz = testCase.addClassInfo("java.util.ArrayList", ClassType.CLASS, "Test"); - TestCase.TestParameterInfo parameter = clazz.addMethodInfo("add(java.lang.Integer)", "public").addParameter("Integer", "i"); + TestCase.TestParameterInfo parameter = clazz.addMethodInfo("add(Integer)", "public").addParameter("Integer", "i"); annotations.annotate(parameter); - TestCase.TestParameterInfo synParameter = clazz.addMethodInfo("add(java.lang.Object)", true, "public").addParameter("Object", "i"); + TestCase.TestParameterInfo synParameter = clazz.addMethodInfo("add(Object)", true, "public").addParameter("Object", "i"); annotations.annotate(synParameter); testCases.add(testCase); } diff --git a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsForLambdaTest.java b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsForLambdaTest.java index 33b1f676a78..26ee86a730c 100644 --- a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsForLambdaTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsForLambdaTest.java @@ -25,7 +25,12 @@ * @test * @bug 8044411 8079060 8138612 * @summary Tests the RuntimeParameterVisibleAnnotations/RuntimeParameterInvisibleAnnotations attribute. - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * @library /tools/lib /tools/javac/lib ../lib @@ -35,10 +40,15 @@ * @run main RuntimeParameterAnnotationsForLambdaTest */ +import jdk.internal.classfile.Attributes; +import jdk.internal.classfile.ClassModel; +import jdk.internal.classfile.MethodModel; +import jdk.internal.classfile.attribute.RuntimeInvisibleParameterAnnotationsAttribute; +import jdk.internal.classfile.attribute.RuntimeVisibleParameterAnnotationsAttribute; + import java.util.List; import java.util.stream.Collectors; -import com.sun.tools.classfile.*; /** * RuntimeParameterAnnotationsForLambdaTest is a test which checks that RuntimeVisibleParameterAnnotationsAttribute @@ -55,9 +65,9 @@ public class RuntimeParameterAnnotationsForLambdaTest extends RuntimeParameterAn private static final String CLASS_NAME = "Test"; private static final String SOURCE_TEMPLATE = "public class " + CLASS_NAME + " {\n" + - " interface I { void method(int a, double b, String c); }\n" + - " %SOURCE%\n" + - "}"; + " interface I { void method(int a, double b, String c); }\n" + + " %SOURCE%\n" + + "}"; public static void main(String[] args) throws TestFailedException { new RuntimeParameterAnnotationsForLambdaTest().test(); @@ -77,12 +87,12 @@ public class RuntimeParameterAnnotationsForLambdaTest extends RuntimeParameterAn String source = SOURCE_TEMPLATE.replace("%SOURCE%", generateLambdaSource(testMethodInfo)); addTestCase(source); echo("Testing:\n" + source); - ClassFile classFile = readClassFile(compile(source).getClasses().get(CLASS_NAME)); + ClassModel classFile = readClassFile(compile(source).getClasses().get(CLASS_NAME)); boolean isFoundLambda = false; - for (Method method : classFile.methods) { - if (method.getName(classFile.constant_pool).startsWith("lambda$")) { + for (MethodModel method : classFile.methods()) { + if (method.methodName().stringValue().startsWith("lambda$")) { isFoundLambda = true; - testAttributes(testMethodInfo, classFile, method); + testAttributes(testMethodInfo, method); } } checkTrue(isFoundLambda, "The tested lambda method was not found."); @@ -97,13 +107,11 @@ public class RuntimeParameterAnnotationsForLambdaTest extends RuntimeParameterAn protected void testAttributes( TestCase.TestMethodInfo testMethod, - ClassFile classFile, - Method method) throws ConstantPoolException { - Attributes attributes = method.attributes; - RuntimeParameterAnnotations_attribute attr = (RuntimeParameterAnnotations_attribute) attributes.get(Attribute.RuntimeInvisibleParameterAnnotations); - checkNull(attr, String.format("%s should be null", Attribute.RuntimeInvisibleParameterAnnotations)); - attr = (RuntimeParameterAnnotations_attribute) attributes.get(Attribute.RuntimeVisibleParameterAnnotations); - checkNull(attr, String.format("%s should be null", Attribute.RuntimeVisibleParameterAnnotations)); + MethodModel method) { + RuntimeInvisibleParameterAnnotationsAttribute invAttr = method.findAttribute(Attributes.RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS).orElse(null); + checkNull(invAttr, String.format("%s should be null", Attributes.RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS)); + RuntimeVisibleParameterAnnotationsAttribute vAttr = method.findAttribute(Attributes.RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS).orElse(null); + checkNull(vAttr, String.format("%s should be null", Attributes.RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS)); } public String generateLambdaSource(TestCase.TestMethodInfo method) { diff --git a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsTest.java b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsTest.java index 67c1db2bcbe..e7c878ce7cb 100644 --- a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsTest.java @@ -25,7 +25,12 @@ * @test * @bug 8044411 * @summary Tests the RuntimeParameterVisibleAnnotations/RuntimeParameterInvisibleAnnotations attribute. - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * @library /tools/lib /tools/javac/lib ../lib @@ -85,7 +90,7 @@ public class RuntimeParameterAnnotationsTest extends RuntimeParameterAnnotations * @param mods an array of modifiers */ private void initMethod(TestAnnotationInfos annotations, TestCase.TestClassInfo clazz, String methodName, String...mods) { - String methodDescriptor = methodName + "(int, double, java.lang.String)"; + String methodDescriptor = methodName + "(int,double,String)"; TestCase.TestMethodInfo method = clazz.addMethodInfo(methodDescriptor, mods); TestCase.TestParameterInfo p1 = method.addParameter("int", "a"); annotations.annotate(p1); diff --git a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsTestBase.java b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsTestBase.java index 6377047b0c9..286e64ff5be 100644 --- a/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsTestBase.java +++ b/test/langtools/tools/javac/classfiles/attributes/annotations/RuntimeParameterAnnotationsTestBase.java @@ -21,7 +21,8 @@ * questions. */ -import com.sun.tools.classfile.*; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; import javax.tools.JavaFileObject; import java.io.IOException; @@ -32,15 +33,16 @@ public abstract class RuntimeParameterAnnotationsTestBase extends AnnotationsTes @Override public void test(TestCase testCase, Map classes) - throws IOException, ConstantPoolException, Descriptor.InvalidDescriptor { + throws IOException { for (Map.Entry entry : classes.entrySet()) { - ClassFile classFile = readClassFile(classes.get(entry.getKey())); + ClassModel classFile = readClassFile(classes.get(entry.getKey())); Set foundMethods = new HashSet<>(); - String className = classFile.getName(); + String className = classFile.thisClass().name().stringValue(); TestCase.TestClassInfo testClassInfo = testCase.classes.get(className); - for (Method method : classFile.methods) { - String methodName = method.getName(classFile.constant_pool) + - method.descriptor.getParameterTypes(classFile.constant_pool); + for (MethodModel method : classFile.methods()) { + String methodName = method.methodName().stringValue() + + method.methodTypeSymbol().displayDescriptor(); + methodName = methodName.substring(0, methodName.indexOf(")") + 1); if (methodName.startsWith("")) { methodName = methodName.replace("", className); } @@ -59,18 +61,18 @@ public abstract class RuntimeParameterAnnotationsTestBase extends AnnotationsTes protected void testAttributes( TestCase.TestMethodInfo testMethod, - ClassFile classFile, - Method method) throws ConstantPoolException { + ClassModel classFile, + MethodModel method) { List> actualInvisible = collectAnnotations( classFile, testMethod, method, - Attribute.RuntimeInvisibleParameterAnnotations); + Attributes.RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS); List> actualVisible = collectAnnotations( classFile, testMethod, method, - Attribute.RuntimeVisibleParameterAnnotations); + Attributes.RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS); List parameters = testMethod.parameters; for (int i = 0; i < parameters.size(); ++i) { @@ -103,38 +105,46 @@ public abstract class RuntimeParameterAnnotationsTestBase extends AnnotationsTes } } - private List> collectAnnotations( - ClassFile classFile, + private > List> collectAnnotations( + ClassModel classFile, TestCase.TestMethodInfo testMethod, - Method method, - String attribute) throws ConstantPoolException { - - Attributes attributes = method.attributes; - RuntimeParameterAnnotations_attribute attr = (RuntimeParameterAnnotations_attribute) attributes.get(attribute); + MethodModel method, + AttributeMapper attribute) { + Object attr = method.findAttribute(attribute).orElse(null); List> actualAnnotations = new ArrayList<>(); - RetentionPolicy policy = getRetentionPolicy(attribute); + RetentionPolicy policy = getRetentionPolicy(attribute.name()); if (testMethod.isParameterAnnotated(policy)) { - if (!checkNotNull(attr, "Attribute " + attribute + " must not be null")) { + if (!checkNotNull(attr, "Attribute " + attribute.name() + " must not be null")) { testMethod.parameters.forEach($ -> actualAnnotations.add(new HashMap<>())); return actualAnnotations; } - for (Annotation[] anns : attr.parameter_annotations) { + List> annotationList; + switch (attr) { + case RuntimeVisibleParameterAnnotationsAttribute pAnnots -> { + annotationList = pAnnots.parameterAnnotations(); + } + case RuntimeInvisibleParameterAnnotationsAttribute pAnnots -> { + annotationList = pAnnots.parameterAnnotations(); + } + default -> throw new AssertionError(); + } + for (List anns: annotationList) { Map annotations = new HashMap<>(); - for (Annotation ann : anns) { - String name = classFile.constant_pool.getUTF8Value(ann.type_index); - annotations.put(name.substring(1, name.length() - 1), ann); + for (Annotation ann: anns) { + String name = ann.classSymbol().displayName(); + annotations.put(name, ann); } actualAnnotations.add(annotations); } - checkEquals(countNumberOfAttributes(attributes.attrs, - getRetentionPolicy(attribute) == RetentionPolicy.RUNTIME - ? RuntimeVisibleParameterAnnotations_attribute.class - : RuntimeInvisibleParameterAnnotations_attribute.class), - 1l, - String.format("Number of %s", attribute)); + checkEquals(countNumberOfAttributes(method.attributes(), + getRetentionPolicy(attribute.name()) == RetentionPolicy.RUNTIME + ? RuntimeVisibleParameterAnnotationsAttribute.class + : RuntimeInvisibleParameterAnnotationsAttribute.class), + 1L, + String.format("Number of %s", attribute.name())); } else { - checkNull(attr, String.format("%s should be null", attribute)); + checkNull(attr, String.format("%s should be null", attribute.name())); testMethod.parameters.forEach($ -> actualAnnotations.add(new HashMap<>())); } return actualAnnotations; diff --git a/test/langtools/tools/javac/classfiles/attributes/annotations/TestAnnotationInfo.java b/test/langtools/tools/javac/classfiles/attributes/annotations/TestAnnotationInfo.java index e23a8d84bab..46811a11c0f 100644 --- a/test/langtools/tools/javac/classfiles/attributes/annotations/TestAnnotationInfo.java +++ b/test/langtools/tools/javac/classfiles/attributes/annotations/TestAnnotationInfo.java @@ -21,10 +21,8 @@ * questions. */ -import com.sun.tools.classfile.Annotation; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.ConstantPool; -import com.sun.tools.classfile.ConstantPoolException; +import jdk.internal.classfile.*; +import jdk.internal.classfile.AnnotationValue; import java.lang.annotation.RetentionPolicy; import java.util.*; @@ -47,21 +45,20 @@ public class TestAnnotationInfo { elementValues = Arrays.asList(values); } - public void testAnnotation(TestResult testResult, ClassFile classFile, Annotation annotation) - throws ConstantPoolException { - testResult.checkEquals(classFile.constant_pool.getUTF8Value(annotation.type_index), + public void testAnnotation(TestResult testResult, ClassModel classFile, Annotation annotation) { + testResult.checkEquals(annotation.classSymbol().descriptorString(), String.format("L%s;", annotationName), "Testing annotation name : " + annotationName); - testResult.checkEquals(annotation.num_element_value_pairs, + testResult.checkEquals(annotation.elements().size(), elementValues.size(), "Number of element values"); - if (!testResult.checkEquals(annotation.num_element_value_pairs, elementValues.size(), + if (!testResult.checkEquals(annotation.elements().size(), elementValues.size(), "Number of element value pairs")) { return; } - for (int i = 0; i < annotation.num_element_value_pairs; ++i) { - Annotation.element_value_pair pair = annotation.element_value_pairs[i]; - testResult.checkEquals(classFile.constant_pool.getUTF8Value(pair.element_name_index), + for (int i = 0; i < annotation.elements().size(); ++i) { + AnnotationElement pair = annotation.elements().get(i); + testResult.checkEquals(pair.name().stringValue(), elementValues.get(i).elementName, "element_name_index : " + elementValues.get(i).elementName); - elementValues.get(i).elementValue.testElementValue(testResult, classFile, pair.value); + elementValues.get(i).elementValue.testElementValue(testResult, classFile, pair.value()); } } @@ -101,9 +98,8 @@ public class TestAnnotationInfo { } public abstract void testElementValue(TestResult testResult, - ClassFile classFile, - Annotation.element_value element_value) - throws ConstantPoolException; + ClassModel classFile, + AnnotationValue element_value); } public static class TestIntegerElementValue extends TestElementValue { @@ -116,15 +112,19 @@ public class TestAnnotationInfo { @Override public void testElementValue(TestResult testResult, - ClassFile classFile, - Annotation.element_value element_value) - throws ConstantPoolException { - testTag(testResult, element_value.tag); - Annotation.Primitive_element_value ev = - (Annotation.Primitive_element_value) element_value; - ConstantPool.CONSTANT_Integer_info info = - (ConstantPool.CONSTANT_Integer_info) classFile.constant_pool.get(ev.const_value_index); - testResult.checkEquals(info.value, value, "const_value_index : " + value); + ClassModel classFile, + AnnotationValue element_value) { + testTag(testResult, element_value.tag()); + switch (element_value.tag()) { + case 'B': + testResult.checkEquals((int)((AnnotationValue.OfByte) element_value).byteValue(), value, "const_value_index : " + value); + break; + case 'S': + testResult.checkEquals((int)((AnnotationValue.OfShort) element_value).shortValue(), value, "const_value_index : " + value); + break; + default: + testResult.checkEquals(((AnnotationValue.OfInteger) element_value).intValue(), value, "const_value_index : " + value); + } } @Override @@ -143,15 +143,11 @@ public class TestAnnotationInfo { @Override public void testElementValue(TestResult testResult, - ClassFile classFile, - Annotation.element_value element_value) - throws ConstantPoolException { - testTag(testResult, element_value.tag); - Annotation.Primitive_element_value ev = - (Annotation.Primitive_element_value) element_value; - ConstantPool.CONSTANT_Integer_info info = - (ConstantPool.CONSTANT_Integer_info) classFile.constant_pool.get(ev.const_value_index); - testResult.checkEquals(info.value, value ? 1 : 0, "const_value_index : " + value); + ClassModel classFile, + AnnotationValue element_value) { + testTag(testResult, element_value.tag()); + AnnotationValue.OfBoolean ev = (AnnotationValue.OfBoolean) element_value; + testResult.checkEquals(ev.booleanValue(), value, "const_value_index : " + value); } @Override @@ -170,16 +166,12 @@ public class TestAnnotationInfo { @Override public void testElementValue(TestResult testResult, - ClassFile classFile, - Annotation.element_value element_value) - throws ConstantPoolException { - testTag(testResult, element_value.tag); - Annotation.Primitive_element_value ev = - (Annotation.Primitive_element_value) element_value; - ConstantPool.CONSTANT_Integer_info info = - (ConstantPool.CONSTANT_Integer_info) - classFile.constant_pool.get(ev.const_value_index); - testResult.checkEquals(info.value, (int) value, "const_value_index : " + value); + ClassModel classFile, + AnnotationValue element_value) { + testTag(testResult, element_value.tag()); + AnnotationValue.OfCharacter ev = + (AnnotationValue.OfCharacter) element_value; + testResult.checkEquals(ev.charValue(), value, "const_value_index : " + value); } @Override @@ -198,15 +190,12 @@ public class TestAnnotationInfo { @Override public void testElementValue(TestResult testResult, - ClassFile classFile, - Annotation.element_value element_value) - throws ConstantPool.InvalidIndex { - testTag(testResult, element_value.tag); - Annotation.Primitive_element_value ev = - (Annotation.Primitive_element_value) element_value; - ConstantPool.CONSTANT_Long_info info = - (ConstantPool.CONSTANT_Long_info) classFile.constant_pool.get(ev.const_value_index); - testResult.checkEquals(info.value, value, "const_value_index"); + ClassModel classFile, + AnnotationValue element_value) { + testTag(testResult, element_value.tag()); + AnnotationValue.OfLong ev = + (AnnotationValue.OfLong) element_value; + testResult.checkEquals(ev.longValue(), value, "const_value_index"); } @Override @@ -225,15 +214,12 @@ public class TestAnnotationInfo { @Override public void testElementValue(TestResult testResult, - ClassFile classFile, - Annotation.element_value element_value) - throws ConstantPool.InvalidIndex { - testTag(testResult, element_value.tag); - Annotation.Primitive_element_value ev = - (Annotation.Primitive_element_value) element_value; - ConstantPool.CONSTANT_Float_info info = - (ConstantPool.CONSTANT_Float_info) classFile.constant_pool.get(ev.const_value_index); - testResult.checkEquals(info.value, value, "const_value_index"); + ClassModel classFile, + AnnotationValue element_value) { + testTag(testResult, element_value.tag()); + AnnotationValue.OfFloat ev = + (AnnotationValue.OfFloat) element_value; + testResult.checkEquals(ev.floatValue(), value, "const_value_index"); } @Override @@ -252,15 +238,12 @@ public class TestAnnotationInfo { @Override public void testElementValue(TestResult testResult, - ClassFile classFile, - Annotation.element_value element_value) - throws ConstantPoolException { - testTag(testResult, element_value.tag); - Annotation.Primitive_element_value ev = - (Annotation.Primitive_element_value) element_value; - ConstantPool.CONSTANT_Double_info info = (ConstantPool.CONSTANT_Double_info) - classFile.constant_pool.get(ev.const_value_index); - testResult.checkEquals(info.value, value, "const_value_index"); + ClassModel classFile, + AnnotationValue element_value) { + testTag(testResult, element_value.tag()); + AnnotationValue.OfDouble ev = + (AnnotationValue.OfDouble) element_value; + testResult.checkEquals(ev.doubleValue(), value, "const_value_index"); } @Override @@ -279,15 +262,12 @@ public class TestAnnotationInfo { @Override public void testElementValue(TestResult testResult, - ClassFile classFile, - Annotation.element_value element_value) - throws ConstantPoolException { - testTag(testResult, element_value.tag); - Annotation.Primitive_element_value ev = - (Annotation.Primitive_element_value) element_value; - ConstantPool.CONSTANT_Utf8_info info = - (ConstantPool.CONSTANT_Utf8_info) classFile.constant_pool.get(ev.const_value_index); - testResult.checkEquals(info.value, value, "const_value_index"); + ClassModel classFile, + AnnotationValue element_value) { + testTag(testResult, element_value.tag()); + AnnotationValue.OfString ev = + (AnnotationValue.OfString) element_value; + testResult.checkEquals(ev.stringValue(), value, "const_value_index"); } @Override @@ -309,14 +289,13 @@ public class TestAnnotationInfo { @Override public void testElementValue( TestResult testResult, - ClassFile classFile, - Annotation.element_value element_value) - throws ConstantPoolException { - testTag(testResult, element_value.tag); - Annotation.Enum_element_value ev = (Annotation.Enum_element_value) element_value; - testResult.checkEquals(classFile.constant_pool.getUTF8Info(ev.type_name_index).value, + ClassModel classFile, + AnnotationValue element_value) { + testTag(testResult, element_value.tag()); + AnnotationValue.OfEnum ev = (AnnotationValue.OfEnum) element_value; + testResult.checkEquals(ev.classSymbol().descriptorString(), String.format("L%s;", typeName), "type_name_index"); - testResult.checkEquals(classFile.constant_pool.getUTF8Info(ev.const_name_index).value, + testResult.checkEquals(ev.constantName().stringValue(), constName, "const_name_index"); } @@ -351,16 +330,14 @@ public class TestAnnotationInfo { @Override public void testElementValue( TestResult testResult, - ClassFile classFile, - Annotation.element_value element_value) - throws ConstantPoolException { - testTag(testResult, element_value.tag); - Annotation.Class_element_value ev = (Annotation.Class_element_value) element_value; + ClassModel classFile, + AnnotationValue element_value) { + testTag(testResult, element_value.tag()); + AnnotationValue.OfClass ev = (AnnotationValue.OfClass) element_value; String expectedClassName = className.replace(".class", ""); expectedClassName = mappedClassName.getOrDefault(expectedClassName, String.format("Ljava/lang/%s;", expectedClassName)); - testResult.checkEquals( - classFile.constant_pool.getUTF8Info(ev.class_info_index).value, + testResult.checkEquals(ev.classSymbol().descriptorString(), expectedClassName, "class_info_index : " + expectedClassName); } @@ -381,14 +358,13 @@ public class TestAnnotationInfo { @Override public void testElementValue( TestResult testResult, - ClassFile classFile, - Annotation.element_value element_value) - throws ConstantPoolException { - testTag(testResult, element_value.tag); - Annotation.Array_element_value ev = (Annotation.Array_element_value) element_value; + ClassModel classFile, + AnnotationValue element_value) { + testTag(testResult, element_value.tag()); + AnnotationValue.OfArray ev = (AnnotationValue.OfArray) element_value; for (int i = 0; i < values.size(); ++i) { - values.get(i).testElementValue(testResult, classFile, ev.values[i]); + values.get(i).testElementValue(testResult, classFile, ev.values().get(i)); } } @@ -413,21 +389,20 @@ public class TestAnnotationInfo { @Override public void testElementValue( TestResult testResult, - ClassFile classFile, - Annotation.element_value element_value) - throws ConstantPoolException { - testTag(testResult, element_value.tag); - Annotation ev = ((Annotation.Annotation_element_value) element_value).annotation_value; + ClassModel classFile, + AnnotationValue element_value) { + testTag(testResult, element_value.tag()); + Annotation ev = ((AnnotationValue.OfAnnotation) element_value).annotation(); testResult.checkEquals( - classFile.constant_pool.getUTF8Info(ev.type_index).value, + ev.classSymbol().descriptorString(), String.format("L%s;", annotationName), "type_index"); - for (int i = 0; i < ev.num_element_value_pairs; ++i) { - Annotation.element_value_pair pair = ev.element_value_pairs[i]; + for (int i = 0; i < ev.elements().size(); ++i) { + AnnotationElement pair = ev.elements().get(i); Pair expectedPair = annotation.elementValues.get(i); - expectedPair.elementValue.testElementValue(testResult, classFile, pair.value); + expectedPair.elementValue.testElementValue(testResult, classFile, pair.value()); testResult.checkEquals( - classFile.constant_pool.getUTF8Info(pair.element_name_index).value, + pair.name().stringValue(), expectedPair.elementName, "element_name_index"); } diff --git a/test/langtools/tools/javac/classfiles/attributes/deprecated/DeprecatedPackageTest.java b/test/langtools/tools/javac/classfiles/attributes/deprecated/DeprecatedPackageTest.java index 49def489f2e..b0482fe5a89 100644 --- a/test/langtools/tools/javac/classfiles/attributes/deprecated/DeprecatedPackageTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/deprecated/DeprecatedPackageTest.java @@ -29,14 +29,19 @@ * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * jdk.compiler/com.sun.tools.javac.util - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @run main DeprecatedPackageTest */ -import com.sun.tools.classfile.Attribute; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.Deprecated_attribute; +import jdk.internal.classfile.Attributes; +import jdk.internal.classfile.ClassModel; +import jdk.internal.classfile.attribute.DeprecatedAttribute; public class DeprecatedPackageTest extends TestResult { @@ -78,12 +83,11 @@ public class DeprecatedPackageTest extends TestResult { addTestCase(src); printf("Testing test case: \n%s\n", src); try { - ClassFile cf = readClassFile(compile( + ClassModel cm = readClassFile(compile( new String[]{"package-info.java", package_info}, new String[]{"notDeprecated.java", src}) .getClasses().get(CLASS_NAME)); - Deprecated_attribute attr = - (Deprecated_attribute) cf.getAttribute(Attribute.Deprecated); + DeprecatedAttribute attr = cm.findAttribute(Attributes.DEPRECATED).orElse(null); checkNull(attr, "Class can not have deprecated attribute : " + CLASS_NAME); } catch (Exception e) { addFailure(e); diff --git a/test/langtools/tools/javac/classfiles/attributes/deprecated/DeprecatedTest.java b/test/langtools/tools/javac/classfiles/attributes/deprecated/DeprecatedTest.java index 780bcf70a24..82dc95f9366 100644 --- a/test/langtools/tools/javac/classfiles/attributes/deprecated/DeprecatedTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/deprecated/DeprecatedTest.java @@ -30,19 +30,19 @@ * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * jdk.compiler/com.sun.tools.javac.util - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @run main DeprecatedTest */ -import com.sun.tools.classfile.Attribute; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.ConstantPoolException; -import com.sun.tools.classfile.Deprecated_attribute; -import com.sun.tools.classfile.Field; -import com.sun.tools.classfile.InnerClasses_attribute; -import com.sun.tools.classfile.InnerClasses_attribute.Info; -import com.sun.tools.classfile.Method; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; +import jdk.internal.classfile.impl.BoundAttribute; import javax.tools.JavaFileObject; import java.io.IOException; @@ -244,9 +244,8 @@ public class DeprecatedTest extends TestResult { ? "deprecated" : "notDeprecated"; echo("Testing outer class : " + outerClassName); - ClassFile cf = readClassFile(classes.get(outerClassName)); - Deprecated_attribute attr = (Deprecated_attribute) - cf.getAttribute(Attribute.Deprecated); + ClassModel cf = readClassFile(classes.get(outerClassName)); + DeprecatedAttribute attr = cf.findAttribute(Attributes.DEPRECATED).orElse(null); testAttribute(outerClassName, attr, cf); testInnerClasses(cf, classes); testMethods(cf); @@ -256,18 +255,17 @@ public class DeprecatedTest extends TestResult { } } - private void testInnerClasses(ClassFile cf, Map classes) - throws ConstantPoolException, IOException { - InnerClasses_attribute innerAttr = (InnerClasses_attribute) - cf.getAttribute(Attribute.InnerClasses); - for (Info innerClass : innerAttr.classes) { - String innerClassName = cf.constant_pool. - getClassInfo(innerClass.inner_class_info_index).getName(); + private void testInnerClasses(ClassModel cf, Map classes) + throws IOException { + InnerClassesAttribute innerAttr = cf.findAttribute(Attributes.INNER_CLASSES).orElse(null); + assert innerAttr != null; + for (InnerClassInfo innerClass : innerAttr.classes()) { + String innerClassName = innerClass.innerClass().name().stringValue(); echo("Testing inner class : " + innerClassName); - ClassFile innerCf = readClassFile(classes.get(innerClassName)); - Deprecated_attribute attr = (Deprecated_attribute) - innerCf.getAttribute(Attribute.Deprecated); - String innerClassSimpleName = innerClass.getInnerName(cf.constant_pool); + ClassModel innerCf = readClassFile(classes.get(innerClassName)); + DeprecatedAttribute attr = innerCf.findAttribute(Attributes.DEPRECATED).orElse(null); + assert innerClass.innerName().isPresent(); + String innerClassSimpleName = innerClass.innerName().get().stringValue(); testAttribute(innerClassSimpleName, attr, innerCf); if (innerClassName.contains("Local")) { testMethods(innerCf); @@ -276,29 +274,25 @@ public class DeprecatedTest extends TestResult { } } - private void testMethods(ClassFile cf) - throws ConstantPoolException { - for (Method m : cf.methods) { - String methodName = cf.constant_pool.getUTF8Value(m.name_index); + private void testMethods(ClassModel cf) { + for (MethodModel m : cf.methods()) { + String methodName = m.methodName().stringValue(); echo("Testing method : " + methodName); - Deprecated_attribute attr = (Deprecated_attribute) - m.attributes.get(Attribute.Deprecated); + DeprecatedAttribute attr = m.findAttribute(Attributes.DEPRECATED).orElse(null); testAttribute(methodName, attr, cf); } } - private void testFields(ClassFile cf) throws ConstantPoolException { - for (Field f : cf.fields) { - String fieldName = cf.constant_pool.getUTF8Value(f.name_index); + private void testFields(ClassModel cm) { + for (FieldModel f : cm.fields()) { + String fieldName = f.fieldName().stringValue(); echo("Testing field : " + fieldName); - Deprecated_attribute attr = (Deprecated_attribute) - f.attributes.get(Attribute.Deprecated); - testAttribute(fieldName, attr, cf); + DeprecatedAttribute attr = f.findAttribute(Attributes.DEPRECATED).orElse(null); + testAttribute(fieldName, attr, cm); } } - private void testAttribute(String name, Deprecated_attribute attr, ClassFile cf) - throws ConstantPoolException { + private void testAttribute(String name, DeprecatedAttribute attr, ClassModel cf) { if (name.contains("deprecated")) { testDeprecatedAttribute(name, attr, cf); } else { @@ -306,13 +300,11 @@ public class DeprecatedTest extends TestResult { } } - private void testDeprecatedAttribute(String name, Deprecated_attribute attr, ClassFile cf) - throws ConstantPoolException { + private void testDeprecatedAttribute(String name, DeprecatedAttribute attr, ClassModel cm) { if (checkNotNull(attr, name + " must have deprecated attribute")) { - checkEquals(0, attr.attribute_length, + checkEquals(0, ((BoundAttribute)attr).payloadLen(), "attribute_length should equal to 0"); - checkEquals("Deprecated", - cf.constant_pool.getUTF8Value(attr.attribute_name_index), + checkEquals("Deprecated", attr.attributeName(), name + " attribute_name_index"); } } diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerAnnotationsInInnerAnnotationTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerAnnotationsInInnerAnnotationTest.java index fcd07d482d9..4f6c7da4730 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerAnnotationsInInnerAnnotationTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerAnnotationsInInnerAnnotationTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesInInnerClassTestBase InnerClassesTestBase * @run main InnerAnnotationsInInnerAnnotationTest diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerAnnotationsInInnerClassTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerAnnotationsInInnerClassTest.java index ae54cc88a48..a6787faf860 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerAnnotationsInInnerClassTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerAnnotationsInInnerClassTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesInInnerClassTestBase InnerClassesTestBase * @run main InnerAnnotationsInInnerClassTest diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerAnnotationsInInnerEnumTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerAnnotationsInInnerEnumTest.java index fbbdbdf09b5..4b34834558a 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerAnnotationsInInnerEnumTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerAnnotationsInInnerEnumTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesInInnerClassTestBase InnerClassesTestBase * @run main InnerAnnotationsInInnerEnumTest diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerAnnotationsInInnerInterfaceTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerAnnotationsInInnerInterfaceTest.java index 3bcfe9557b1..baae4e0752a 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerAnnotationsInInnerInterfaceTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerAnnotationsInInnerInterfaceTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesInInnerClassTestBase InnerClassesTestBase * @run main InnerAnnotationsInInnerInterfaceTest diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesHierarchyTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesHierarchyTest.java index 7a70be4befb..6d3055e5fef 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesHierarchyTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesHierarchyTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @run main InnerClassesHierarchyTest */ @@ -40,8 +45,9 @@ import java.lang.annotation.Annotation; import java.util.*; import java.util.stream.Collectors; -import com.sun.tools.classfile.*; -import com.sun.tools.classfile.InnerClasses_attribute.Info; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; +import jdk.internal.classfile.constantpool.*; public class InnerClassesHierarchyTest extends TestResult { @@ -55,13 +61,12 @@ public class InnerClassesHierarchyTest extends TestResult { FilenameFilter filter = (dir, name) -> name.matches(outerClassName + ".*\\.class"); for (File file : Arrays.asList(classDir.listFiles(filter))) { - ClassFile classFile = readClassFile(file); - String className = classFile.getName(); - for (ConstantPool.CPInfo info : classFile.constant_pool.entries()) { - if (info instanceof ConstantPool.CONSTANT_Class_info) { - ConstantPool.CONSTANT_Class_info classInfo = - (ConstantPool.CONSTANT_Class_info) info; - String cpClassName = classInfo.getBaseName(); + ClassModel classFile = readClassFile(file); + String className = classFile.thisClass().name().stringValue(); + for (int i = 1; i < classFile.constantPool().entryCount(); ++i) { + if (classFile.constantPool().entryByIndex(i) instanceof ClassEntry classInfo + && classInfo.asSymbol().isClassOrInterface()) { + String cpClassName = classInfo.asInternalName(); if (isInnerClass(cpClassName)) { get(className).add(cpClassName); } @@ -96,9 +101,8 @@ public class InnerClassesHierarchyTest extends TestResult { if (!currentClassName.startsWith(outerClassName)) { continue; } - ClassFile cf = readClassFile(currentClassName); - InnerClasses_attribute attr = (InnerClasses_attribute) - cf.getAttribute(Attribute.InnerClasses); + ClassModel cf = readClassFile(currentClassName); + InnerClassesAttribute attr = cf.findAttribute(Attributes.INNER_CLASSES).orElse(null); checkNotNull(attr, "Class should not contain " + "inner classes attribute : " + currentClassName); checkTrue(innerClasses.containsKey(currentClassName), @@ -107,12 +111,13 @@ public class InnerClassesHierarchyTest extends TestResult { if (setClasses == null) { continue; } - checkEquals(attr.number_of_classes, + checkEquals(attr.classes().size(), setClasses.size(), "Check number of inner classes : " + setClasses); - for (Info info : attr.classes) { + for (InnerClassInfo info : attr.classes()) { + if (!info.innerClass().asSymbol().isClassOrInterface()) continue; String innerClassName = info - .getInnerClassInfo(cf.constant_pool).getBaseName(); + .innerClass().asInternalName(); checkTrue(setClasses.contains(innerClassName), currentClassName + " contains inner class : " + innerClassName); diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInAnonymousClassTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInAnonymousClassTest.java index dab73c2bd67..970195f32bd 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInAnonymousClassTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInAnonymousClassTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesTestBase * @run main InnerClassesInAnonymousClassTest diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInInnerAnnotationTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInInnerAnnotationTest.java index d2c7eb1d7d8..ab193de085f 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInInnerAnnotationTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInInnerAnnotationTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesInInnerClassTestBase InnerClassesTestBase * @run main InnerClassesInInnerAnnotationTest diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInInnerClassTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInInnerClassTest.java index 4610d98a877..91fed895d07 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInInnerClassTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInInnerClassTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesInInnerClassTestBase InnerClassesTestBase * @run main InnerClassesInInnerClassTest true diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInInnerEnumTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInInnerEnumTest.java index 51ba66006fa..bf0d4c653f6 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInInnerEnumTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInInnerEnumTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesInInnerClassTestBase InnerClassesTestBase * @run main InnerClassesInInnerEnumTest true diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInInnerInterfaceTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInInnerInterfaceTest.java index 5901cf8b87e..651c3f41c02 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInInnerInterfaceTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInInnerInterfaceTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesInInnerClassTestBase InnerClassesTestBase * @run main InnerClassesInInnerInterfaceTest diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInLocalClassTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInLocalClassTest.java index 371eb01c4f6..97ad8387936 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInLocalClassTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesInLocalClassTest.java @@ -26,7 +26,12 @@ * @bug 8042251 * @summary Testing InnerClasses_attribute of inner classes in local class. * @library /tools/lib /tools/javac/lib ../lib - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesIndexTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesIndexTest.java index 16b761d8bf4..241a6422c40 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesIndexTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesIndexTest.java @@ -28,22 +28,27 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @run main InnerClassesIndexTest */ +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; +import jdk.internal.classfile.constantpool.ClassEntry; import java.io.File; import java.io.FilenameFilter; +import java.util.Arrays; import java.util.HashSet; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; -import java.util.stream.Stream; -import com.sun.tools.classfile.Attribute; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.InnerClasses_attribute; -import com.sun.tools.classfile.InnerClasses_attribute.Info; public class InnerClassesIndexTest extends TestResult { @@ -58,7 +63,7 @@ public class InnerClassesIndexTest extends TestResult { private Set getInnerClasses() { FilenameFilter filter = (dir, name) -> name.matches("InnerClassesIndexTest\\$.*\\.class"); - return Stream.of(getClassDir().listFiles(filter)) + return Arrays.stream(Objects.requireNonNull(getClassDir().listFiles(filter))) .map(File::getName) .map(s -> s.replace(".class", "")) .collect(Collectors.toSet()); @@ -67,24 +72,24 @@ public class InnerClassesIndexTest extends TestResult { public void test() throws TestFailedException { try { addTestCase("Source is InnerClassesIndexTest.java"); - ClassFile classFile = readClassFile(InnerClassesIndexTest.class); - InnerClasses_attribute attr = (InnerClasses_attribute) - classFile.getAttribute(Attribute.InnerClasses); + ClassModel classFile = readClassFile(InnerClassesIndexTest.class); + InnerClassesAttribute attr = classFile.findAttribute(Attributes.INNER_CLASSES).orElse(null); Set foundClasses = new HashSet<>(); - for (Info info : attr.classes) { - String innerName = classFile.constant_pool. - getClassInfo(info.inner_class_info_index).getBaseName(); + assert attr != null; + for (InnerClassInfo info : attr.classes()) { + String innerName = info.innerClass().asInternalName(); echo("Testing class : " + innerName); if (isExcluded(innerName)) { echo("Ignored : " + innerName); continue; } foundClasses.add(innerName); - checkEquals(info.outer_class_info_index, 0, + ClassEntry out = info.outerClass().orElse(null); + checkEquals(out == null? 0: out.index(), 0, "outer_class_info_index of " + innerName); if (innerName.matches("\\$\\d+")) { - checkEquals(info.inner_name_index, 0, + checkEquals(Objects.requireNonNull(info.innerName().orElse(null)).index(), 0, "inner_name_index of anonymous class"); } } diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesTest.java index 14e84c3d5b3..db293fddde6 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesTestBase * @run main InnerClassesTest true diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesTestBase.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesTestBase.java index 1faf22f3433..cf36ed9cca5 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesTestBase.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesTestBase.java @@ -21,21 +21,15 @@ * questions. */ -import com.sun.tools.classfile.Attribute; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.InnerClasses_attribute; -import com.sun.tools.classfile.InnerClasses_attribute.Info; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; +import jdk.internal.classfile.constantpool.*; +import jdk.internal.classfile.impl.BoundAttribute; import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; +import java.lang.reflect.AccessFlag; /** * Base class for tests of inner classes attribute. @@ -199,13 +193,12 @@ public abstract class InnerClassesTestBase extends TestResult { printf("Testing :\n%s\n", test.getSource()); try { Map> class2Flags = test.getFlags(); - ClassFile cf = readClassFile(compile(getCompileOptions(), test.getSource()) + ClassModel cm = readClassFile(compile(getCompileOptions(), test.getSource()) .getClasses().get(classToTest)); - InnerClasses_attribute innerClasses = (InnerClasses_attribute) - cf.getAttribute(Attribute.InnerClasses); + InnerClassesAttribute innerClasses = cm.findAttribute(Attributes.INNER_CLASSES).orElse(null); int count = 0; - for (Attribute a : cf.attributes.attrs) { - if (a instanceof InnerClasses_attribute) { + for (Attribute a : cm.attributes()) { + if (a instanceof InnerClassesAttribute) { ++count; } } @@ -213,21 +206,21 @@ public abstract class InnerClassesTestBase extends TestResult { if (!checkNotNull(innerClasses, "InnerClasses attribute should not be null")) { return; } - checkEquals(cf.constant_pool. - getUTF8Info(innerClasses.attribute_name_index).value, "InnerClasses", + checkEquals(innerClasses.attributeName(), "InnerClasses", "innerClasses.attribute_name_index"); // Inner Classes attribute consists of length (2 bytes) // and 8 bytes for each inner class's entry. - checkEquals(innerClasses.attribute_length, + checkEquals(((BoundAttribute)innerClasses).payloadLen(), 2 + 8 * class2Flags.size(), "innerClasses.attribute_length"); - checkEquals(innerClasses.number_of_classes, + checkEquals(innerClasses.classes().size(), class2Flags.size(), "innerClasses.number_of_classes"); Set visitedClasses = new HashSet<>(); - for (Info e : innerClasses.classes) { - String baseName = cf.constant_pool.getClassInfo( - e.inner_class_info_index).getBaseName(); - if (cf.major_version >= 51 && e.inner_name_index == 0) { - checkEquals(e.outer_class_info_index, 0, + for (InnerClassInfo e : innerClasses.classes()) { + String baseName = e.innerClass().asInternalName(); + if (cm.majorVersion() >= 51 && e.innerClass().index() == 0) { + ClassEntry out = e.outerClass().orElse(null); + // The outer_class_info_index of sun.tools.classfile will return 0 if it is not a member of a class or interface. + checkEquals(out == null? 0: out.index(), 0, "outer_class_info_index " + "in case of inner_name_index is zero : " + baseName); @@ -237,17 +230,21 @@ public abstract class InnerClassesTestBase extends TestResult { className); checkTrue(visitedClasses.add(className), "there are no duplicates in attribute : " + className); - checkEquals(e.inner_class_access_flags.getInnerClassFlags(), - class2Flags.get(className), + //Convert the Set to Set + Set accFlags = class2Flags.get(className).stream() + .map(str -> AccessFlag.valueOf(str.substring(str.indexOf("_") + 1))) + .collect(Collectors.toSet()); + checkEquals(e.flags(), + accFlags, "inner_class_access_flags " + className); if (!Arrays.asList(skipClasses).contains(className)) { + checkEquals( + e.innerClass().asInternalName(), + classToTest + "$" + className, + "inner_class_info_index of " + className); + if (e.outerClass().orElse(null) != null && e.outerClass().get().index() > 0) { checkEquals( - cf.constant_pool.getClassInfo(e.inner_class_info_index).getBaseName(), - classToTest + "$" + className, - "inner_class_info_index of " + className); - if (e.outer_class_info_index > 0) { - checkEquals( - cf.constant_pool.getClassInfo(e.outer_class_info_index).getName(), + e.outerClass().get().name().stringValue(), classToTest, "outer_class_info_index of " + className); } diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerEnumInInnerAnnotationTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerEnumInInnerAnnotationTest.java index b7358a5c1c1..dbafcbfa45c 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerEnumInInnerAnnotationTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerEnumInInnerAnnotationTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesInInnerClassTestBase InnerClassesTestBase * @run main InnerEnumInInnerAnnotationTest diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerEnumInInnerEnumTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerEnumInInnerEnumTest.java index 9010e2ef386..f37c21dc85f 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerEnumInInnerEnumTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerEnumInInnerEnumTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesInInnerClassTestBase InnerClassesTestBase * @run main InnerEnumInInnerEnumTest diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerEnumInInnerInterfaceTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerEnumInInnerInterfaceTest.java index ff878f0a2ba..7286dc61b7c 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerEnumInInnerInterfaceTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerEnumInInnerInterfaceTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesInInnerClassTestBase InnerClassesTestBase * @run main InnerEnumInInnerInterfaceTest diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerEnumsInInnerClassTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerEnumsInInnerClassTest.java index fb5ee1dbe5b..d6bebd285ed 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerEnumsInInnerClassTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerEnumsInInnerClassTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesInInnerClassTestBase InnerClassesTestBase * @run main InnerEnumsInInnerClassTest diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerInterfacesInInnerAnnotationTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerInterfacesInInnerAnnotationTest.java index 5c2407353eb..b9a65195802 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerInterfacesInInnerAnnotationTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerInterfacesInInnerAnnotationTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesInInnerClassTestBase InnerClassesTestBase * @run main InnerInterfacesInInnerAnnotationTest diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerInterfacesInInnerClassTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerInterfacesInInnerClassTest.java index 2a987e02c34..cb14d1c3f21 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerInterfacesInInnerClassTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerInterfacesInInnerClassTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesInInnerClassTestBase InnerClassesTestBase * @run main InnerInterfacesInInnerClassTest diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerInterfacesInInnerEnumTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerInterfacesInInnerEnumTest.java index 461a7b068a9..a84240c00f6 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerInterfacesInInnerEnumTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerInterfacesInInnerEnumTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesInInnerClassTestBase InnerClassesTestBase * @run main InnerInterfacesInInnerEnumTest diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerInterfacesInInnerInterfaceTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerInterfacesInInnerInterfaceTest.java index 25a710f9d77..b7b5ceb0fe0 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerInterfacesInInnerInterfaceTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerInterfacesInInnerInterfaceTest.java @@ -28,7 +28,12 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase * @build InnerClassesInInnerClassTestBase InnerClassesTestBase * @run main InnerInterfacesInInnerInterfaceTest diff --git a/test/langtools/tools/javac/classfiles/attributes/innerclasses/NoInnerClassesTest.java b/test/langtools/tools/javac/classfiles/attributes/innerclasses/NoInnerClassesTest.java index 48adfce3ad6..576b3872430 100644 --- a/test/langtools/tools/javac/classfiles/attributes/innerclasses/NoInnerClassesTest.java +++ b/test/langtools/tools/javac/classfiles/attributes/innerclasses/NoInnerClassesTest.java @@ -28,25 +28,29 @@ * @library /tools/lib /tools/javac/lib ../lib * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main - * jdk.jdeps/com.sun.tools.classfile + * java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * @build toolbox.ToolBox InMemoryFileManager TestBase * @run main NoInnerClassesTest */ -import com.sun.tools.classfile.Attribute; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.ConstantPoolException; +import jdk.internal.classfile.Attributes; +import jdk.internal.classfile.ClassModel; import java.io.IOException; public class NoInnerClassesTest extends TestBase { - public static void main(String[] args) throws IOException, ConstantPoolException { + public static void main(String[] args) throws IOException { new NoInnerClassesTest().test(); } - public void test() throws IOException, ConstantPoolException { - ClassFile classFile = readClassFile("NoInnerClassesTest"); - assertNull(classFile.getAttribute(Attribute.InnerClasses), "Found inner class attribute"); + public void test() throws IOException { + ClassModel classModel = readClassFile("NoInnerClassesTest"); + assertNull(classModel.findAttribute(Attributes.INNER_CLASSES).orElse(null), "Found inner class attribute"); } } diff --git a/test/langtools/tools/javac/classfiles/attributes/lib/TestBase.java b/test/langtools/tools/javac/classfiles/attributes/lib/TestBase.java index 08de973cc3f..15364cafeb8 100644 --- a/test/langtools/tools/javac/classfiles/attributes/lib/TestBase.java +++ b/test/langtools/tools/javac/classfiles/attributes/lib/TestBase.java @@ -33,8 +33,7 @@ import javax.tools.JavaCompiler; import javax.tools.JavaFileObject; import javax.tools.ToolProvider; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.ConstantPoolException; +import jdk.internal.classfile.*; import toolbox.JavacTask; import toolbox.ToolBox; @@ -127,10 +126,9 @@ public class TestBase { * @param is an input stream * @return class file that is read from {@code is} * @throws IOException if I/O error occurs - * @throws ConstantPoolException if constant pool error occurs */ - public ClassFile readClassFile(InputStream is) throws IOException, ConstantPoolException { - return ClassFile.read(is); + public ClassModel readClassFile(InputStream is) throws IOException { + return Classfile.of().parse(is.readAllBytes()); } /** @@ -139,9 +137,8 @@ public class TestBase { * @param fileObject a file object * @return class file that is read from {@code fileObject} * @throws IOException if I/O error occurs - * @throws ConstantPoolException if constant pool error occurs */ - public ClassFile readClassFile(JavaFileObject fileObject) throws IOException, ConstantPoolException { + public ClassModel readClassFile(JavaFileObject fileObject) throws IOException { try (InputStream is = fileObject.openInputStream()) { return readClassFile(is); } @@ -153,9 +150,8 @@ public class TestBase { * @param clazz a class * @return class file that is read from {@code clazz} * @throws IOException if I/O error occurs - * @throws ConstantPoolException if constant pool error occurs */ - public ClassFile readClassFile(Class clazz) throws IOException, ConstantPoolException { + public ClassModel readClassFile(Class clazz) throws IOException { return readClassFile(getClassFile(clazz)); } @@ -165,9 +161,8 @@ public class TestBase { * @param className a class name * @return class file that is read from {@code className} * @throws IOException if I/O error occurs - * @throws ConstantPoolException if constant pool error occurs */ - public ClassFile readClassFile(String className) throws IOException, ConstantPoolException { + public ClassModel readClassFile(String className) throws IOException { return readClassFile(getClassFile(className + ".class")); } @@ -177,9 +172,8 @@ public class TestBase { * @param file a file * @return class file that is read from {@code file} * @throws IOException if I/O error occurs - * @throws ConstantPoolException if constant pool error occurs */ - public ClassFile readClassFile(File file) throws IOException, ConstantPoolException { + public ClassModel readClassFile(File file) throws IOException { try (InputStream is = new FileInputStream(file)) { return readClassFile(is); } diff --git a/test/langtools/tools/javac/classreader/BadClass.java b/test/langtools/tools/javac/classreader/BadClass.java index 3709fa0ac28..2fc1e8503a4 100644 --- a/test/langtools/tools/javac/classreader/BadClass.java +++ b/test/langtools/tools/javac/classreader/BadClass.java @@ -25,7 +25,6 @@ * @test * @bug 6898851 * @summary Compiling against this corrupt class file causes a stacktrace from javac - * @modules jdk.jdeps/com.sun.tools.classfile */ import java.io.File; @@ -33,9 +32,8 @@ import java.io.FileWriter; import java.io.StringWriter; import java.io.PrintWriter; import java.io.IOException; +import java.nio.file.Files; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.ClassWriter; import com.sun.tools.javac.Main; public class BadClass { @@ -62,10 +60,9 @@ public class BadClass { makeClass("d1", "Empty.java", "abstract class Empty implements Readable {}"); // Step 2. Modify classfile to have invalid constant pool index - ClassFile cf = ClassFile.read(new File("d1","Empty.class")); - cf.interfaces[0] = cf.constant_pool.size() + 10; - ClassWriter cw = new ClassWriter(); - cw.write(cf, new File("d2","Empty.class")); + var cf = Files.readAllBytes(new File("d1","Empty.class").toPath()); + cf[9]--; + Files.write(new File("d2","Empty.class").toPath(), cf); // Step 3. Compile use of invalid class String result = makeClass("d2", "EmptyUse.java", "class EmptyUse { Empty e; }"); diff --git a/test/langtools/tools/javac/constDebug/ConstDebugTest.java b/test/langtools/tools/javac/constDebug/ConstDebugTest.java index a846fb5316e..4920d305b0f 100644 --- a/test/langtools/tools/javac/constDebug/ConstDebugTest.java +++ b/test/langtools/tools/javac/constDebug/ConstDebugTest.java @@ -25,23 +25,26 @@ * @test * @bug 4645152 4785453 * @summary javac compiler incorrectly inserts when -g is specified - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components * @run compile -g ConstDebugTest.java * @run main ConstDebugTest */ import java.nio.file.Paths; -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.Method; +import jdk.internal.classfile.*; public class ConstDebugTest { public static final long l = 12; public static void main(String args[]) throws Exception { - ClassFile classFile = ClassFile.read(Paths.get(System.getProperty("test.classes"), + ClassModel classModel = Classfile.of().parse(Paths.get(System.getProperty("test.classes"), ConstDebugTest.class.getSimpleName() + ".class")); - for (Method method: classFile.methods) { - if (method.getName(classFile.constant_pool).equals("")) { + for (MethodModel method: classModel.methods()) { + if (method.methodName().equalsString("")) { throw new AssertionError( "javac should not create a method for ConstDebugTest class"); } diff --git a/test/langtools/tools/javac/lambda/LambdaTestStrictFPFlag.java b/test/langtools/tools/javac/lambda/LambdaTestStrictFPFlag.java index 30ec06afd18..7ec06d21f77 100644 --- a/test/langtools/tools/javac/lambda/LambdaTestStrictFPFlag.java +++ b/test/langtools/tools/javac/lambda/LambdaTestStrictFPFlag.java @@ -25,15 +25,18 @@ * @test * @bug 8046060 * @summary Different results of floating point multiplication for lambda code block - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components * @compile -source 16 -target 16 LambdaTestStrictFPFlag.java * @run main LambdaTestStrictFPFlag */ import java.io.*; import java.net.URL; -import com.sun.tools.classfile.*; -import static com.sun.tools.classfile.AccessFlags.ACC_STRICT; +import jdk.internal.classfile.*; public class LambdaTestStrictFPFlag { public static void main(String[] args) throws Exception { @@ -41,12 +44,11 @@ public class LambdaTestStrictFPFlag { } void run() throws Exception { - ClassFile cf = getClassFile("LambdaTestStrictFPFlag$Test.class"); - ConstantPool cp = cf.constant_pool; + ClassModel cm = getClassFile("LambdaTestStrictFPFlag$Test.class"); boolean found = false; - for (Method meth: cf.methods) { - if (meth.getName(cp).startsWith("lambda$")) { - if ((meth.access_flags.flags & ACC_STRICT) == 0) { + for (MethodModel meth: cm.methods()) { + if (meth.methodName().stringValue().startsWith("lambda$")) { + if ((meth.flags().flagsMask() & Classfile.ACC_STRICT) == 0){ throw new Exception("strict flag missing from lambda"); } found = true; @@ -57,13 +59,11 @@ public class LambdaTestStrictFPFlag { } } - ClassFile getClassFile(String name) throws IOException, ConstantPoolException { + ClassModel getClassFile(String name) throws IOException { URL url = getClass().getResource(name); - InputStream in = url.openStream(); - try { - return ClassFile.read(in); - } finally { - in.close(); + assert url != null; + try (InputStream in = url.openStream()) { + return Classfile.of().parse(in.readAllBytes()); } } diff --git a/test/langtools/tools/javac/linenumbers/ConditionalLineNumberTest.java b/test/langtools/tools/javac/linenumbers/ConditionalLineNumberTest.java index d6a56dde570..a8d4c49eecd 100644 --- a/test/langtools/tools/javac/linenumbers/ConditionalLineNumberTest.java +++ b/test/langtools/tools/javac/linenumbers/ConditionalLineNumberTest.java @@ -25,43 +25,44 @@ * @test * @bug 8034091 * @summary Add LineNumberTable attributes for conditional operator (?:) split across several lines. - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components */ -import com.sun.tools.classfile.ClassFile; -import com.sun.tools.classfile.ConstantPoolException; -import com.sun.tools.classfile.Method; -import com.sun.tools.classfile.Attribute; -import com.sun.tools.classfile.Code_attribute; -import com.sun.tools.classfile.LineNumberTable_attribute; -import com.sun.tools.classfile.LineNumberTable_attribute.Entry; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.*; import java.io.File; import java.io.IOException; +import java.util.List; public class ConditionalLineNumberTest { public static void main(String[] args) throws Exception { // check that we have 5 consecutive entries for method() - Entry[] lines = findEntries(); - if (lines == null || lines.length != 5) + List lines = findEntries(); + if (lines == null || lines.size() != 5) throw new Exception("conditional line number table incorrect"); - int current = lines[0].line_number; - for (Entry e : lines) { - if (e.line_number != current) + int current = lines.get(0).lineNumber(); + for (LineNumberInfo e : lines) { + if (e.lineNumber() != current) throw new Exception("conditional line number table incorrect"); current++; } - } + } - static Entry[] findEntries() throws IOException, ConstantPoolException { - ClassFile self = ClassFile.read(ConditionalLineNumberTest.class.getResourceAsStream("ConditionalLineNumberTest.class")); - for (Method m : self.methods) { - if ("method".equals(m.getName(self.constant_pool))) { - Code_attribute code_attribute = (Code_attribute)m.attributes.get(Attribute.Code); - for (Attribute at : code_attribute.attributes) { - if (Attribute.LineNumberTable.equals(at.getName(self.constant_pool))) { - return ((LineNumberTable_attribute)at).line_number_table; + static List findEntries() throws IOException { + ClassModel self = Classfile.of().parse(ConditionalLineNumberTest.class.getResourceAsStream("ConditionalLineNumberTest.class").readAllBytes()); + for (MethodModel m : self.methods()) { + if (m.methodName().equalsString("method")) { + CodeAttribute code_attribute = m.findAttribute(Attributes.CODE).orElse(null); + assert code_attribute != null; + for (Attribute at : code_attribute.attributes()) { + if (at instanceof LineNumberTableAttribute) { + return ((LineNumberTableAttribute)at).lineNumbers(); } } } diff --git a/test/langtools/tools/javac/resolve/NoObjectToString.java b/test/langtools/tools/javac/resolve/NoObjectToString.java index c7275a8cd5e..bac11f80448 100644 --- a/test/langtools/tools/javac/resolve/NoObjectToString.java +++ b/test/langtools/tools/javac/resolve/NoObjectToString.java @@ -25,14 +25,18 @@ * @test * @bug 8272564 * @summary Correct resolution of toString() (and other similar calls) on interfaces - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components * @compile NoObjectToString.java * @run main NoObjectToString */ import java.io.*; -import com.sun.tools.classfile.*; -import com.sun.tools.classfile.ConstantPool.CONSTANT_Methodref_info; +import jdk.internal.classfile.*; +import jdk.internal.classfile.constantpool.*; public class NoObjectToString { public static void main(String... args) throws Exception { @@ -42,23 +46,19 @@ public class NoObjectToString { void run(String... args) throws Exception { //Verify there are no references to Object.toString() in a Test: - InputStream in = NoObjectToString.class.getResourceAsStream("NoObjectToString$Test.class"); - try { - ClassFile cf = ClassFile.read(in); - for (ConstantPool.CPInfo cpinfo: cf.constant_pool.entries()) { - if (cpinfo.getTag() == ConstantPool.CONSTANT_Methodref) { - CONSTANT_Methodref_info ref = (CONSTANT_Methodref_info) cpinfo; - String methodDesc = ref.getClassInfo().getName() + "." + ref.getNameAndTypeInfo().getName() + ":" + ref.getNameAndTypeInfo().getType(); + try (InputStream in = NoObjectToString.class.getResourceAsStream("NoObjectToString$Test.class")) { + assert in != null; + ClassModel cm = Classfile.of().parse(in.readAllBytes()); + for (int i = 1; i < cm.constantPool().entryCount(); ++i) { + PoolEntry pe = cm.constantPool().entryByIndex(i); + if (pe instanceof MethodRefEntry ref) { + String methodDesc = ref.owner().name() + "." + ref.nameAndType().name() + ":" + ref.nameAndType().type(); if ("java/lang/Object.toString:()Ljava/lang/String;".equals(methodDesc)) { throw new AssertionError("Found call to j.l.Object.toString"); } } } - } catch (ConstantPoolException ignore) { - throw new AssertionError(ignore); - } finally { - in.close(); } } diff --git a/test/langtools/tools/javac/sealed/CheckSubtypesOfSealedTest.java b/test/langtools/tools/javac/sealed/CheckSubtypesOfSealedTest.java index 21bd2279d3f..c5dd8defe99 100644 --- a/test/langtools/tools/javac/sealed/CheckSubtypesOfSealedTest.java +++ b/test/langtools/tools/javac/sealed/CheckSubtypesOfSealedTest.java @@ -25,7 +25,12 @@ * @test * @summary check subtypes of sealed classes * @library /tools/lib /tools/javac/lib /tools/javac/classfiles/attributes/lib - * @modules jdk.jdeps/com.sun.tools.classfile + * @modules java.base/jdk.internal.classfile + * java.base/jdk.internal.classfile.attribute + * java.base/jdk.internal.classfile.constantpool + * java.base/jdk.internal.classfile.instruction + * java.base/jdk.internal.classfile.components + * java.base/jdk.internal.classfile.impl * jdk.compiler/com.sun.tools.javac.code * jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.main @@ -35,10 +40,10 @@ */ import java.util.List; -import com.sun.tools.classfile.*; -import com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8_info; import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.util.Assert; +import jdk.internal.classfile.*; +import jdk.internal.classfile.attribute.PermittedSubclassesAttribute; public class CheckSubtypesOfSealedTest extends TestBase { @@ -72,46 +77,46 @@ public class CheckSubtypesOfSealedTest extends TestBase { enum CheckFor { SEALED { - void check(ClassFile classFile) throws Exception { + void check(ClassModel classFile) throws Exception { boolean found = false; - for (Attribute attr: classFile.attributes) { - if (attr.getName(classFile.constant_pool).equals("PermittedSubclasses")) { - PermittedSubclasses_attribute permittedSubclasses = (PermittedSubclasses_attribute)attr; + for (Attribute attr: classFile.attributes()) { + if (attr.attributeName().equals("PermittedSubclasses")) { + PermittedSubclassesAttribute permittedSubclasses = (PermittedSubclassesAttribute)attr; found = true; - if (permittedSubclasses.subtypes == null || permittedSubclasses.subtypes.length == 0) { - throw new AssertionError(classFile.getName() + " should be sealed"); + if (permittedSubclasses.permittedSubclasses().isEmpty()) { + throw new AssertionError(classFile.thisClass().name() + " should be sealed"); } } } if (!found) { - throw new AssertionError(classFile.getName() + " should be sealed"); + throw new AssertionError(classFile.thisClass().name() + " should be sealed"); } } }, FINAL { - void check(ClassFile classFile) throws Exception { - if ((classFile.access_flags.flags & Flags.FINAL) == 0) { - throw new AssertionError(classFile.getName() + " should be final"); + void check(ClassModel classFile) throws Exception { + if ((classFile.flags().flagsMask() & Flags.FINAL) == 0) { + throw new AssertionError(classFile.thisClass().name() + " should be final"); } } }, NOT_SEALED { - void check(ClassFile classFile) throws Exception { - for (Attribute attr: classFile.attributes) { - if (attr.getName(classFile.constant_pool).equals("PermittedSubclasses")) { - throw new AssertionError(classFile.getName() + " should not be sealed"); + void check(ClassModel classFile) throws Exception { + for (Attribute attr: classFile.attributes()) { + if (attr.attributeName().equals("PermittedSubclasses")) { + throw new AssertionError(classFile.thisClass().name() + " should not be sealed"); } } - if ((classFile.access_flags.flags & Flags.FINAL) != 0) { - throw new AssertionError(classFile.getName() + " should not be final"); + if ((classFile.flags().flagsMask() & Flags.FINAL) != 0) { + throw new AssertionError(classFile.thisClass().name() + " should not be final"); } } }; - abstract void check(ClassFile classFile) throws Exception; + abstract void check(ClassModel classFile) throws Exception; } - void checkClassFile(final ClassFile classFile, CheckFor... checkFor) throws Exception { + void checkClassFile(final ClassModel classFile, CheckFor... checkFor) throws Exception { for (CheckFor whatToCheckFor : checkFor) { whatToCheckFor.check(classFile); }