001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 BEA Systems, Inc.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * tyeung@bea.com - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.apt.core.internal.env;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.jdt.apt.core.env.EclipseAnnotationProcessorEnvironment;
014: import org.eclipse.jdt.apt.core.env.Phase;
015: import org.eclipse.jdt.apt.core.internal.AptPlugin;
016: import org.eclipse.jdt.apt.core.internal.env.MessagerImpl.Severity;
017: import org.eclipse.jdt.core.ICompilationUnit;
018: import org.eclipse.jdt.core.IJavaProject;
019: import org.eclipse.jdt.core.compiler.ReconcileContext;
020: import org.eclipse.jdt.core.dom.ASTRequestor;
021: import org.eclipse.jdt.core.dom.CompilationUnit;
022: import org.eclipse.jdt.core.dom.IBinding;
023:
024: import com.sun.mirror.apt.Filer;
025:
026: public class ReconcileEnv extends AbstractCompilationEnv implements
027: EclipseAnnotationProcessorEnvironment {
028:
029: /** The compilation unit being reconciled */
030: private final ICompilationUnit _workingCopy;
031:
032: private final ReconcileContext _context;
033:
034: /**
035: * Create a reconcile environment from the given context.
036: * @param reconcileContext
037: * @return the reconcile environment or null if creation failed.
038: */
039: static ReconcileEnv newEnv(ReconcileContext context) {
040: final ICompilationUnit workingCopy = context.getWorkingCopy();
041: IJavaProject javaProject = workingCopy.getJavaProject();
042: final IFile file = (IFile) workingCopy.getResource();
043: return new ReconcileEnv(context, workingCopy, file, javaProject);
044: }
045:
046: private ReconcileEnv(ReconcileContext context,
047: ICompilationUnit workingCopy, IFile file,
048: IJavaProject javaProj) {
049: // See bug 133744: calling ReconcileContext.getAST3() here would result in
050: // a typesystem whose types are not comparable with the types we get after
051: // openPipeline(). Instead, we start the env with an EMPTY_AST_UNIT, and
052: // replace it with the real thing inside the openPipeline() ASTRequestor's
053: // acceptAST() callback.
054: super (EMPTY_AST_UNIT, file, javaProj, Phase.RECONCILE);
055: _context = context;
056: _workingCopy = workingCopy;
057: if (AptPlugin.DEBUG_COMPILATION_ENV)
058: AptPlugin
059: .trace("constructed " + this + " for " + _workingCopy.getElementName()); //$NON-NLS-1$ //$NON-NLS-2$
060: }
061:
062: void addMessage(IFile resource, int start, int end,
063: Severity severity, String msg, int line, String[] arguments) {
064: checkValid();
065:
066: if (resource == null)
067: resource = getFile();
068:
069: assert resource != null : "don't know about the current resource"; //$NON-NLS-1$
070:
071: // not going to post any markers to resource outside of the one we are currently
072: // processing during reconcile phase.
073: if (resource != null && !resource.equals(getFile()))
074: return;
075:
076: _problems.add(createProblem(resource, start, end, severity,
077: msg, line, arguments));
078: }
079:
080: public CompilationUnit getASTFrom(final IFile file) {
081: if (_file.equals(file))
082: return _astRoot;
083: else
084: return null;
085: }
086:
087: public void addTypeDependency(String fullyQualifiedTypeName) {
088: // do not store type dependency during reconcile.
089: return;
090: }
091:
092: public Filer getFiler() {
093: return new ReconcileFilerImpl(this );
094: }
095:
096: void openPipeline() {
097: _requestor = new CallbackRequestor();
098: createASTs(_javaProject,
099: new ICompilationUnit[] { _workingCopy }, _requestor);
100: }
101:
102: /* (non-Javadoc)
103: * @see org.eclipse.jdt.apt.core.internal.env.AbstractCompilationEnv#close()
104: */
105: @Override
106: public void close() {
107: // Notify the compiler that the working copy was modified, so that the editor
108: // and any downstream compilationParticipants will get a recomputed AST,
109: // taking into account any changes to generated types.
110: //TODO: don't call unless generated types were changed - WSH 10/06
111: _context.resetAST();
112: super .close();
113: }
114:
115: class CallbackRequestor extends ASTRequestor {
116: @Override
117: public void acceptAST(ICompilationUnit source,
118: CompilationUnit ast) {
119: // Use the AST from the pipeline's parser, not the one from ReconcileContext.getAST3().
120: _astRoot = ast;
121: }
122:
123: @Override
124: public void acceptBinding(String bindingKey, IBinding binding) {
125: // This is called when the only binding has been passed, hence it is time
126: // to dispatch
127: _callback.run(ReconcileEnv.this );
128:
129: }
130: }
131:
132: /* package scope */
133: ICompilationUnit getCompilationUnit() {
134: return _workingCopy;
135: }
136:
137: }
|