test/tools/javac/T8010737/ParameterNamesAreNotCopiedToAnonymousInitTest.java

Wed, 24 Sep 2014 11:38:26 -0700

author
katleman
date
Wed, 24 Sep 2014 11:38:26 -0700
changeset 2562
ed1a48bedfa8
parent 0
959103a6100f
permissions
-rw-r--r--

Added tag jdk8u40-b07 for changeset 2fa3858a281f

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 8010737
aoqi@0 27 * @summary javac, known parameter's names should be copied to automatically
aoqi@0 28 * generated constructors for inner classes
aoqi@0 29 * @run main ParameterNamesAreNotCopiedToAnonymousInitTest check_class_file check_init_symbol
aoqi@0 30 */
aoqi@0 31
aoqi@0 32 import java.io.File;
aoqi@0 33 import java.io.IOException;
aoqi@0 34 import java.lang.annotation.ElementType;
aoqi@0 35 import java.lang.annotation.Target;
aoqi@0 36 import java.nio.file.Paths;
aoqi@0 37 import java.util.Arrays;
aoqi@0 38
aoqi@0 39 import javax.tools.JavaCompiler;
aoqi@0 40 import javax.tools.JavaFileObject;
aoqi@0 41 import javax.tools.StandardJavaFileManager;
aoqi@0 42 import javax.tools.ToolProvider;
aoqi@0 43
aoqi@0 44 import com.sun.source.tree.CompilationUnitTree;
aoqi@0 45 import com.sun.source.tree.Tree;
aoqi@0 46 import com.sun.source.util.JavacTask;
aoqi@0 47 import com.sun.source.util.TaskEvent;
aoqi@0 48 import com.sun.source.util.TaskListener;
aoqi@0 49 import com.sun.tools.classfile.ClassFile;
aoqi@0 50 import com.sun.tools.classfile.Method;
aoqi@0 51 import com.sun.tools.javac.api.BasicJavacTask;
aoqi@0 52 import com.sun.tools.javac.code.Attribute.Compound;
aoqi@0 53 import com.sun.tools.javac.code.Symbol.ClassSymbol;
aoqi@0 54 import com.sun.tools.javac.code.Symbol.VarSymbol;
aoqi@0 55 import com.sun.tools.javac.tree.JCTree;
aoqi@0 56 import com.sun.tools.javac.tree.TreeScanner;
aoqi@0 57 import com.sun.tools.javac.util.Assert;
aoqi@0 58 import com.sun.tools.javac.util.Context;
aoqi@0 59 import com.sun.tools.javac.util.List;
aoqi@0 60 import com.sun.tools.javac.util.Names;
aoqi@0 61
aoqi@0 62 public class ParameterNamesAreNotCopiedToAnonymousInitTest {
aoqi@0 63
aoqi@0 64 static final String noParamsErrorMsg =
aoqi@0 65 "Test most be invoked with at least one parameter: check_class_file " +
aoqi@0 66 "and/or check_init_symbol";
aoqi@0 67 static final String wrongParamsErrorMsg =
aoqi@0 68 "Accepted arguments are: check_class_file and check_init_symbol";
aoqi@0 69 static final String paramNameNotCopiedAssertionMsg =
aoqi@0 70 "The param name hasn't been copied to the init method";
aoqi@0 71 static final String noAnnotationsForParameterMsg =
aoqi@0 72 "No annotations for seek parameter";
aoqi@0 73 static final String seekMethodNotFound =
aoqi@0 74 "The seek init method was not found or conditions were not met";
aoqi@0 75 static final String nonNullParamPositionsMsg =
aoqi@0 76 "Parameter positions shold not be null";
aoqi@0 77 static final String compilationFailed =
aoqi@0 78 "Compilation failed";
aoqi@0 79 static final String seekMethodNotFoundMsg =
aoqi@0 80 "The seek method was not found";
aoqi@0 81
aoqi@0 82 static final String ParamAnnotationClassName =
aoqi@0 83 ParameterNamesAreNotCopiedToAnonymousInitTest.class.getSimpleName() + "." +
aoqi@0 84 ParamAnnotation.class.getSimpleName();
aoqi@0 85
aoqi@0 86 public static void main(String[] args) throws Exception {
aoqi@0 87 if (args.length == 0) {
aoqi@0 88 throw new Error(noParamsErrorMsg);
aoqi@0 89 }
aoqi@0 90 new ParameterNamesAreNotCopiedToAnonymousInitTest().run(args);
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 void run(String[] args) throws Exception {
aoqi@0 94 for (String arg : args) {
aoqi@0 95 if (arg.equals("check_class_file")) {
aoqi@0 96 checkClassFile(new File(Paths.get(System.getProperty("test.classes"),
aoqi@0 97 this.getClass().getName() + "$initParams$1.class").toUri()), 1);
aoqi@0 98 checkClassFile(new File(Paths.get(System.getProperty("test.classes"),
aoqi@0 99 this.getClass().getName() + "$Generics$1.class").toUri()), 2);
aoqi@0 100 } else if (arg.equals("check_init_symbol")) {
aoqi@0 101 checkInitSymbol("m1", Arrays.asList(0), Arrays.asList("i"));
aoqi@0 102 checkInitSymbol("m2", Arrays.asList(0, 1), Arrays.asList("t1", "t2"));
aoqi@0 103 } else {
aoqi@0 104 error(wrongParamsErrorMsg);
aoqi@0 105 }
aoqi@0 106 }
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 void checkClassFile(final File cfile, int numberOfParams) throws Exception {
aoqi@0 110 ClassFile classFile = ClassFile.read(cfile);
aoqi@0 111 boolean methodFound = false;
aoqi@0 112 for (Method method : classFile.methods) {
aoqi@0 113 if (method.getName(classFile.constant_pool).equals("<init>")) {
aoqi@0 114 methodFound = true;
aoqi@0 115 }
aoqi@0 116 }
aoqi@0 117 Assert.check(methodFound, seekMethodNotFoundMsg);
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 /* This method expect a non-null ordered list of integers, listing the
aoqi@0 121 * position of the parameters to be checked on the init method. Position 0
aoqi@0 122 * corresponds to the first parameter.
aoqi@0 123 *
aoqi@0 124 * As we are looking for a constructor of an anonymous class, the
aoqi@0 125 * classOwnerName parameter must be the name of the method where the
aoqi@0 126 * anonymous class is declared.
aoqi@0 127 */
aoqi@0 128 void checkInitSymbol(
aoqi@0 129 final String classOwnerName,
aoqi@0 130 final java.util.List<Integer> paramsToCheck,
aoqi@0 131 final java.util.List<String> paramNames)
aoqi@0 132 throws IOException {
aoqi@0 133 Assert.checkNonNull(paramsToCheck, nonNullParamPositionsMsg);
aoqi@0 134 JavaCompiler c = ToolProvider.getSystemJavaCompiler();
aoqi@0 135 StandardJavaFileManager fm = c.getStandardFileManager(null, null, null);
aoqi@0 136 Iterable<? extends JavaFileObject> fos =
aoqi@0 137 fm.getJavaFileObjectsFromFiles(
aoqi@0 138 Arrays.asList(new File(System.getProperty("test.src"),
aoqi@0 139 this.getClass().getName() + ".java")));
aoqi@0 140 JavacTask task = (JavacTask) c.getTask(null, fm, null,
aoqi@0 141 Arrays.asList("-d", System.getProperty("user.dir")), null, fos);
aoqi@0 142
aoqi@0 143 BasicJavacTask impl = (BasicJavacTask)task;
aoqi@0 144 Context context = impl.getContext();
aoqi@0 145 final Names names = Names.instance(context);
aoqi@0 146
aoqi@0 147 task.addTaskListener(new TaskListener() {
aoqi@0 148
aoqi@0 149 @Override
aoqi@0 150 public void started(TaskEvent e) {}
aoqi@0 151
aoqi@0 152 @Override
aoqi@0 153 public void finished(TaskEvent e) {
aoqi@0 154 class TheTreeScanner extends TreeScanner {
aoqi@0 155 boolean foundAndCorrect = false;
aoqi@0 156
aoqi@0 157 @Override
aoqi@0 158 public void visitMethodDef(JCTree.JCMethodDecl tree) {
aoqi@0 159 ClassSymbol clazz = (ClassSymbol)tree.sym.owner;
aoqi@0 160 if (clazz.owner.name.toString().equals(classOwnerName) &&
aoqi@0 161 tree.sym.name == names.init) {
aoqi@0 162
aoqi@0 163 int currentParamPos = 0;
aoqi@0 164 int paramArrayIndex = 0;
aoqi@0 165
aoqi@0 166 List<VarSymbol> params = tree.sym.params;
aoqi@0 167 while (params.nonEmpty() && paramArrayIndex < paramsToCheck.size()) {
aoqi@0 168 VarSymbol param = params.head;
aoqi@0 169 if (currentParamPos == paramsToCheck.get(paramArrayIndex)) {
aoqi@0 170 if (!param.name.toString()
aoqi@0 171 .equals(paramNames.get(paramArrayIndex))) {
aoqi@0 172 error(paramNameNotCopiedAssertionMsg);
aoqi@0 173 }
aoqi@0 174 paramArrayIndex++;
aoqi@0 175 }
aoqi@0 176 currentParamPos++;
aoqi@0 177 params = params.tail;
aoqi@0 178 }
aoqi@0 179 foundAndCorrect = paramArrayIndex >= paramsToCheck.size();
aoqi@0 180 }
aoqi@0 181 super.visitMethodDef(tree);
aoqi@0 182 }
aoqi@0 183 }
aoqi@0 184
aoqi@0 185 if (e.getKind() == TaskEvent.Kind.ANALYZE) {
aoqi@0 186 CompilationUnitTree compUnitTree = e.getCompilationUnit();
aoqi@0 187 boolean foundAndCorrect = false;
aoqi@0 188 for (Tree tree : compUnitTree.getTypeDecls()) {
aoqi@0 189 TheTreeScanner scanner = new TheTreeScanner();
aoqi@0 190 scanner.scan((JCTree) tree);
aoqi@0 191 foundAndCorrect = foundAndCorrect | scanner.foundAndCorrect;
aoqi@0 192 }
aoqi@0 193 if (!foundAndCorrect) {
aoqi@0 194 error(seekMethodNotFound);
aoqi@0 195 }
aoqi@0 196 }
aoqi@0 197 }
aoqi@0 198 });
aoqi@0 199
aoqi@0 200 if (!task.call()) {
aoqi@0 201 error(compilationFailed);
aoqi@0 202 }
aoqi@0 203 }
aoqi@0 204
aoqi@0 205 void error(String msg) {
aoqi@0 206 throw new AssertionError(msg);
aoqi@0 207 }
aoqi@0 208
aoqi@0 209 @Target(value = {ElementType.PARAMETER})
aoqi@0 210 @interface ParamAnnotation {}
aoqi@0 211
aoqi@0 212 /* If more cases are added in the future, it should be taken into account
aoqi@0 213 * that method checkInitSymbol locates the inner class looking for its
aoqi@0 214 * container method, which in the cases below are m1 and m2. So new cases
aoqi@0 215 * must have different names for container methods or method checkInitSymbol
aoqi@0 216 * should be changed.
aoqi@0 217 */
aoqi@0 218 public class initParams {
aoqi@0 219 public initParams(@ParamAnnotation int i) {}
aoqi@0 220
aoqi@0 221 public void m1() {
aoqi@0 222 new initParams(2) {};
aoqi@0 223 }
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 class Generics<T1> {
aoqi@0 227 T1 obj1;
aoqi@0 228 Object obj2;
aoqi@0 229 <T2> Generics(@ParamAnnotation T1 t1, @ParamAnnotation T2 t2) {
aoqi@0 230 obj1 = t1;
aoqi@0 231 obj2 = t2;
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 void m2() {
aoqi@0 235 Generics<Integer> a = new <String>Generics<Integer>(
aoqi@0 236 new Integer(11), "foo") {};
aoqi@0 237 }
aoqi@0 238 }
aoqi@0 239 }

mercurial