1 /* 2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 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.compiler/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 "}"; 102 public JavaSource() { 103 super(URI.create("Test.java"), JavaFileObject.Kind.SOURCE); 104 } 105 106 @Override 107 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 108 return internalSource; 109 } 110 } 111 }