KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > env > ReconcileEnv


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
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  * tyeung@bea.com - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.apt.core.internal.env;
12
13
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.jdt.apt.core.env.EclipseAnnotationProcessorEnvironment;
16 import org.eclipse.jdt.apt.core.env.Phase;
17 import org.eclipse.jdt.apt.core.internal.AptPlugin;
18 import org.eclipse.jdt.apt.core.internal.env.MessagerImpl.Severity;
19 import org.eclipse.jdt.core.ICompilationUnit;
20 import org.eclipse.jdt.core.IJavaProject;
21 import org.eclipse.jdt.core.compiler.ReconcileContext;
22 import org.eclipse.jdt.core.dom.ASTRequestor;
23 import org.eclipse.jdt.core.dom.CompilationUnit;
24 import org.eclipse.jdt.core.dom.IBinding;
25
26 import com.sun.mirror.apt.Filer;
27
28 public class ReconcileEnv extends AbstractCompilationEnv implements EclipseAnnotationProcessorEnvironment{
29     
30     /** The compilation unit being reconciled */
31     private final ICompilationUnit _workingCopy;
32     
33     private final ReconcileContext _context;
34     
35     /**
36      * Create a reconcile environment from the given context.
37      * @param reconcileContext
38      * @return the reconcile environment or null if creation failed.
39      */

40     static ReconcileEnv newEnv(ReconcileContext context)
41     {
42         final ICompilationUnit workingCopy = context.getWorkingCopy();
43         IJavaProject javaProject = workingCopy.getJavaProject();
44         final IFile file = (IFile)workingCopy.getResource();
45         return new ReconcileEnv(context, workingCopy, file, javaProject);
46     }
47     
48     private ReconcileEnv(
49             ReconcileContext context,
50             ICompilationUnit workingCopy,
51             IFile file,
52             IJavaProject javaProj)
53     {
54         // See bug 133744: calling ReconcileContext.getAST3() here would result in
55
// a typesystem whose types are not comparable with the types we get after
56
// openPipeline(). Instead, we start the env with an EMPTY_AST_UNIT, and
57
// replace it with the real thing inside the openPipeline() ASTRequestor's
58
// acceptAST() callback.
59
super(EMPTY_AST_UNIT, file, javaProj, Phase.RECONCILE);
60         _context = context;
61         _workingCopy = workingCopy;
62         if (AptPlugin.DEBUG_COMPILATION_ENV) AptPlugin.trace(
63                 "constructed " + this + " for " + _workingCopy.getElementName()); //$NON-NLS-1$ //$NON-NLS-2$
64
}
65     
66     void addMessage(
67             IFile resource,
68             int start,
69             int end,
70             Severity severity,
71             String JavaDoc msg,
72             int line,
73             String JavaDoc[] arguments)
74     {
75         checkValid();
76         
77         if( resource == null )
78             resource = getFile();
79         
80         assert resource != null : "don't know about the current resource"; //$NON-NLS-1$
81

82         // not going to post any markers to resource outside of the one we are currently
83
// processing during reconcile phase.
84
if( resource != null && !resource.equals( getFile() ) )
85             return;
86         
87         _problems.add(createProblem(resource, start, end, severity, msg, line, arguments));
88     }
89     
90     public CompilationUnit getASTFrom(final IFile file){
91         if( _file.equals(file) )
92             return _astRoot;
93         else
94             return null;
95     }
96     
97     public void addTypeDependency(String JavaDoc fullyQualifiedTypeName) {
98         // do not store type dependency during reconcile.
99
return;
100     }
101     
102     public Filer getFiler(){
103         return new ReconcileFilerImpl(this);
104     }
105     
106     void openPipeline() {
107         _requestor = new CallbackRequestor();
108         createASTs(_javaProject, new ICompilationUnit[]{_workingCopy}, _requestor);
109     }
110     
111     /* (non-Javadoc)
112      * @see org.eclipse.jdt.apt.core.internal.env.AbstractCompilationEnv#close()
113      */

114     @Override JavaDoc
115     public void close() {
116         // Notify the compiler that the working copy was modified, so that the editor
117
// and any downstream compilationParticipants will get a recomputed AST,
118
// taking into account any changes to generated types.
119
//TODO: don't call unless generated types were changed - WSH 10/06
120
_context.resetAST();
121         super.close();
122     }
123
124     class CallbackRequestor extends ASTRequestor {
125         @Override JavaDoc
126         public void acceptAST(ICompilationUnit source, CompilationUnit ast) {
127             // Use the AST from the pipeline's parser, not the one from ReconcileContext.getAST3().
128
_astRoot = ast;
129         }
130
131         @Override JavaDoc
132         public void acceptBinding(String JavaDoc bindingKey, IBinding binding) {
133             // This is called when the only binding has been passed, hence it is time
134
// to dispatch
135
_callback.run(ReconcileEnv.this);
136
137         }
138     }
139
140     /* package scope */
141     ICompilationUnit getCompilationUnit() {
142         return _workingCopy;
143     }
144
145 }
146
Popular Tags