KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > compiler > ReconcileContext


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * mkaufman@bea.com - initial API and implementation
10  * IBM - renamed from PreReconcileCompilationResult to ReconcileContext
11  * IBM - rewrote spec
12  *
13  *******************************************************************************/

14
15 package org.eclipse.jdt.core.compiler;
16
17 import java.util.HashMap JavaDoc;
18
19 import org.eclipse.jdt.core.ICompilationUnit;
20 import org.eclipse.jdt.core.IJavaElementDelta;
21 import org.eclipse.jdt.core.IJavaModelMarker;
22 import org.eclipse.jdt.core.JavaModelException;
23 import org.eclipse.jdt.core.dom.AST;
24 import org.eclipse.jdt.core.dom.ASTParser;
25 import org.eclipse.jdt.internal.core.CompilationUnit;
26 import org.eclipse.jdt.internal.core.JavaProject;
27 import org.eclipse.jdt.internal.core.ReconcileWorkingCopyOperation;
28
29 /**
30  * The context of a reconcile event that is notified to interested compilation
31  * participants while a reconcile operation is running.
32  * <p>
33  * A reconcile participant can get the AST for the reconcile-operation using
34  * {@link #getAST3()}. If the participant modifies in any way the AST
35  * (either by modifying the source of the working copy, or modifying another entity
36  * that would result in different bindings for the AST), it is expected to reset the
37  * AST in the context using {@link #resetAST()}.
38  * </p><p>
39  * A reconcile participant can also create and return problems using
40  * {@link #putProblems(String, CategorizedProblem[])}. These problems are then reported
41  * to the problem requestor of the reconcile operation.
42  * </p><p>
43  * This class is not intended to be instanciated or subclassed by clients.
44  * </p>
45  *
46  * @see CompilationParticipant#reconcile(ReconcileContext)
47  * @since 3.2
48  */

49 public class ReconcileContext {
50     
51     private ReconcileWorkingCopyOperation operation;
52     private CompilationUnit workingCopy;
53
54 /**
55  * Creates a reconcile context for the given reconcile operation.
56  * <p>
57  * This constructor is not intended to be called by clients.
58  * </p>
59  *
60  * @param operation the reconcile operation
61  */

62 public ReconcileContext(ReconcileWorkingCopyOperation operation, CompilationUnit workingCopy) {
63     this.operation = operation;
64     this.workingCopy = workingCopy;
65 }
66
67 /**
68  * Returns a resolved AST with {@link AST#JLS3 JLS3} level.
69  * It is created from the current state of the working copy.
70  * Creates one if none exists yet.
71  * Returns <code>null</code> if the current state of the working copy
72  * doesn't allow the AST to be created (e.g. if the working copy's content
73  * cannot be parsed).
74  * <p>
75  * If the AST level requested during reconciling is not {@link AST#JLS3}
76  * or if binding resolutions was not requested, then a different AST is created.
77  * Note that this AST does not become the current AST and it is only valid for
78  * the requestor.
79  * </p>
80  *
81  * @return the AST created from the current state of the working copy,
82  * or <code>null</code> if none could be created
83  * @exception JavaModelException if the contents of the working copy
84  * cannot be accessed. Reasons include:
85  * <ul>
86  * <li> The working copy does not exist (ELEMENT_DOES_NOT_EXIST)</li>
87  * </ul>
88  */

89 public org.eclipse.jdt.core.dom.CompilationUnit getAST3() throws JavaModelException {
90     if (this.operation.astLevel != AST.JLS3 || !this.operation.resolveBindings) {
91         // create AST (optionally resolving bindings)
92
ASTParser parser = ASTParser.newParser(AST.JLS3);
93         parser.setCompilerOptions(workingCopy.getJavaProject().getOptions(true));
94         if (JavaProject.hasJavaNature(workingCopy.getJavaProject().getProject()))
95             parser.setResolveBindings(true);
96         parser.setStatementsRecovery((this.operation.reconcileFlags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0);
97         parser.setBindingsRecovery((this.operation.reconcileFlags & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0);
98         parser.setSource(workingCopy);
99         return (org.eclipse.jdt.core.dom.CompilationUnit) parser.createAST(this.operation.progressMonitor);
100     }
101     return this.operation.makeConsistent(this.workingCopy);
102 }
103
104 /**
105  * Returns the AST level requested by the reconcile operation.
106  * It is either {@link ICompilationUnit#NO_AST}, or one of the JLS constants defined on {@link AST}.
107  *
108  * @return the AST level requested by the reconcile operation
109  */

110 public int getASTLevel() {
111     return this.operation.astLevel;
112 }
113
114 /**
115  * Returns whether the reconcile operation is resolving bindings.
116  *
117  * @return whether the reconcile operation is resolving bindings
118  */

119 public boolean isResolvingBindings() {
120     return this.operation.resolveBindings;
121 }
122
123 /**
124  * Returns the delta describing the change to the working copy being reconciled.
125  * Returns <code>null</code> if there is no change.
126  * Note that the delta's AST is not yet positionnned at this stage. Use {@link #getAST3()}
127  * to get the current AST.
128  *
129  * @return the delta describing the change, or <code>null</code> if none
130  */

131 public IJavaElementDelta getDelta() {
132     return this.operation.deltaBuilder.delta;
133 }
134
135 /**
136  * Returns the problems to be reported to the problem requestor of the reconcile operation
137  * for the given marker type.
138  * Returns <code>null</code> if no problems need to be reported for this marker type.
139  *
140  * @param markerType the given marker type
141  * @return problems to be reported to the problem requesto
142  */

143 public CategorizedProblem[] getProblems(String JavaDoc markerType) {
144     if (this.operation.problems == null) return null;
145     return (CategorizedProblem[]) this.operation.problems.get(markerType);
146 }
147
148 /**
149  * Returns the working copy this context refers to.
150  *
151  * @return the working copy this context refers to
152  */

153 public ICompilationUnit getWorkingCopy() {
154     return this.workingCopy;
155 }
156
157 /**
158  * Resets the AST carried by this context.
159  * A compilation participant that modifies the environment that would result in different
160  * bindings for the AST is expected to reset the AST on this context, so that other
161  * participants don't get a stale AST.
162  * <p>
163  * Note that resetting the AST will not restart the reconcile process. Only further
164  * participants will see the new AST. Thus participants running before the one that
165  * resets the AST will have a stale view of the AST and its problems. Use
166  * the compilation participant extension point to order the participants.
167  * </p>
168  */

169 public void resetAST() {
170     this.operation.ast = null;
171     putProblems(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, null);
172     putProblems(IJavaModelMarker.TASK_MARKER, null);
173 }
174
175 /**
176  * Sets the problems to be reported to the problem requestor of the reconcile operation
177  * for the given marker type.
178  * <code>null</code> indicates that no problems need to be reported.
179  * <p>
180  * Using this functionality, a participant that resolves problems for a given marker type
181  * can hide those problems since they don't exist any longer.
182  * </p>
183  *
184  * @param markerType the marker type of the given problems
185  * @param problems the problems to be reported to the problem requestor of the reconcile operation,
186  * or <code>null</code> if none
187  */

188 public void putProblems(String JavaDoc markerType, CategorizedProblem[] problems) {
189     if (this.operation.problems == null)
190         this.operation.problems = new HashMap JavaDoc();
191     this.operation.problems.put(markerType, problems);
192 }
193
194 }
195
Popular Tags