mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-20 11:04:34 +02:00
8040129: Implement classfile tests for SourceFile attribute
Reviewed-by: shurailine, jjg
This commit is contained in:
parent
282ee77f73
commit
2c6668c63d
8 changed files with 440 additions and 0 deletions
|
@ -0,0 +1,37 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @test
|
||||||
|
* @summary sourcefile attribute test for anonymous class.
|
||||||
|
* @bug 8040129
|
||||||
|
* @library /tools/javac/lib ../lib
|
||||||
|
* @build SourceFileTestBase TestBase InMemoryFileManager ToolBox
|
||||||
|
* @run main AnonymousClassTest
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class AnonymousClassTest extends SourceFileTestBase {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
new AnonymousClassTest().test(new Object(){}.getClass(), "AnonymousClassTest.java");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @test
|
||||||
|
* @summary sourcefile attribute test for inner class.
|
||||||
|
* @bug 8040129
|
||||||
|
* @library /tools/javac/lib ../lib
|
||||||
|
* @build SourceFileTestBase TestBase InMemoryFileManager ToolBox
|
||||||
|
* @run main InnerClassTest
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class InnerClassTest extends SourceFileTestBase {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
new InnerClassTest().test(Inner.class, "InnerClassTest.java");
|
||||||
|
}
|
||||||
|
|
||||||
|
class Inner {
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @test
|
||||||
|
* @summary sourcefile attribute test for local class.
|
||||||
|
* @bug 8040129
|
||||||
|
* @library /tools/javac/lib ../lib
|
||||||
|
* @build SourceFileTestBase TestBase InMemoryFileManager ToolBox
|
||||||
|
* @run main LocalClassTest
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class LocalClassTest extends SourceFileTestBase {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
class Local {
|
||||||
|
}
|
||||||
|
new LocalClassTest().test(Local.class, "LocalClassTest.java");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,83 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @test
|
||||||
|
* @summary sourcefile attribute test for complex structure of nested classes and other types.
|
||||||
|
* @bug 8040129
|
||||||
|
* @library /tools/javac/lib ../lib
|
||||||
|
* @build SourceFileTestBase TestBase InMemoryFileManager ToolBox
|
||||||
|
* @run main MixTest
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class MixTest extends SourceFileTestBase {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
new InnerClass();
|
||||||
|
Runnable r = new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Runnable run = () -> {
|
||||||
|
class Local {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
class innerInAnonymous {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
new MixTest().run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() throws Exception {
|
||||||
|
String fileName = getClass().getName() + ".java";
|
||||||
|
test("MixTest", fileName);
|
||||||
|
test("MixTest$1", fileName);
|
||||||
|
test("MixTest$InnerClass", fileName);
|
||||||
|
test("MixTest$1$innerInAnonymous", fileName);
|
||||||
|
test("MixTest$1$1Local", fileName);
|
||||||
|
test("MixTest$InnerClass$innerEnum", fileName);
|
||||||
|
test("MixTest$InnerClass$innerInterface", fileName);
|
||||||
|
test("MixTest$InnerClass$innerEnum$innerClassInnerEnum", fileName);
|
||||||
|
test("MixTest$InnerClass$innerEnum$innerClassInnerEnum$1InnerLocal", fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
static class InnerClass {
|
||||||
|
private InnerClass() {
|
||||||
|
}
|
||||||
|
|
||||||
|
enum innerEnum {
|
||||||
|
E;
|
||||||
|
|
||||||
|
class innerClassInnerEnum {
|
||||||
|
void method() {
|
||||||
|
class InnerLocal {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@interface innerInterface {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @test
|
||||||
|
* @summary sourcefile attribute test for file compiled without debug information.
|
||||||
|
* @library /tools/javac/lib ../lib
|
||||||
|
* @build SourceFileTestBase TestBase InMemoryFileManager ToolBox
|
||||||
|
* @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 java.io.IOException;
|
||||||
|
|
||||||
|
|
||||||
|
public class NoSourceFileAttribute extends TestBase {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
new NoSourceFileAttribute().test();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void test() throws IOException, ConstantPoolException {
|
||||||
|
assertNull(
|
||||||
|
ClassFile.read(getClassFile(NoSourceFileAttribute.class)).getAttribute(Attribute.SourceFile),
|
||||||
|
"Classfile should have no SourceFile attribute when compiled without debug information.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,76 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import com.sun.tools.classfile.Attribute;
|
||||||
|
import com.sun.tools.classfile.ClassFile;
|
||||||
|
import com.sun.tools.classfile.SourceFile_attribute;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import javax.tools.JavaFileObject;
|
||||||
|
|
||||||
|
public class SourceFileTestBase extends TestBase {
|
||||||
|
|
||||||
|
protected void test(Class<?> classToTest, String fileName) throws Exception {
|
||||||
|
assertAttributePresent(ClassFile.read(getClassFile(classToTest)), fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void test(String classToTest, String fileName) throws Exception {
|
||||||
|
assertAttributePresent(ClassFile.read(getClassFile(classToTest + ".class")), fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compile sourceCode and for all "classesToTest" checks SourceFile attribute.
|
||||||
|
*/
|
||||||
|
protected void compileAndTest(String sourceCode, String... classesToTest) throws Exception {
|
||||||
|
|
||||||
|
Map<String, ? extends JavaFileObject> classes = compile(sourceCode);
|
||||||
|
String fileName = ToolBox.getJavaFileNameFromSource(sourceCode);
|
||||||
|
for (String className : classesToTest) {
|
||||||
|
assertAttributePresent(ClassFile.read(classes.get(className).openInputStream()), fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertAttributePresent(ClassFile classFile, String fileName) throws Exception {
|
||||||
|
|
||||||
|
//We need to count attributes with the same names because there is no appropriate API in the ClassFile.
|
||||||
|
|
||||||
|
List<SourceFile_attribute> sourceFileAttributes = new ArrayList<>();
|
||||||
|
for (Attribute a : classFile.attributes.attrs) {
|
||||||
|
if (Attribute.SourceFile.equals(a.getName(classFile.constant_pool))) {
|
||||||
|
sourceFileAttributes.add((SourceFile_attribute) a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(sourceFileAttributes.size(), 1, "Should be the only SourceFile attribute");
|
||||||
|
|
||||||
|
SourceFile_attribute 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,
|
||||||
|
"Incorrect source file name");
|
||||||
|
assertEquals(attribute.attribute_length, 2, "Incorrect attribute length");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @test
|
||||||
|
* @summary sourcefile attribute test for synthetic class.
|
||||||
|
* @bug 8040129
|
||||||
|
* @library /tools/javac/lib ../lib
|
||||||
|
* @build SourceFileTestBase TestBase InMemoryFileManager ToolBox
|
||||||
|
* @run main SyntheticClassTest
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class SyntheticClassTest extends SourceFileTestBase {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
new Inner();
|
||||||
|
|
||||||
|
new SyntheticClassTest().test("SyntheticClassTest$1", "SyntheticClassTest.java");
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Inner {
|
||||||
|
private Inner() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,71 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @test
|
||||||
|
* @summary sourcefile attribute test for two type in one file.
|
||||||
|
* @bug 8040129
|
||||||
|
* @library /tools/javac/lib ../lib
|
||||||
|
* @build SourceFileTestBase TestBase InMemoryFileManager ToolBox
|
||||||
|
* @run main TopLevelClassesOneFileTest
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class TopLevelClassesOneFileTest extends SourceFileTestBase {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
new TopLevelClassesOneFileTest().run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() throws Exception {
|
||||||
|
int failed = 0;
|
||||||
|
for (Type firstType : Type.values()) {
|
||||||
|
for (Type secondType : Type.values()) {
|
||||||
|
if (firstType != secondType) {
|
||||||
|
try {
|
||||||
|
compileAndTest("public " + firstType.source + secondType.source,
|
||||||
|
firstType.name(), secondType.name());
|
||||||
|
} catch (AssertionFailedException | CompilationException ex) {
|
||||||
|
System.err.println("Failed with public type " + firstType.name()
|
||||||
|
+ " and type " + secondType.name());
|
||||||
|
ex.printStackTrace();
|
||||||
|
failed++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (failed > 0)
|
||||||
|
throw new AssertionFailedException("Test failed. Failed cases count = " + failed + " .See log.");
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Type {
|
||||||
|
CLASS("class CLASS{}"),
|
||||||
|
INTERFACE("interface INTERFACE{}"),
|
||||||
|
ENUM("enum ENUM{;}"),
|
||||||
|
ANNOTATION("@interface ANNOTATION{}");
|
||||||
|
|
||||||
|
String source;
|
||||||
|
|
||||||
|
Type(String source) {
|
||||||
|
this.source = source;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue