aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 8010737 aoqi@0: * @summary javac, known parameter's names should be copied to automatically aoqi@0: * generated constructors for inner classes aoqi@0: * @run main ParameterNamesAreNotCopiedToAnonymousInitTest check_class_file check_init_symbol aoqi@0: */ aoqi@0: aoqi@0: import java.io.File; aoqi@0: import java.io.IOException; aoqi@0: import java.lang.annotation.ElementType; aoqi@0: import java.lang.annotation.Target; aoqi@0: import java.nio.file.Paths; aoqi@0: import java.util.Arrays; aoqi@0: aoqi@0: import javax.tools.JavaCompiler; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.StandardJavaFileManager; aoqi@0: import javax.tools.ToolProvider; aoqi@0: aoqi@0: import com.sun.source.tree.CompilationUnitTree; aoqi@0: import com.sun.source.tree.Tree; aoqi@0: import com.sun.source.util.JavacTask; aoqi@0: import com.sun.source.util.TaskEvent; aoqi@0: import com.sun.source.util.TaskListener; aoqi@0: import com.sun.tools.classfile.ClassFile; aoqi@0: import com.sun.tools.classfile.Method; aoqi@0: import com.sun.tools.javac.api.BasicJavacTask; aoqi@0: import com.sun.tools.javac.code.Attribute.Compound; aoqi@0: import com.sun.tools.javac.code.Symbol.ClassSymbol; aoqi@0: import com.sun.tools.javac.code.Symbol.VarSymbol; aoqi@0: import com.sun.tools.javac.tree.JCTree; aoqi@0: import com.sun.tools.javac.tree.TreeScanner; aoqi@0: import com.sun.tools.javac.util.Assert; aoqi@0: import com.sun.tools.javac.util.Context; aoqi@0: import com.sun.tools.javac.util.List; aoqi@0: import com.sun.tools.javac.util.Names; aoqi@0: aoqi@0: public class ParameterNamesAreNotCopiedToAnonymousInitTest { aoqi@0: aoqi@0: static final String noParamsErrorMsg = aoqi@0: "Test most be invoked with at least one parameter: check_class_file " + aoqi@0: "and/or check_init_symbol"; aoqi@0: static final String wrongParamsErrorMsg = aoqi@0: "Accepted arguments are: check_class_file and check_init_symbol"; aoqi@0: static final String paramNameNotCopiedAssertionMsg = aoqi@0: "The param name hasn't been copied to the init method"; aoqi@0: static final String noAnnotationsForParameterMsg = aoqi@0: "No annotations for seek parameter"; aoqi@0: static final String seekMethodNotFound = aoqi@0: "The seek init method was not found or conditions were not met"; aoqi@0: static final String nonNullParamPositionsMsg = aoqi@0: "Parameter positions shold not be null"; aoqi@0: static final String compilationFailed = aoqi@0: "Compilation failed"; aoqi@0: static final String seekMethodNotFoundMsg = aoqi@0: "The seek method was not found"; aoqi@0: aoqi@0: static final String ParamAnnotationClassName = aoqi@0: ParameterNamesAreNotCopiedToAnonymousInitTest.class.getSimpleName() + "." + aoqi@0: ParamAnnotation.class.getSimpleName(); aoqi@0: aoqi@0: public static void main(String[] args) throws Exception { aoqi@0: if (args.length == 0) { aoqi@0: throw new Error(noParamsErrorMsg); aoqi@0: } aoqi@0: new ParameterNamesAreNotCopiedToAnonymousInitTest().run(args); aoqi@0: } aoqi@0: aoqi@0: void run(String[] args) throws Exception { aoqi@0: for (String arg : args) { aoqi@0: if (arg.equals("check_class_file")) { aoqi@0: checkClassFile(new File(Paths.get(System.getProperty("test.classes"), aoqi@0: this.getClass().getName() + "$initParams$1.class").toUri()), 1); aoqi@0: checkClassFile(new File(Paths.get(System.getProperty("test.classes"), aoqi@0: this.getClass().getName() + "$Generics$1.class").toUri()), 2); aoqi@0: } else if (arg.equals("check_init_symbol")) { aoqi@0: checkInitSymbol("m1", Arrays.asList(0), Arrays.asList("i")); aoqi@0: checkInitSymbol("m2", Arrays.asList(0, 1), Arrays.asList("t1", "t2")); aoqi@0: } else { aoqi@0: error(wrongParamsErrorMsg); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void checkClassFile(final File cfile, int numberOfParams) throws Exception { aoqi@0: ClassFile classFile = ClassFile.read(cfile); aoqi@0: boolean methodFound = false; aoqi@0: for (Method method : classFile.methods) { aoqi@0: if (method.getName(classFile.constant_pool).equals("")) { aoqi@0: methodFound = true; aoqi@0: } aoqi@0: } aoqi@0: Assert.check(methodFound, seekMethodNotFoundMsg); aoqi@0: } aoqi@0: aoqi@0: /* This method expect a non-null ordered list of integers, listing the aoqi@0: * position of the parameters to be checked on the init method. Position 0 aoqi@0: * corresponds to the first parameter. aoqi@0: * aoqi@0: * As we are looking for a constructor of an anonymous class, the aoqi@0: * classOwnerName parameter must be the name of the method where the aoqi@0: * anonymous class is declared. aoqi@0: */ aoqi@0: void checkInitSymbol( aoqi@0: final String classOwnerName, aoqi@0: final java.util.List paramsToCheck, aoqi@0: final java.util.List paramNames) aoqi@0: throws IOException { aoqi@0: Assert.checkNonNull(paramsToCheck, nonNullParamPositionsMsg); aoqi@0: JavaCompiler c = ToolProvider.getSystemJavaCompiler(); aoqi@0: StandardJavaFileManager fm = c.getStandardFileManager(null, null, null); aoqi@0: Iterable fos = aoqi@0: fm.getJavaFileObjectsFromFiles( aoqi@0: Arrays.asList(new File(System.getProperty("test.src"), aoqi@0: this.getClass().getName() + ".java"))); aoqi@0: JavacTask task = (JavacTask) c.getTask(null, fm, null, aoqi@0: Arrays.asList("-d", System.getProperty("user.dir")), null, fos); aoqi@0: aoqi@0: BasicJavacTask impl = (BasicJavacTask)task; aoqi@0: Context context = impl.getContext(); aoqi@0: final Names names = Names.instance(context); aoqi@0: aoqi@0: task.addTaskListener(new TaskListener() { aoqi@0: aoqi@0: @Override aoqi@0: public void started(TaskEvent e) {} aoqi@0: aoqi@0: @Override aoqi@0: public void finished(TaskEvent e) { aoqi@0: class TheTreeScanner extends TreeScanner { aoqi@0: boolean foundAndCorrect = false; aoqi@0: aoqi@0: @Override aoqi@0: public void visitMethodDef(JCTree.JCMethodDecl tree) { aoqi@0: ClassSymbol clazz = (ClassSymbol)tree.sym.owner; aoqi@0: if (clazz.owner.name.toString().equals(classOwnerName) && aoqi@0: tree.sym.name == names.init) { aoqi@0: aoqi@0: int currentParamPos = 0; aoqi@0: int paramArrayIndex = 0; aoqi@0: aoqi@0: List params = tree.sym.params; aoqi@0: while (params.nonEmpty() && paramArrayIndex < paramsToCheck.size()) { aoqi@0: VarSymbol param = params.head; aoqi@0: if (currentParamPos == paramsToCheck.get(paramArrayIndex)) { aoqi@0: if (!param.name.toString() aoqi@0: .equals(paramNames.get(paramArrayIndex))) { aoqi@0: error(paramNameNotCopiedAssertionMsg); aoqi@0: } aoqi@0: paramArrayIndex++; aoqi@0: } aoqi@0: currentParamPos++; aoqi@0: params = params.tail; aoqi@0: } aoqi@0: foundAndCorrect = paramArrayIndex >= paramsToCheck.size(); aoqi@0: } aoqi@0: super.visitMethodDef(tree); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (e.getKind() == TaskEvent.Kind.ANALYZE) { aoqi@0: CompilationUnitTree compUnitTree = e.getCompilationUnit(); aoqi@0: boolean foundAndCorrect = false; aoqi@0: for (Tree tree : compUnitTree.getTypeDecls()) { aoqi@0: TheTreeScanner scanner = new TheTreeScanner(); aoqi@0: scanner.scan((JCTree) tree); aoqi@0: foundAndCorrect = foundAndCorrect | scanner.foundAndCorrect; aoqi@0: } aoqi@0: if (!foundAndCorrect) { aoqi@0: error(seekMethodNotFound); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: }); aoqi@0: aoqi@0: if (!task.call()) { aoqi@0: error(compilationFailed); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void error(String msg) { aoqi@0: throw new AssertionError(msg); aoqi@0: } aoqi@0: aoqi@0: @Target(value = {ElementType.PARAMETER}) aoqi@0: @interface ParamAnnotation {} aoqi@0: aoqi@0: /* If more cases are added in the future, it should be taken into account aoqi@0: * that method checkInitSymbol locates the inner class looking for its aoqi@0: * container method, which in the cases below are m1 and m2. So new cases aoqi@0: * must have different names for container methods or method checkInitSymbol aoqi@0: * should be changed. aoqi@0: */ aoqi@0: public class initParams { aoqi@0: public initParams(@ParamAnnotation int i) {} aoqi@0: aoqi@0: public void m1() { aoqi@0: new initParams(2) {}; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: class Generics { aoqi@0: T1 obj1; aoqi@0: Object obj2; aoqi@0: Generics(@ParamAnnotation T1 t1, @ParamAnnotation T2 t2) { aoqi@0: obj1 = t1; aoqi@0: obj2 = t2; aoqi@0: } aoqi@0: aoqi@0: void m2() { aoqi@0: Generics a = new Generics( aoqi@0: new Integer(11), "foo") {}; aoqi@0: } aoqi@0: } aoqi@0: }