TaskEvent.java revision 2571:10fc81ac75b4
1/*
2 * Copyright (c) 2005, 2014, 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.source.util;
27
28import javax.lang.model.element.TypeElement;
29import javax.tools.JavaFileObject;
30
31import com.sun.source.tree.CompilationUnitTree;
32
33/**
34 * Provides details about work that has been done by the JDK Java Compiler, javac.
35 *
36 * @author Jonathan Gibbons
37 * @since 1.6
38 */
39@jdk.Exported
40public final class TaskEvent
41{
42    /**
43     * Kind of task event.
44     * @since 1.6
45     */
46    @jdk.Exported
47    public enum Kind {
48        /**
49         * For events related to the parsing of a file.
50         */
51        PARSE,
52        /**
53         * For events relating to elements being entered.
54         **/
55        ENTER,
56        /**
57         * For events relating to elements being analyzed for errors.
58         **/
59        ANALYZE,
60        /**
61         * For events relating to class files being generated.
62         **/
63        GENERATE,
64        /**
65         * For events relating to overall annotation processing.
66         **/
67        ANNOTATION_PROCESSING,
68        /**
69         * For events relating to an individual annotation processing round.
70         **/
71        ANNOTATION_PROCESSING_ROUND,
72        /**
73         * Sent before parsing first source file, and after writing the last output file.
74         * This event is not sent when using {@link JavacTask#parse()},
75         * {@link JavacTask#analyze()} or {@link JavacTask#generate()}.
76         *
77         * @since 1.9
78         */
79        COMPILATION,
80    }
81
82    /**
83     * Creates a task event for a given kind.
84     * The source file, compilation unit and type element
85     * are all set to {@code null}.
86     * @param kind the kind of the event
87     */
88    public TaskEvent(Kind kind) {
89        this(kind, null, null, null);
90    }
91
92    /**
93     * Creates a task event for a given kind and source file.
94     * The compilation unit and type element are both set to {@code null}.
95     * @param kind the kind of the event
96     * @param sourceFile the source file
97     */
98    public TaskEvent(Kind kind, JavaFileObject sourceFile) {
99        this(kind, sourceFile, null, null);
100    }
101
102    /**
103     * Creates a task event for a given kind and compilation unit.
104     * The source file is set from the compilation unit,
105     * and the type element is set to {@code null}.
106     * @param kind the kind of the event
107     * @param unit the compilation unit
108     */
109    public TaskEvent(Kind kind, CompilationUnitTree unit) {
110        this(kind, unit.getSourceFile(), unit, null);
111    }
112
113    /**
114     * Creates a task event for a given kind, compilation unit
115     * and type element.
116     * The source file is set from the compilation unit.
117     * @param kind the kind of the event
118     * @param unit the compilation unit
119     * @param clazz the type element
120     */
121    public TaskEvent(Kind kind, CompilationUnitTree unit, TypeElement clazz) {
122        this(kind, unit.getSourceFile(), unit, clazz);
123    }
124
125    private TaskEvent(Kind kind, JavaFileObject file, CompilationUnitTree unit, TypeElement clazz) {
126        this.kind = kind;
127        this.file = file;
128        this.unit = unit;
129        this.clazz = clazz;
130    }
131
132    /**
133     * Returns the kind for this event.
134     * @return the kind
135     */
136    public Kind getKind() {
137        return kind;
138    }
139
140    /**
141     * Returns the source file for this event.
142     * May be {@code null}.
143     * @return the source file
144     */
145    public JavaFileObject getSourceFile() {
146        return file;
147    }
148
149    /**
150     * Returns the compilation unit for this event.
151     * May be {@code null}.
152     * @return the compilation unit
153     */
154    public CompilationUnitTree getCompilationUnit() {
155        return unit;
156    }
157
158    /**
159     * Returns the type element for this event.
160     * May be {@code null}.
161     * @return the type element
162     */
163    public TypeElement getTypeElement() {
164        return clazz;
165    }
166
167    @Override
168    public String toString() {
169        return "TaskEvent["
170            + kind + ","
171            + file + ","
172            // the compilation unit is identified by the file
173            + clazz + "]";
174    }
175
176    private Kind kind;
177    private JavaFileObject file;
178    private CompilationUnitTree unit;
179    private TypeElement clazz;
180}
181