T6358024.java revision 53:eaf608c64fec
1/*
2 * Copyright 2006-2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 6358024
27 * @summary TaskListener should be propogated between processing rounds
28 */
29
30import java.io.*;
31import java.util.*;
32import java.util.List;
33import javax.annotation.processing.*;
34import javax.lang.model.element.*;
35import javax.tools.*;
36import com.sun.source.util.*;
37import com.sun.tools.javac.api.*;
38import com.sun.tools.javac.file.*;
39import com.sun.tools.javac.main.*;
40import com.sun.tools.javac.util.*;
41
42
43@SupportedAnnotationTypes("*")
44public class T6358024 extends AbstractProcessor {
45    static JavacFileManager fm;
46    public static void main(String... args) throws Throwable {
47        String self = T6358024.class.getName();
48
49        String testSrc = System.getProperty("test.src");
50
51        fm = new JavacFileManager(new Context(), false, null);
52        JavaFileObject f = fm.getFileForInput(testSrc + File.separatorChar + self + ".java");
53
54        test(fm, f,
55             new Option[] { new Option("-d", ".")},
56             7);
57
58        test(fm, f,
59             new Option[] { new XOption("-XprintRounds"),
60                            new Option("-processorpath", "."),
61                            new Option("-processor", self) },
62             11);
63    }
64
65    static void test(JavacFileManager fm, JavaFileObject f, Option[] opts, int expect) throws Throwable {
66        PrintWriter out = new PrintWriter(System.err, true);
67
68        JavacTool tool = JavacTool.create();
69        List<String> flags = new ArrayList<String>();
70        for (Option opt: opts) {
71            flags.add(opt.name);
72            for (Object arg : opt.args)
73                flags.add(arg.toString());
74        }
75
76        JavacTaskImpl task = (JavacTaskImpl) tool.getTask(out,
77                                                          fm,
78                                                          null,
79                                                          flags,
80                                                          null,
81                                                          Arrays.asList(f));
82        MyTaskListener tl = new MyTaskListener();
83        task.setTaskListener(tl);
84        task.call();
85        if (tl.started != expect)
86            throw new AssertionError("Unexpected number of TaskListener events; "
87                                     + "expected " + expect + ", found " + tl.started);
88    }
89
90    public boolean process(Set<? extends TypeElement> tes, RoundEnvironment renv) {
91        return true;
92    }
93
94    static class MyTaskListener implements TaskListener {
95        public void started(TaskEvent e) {
96            System.err.println("Started: " + e);
97            started++;
98        }
99        public void finished(TaskEvent e) {
100        }
101
102        int started = 0;
103    }
104
105    static class Option {
106        Option(String name, String... args) {
107            this.name = name;
108            this.args = args;
109        }
110        public final String name;
111        public final String[] args;
112    }
113
114    static class XOption extends Option {
115        XOption(String name, String... args) {
116            super(name, args);
117        }
118    }
119}
120