# HG changeset patch # User anazarov # Date 1399497734 25200 # Node ID 1599838bcaba7e3676761cacf40afeef733169e4 # Parent 0d889f759fac9128c910f2225ad2eb9dc7e2c558 8040129: Implement classfile tests for SourceFile attribute. Reviewed-by: shurailine, jjg diff -r 0d889f759fac -r 1599838bcaba langtools/test/tools/javac/classfiles/attributes/SourceFile/AnonymousClassTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/test/tools/javac/classfiles/attributes/SourceFile/AnonymousClassTest.java Wed May 07 14:22:14 2014 -0700 @@ -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"); + } +} diff -r 0d889f759fac -r 1599838bcaba langtools/test/tools/javac/classfiles/attributes/SourceFile/InnerClassTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/test/tools/javac/classfiles/attributes/SourceFile/InnerClassTest.java Wed May 07 14:22:14 2014 -0700 @@ -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 { + } +} diff -r 0d889f759fac -r 1599838bcaba langtools/test/tools/javac/classfiles/attributes/SourceFile/LocalClassTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/test/tools/javac/classfiles/attributes/SourceFile/LocalClassTest.java Wed May 07 14:22:14 2014 -0700 @@ -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"); + } +} diff -r 0d889f759fac -r 1599838bcaba langtools/test/tools/javac/classfiles/attributes/SourceFile/MixTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/test/tools/javac/classfiles/attributes/SourceFile/MixTest.java Wed May 07 14:22:14 2014 -0700 @@ -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 { + } + } +} diff -r 0d889f759fac -r 1599838bcaba langtools/test/tools/javac/classfiles/attributes/SourceFile/NoSourceFileAttribute.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/test/tools/javac/classfiles/attributes/SourceFile/NoSourceFileAttribute.java Wed May 07 14:22:14 2014 -0700 @@ -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."); + } +} diff -r 0d889f759fac -r 1599838bcaba langtools/test/tools/javac/classfiles/attributes/SourceFile/SourceFileTestBase.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/test/tools/javac/classfiles/attributes/SourceFile/SourceFileTestBase.java Wed May 07 14:22:14 2014 -0700 @@ -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 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 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"); + } +} diff -r 0d889f759fac -r 1599838bcaba langtools/test/tools/javac/classfiles/attributes/SourceFile/SyntheticClassTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/test/tools/javac/classfiles/attributes/SourceFile/SyntheticClassTest.java Wed May 07 14:22:14 2014 -0700 @@ -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() { + } + } +} diff -r 0d889f759fac -r 1599838bcaba langtools/test/tools/javac/classfiles/attributes/SourceFile/TopLevelClassesOneFileTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/test/tools/javac/classfiles/attributes/SourceFile/TopLevelClassesOneFileTest.java Wed May 07 14:22:14 2014 -0700 @@ -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; + } + } +}