KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > apt > dispatch > BaseProcessingEnvImpl


1 /*******************************************************************************
2  * Copyright (c) 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  * wharley@bea.com - initial API and implementation
10  *
11  *******************************************************************************/

12
13 package org.eclipse.jdt.internal.compiler.apt.dispatch;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import javax.annotation.processing.Filer;
20 import javax.annotation.processing.Messager;
21 import javax.annotation.processing.ProcessingEnvironment;
22 import javax.lang.model.SourceVersion;
23 import javax.lang.model.util.Elements;
24 import javax.lang.model.util.Types;
25
26 import org.eclipse.jdt.internal.compiler.Compiler;
27 import org.eclipse.jdt.internal.compiler.apt.model.ElementsImpl;
28 import org.eclipse.jdt.internal.compiler.apt.model.Factory;
29 import org.eclipse.jdt.internal.compiler.apt.model.TypesImpl;
30 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
31 import org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment;
32 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
33
34 /**
35  * Implementation of ProcessingEnvironment that is common to batch and IDE environments.
36  */

37 public abstract class BaseProcessingEnvImpl implements ProcessingEnvironment {
38
39     // Initialized in subclasses:
40
protected Filer _filer;
41     protected Messager _messager;
42     protected Map JavaDoc<String JavaDoc, String JavaDoc> _processorOptions;
43     protected Compiler JavaDoc _compiler;
44     
45     // Initialized in this base class:
46
protected Elements _elementUtils;
47     protected Types _typeUtils;
48     private List JavaDoc<ICompilationUnit> _addedUnits;
49     private List JavaDoc<ReferenceBinding> _addedClassFiles;
50     private List JavaDoc<ICompilationUnit> _deletedUnits;
51     private boolean _errorRaised;
52     private Factory _factory;
53
54     public BaseProcessingEnvImpl() {
55         _addedUnits = new ArrayList JavaDoc<ICompilationUnit>();
56         _addedClassFiles = new ArrayList JavaDoc<ReferenceBinding>();
57         _deletedUnits = new ArrayList JavaDoc<ICompilationUnit>();
58         _elementUtils = new ElementsImpl(this);
59         _typeUtils = new TypesImpl(this);
60         _factory = new Factory(this);
61         _errorRaised = false;
62     }
63
64     public void addNewUnit(ICompilationUnit unit) {
65         _addedUnits.add(unit);
66     }
67
68     public void addNewClassFile(ReferenceBinding binding) {
69         _addedClassFiles.add(binding);
70     }
71     
72     public Compiler JavaDoc getCompiler() {
73         return _compiler;
74     }
75
76     public ICompilationUnit[] getDeletedUnits() {
77         ICompilationUnit[] result = new ICompilationUnit[_deletedUnits.size()];
78         _deletedUnits.toArray(result);
79         return result;
80     }
81
82     public ICompilationUnit[] getNewUnits() {
83         ICompilationUnit[] result = new ICompilationUnit[_addedUnits.size()];
84         _addedUnits.toArray(result);
85         return result;
86     }
87
88     @Override JavaDoc
89     public Elements getElementUtils() {
90         return _elementUtils;
91     }
92
93     @Override JavaDoc
94     public Filer getFiler() {
95         return _filer;
96     }
97
98     @Override JavaDoc
99     public Messager getMessager() {
100         return _messager;
101     }
102     
103     @Override JavaDoc
104     public Map JavaDoc<String JavaDoc, String JavaDoc> getOptions() {
105         return _processorOptions;
106     }
107
108     @Override JavaDoc
109     public Types getTypeUtils() {
110         return _typeUtils;
111     }
112
113     public LookupEnvironment getLookupEnvironment() {
114         return _compiler.lookupEnvironment;
115     }
116
117     @Override JavaDoc
118     public SourceVersion getSourceVersion() {
119         // As of this writing, RELEASE_6 is the highest level available.
120
// It is also the lowest level for which this code can possibly
121
// be called. When Java 7 is released, this method will need to
122
// return a value based on _compiler.options.sourceLevel.
123
return SourceVersion.RELEASE_6;
124     }
125
126     /**
127      * Called when AnnotationProcessorManager has retrieved the list of
128      * newly generated compilation units (ie, once per round)
129      */

130     public void reset() {
131         _addedUnits.clear();
132         _addedClassFiles.clear();
133         _deletedUnits.clear();
134     }
135
136     /**
137      * Has an error been raised in any of the rounds of processing in this build?
138      * @return
139      */

140     public boolean errorRaised()
141     {
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     {
151         _errorRaised = true;
152     }
153
154     public Factory getFactory()
155     {
156         return _factory;
157     }
158
159     public ReferenceBinding[] getNewClassFiles() {
160         ReferenceBinding[] result = new ReferenceBinding[_addedClassFiles.size()];
161         _addedClassFiles.toArray(result);
162         return result;
163     }
164
165 }
166
Popular Tags