12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 7199823 27 * @summary javac generates inner class that can't be verified 28 * @modules jdk.jdeps/com.sun.tools.classfile 29 * @run main InnerClassCannotBeVerified 30 */ 31 32 import java.util.Arrays; 33 import javax.tools.JavaFileObject; 34 import java.net.URI; 35 import javax.tools.SimpleJavaFileObject; 36 import javax.tools.ToolProvider; 37 import javax.tools.JavaCompiler; 38 import com.sun.source.util.JavacTask; 39 import com.sun.tools.classfile.ClassFile; 40 import com.sun.tools.classfile.ConstantPoolException; 41 import java.io.File; 42 import java.io.IOException; 43 44 public class InnerClassCannotBeVerified { 45 46 private static final String errorMessage = 47 "Compile error while compiling the following source:\n"; 48 49 public static void main(String... args) throws Exception { 50 new InnerClassCannotBeVerified().run(); 51 } 52 53 void run() throws Exception { 54 JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); 55 JavaSource source = new JavaSource(); 56 JavacTask ct = (JavacTask)comp.getTask(null, null, null, 57 null, null, Arrays.asList(source)); 58 try { 59 if (!ct.call()) { 60 throw new AssertionError(errorMessage + 61 source.getCharContent(true)); 62 } 63 } catch (Throwable ex) { 64 throw new AssertionError(errorMessage + 65 source.getCharContent(true)); 66 } 67 check(); 68 } 69 70 private void check() throws IOException, ConstantPoolException { 71 File file = new File("Test$1.class"); 72 ClassFile classFile = ClassFile.read(file); 73 boolean inheritsFromObject = 74 classFile.getSuperclassName().equals("java/lang/Object"); 75 boolean implementsNoInterface = classFile.interfaces.length == 0; 76 boolean noMethods = classFile.methods.length == 0; 77 if (!(inheritsFromObject && 78 implementsNoInterface && 79 noMethods)) { 80 throw new AssertionError("The inner classes reused as " + 81 "access constructor tag for this code must be empty"); 82 } 83 } 84 85 class JavaSource extends SimpleJavaFileObject { 86 87 String internalSource = 88 "public class Test {\n" + 89 " private static class Foo {}\n" + 90 " public static void main(String[] args){ \n" + 91 " new Foo();\n" + 92 " if(false) {\n" + 93 " new Runnable() {\n" + 94 " @Override\n" + 95 " public void run() {\n" + 96 " System.out.println();\n" + 97 " }\n" + 98 " }.run();\n" + 99 " }\n" + 100 " }\n" + 101 "}"; | 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 7199823 27 * @summary javac generates inner class that can't be verified 28 * @modules jdk.jdeps/com.sun.tools.classfile 29 * @run main InnerClassCannotBeVerified 30 */ 31 32 import java.nio.file.NoSuchFileException; 33 import java.util.Arrays; 34 import javax.tools.JavaFileObject; 35 import java.net.URI; 36 import javax.tools.SimpleJavaFileObject; 37 import javax.tools.ToolProvider; 38 import javax.tools.JavaCompiler; 39 import com.sun.source.util.JavacTask; 40 import com.sun.tools.classfile.ClassFile; 41 import com.sun.tools.classfile.ConstantPoolException; 42 import java.io.File; 43 import java.io.IOException; 44 45 public class InnerClassCannotBeVerified { 46 47 enum CompilationKind { 48 PRE_NESTMATES("-source", "10", "-target", "10"), 49 POST_NESTMATES(); 50 51 String[] opts; 52 53 CompilationKind(String... opts) { 54 this.opts = opts; 55 } 56 } 57 58 private static final String errorMessage = 59 "Compile error while compiling the following source:\n"; 60 61 public static void main(String... args) throws Exception { 62 new InnerClassCannotBeVerified().run(); 63 } 64 65 void run() throws Exception { 66 for (CompilationKind ck : CompilationKind.values()) { 67 File file = new File("Test$1.class"); 68 if (file.exists()) { 69 file.delete(); 70 } 71 JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); 72 JavaSource source = new JavaSource(); 73 JavacTask ct = (JavacTask)comp.getTask(null, null, null, 74 Arrays.asList(ck.opts), null, Arrays.asList(source)); 75 try { 76 if (!ct.call()) { 77 throw new AssertionError(errorMessage + 78 source.getCharContent(true)); 79 } 80 } catch (Throwable ex) { 81 throw new AssertionError(errorMessage + 82 source.getCharContent(true)); 83 } 84 check(ck); 85 } 86 } 87 88 private void check(CompilationKind ck) throws IOException, ConstantPoolException { 89 try { 90 File file = new File("Test$1.class"); 91 ClassFile classFile = ClassFile.read(file); 92 if (ck == CompilationKind.POST_NESTMATES) { 93 throw new AssertionError("Unexpected constructor tag class!"); 94 } 95 boolean inheritsFromObject = 96 classFile.getSuperclassName().equals("java/lang/Object"); 97 boolean implementsNoInterface = classFile.interfaces.length == 0; 98 boolean noMethods = classFile.methods.length == 0; 99 if (!(inheritsFromObject && 100 implementsNoInterface && 101 noMethods)) { 102 throw new AssertionError("The inner classes reused as " + 103 "access constructor tag for this code must be empty"); 104 } 105 } catch (NoSuchFileException ex) { 106 if (ck == CompilationKind.PRE_NESTMATES) { 107 throw new AssertionError("Constructor tag class missing!"); 108 } 109 } 110 } 111 112 class JavaSource extends SimpleJavaFileObject { 113 114 String internalSource = 115 "public class Test {\n" + 116 " private static class Foo {}\n" + 117 " public static void main(String[] args){ \n" + 118 " new Foo();\n" + 119 " if(false) {\n" + 120 " new Runnable() {\n" + 121 " @Override\n" + 122 " public void run() {\n" + 123 " System.out.println();\n" + 124 " }\n" + 125 " }.run();\n" + 126 " }\n" + 127 " }\n" + 128 "}"; |