Skip to content

Commit

Permalink
8313422: test/langtools/tools/javac 144 test classes uses com.sun.too…
Browse files Browse the repository at this point in the history
…ls.classfile library

Reviewed-by: asotona
  • Loading branch information
xqoasis authored and asotona committed Sep 7, 2023
1 parent 8557205 commit 8f7e29b
Show file tree
Hide file tree
Showing 144 changed files with 2,738 additions and 2,757 deletions.
19 changes: 12 additions & 7 deletions test/langtools/tools/javac/4241573/T4241573.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@
* @test
* @bug 4241573
* @summary SourceFile attribute includes full path
* @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.Attribute;
import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.SourceFile_attribute;
import jdk.internal.classfile.*;
import jdk.internal.classfile.Attributes;
import jdk.internal.classfile.attribute.*;
import java.io.*;
import java.util.*;
import java.util.jar.*;
Expand Down Expand Up @@ -107,9 +112,9 @@ else if (expect.containsAll(found))
void verifySourceFileAttribute(File f) {
System.err.println("verify: " + f);
try {
ClassFile cf = ClassFile.read(f);
SourceFile_attribute sfa = (SourceFile_attribute) cf.getAttribute(Attribute.SourceFile);
String found = sfa.getSourceFile(cf.constant_pool);
ClassModel cf = Classfile.of().parse(f.toPath());
SourceFileAttribute sfa = cf.findAttribute(Attributes.SOURCE_FILE).orElseThrow();
String found = sfa.sourceFile().stringValue();
String expect = f.getName().replaceAll("([$.].*)?\\.class", ".java");
if (!expect.equals(found)) {
error("bad value found: " + found + ", expected: " + expect);
Expand Down
23 changes: 13 additions & 10 deletions test/langtools/tools/javac/7003595/T7003595.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@
* @test
* @bug 7003595
* @summary IncompatibleClassChangeError with unreferenced local class with subclass
* @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.file
*/

import com.sun.source.util.JavacTask;
import com.sun.tools.classfile.Attribute;
import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.InnerClasses_attribute;
import com.sun.tools.classfile.ConstantPool.*;
import jdk.internal.classfile.*;
import jdk.internal.classfile.attribute.*;
import com.sun.tools.javac.api.JavacTool;

import java.io.File;
Expand Down Expand Up @@ -159,17 +162,17 @@ void verifyBytecode(JavaSource source) {
String filename = cks[i].getClassfileName(cnames, cks, i);
File compiledTest = new File(filename + ".class");
try {
ClassFile cf = ClassFile.read(compiledTest);
ClassModel cf = Classfile.of().parse(compiledTest.toPath());
if (cf == null) {
throw new Error("Classfile not found: " + filename);
}

InnerClasses_attribute innerClasses = (InnerClasses_attribute)cf.getAttribute(Attribute.InnerClasses);
InnerClassesAttribute innerClasses = cf.findAttribute(Attributes.INNER_CLASSES).orElse(null);

ArrayList<String> foundInnerSig = new ArrayList<>();
if (innerClasses != null) {
for (InnerClasses_attribute.Info info : innerClasses.classes) {
String foundSig = info.getInnerClassInfo(cf.constant_pool).getName();
for (InnerClassInfo info : innerClasses.classes()) {
String foundSig = info.innerClass().asInternalName();
foundInnerSig.add(foundSig);
}
}
Expand All @@ -187,7 +190,7 @@ void verifyBytecode(JavaSource source) {
if (expectedInnerSig.size() != foundInnerSig.size()) {
throw new Error("InnerClasses attribute for " + cnames[i] + " has wrong size\n" +
"expected " + expectedInnerSig.size() + "\n" +
"found " + innerClasses.number_of_classes + "\n" +
"found " + (innerClasses == null? 0: innerClasses.classes().size()) + "\n" +
source);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@
* @test
* @bug 7153958 8073372
* @summary add constant pool reference to class containing inlined constants
* @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 pkg/ClassToBeStaticallyImportedA.java pkg/ClassToBeStaticallyImportedB.java CPoolRefClassContainingInlinedCts.java
* @run main CPoolRefClassContainingInlinedCts
*/

import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info;
import com.sun.tools.classfile.ConstantPool.CPInfo;
import com.sun.tools.classfile.ConstantPoolException;
import jdk.internal.classfile.*;
import jdk.internal.classfile.constantpool.ClassEntry;
import jdk.internal.classfile.constantpool.PoolEntry;
import java.io.File;
import java.io.IOException;

Expand Down Expand Up @@ -66,19 +70,19 @@ void checkClassName(String className) {
}
}

void checkReferences() throws IOException, ConstantPoolException {
void checkReferences() throws IOException {
File testClasses = new File(System.getProperty("test.classes"));
File file = new File(testClasses,
CPoolRefClassContainingInlinedCts.class.getName() + ".class");
ClassFile classFile = ClassFile.read(file);
ClassModel classFile = Classfile.of().parse(file.toPath());
int i = 1;
CPInfo cpInfo;
while (i < classFile.constant_pool.size()) {
cpInfo = classFile.constant_pool.get(i);
if (cpInfo instanceof CONSTANT_Class_info) {
checkClassName(((CONSTANT_Class_info)cpInfo).getName());
PoolEntry cpInfo;
while (i < classFile.constantPool().entryCount()) {
cpInfo = classFile.constantPool().entryByIndex(i);
if (cpInfo instanceof ClassEntry classEntry) {
checkClassName(classEntry.asInternalName());
}
i += cpInfo.size();
i += cpInfo.width();
}
if (numberOfReferencedClassesToBeChecked != 16) {
throw new AssertionError("Class reference missing in the constant pool");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
* @test
* @bug 7166455
* @summary javac doesn't set ACC_STRICT bit on <clinit> for strictfp 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
* @compile -source 16 -target 16 CheckACC_STRICTFlagOnclinitTest.java
* @run main CheckACC_STRICTFlagOnclinitTest
*/
Expand All @@ -34,13 +39,7 @@
import java.util.List;
import java.io.File;
import java.io.IOException;
import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.ConstantPoolException;
import com.sun.tools.classfile.Descriptor;
import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
import com.sun.tools.classfile.Method;

import static com.sun.tools.classfile.AccessFlags.ACC_STRICT;
import jdk.internal.classfile.*;

public strictfp class CheckACC_STRICTFlagOnclinitTest {
private static final String AssertionErrorMessage =
Expand All @@ -65,13 +64,11 @@ void m21() {}

private List<String> errors = new ArrayList<>();

public static void main(String[] args)
throws IOException, ConstantPoolException, InvalidDescriptor {
public static void main(String[] args) throws IOException {
new CheckACC_STRICTFlagOnclinitTest().run();
}

private void run()
throws IOException, ConstantPoolException, InvalidDescriptor {
private void run() throws IOException {
String testClasses = System.getProperty("test.classes");
check(testClasses,
"CheckACC_STRICTFlagOnclinitTest.class",
Expand All @@ -86,19 +83,15 @@ private void run()
}
}

void check(String dir, String... fileNames)
throws
IOException,
ConstantPoolException,
Descriptor.InvalidDescriptor {
void check(String dir, String... fileNames) throws IOException{
for (String fileName : fileNames) {
ClassFile classFileToCheck = ClassFile.read(new File(dir, fileName));
ClassModel classFileToCheck = Classfile.of().parse(new File(dir, fileName).toPath());

for (Method method : classFileToCheck.methods) {
if ((method.access_flags.flags & ACC_STRICT) == 0) {
for (MethodModel method : classFileToCheck.methods()) {
if ((method.flags().flagsMask() & Classfile.ACC_STRICT) == 0) {
errors.add(String.format(offendingMethodErrorMessage,
method.getName(classFileToCheck.constant_pool),
classFileToCheck.getName()));
method.methodName().stringValue(),
classFileToCheck.thisClass().asInternalName()));
}
}
}
Expand Down
20 changes: 12 additions & 8 deletions test/langtools/tools/javac/7199823/InnerClassCannotBeVerified.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
* @test
* @bug 7199823
* @summary javac generates inner class that can't be verified
* @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 InnerClassCannotBeVerified
*/

Expand All @@ -37,8 +42,7 @@
import javax.tools.ToolProvider;
import javax.tools.JavaCompiler;
import com.sun.source.util.JavacTask;
import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.ConstantPoolException;
import jdk.internal.classfile.*;
import java.io.File;
import java.io.IOException;

Expand Down Expand Up @@ -85,17 +89,17 @@ void run() throws Exception {
}
}

private void check(CompilationKind ck) throws IOException, ConstantPoolException {
private void check(CompilationKind ck) throws IOException {
try {
File file = new File("Test$1.class");
ClassFile classFile = ClassFile.read(file);
ClassModel classFile = Classfile.of().parse(file.toPath());
if (ck == CompilationKind.POST_NESTMATES) {
throw new AssertionError("Unexpected constructor tag class!");
}
boolean inheritsFromObject =
classFile.getSuperclassName().equals("java/lang/Object");
boolean implementsNoInterface = classFile.interfaces.length == 0;
boolean noMethods = classFile.methods.length == 0;
classFile.superclass().orElseThrow().asInternalName().equals("java/lang/Object");
boolean implementsNoInterface = classFile.interfaces().size() == 0;
boolean noMethods = classFile.methods().size() == 0;
if (!(inheritsFromObject &&
implementsNoInterface &&
noMethods)) {
Expand Down
30 changes: 18 additions & 12 deletions test/langtools/tools/javac/8000518/DuplicateConstantPoolEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@
* @bug 8000518
* @summary Javac generates duplicate name_and_type constant pool entry for
* class BinaryOpValueExp.java
* @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 DuplicateConstantPoolEntry
*/

import com.sun.source.util.JavacTask;
import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.ConstantPoolException;
import jdk.internal.classfile.*;
import jdk.internal.classfile.constantpool.ConstantPool;
import java.io.File;
import java.io.IOException;
import java.net.URI;
Expand Down Expand Up @@ -87,17 +92,18 @@ void generateFilesNeeded() throws Exception {
}
}

void checkReference() throws IOException, ConstantPoolException {
void checkReference() throws IOException {
File file = new File("A.class");
ClassFile classFile = ClassFile.read(file);
ClassModel classFile = Classfile.of().parse(file.toPath());
ConstantPool constantPool = classFile.constantPool();
for (int i = 1;
i < classFile.constant_pool.size() - 1;
i += classFile.constant_pool.get(i).size()) {
for (int j = i + classFile.constant_pool.get(i).size();
j < classFile.constant_pool.size();
j += classFile.constant_pool.get(j).size()) {
if (classFile.constant_pool.get(i).toString().
equals(classFile.constant_pool.get(j).toString())) {
i < constantPool.entryCount() - 1;
i += constantPool.entryByIndex(i).width()) {
for (int j = i + constantPool.entryByIndex(i).width();
j < constantPool.entryCount();
j += constantPool.entryByIndex(j).width()) {
if (constantPool.entryByIndex(i).toString().
equals(constantPool.entryByIndex(j).toString())) {
throw new AssertionError(
"Duplicate entries in the constant pool at positions " +
i + " and " + j);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
* @test
* @bug 8005931
* @summary javac doesn't set ACC_STRICT for classes with package access
* @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.components
* java.base/jdk.internal.classfile.instruction
* java.base/jdk.internal.classfile.impl
* @run main CheckACC_STRICTFlagOnPkgAccessClassTest
*/

Expand All @@ -40,13 +45,7 @@
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;
import com.sun.source.util.JavacTask;
import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.ConstantPoolException;
import com.sun.tools.classfile.Descriptor;
import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
import com.sun.tools.classfile.Method;

import static com.sun.tools.classfile.AccessFlags.ACC_STRICT;
import jdk.internal.classfile.*;

public class CheckACC_STRICTFlagOnPkgAccessClassTest {

Expand All @@ -62,14 +61,12 @@ public class CheckACC_STRICTFlagOnPkgAccessClassTest {

private List<String> errors = new ArrayList<>();

public static void main(String[] args)
throws IOException, ConstantPoolException, InvalidDescriptor {
public static void main(String[] args) throws IOException {
JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
new CheckACC_STRICTFlagOnPkgAccessClassTest().run(comp);
}

private void run(JavaCompiler comp)
throws IOException, ConstantPoolException, InvalidDescriptor {
private void run(JavaCompiler comp) throws IOException {
compile(comp);
check();
if (errors.size() > 0) {
Expand All @@ -95,18 +92,14 @@ private void compile(JavaCompiler comp) {
}
}

void check()
throws
IOException,
ConstantPoolException,
Descriptor.InvalidDescriptor {
ClassFile classFileToCheck = ClassFile.read(new File("Test.class"));
void check() throws IOException {
ClassModel classFileToCheck = Classfile.of().parse(new File("Test.class").toPath());

for (Method method : classFileToCheck.methods) {
if ((method.access_flags.flags & ACC_STRICT) == 0) {
for (MethodModel method : classFileToCheck.methods()) {
if ((method.flags().flagsMask() & Classfile.ACC_STRICT) == 0) {
errors.add(String.format(offendingMethodErrorMessage,
method.getName(classFileToCheck.constant_pool),
classFileToCheck.getName()));
method.methodName().stringValue(),
classFileToCheck.thisClass().asInternalName()));
}
}
}