KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > CompilationController


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.java.source;
21
22 import com.sun.source.tree.CompilationUnitTree;
23 import com.sun.source.util.Trees;
24 import com.sun.tools.javac.api.JavacTaskImpl;
25 import java.io.IOException JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.lang.model.util.Elements;
28 import javax.lang.model.util.Types;
29 import javax.swing.text.Document JavaDoc;
30 import javax.tools.Diagnostic;
31 import org.netbeans.api.lexer.TokenHierarchy;
32 import static org.netbeans.api.java.source.JavaSource.Phase.*;
33 import org.openide.filesystems.FileObject;
34
35 /** Class for explicit invocation of compilation phases on a java source.
36  * The implementation delegates to the {@link CompilationInfo} to get the data,
37  * the access to {@link CompilationInfo} is not synchronized, so the class isn't
38  * reentrant.
39  *
40  * XXX: make toPhase automatic in getTrees(), Trees.getElement, etc....
41  * @author Petr Hrebejk, Tomas Zezula
42  */

43 public class CompilationController extends CompilationInfo {
44     
45     //Not private for unit tests
46
/*private*/final CompilationInfo delegate;
47     
48     CompilationController(final CompilationInfo delegate) throws IOException JavaDoc {
49         super();
50         assert delegate != null;
51         this.delegate = delegate;
52     }
53         
54     // API of the class --------------------------------------------------------
55

56     /** Moves the state to required phase. If given state was already reached
57      * the state is not changed. The method will throw exception if a state is
58      * illegal required. Acceptable parameters for thid method are <BR>
59      * <LI>{@link org.netbeans.api.java.source.JavaSource.Phase.PARSED}
60      * <LI>{@link org.netbeans.api.java.source.JavaSource.Phase.ELEMENTS_RESOLVED}
61      * <LI>{@link org.netbeans.api.java.source.JavaSource.Phase.RESOLVED}
62      * <LI>{@link org.netbeans.api.java.source.JavaSource.Phase.UP_TO_DATE}
63      * @param phase The required phase
64      * @return the reached state
65      * @throws IllegalArgumentException in case that given state can not be
66      * reached using this method
67      * @throws IOException when the file cannot be red
68      */

69     public JavaSource.Phase toPhase(JavaSource.Phase phase ) throws IOException JavaDoc {
70         if (phase == MODIFIED) {
71             throw new IllegalArgumentException JavaDoc( "Wrong phase" + phase );
72         }
73         if (delegate.jfo == null) {
74             JavaSource.Phase currentPhase = delegate.getPhase();
75             if (currentPhase.compareTo(phase)<0) {
76                 delegate.setPhase(phase);
77             }
78             return delegate.getPhase();
79         }
80         else {
81             JavaSource.Phase currentPhase = JavaSource.moveToPhase(phase, this.delegate,false);
82             return currentPhase.compareTo (phase) < 0 ? currentPhase : phase;
83         }
84     }
85
86     /**
87      * Returns the current phase of the {@link JavaSource}.
88      *
89      * @return {@link JavaSource.Phase} the state which was reached by the {@link JavaSource}.
90      */

91     @Override JavaDoc
92     public JavaSource.Phase getPhase() {
93         return this.delegate.getPhase();
94     }
95         
96     /**
97      * Returns the javac tree representing the source file.
98      *
99      * @return {@link CompilationUnitTree} the compilation unit cantaining the top level classes contained in the
100      * java source file. It may return null when the {@link CompilationController#getPhase} is lower than
101      * {@link JavaSource.Phase#PARSED}. Before calling this method the client has to call {@link CompilationController#toPhase}
102      * with required {@link JavaSource.Phase}.
103      */

104     @Override JavaDoc
105     public CompilationUnitTree getCompilationUnit() {
106         return this.delegate.getCompilationUnit();
107     }
108
109     /**
110      * Returns the content of the file represented by the {@link JavaSource}.
111      *
112      * @return String the java source
113      */

114     @Override JavaDoc
115     public String JavaDoc getText() {
116         return this.delegate.getText();
117     }
118
119     /**@inheritDoc*/
120     @Override JavaDoc
121     public TokenHierarchy getTokenHierarchy() {
122         return this.delegate.getTokenHierarchy();
123     }
124     
125     /**
126      * Returns the errors in the file represented by the {@link JavaSource}.
127      *
128      * @return an list of {@link Diagnostic}
129      */

130     @Override JavaDoc
131     public List JavaDoc<Diagnostic> getDiagnostics() {
132         return this.delegate.getDiagnostics();
133     }
134
135     @Override JavaDoc
136     public Trees getTrees() {
137         return this.delegate.getTrees();
138     }
139
140     @Override JavaDoc
141     public Types getTypes() {
142         return this.delegate.getTypes();
143     }
144     
145     @Override JavaDoc
146     public Elements getElements() {
147         return this.delegate.getElements();
148     }
149     
150     @Override JavaDoc
151     public JavaSource getJavaSource() {
152         return this.delegate.getJavaSource();
153     }
154
155     @Override JavaDoc
156     public ClasspathInfo getClasspathInfo() {
157         return this.delegate.getClasspathInfo();
158     }
159
160     @Override JavaDoc
161     public FileObject getFileObject() {
162         return this.delegate.getFileObject();
163     }
164
165     @Override JavaDoc
166     public Document JavaDoc getDocument() throws IOException JavaDoc {
167         return this.delegate.getDocument();
168     }
169     
170     @Override JavaDoc
171     public synchronized TreeUtilities getTreeUtilities() {
172         return this.delegate.getTreeUtilities();
173     }
174     
175     @Override JavaDoc
176     public synchronized ElementUtilities getElementUtilities() {
177         return this.delegate.getElementUtilities();
178     }
179     
180     @Override JavaDoc
181     public synchronized CommentUtilities getCommentUtilities() {
182         return this.delegate.getCommentUtilities();
183     }
184     
185     // Package private methods -------------------------------------------------
186

187     @Override JavaDoc
188     void setPhase(final JavaSource.Phase phase) {
189         throw new UnsupportedOperationException JavaDoc ("CompilationController supports only read interface"); //NOI18N
190
}
191
192     @Override JavaDoc
193     void setCompilationUnit(final CompilationUnitTree compilationUnit) {
194         throw new UnsupportedOperationException JavaDoc ("CompilationController supports only read interface"); //NOI18N
195
}
196
197     @Override JavaDoc
198     JavacTaskImpl getJavacTask() {
199         return this.delegate.getJavacTask();
200     }
201 }
202
Popular Tags