Skip to content

Commit 97b6883

Browse files
xqoasisasotona
authored andcommitted
8295059: test/langtools/tools/javap 12 test classes use com.sun.tools.classfile library
Reviewed-by: asotona
1 parent 3671d83 commit 97b6883

File tree

11 files changed

+523
-591
lines changed

11 files changed

+523
-591
lines changed

test/langtools/tools/javap/T6716452.java

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@
2424
/*
2525
* @test 6716452
2626
* @summary need a method to get an index of an attribute
27-
* @modules jdk.jdeps/com.sun.tools.classfile
27+
* @modules java.base/jdk.internal.classfile
28+
* java.base/jdk.internal.classfile.attribute
29+
* java.base/jdk.internal.classfile.constantpool
30+
* java.base/jdk.internal.classfile.instruction
31+
* java.base/jdk.internal.classfile.components
2832
*/
2933

3034
import java.io.*;
31-
import com.sun.tools.classfile.*;
35+
import java.nio.file.Files;
36+
37+
import jdk.internal.classfile.*;
38+
import jdk.internal.classfile.attribute.*;
3239

3340
public class T6716452 {
3441
public static void main(String[] args) throws Exception {
@@ -38,49 +45,44 @@ public static void main(String[] args) throws Exception {
3845
public void run() throws Exception {
3946
File javaFile = writeTestFile();
4047
File classFile = compileTestFile(javaFile);
41-
42-
ClassFile cf = ClassFile.read(classFile);
43-
for (Method m: cf.methods) {
44-
test(cf, m);
48+
ClassModel cm = Classfile.of().parse(classFile.toPath());
49+
for (MethodModel mm: cm.methods()) {
50+
test(mm);
4551
}
4652

4753
if (errors > 0)
4854
throw new Exception(errors + " errors found");
4955
}
5056

51-
void test(ClassFile cf, Method m) {
52-
test(cf, m, Attribute.Code, Code_attribute.class);
53-
test(cf, m, Attribute.Exceptions, Exceptions_attribute.class);
57+
void test(MethodModel mm) {
58+
test(mm, Attributes.CODE, CodeAttribute.class);
59+
test(mm, Attributes.EXCEPTIONS, ExceptionsAttribute.class);
5460
}
5561

56-
// test the result of Attributes.getIndex according to expectations
62+
// test the result of MethodModel.findAttribute, MethodModel.attributes().indexOf() according to expectations
5763
// encoded in the method's name
58-
void test(ClassFile cf, Method m, String name, Class<?> c) {
59-
int index = m.attributes.getIndex(cf.constant_pool, name);
60-
try {
61-
String m_name = m.getName(cf.constant_pool);
62-
System.err.println("Method " + m_name + " name:" + name + " index:" + index + " class: " + c);
63-
boolean expect = (m_name.equals("<init>") && name.equals("Code"))
64-
|| (m_name.indexOf(name) != -1);
65-
boolean found = (index != -1);
66-
if (expect) {
67-
if (found) {
68-
Attribute attr = m.attributes.get(index);
69-
if (!c.isAssignableFrom(attr.getClass())) {
70-
error(m + ": unexpected attribute found,"
71-
+ " expected " + c.getName()
72-
+ " found " + attr.getClass().getName());
73-
}
74-
} else {
75-
error(m + ": expected attribute " + name + " not found");
64+
<T extends Attribute<T>> void test(MethodModel mm, AttributeMapper<T> attr, Class<?> c) {
65+
Attribute<T> attr_instance = mm.findAttribute(attr).orElse(null);
66+
int index = mm.attributes().indexOf(attr_instance);
67+
String mm_name = mm.methodName().stringValue();
68+
System.err.println("Method " + mm_name + " name:" + attr.name() + " index:" + index + " class: " + c);
69+
boolean expect = (mm_name.equals("<init>") && attr.name().equals("Code"))
70+
|| (mm_name.contains(attr.name()));
71+
boolean found = (index != -1);
72+
if (expect) {
73+
if (found) {
74+
if (!c.isAssignableFrom(mm.attributes().get(index).getClass())) {
75+
error(mm + ": unexpected attribute found,"
76+
+ " expected " + c.getName()
77+
+ " found " + mm.attributes().get(index).attributeName());
7678
}
7779
} else {
78-
if (found) {
79-
error(m + ": unexpected attribute " + name);
80-
}
80+
error(mm + ": expected attribute " + attr.name() + " not found");
81+
}
82+
} else {
83+
if (found) {
84+
error(mm + ": unexpected attribute " + attr.name());
8185
}
82-
} catch (ConstantPoolException e) {
83-
error(m + ": " + e);
8486
}
8587
}
8688

test/langtools/tools/javap/TestClassNameWarning.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@
2929
* @modules
3030
* jdk.compiler/com.sun.tools.javac.api
3131
* jdk.compiler/com.sun.tools.javac.main
32-
* jdk.jdeps/com.sun.tools.classfile
3332
* jdk.jdeps/com.sun.tools.javap
33+
* java.base/jdk.internal.classfile
34+
* java.base/jdk.internal.classfile.attribute
35+
* java.base/jdk.internal.classfile.constantpool
36+
* java.base/jdk.internal.classfile.instruction
37+
* java.base/jdk.internal.classfile.components
3438
* @build toolbox.JavacTask toolbox.JavapTask toolbox.ToolBox toolbox.TestRunner
3539
* @run main TestClassNameWarning
3640
*/
3741

42+
import java.lang.constant.ClassDesc;
3843
import java.nio.file.Files;
3944
import java.nio.file.Path;
4045
import java.nio.file.Paths;
@@ -43,9 +48,8 @@
4348
import java.util.regex.Pattern;
4449
import java.util.stream.Collectors;
4550

46-
import com.sun.tools.classfile.ClassFile;
47-
import com.sun.tools.classfile.ClassWriter;
4851

52+
import jdk.internal.classfile.*;
4953
import toolbox.JavacTask;
5054
import toolbox.JavapTask;
5155
import toolbox.Task;
@@ -140,7 +144,7 @@ public void testLegacyModuleInfo(Path base) throws Exception {
140144
byte[] replaceBytes = "module-info".getBytes("UTF-8");
141145
for (int i = 0; i < bytes.length - searchBytes.length; i++) {
142146
if (Arrays.equals(bytes, i, i + searchBytes.length,
143-
searchBytes, 0, searchBytes.length)) {
147+
searchBytes, 0, searchBytes.length)) {
144148
System.arraycopy(replaceBytes, 0, bytes, i, replaceBytes.length);
145149
}
146150
}
@@ -172,14 +176,15 @@ public void testNoNameClass(Path base) throws Exception {
172176
.files(tb.findJavaFiles(src))
173177
.run()
174178
.writeAll();
175-
176-
ClassFile cf = ClassFile.read(classes.resolve("A.class"));
177-
ClassFile cf2 = new ClassFile(
178-
cf.magic, cf.minor_version, cf.major_version, cf.constant_pool,
179-
cf.access_flags,
180-
0, // this_class,
181-
cf.super_class, cf.interfaces, cf.fields, cf.methods, cf.attributes);
182-
new ClassWriter().write(cf2, Files.newOutputStream(classes.resolve("Z.class")));
179+
ClassModel cm = Classfile.of().parse(classes.resolve("A.class"));
180+
Classfile.of().buildTo(
181+
classes.resolve("Z.class"),
182+
ClassDesc.of("0"), cb -> {
183+
for (ClassElement ce : cm) {
184+
cb.with(ce);
185+
}
186+
}
187+
);
183188

184189
List<String> log = new JavapTask(tb)
185190
.classpath(classes.toString())
@@ -238,4 +243,3 @@ private void checkOutput(List<String> log, boolean expect, String regex) {
238243
}
239244
}
240245
}
241-

0 commit comments

Comments
 (0)