langtools/test/tools/javac/T8010737/ParameterNamesAreNotCopiedToAnonymousInitTest.java
changeset 27319 030080f03e4f
parent 17999 42ae6fe53718
child 30730 d3ce7619db2c
equal deleted inserted replaced
27318:4660a5da7d90 27319:030080f03e4f
     1 /*
     1 /*
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
   130             final java.util.List<Integer> paramsToCheck,
   130             final java.util.List<Integer> paramsToCheck,
   131             final java.util.List<String> paramNames)
   131             final java.util.List<String> paramNames)
   132             throws IOException {
   132             throws IOException {
   133         Assert.checkNonNull(paramsToCheck, nonNullParamPositionsMsg);
   133         Assert.checkNonNull(paramsToCheck, nonNullParamPositionsMsg);
   134         JavaCompiler c = ToolProvider.getSystemJavaCompiler();
   134         JavaCompiler c = ToolProvider.getSystemJavaCompiler();
   135         StandardJavaFileManager fm = c.getStandardFileManager(null, null, null);
   135         try (StandardJavaFileManager fm = c.getStandardFileManager(null, null, null)) {
   136         Iterable<? extends JavaFileObject> fos =
   136             Iterable<? extends JavaFileObject> fos =
   137                 fm.getJavaFileObjectsFromFiles(
   137                     fm.getJavaFileObjectsFromFiles(
   138                 Arrays.asList(new File(System.getProperty("test.src"),
   138                     Arrays.asList(new File(System.getProperty("test.src"),
   139                 this.getClass().getName() + ".java")));
   139                     this.getClass().getName() + ".java")));
   140         JavacTask task = (JavacTask) c.getTask(null, fm, null,
   140             JavacTask task = (JavacTask) c.getTask(null, fm, null,
   141                 Arrays.asList("-d", System.getProperty("user.dir")), null, fos);
   141                     Arrays.asList("-d", System.getProperty("user.dir")), null, fos);
   142 
   142 
   143         BasicJavacTask impl = (BasicJavacTask)task;
   143             BasicJavacTask impl = (BasicJavacTask)task;
   144         Context context = impl.getContext();
   144             Context context = impl.getContext();
   145         final Names names = Names.instance(context);
   145             final Names names = Names.instance(context);
   146 
   146 
   147         task.addTaskListener(new TaskListener() {
   147             task.addTaskListener(new TaskListener() {
   148 
   148 
   149             @Override
   149                 @Override
   150             public void started(TaskEvent e) {}
   150                 public void started(TaskEvent e) {}
   151 
   151 
   152             @Override
   152                 @Override
   153             public void finished(TaskEvent e) {
   153                 public void finished(TaskEvent e) {
   154                 class TheTreeScanner extends TreeScanner {
   154                     class TheTreeScanner extends TreeScanner {
   155                     boolean foundAndCorrect = false;
   155                         boolean foundAndCorrect = false;
   156 
   156 
   157                     @Override
   157                         @Override
   158                     public void visitMethodDef(JCTree.JCMethodDecl tree) {
   158                         public void visitMethodDef(JCTree.JCMethodDecl tree) {
   159                         ClassSymbol clazz = (ClassSymbol)tree.sym.owner;
   159                             ClassSymbol clazz = (ClassSymbol)tree.sym.owner;
   160                         if (clazz.owner.name.toString().equals(classOwnerName) &&
   160                             if (clazz.owner.name.toString().equals(classOwnerName) &&
   161                             tree.sym.name == names.init) {
   161                                 tree.sym.name == names.init) {
   162 
   162 
   163                             int currentParamPos = 0;
   163                                 int currentParamPos = 0;
   164                             int paramArrayIndex = 0;
   164                                 int paramArrayIndex = 0;
   165 
   165 
   166                             List<VarSymbol> params = tree.sym.params;
   166                                 List<VarSymbol> params = tree.sym.params;
   167                             while (params.nonEmpty() && paramArrayIndex < paramsToCheck.size()) {
   167                                 while (params.nonEmpty() && paramArrayIndex < paramsToCheck.size()) {
   168                                 VarSymbol param = params.head;
   168                                     VarSymbol param = params.head;
   169                                 if (currentParamPos == paramsToCheck.get(paramArrayIndex)) {
   169                                     if (currentParamPos == paramsToCheck.get(paramArrayIndex)) {
   170                                     if (!param.name.toString()
   170                                         if (!param.name.toString()
   171                                             .equals(paramNames.get(paramArrayIndex))) {
   171                                                 .equals(paramNames.get(paramArrayIndex))) {
   172                                         error(paramNameNotCopiedAssertionMsg);
   172                                             error(paramNameNotCopiedAssertionMsg);
       
   173                                         }
       
   174                                         paramArrayIndex++;
   173                                     }
   175                                     }
   174                                     paramArrayIndex++;
   176                                     currentParamPos++;
       
   177                                     params = params.tail;
   175                                 }
   178                                 }
   176                                 currentParamPos++;
   179                                 foundAndCorrect = paramArrayIndex >= paramsToCheck.size();
   177                                 params = params.tail;
       
   178                             }
   180                             }
   179                             foundAndCorrect = paramArrayIndex >= paramsToCheck.size();
   181                             super.visitMethodDef(tree);
   180                         }
   182                         }
   181                         super.visitMethodDef(tree);
   183                     }
       
   184 
       
   185                     if (e.getKind() == TaskEvent.Kind.ANALYZE) {
       
   186                         CompilationUnitTree compUnitTree = e.getCompilationUnit();
       
   187                         boolean foundAndCorrect = false;
       
   188                         for (Tree tree : compUnitTree.getTypeDecls()) {
       
   189                             TheTreeScanner scanner = new TheTreeScanner();
       
   190                             scanner.scan((JCTree) tree);
       
   191                             foundAndCorrect = foundAndCorrect | scanner.foundAndCorrect;
       
   192                         }
       
   193                         if (!foundAndCorrect) {
       
   194                             error(seekMethodNotFound);
       
   195                         }
   182                     }
   196                     }
   183                 }
   197                 }
   184 
   198             });
   185                 if (e.getKind() == TaskEvent.Kind.ANALYZE) {
   199 
   186                     CompilationUnitTree compUnitTree = e.getCompilationUnit();
   200             if (!task.call()) {
   187                     boolean foundAndCorrect = false;
   201                 error(compilationFailed);
   188                     for (Tree tree : compUnitTree.getTypeDecls()) {
       
   189                         TheTreeScanner scanner = new TheTreeScanner();
       
   190                         scanner.scan((JCTree) tree);
       
   191                         foundAndCorrect = foundAndCorrect | scanner.foundAndCorrect;
       
   192                     }
       
   193                     if (!foundAndCorrect) {
       
   194                         error(seekMethodNotFound);
       
   195                     }
       
   196                 }
       
   197             }
   202             }
   198         });
       
   199 
       
   200         if (!task.call()) {
       
   201             error(compilationFailed);
       
   202         }
   203         }
   203     }
   204     }
   204 
   205 
   205     void error(String msg) {
   206     void error(String msg) {
   206         throw new AssertionError(msg);
   207         throw new AssertionError(msg);