Test.java revision 936:a2399c8db703
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 */
23
24/*
25 * @test
26 * @bug 6930508
27 * @summary Passing nested class names on javac command line interfere with subsequent name -> class lookup
28 * @library ../../../lib
29 * @build JavacTestingAbstractProcessor p.NestedExamples Test
30 * @run main Test
31 */
32
33import java.io.*;
34import java.util.*;
35import javax.annotation.processing.RoundEnvironment;
36import javax.lang.model.element.TypeElement;
37import javax.lang.model.util.ElementFilter;
38import javax.tools.*;
39
40import p.NestedExamples;
41
42public class Test extends JavacTestingAbstractProcessor {
43    public static void main(String... args) throws Exception {
44        new Test().run();
45    }
46
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));
55
56        if (errors > 0)
57            throw new RuntimeException(errors + " errors occurred");
58    }
59
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    }
66
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    }
81
82    <T> List<T> reverse(List<T> list) {
83        List<T> newList = new ArrayList<T>(list);
84        Collections.reverse(newList);
85        return newList;
86    }
87
88    int errors = 0;
89
90    void error(String msg) {
91        System.out.println("Error: " + msg);
92        errors++;
93    }
94
95    //----------
96
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}
107