001: /*******************************************************************************
002: * Copyright (c) 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: * wharley@bea.com - initial API and implementation
010: *
011: *******************************************************************************/package org.eclipse.jdt.internal.compiler.apt.dispatch;
012:
013: import java.util.ArrayList;
014: import java.util.List;
015: import java.util.Map;
016:
017: import javax.annotation.processing.Filer;
018: import javax.annotation.processing.Messager;
019: import javax.annotation.processing.ProcessingEnvironment;
020: import javax.lang.model.SourceVersion;
021: import javax.lang.model.util.Elements;
022: import javax.lang.model.util.Types;
023:
024: import org.eclipse.jdt.internal.compiler.Compiler;
025: import org.eclipse.jdt.internal.compiler.apt.model.ElementsImpl;
026: import org.eclipse.jdt.internal.compiler.apt.model.Factory;
027: import org.eclipse.jdt.internal.compiler.apt.model.TypesImpl;
028: import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
029: import org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment;
030: import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
031:
032: /**
033: * Implementation of ProcessingEnvironment that is common to batch and IDE environments.
034: */
035: public abstract class BaseProcessingEnvImpl implements
036: ProcessingEnvironment {
037:
038: // Initialized in subclasses:
039: protected Filer _filer;
040: protected Messager _messager;
041: protected Map<String, String> _processorOptions;
042: protected Compiler _compiler;
043:
044: // Initialized in this base class:
045: protected Elements _elementUtils;
046: protected Types _typeUtils;
047: private List<ICompilationUnit> _addedUnits;
048: private List<ReferenceBinding> _addedClassFiles;
049: private List<ICompilationUnit> _deletedUnits;
050: private boolean _errorRaised;
051: private Factory _factory;
052:
053: public BaseProcessingEnvImpl() {
054: _addedUnits = new ArrayList<ICompilationUnit>();
055: _addedClassFiles = new ArrayList<ReferenceBinding>();
056: _deletedUnits = new ArrayList<ICompilationUnit>();
057: _elementUtils = new ElementsImpl(this );
058: _typeUtils = new TypesImpl(this );
059: _factory = new Factory(this );
060: _errorRaised = false;
061: }
062:
063: public void addNewUnit(ICompilationUnit unit) {
064: _addedUnits.add(unit);
065: }
066:
067: public void addNewClassFile(ReferenceBinding binding) {
068: _addedClassFiles.add(binding);
069: }
070:
071: public Compiler getCompiler() {
072: return _compiler;
073: }
074:
075: public ICompilationUnit[] getDeletedUnits() {
076: ICompilationUnit[] result = new ICompilationUnit[_deletedUnits
077: .size()];
078: _deletedUnits.toArray(result);
079: return result;
080: }
081:
082: public ICompilationUnit[] getNewUnits() {
083: ICompilationUnit[] result = new ICompilationUnit[_addedUnits
084: .size()];
085: _addedUnits.toArray(result);
086: return result;
087: }
088:
089: @Override
090: public Elements getElementUtils() {
091: return _elementUtils;
092: }
093:
094: @Override
095: public Filer getFiler() {
096: return _filer;
097: }
098:
099: @Override
100: public Messager getMessager() {
101: return _messager;
102: }
103:
104: @Override
105: public Map<String, String> getOptions() {
106: return _processorOptions;
107: }
108:
109: @Override
110: public Types getTypeUtils() {
111: return _typeUtils;
112: }
113:
114: public LookupEnvironment getLookupEnvironment() {
115: return _compiler.lookupEnvironment;
116: }
117:
118: @Override
119: public SourceVersion getSourceVersion() {
120: // As of this writing, RELEASE_6 is the highest level available.
121: // It is also the lowest level for which this code can possibly
122: // be called. When Java 7 is released, this method will need to
123: // return a value based on _compiler.options.sourceLevel.
124: return SourceVersion.RELEASE_6;
125: }
126:
127: /**
128: * Called when AnnotationProcessorManager has retrieved the list of
129: * newly generated compilation units (ie, once per round)
130: */
131: public void reset() {
132: _addedUnits.clear();
133: _addedClassFiles.clear();
134: _deletedUnits.clear();
135: }
136:
137: /**
138: * Has an error been raised in any of the rounds of processing in this build?
139: * @return
140: */
141: public boolean errorRaised() {
142: return _errorRaised;
143: }
144:
145: /**
146: * Set or clear the errorRaised flag. Typically this will be set by the Messager
147: * when an error has been raised, and it will never be cleared.
148: */
149: public void setErrorRaised(boolean b) {
150: _errorRaised = true;
151: }
152:
153: public Factory getFactory() {
154: return _factory;
155: }
156:
157: public ReferenceBinding[] getNewClassFiles() {
158: ReferenceBinding[] result = new ReferenceBinding[_addedClassFiles
159: .size()];
160: _addedClassFiles.toArray(result);
161: return result;
162: }
163:
164: }
|