test/tools/javac/processing/options/testCommandLineClasses/Test.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1466
b52a38d4536c
parent 0
959103a6100f
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2011, 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  */
    24 /*
    25  * @test
    26  * @bug 6930508
    27  * @summary Passing nested class names on javac command line interfere with subsequent name -> class lookup
    28  * @library /tools/javac/lib
    29  * @build JavacTestingAbstractProcessor p.NestedExamples Test
    30  * @run main Test
    31  */
    33 import java.io.*;
    34 import java.util.*;
    35 import javax.annotation.processing.RoundEnvironment;
    36 import javax.lang.model.element.TypeElement;
    37 import javax.lang.model.util.ElementFilter;
    38 import javax.tools.*;
    40 import p.NestedExamples;
    42 public class Test extends JavacTestingAbstractProcessor {
    43     public static void main(String... args) throws Exception {
    44         new Test().run();
    45     }
    47     void run() throws Exception {
    48         NestedExamples e = new NestedExamples();
    49         List<String> names = getNames(e.getClasses());
    50         test(names);
    51         test(reverse(names));
    52         names = Arrays.asList(e.getClassNames());
    53         test(names);
    54         test(reverse(names));
    56         if (errors > 0)
    57             throw new RuntimeException(errors + " errors occurred");
    58     }
    60     List<String> getNames(Class<?>[] classes) {
    61         List<String> names = new ArrayList<String>();
    62         for (Class<?> c: classes)
    63             names.add(c.getName());
    64         return names;
    65     }
    67     void test(List<String> names) throws Exception {
    68         System.err.println("test: " + names);
    69         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    70         StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    71         File testClasses = new File(System.getProperty("test.classes"));
    72         fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(testClasses));
    73         JavaCompiler.CompilationTask task = compiler.getTask(
    74                 null, null, null, Arrays.asList("-proc:only"), names, null);
    75         task.setProcessors(Arrays.asList(new Test()));
    76         boolean ok = task.call();
    77         if (!ok)
    78             error("compilation failed");
    79         System.err.println();
    80     }
    82     <T> List<T> reverse(List<T> list) {
    83         List<T> newList = new ArrayList<T>(list);
    84         Collections.reverse(newList);
    85         return newList;
    86     }
    88     int errors = 0;
    90     void error(String msg) {
    91         System.out.println("Error: " + msg);
    92         errors++;
    93     }
    95     //----------
    97     public boolean process(Set<? extends TypeElement> annotations,
    98                            RoundEnvironment roundEnv) {
    99         if (!roundEnv.processingOver()) {
   100             for (TypeElement typeElt : ElementFilter.typesIn(roundEnv.getRootElements())) {
   101                 messager.printMessage(Diagnostic.Kind.NOTE, "processing " + typeElt);
   102             }
   103         }
   104         return true;
   105     }
   106 }

mercurial