KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > apt > pluggable > core > dispatch > IdeProcessingEnvImpl


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.apt.pluggable.core.dispatch;
14
15 import java.util.Collections JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Locale JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import javax.lang.model.element.Element;
21
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.runtime.Path;
25 import org.eclipse.jdt.apt.core.env.Phase;
26 import org.eclipse.jdt.apt.core.internal.AptCompilationParticipant;
27 import org.eclipse.jdt.apt.core.internal.AptPlugin;
28 import org.eclipse.jdt.apt.core.internal.AptProject;
29 import org.eclipse.jdt.apt.core.internal.generatedfile.FileGenerationResult;
30 import org.eclipse.jdt.apt.core.util.AptConfig;
31 import org.eclipse.jdt.core.IJavaProject;
32 import org.eclipse.jdt.internal.apt.pluggable.core.filer.IdeFilerImpl;
33 import org.eclipse.jdt.internal.compiler.Compiler;
34 import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl;
35 import org.eclipse.jdt.internal.compiler.apt.model.IElementInfo;
36
37 /**
38  * Implementation of ProcessingEnvironment when running inside IDE.
39  * The lifetime of this object corresponds to the lifetime of the
40  * {@link IdeAnnotationProcessorManager} that owns it.
41  * @see org.eclipse.jdt.internal.compiler.apt.dispatch.BatchProcessingEnvImpl
42  */

43 public abstract class IdeProcessingEnvImpl extends BaseProcessingEnvImpl {
44     
45     private final IdeAnnotationProcessorManager _dispatchManager;
46     private final IJavaProject _javaProject;
47     protected final AptProject _aptProject;
48
49     public IdeProcessingEnvImpl(IdeAnnotationProcessorManager dispatchManager,
50             IJavaProject jproject, Compiler JavaDoc compiler)
51     {
52         _dispatchManager = dispatchManager;
53         _javaProject = jproject;
54         _compiler = compiler;
55         _aptProject = AptPlugin.getAptProject(jproject);
56         _filer = new IdeFilerImpl(_dispatchManager, this);
57         _messager = new IdeMessagerImpl(_dispatchManager, this);
58     }
59     
60     /* (non-Javadoc)
61      * @see javax.annotation.processing.ProcessingEnvironment#getLocale()
62      */

63     @Override JavaDoc
64     public Locale JavaDoc getLocale() {
65         return Locale.getDefault();
66     }
67
68     @Override JavaDoc
69     public Map JavaDoc<String JavaDoc, String JavaDoc> getOptions() {
70         if (null == _processorOptions) {
71             // Java 5 processor options include items on the command line such as -s,
72
// -classpath, etc., but Java 6 options only include the options specified
73
// with -A, which will have been parsed into key/value pairs with no dash.
74
Map JavaDoc<String JavaDoc, String JavaDoc> allOptions = AptConfig.getProcessorOptions(_javaProject);
75             Map JavaDoc<String JavaDoc, String JavaDoc> procOptions = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
76             for (Map.Entry JavaDoc<String JavaDoc, String JavaDoc> entry : allOptions.entrySet()) {
77                 if (!entry.getKey().startsWith("-")) { //$NON-NLS-1$
78
procOptions.put(entry.getKey(), entry.getValue());
79                 }
80             }
81             procOptions.put("phase", getPhase().toString()); //$NON-NLS-1$
82
_processorOptions = Collections.unmodifiableMap(procOptions);
83         }
84         return _processorOptions;
85     }
86     
87     public AptProject getAptProject() {
88         return _aptProject;
89     }
90     
91     public IJavaProject getJavaProject() {
92         return _javaProject;
93     }
94     
95     public IProject getProject() {
96         return _javaProject.getProject();
97     }
98
99     /**
100      * @return whether this environment supports building or reconciling.
101      */

102     public abstract Phase getPhase();
103
104     /**
105      * Get the IFile that contains or represents the specified source element.
106      * If the element is a package, get the IFile corresponding to its
107      * package-info.java file. If the element is a top-level type, get the
108      * IFile corresponding to its type. If the element is a nested element
109      * of some sort (nested type, method, etc.) then get the IFile corresponding
110      * to the containing top-level type.
111      * If the element is not a source type at all, then return null.
112      * @param elem
113      * @return may be null
114      */

115     public IFile getEnclosingIFile(Element elem) {
116         // if this cast fails it could be that a non-Eclipse element got passed in somehow.
117
IElementInfo impl = (IElementInfo)elem;
118         String JavaDoc name = impl.getFileName();
119         if (name == null) {
120             return null;
121         }
122         // The name will be workspace-relative, e.g., /project/src/packages/File.java.
123
IFile file = _javaProject.getProject().getParent().getFile(new Path(name));
124         return file;
125     }
126
127     /**
128      * Inform the environment that a new Java file has been generated.
129      * @param result must be non-null
130      */

131     public void addNewUnit(FileGenerationResult result) {
132         AptCompilationParticipant.getInstance().addJava6GeneratedFile(result.getFile());
133         addNewUnit(_dispatchManager.findCompilationUnit(result.getFile()));
134     }
135     
136     /**
137      * Inform the environment that a new non-Java file has been generated.
138      * This file will not be submitted to a subsequent round of processing in
139      * the current build, even if the file happens to be in a source location
140      * and named with a Java-like name. However, its dependencies will be
141      * tracked in the same manner as Java files, e.g., it will be deleted
142      * if changes in source cause it to no longer be generated.
143      * @param file must be non-null
144      */

145     public void addNewResource(IFile file) {
146         AptCompilationParticipant.getInstance().addJava6GeneratedFile(file);
147     }
148
149     public boolean currentProcessorSupportsRTTG()
150     {
151         // Reconcile time type generation is not currently enabled for Java 6 processors
152
return false;
153     }
154
155 }
156
Popular Tags