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 
26 package com.sun.source.util;
27 
28 import javax.lang.model.element.TypeElement;
29 import javax.tools.JavaFileObject;
30 
31 import 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 public final class TaskEvent
40 {
41     /**
42      * Kind of task event.
43      * @since 1.6
44      */
45     public enum Kind {
46         /**
47          * For events related to the parsing of a file.
48          */
49         PARSE,
50         /**
51          * For events relating to elements being entered.
52          **/
53         ENTER,
54         /**
55          * For events relating to elements being analyzed for errors.
56          **/
57         ANALYZE,
58         /**
59          * For events relating to class files being generated.
60          **/
61         GENERATE,
62         /**
63          * For events relating to overall annotation processing.
64          **/
65         ANNOTATION_PROCESSING,
66         /**
67          * For events relating to an individual annotation processing round.
68          **/
69         ANNOTATION_PROCESSING_ROUND,
70         /**
71          * Sent before parsing first source file, and after writing the last output file.
72          * This event is not sent when using {@link JavacTask#parse()},
73          * {@link JavacTask#analyze()} or {@link JavacTask#generate()}.
74          *
75          * @since 9
76          */
77         COMPILATION,
78     }
79 
80     /**
81      * Creates a task event for a given kind.
82      * The source file, compilation unit and type element
83      * are all set to {@code null}.
84      * @param kind the kind of the event
85      */
TaskEvent(Kind kind)86     public TaskEvent(Kind kind) {
87         this(kind, null, null, null);
88     }
89 
90     /**
91      * Creates a task event for a given kind and source file.
92      * The compilation unit and type element are both set to {@code null}.
93      * @param kind the kind of the event
94      * @param sourceFile the source file
95      */
TaskEvent(Kind kind, JavaFileObject sourceFile)96     public TaskEvent(Kind kind, JavaFileObject sourceFile) {
97         this(kind, sourceFile, null, null);
98     }
99 
100     /**
101      * Creates a task event for a given kind and compilation unit.
102      * The source file is set from the compilation unit,
103      * and the type element is set to {@code null}.
104      * @param kind the kind of the event
105      * @param unit the compilation unit
106      */
TaskEvent(Kind kind, CompilationUnitTree unit)107     public TaskEvent(Kind kind, CompilationUnitTree unit) {
108         this(kind, unit.getSourceFile(), unit, null);
109     }
110 
111     /**
112      * Creates a task event for a given kind, compilation unit
113      * and type element.
114      * The source file is set from the compilation unit.
115      * @param kind the kind of the event
116      * @param unit the compilation unit
117      * @param clazz the type element
118      */
TaskEvent(Kind kind, CompilationUnitTree unit, TypeElement clazz)119     public TaskEvent(Kind kind, CompilationUnitTree unit, TypeElement clazz) {
120         this(kind, unit.getSourceFile(), unit, clazz);
121     }
122 
TaskEvent(Kind kind, JavaFileObject file, CompilationUnitTree unit, TypeElement clazz)123     private TaskEvent(Kind kind, JavaFileObject file, CompilationUnitTree unit, TypeElement clazz) {
124         this.kind = kind;
125         this.file = file;
126         this.unit = unit;
127         this.clazz = clazz;
128     }
129 
130     /**
131      * Returns the kind for this event.
132      * @return the kind
133      */
getKind()134     public Kind getKind() {
135         return kind;
136     }
137 
138     /**
139      * Returns the source file for this event.
140      * May be {@code null}.
141      * @return the source file
142      */
getSourceFile()143     public JavaFileObject getSourceFile() {
144         return file;
145     }
146 
147     /**
148      * Returns the compilation unit for this event.
149      * May be {@code null}.
150      * @return the compilation unit
151      */
getCompilationUnit()152     public CompilationUnitTree getCompilationUnit() {
153         return unit;
154     }
155 
156     /**
157      * Returns the type element for this event.
158      * May be {@code null}.
159      * @return the type element
160      */
getTypeElement()161     public TypeElement getTypeElement() {
162         return clazz;
163     }
164 
165     @Override
toString()166     public String toString() {
167         return "TaskEvent["
168             + kind + ","
169             + file + ","
170             // the compilation unit is identified by the file
171             + clazz + "]";
172     }
173 
174     private Kind kind;
175     private JavaFileObject file;
176     private CompilationUnitTree unit;
177     private TypeElement clazz;
178 }
179