KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > AptPlugin


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  * wharley@bea.com - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.apt.core.internal;
13
14 import java.text.SimpleDateFormat JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.resources.IResourceChangeEvent;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.core.runtime.Plugin;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.jdt.apt.core.internal.generatedfile.GeneratedResourceChangeListener;
25 import org.eclipse.jdt.apt.core.util.AptConfig;
26 import org.eclipse.jdt.core.IJavaProject;
27 import org.eclipse.jdt.core.JavaCore;
28 import org.osgi.framework.BundleContext;
29
30 public class AptPlugin extends Plugin {
31     public static final String JavaDoc PLUGIN_ID = "org.eclipse.jdt.apt.core"; //$NON-NLS-1$
32

33     // Tracing options
34
public static boolean DEBUG = false;
35     public final static String JavaDoc APT_DEBUG_OPTION = AptPlugin.PLUGIN_ID + "/debug"; //$NON-NLS-1$
36
public static boolean DEBUG_GFM = false;
37     public final static String JavaDoc APT_DEBUG_GFM_OPTION = AptPlugin.APT_DEBUG_OPTION + "/generatedFiles"; //$NON-NLS-1$
38
public static boolean DEBUG_GFM_MAPS = false;
39     public final static String JavaDoc APT_DEBUG_GFM_MAPS_OPTION = AptPlugin.APT_DEBUG_OPTION + "/generatedFileMaps"; //$NON-NLS-1$
40
public static boolean DEBUG_COMPILATION_ENV = false;
41     public final static String JavaDoc APT_COMPILATION_ENV_OPTION = AptPlugin.APT_DEBUG_OPTION + "/compilationEnv"; //$NON-NLS-1$
42

43     /**
44      * Status IDs for system log entries. Must be unique per plugin.
45      */

46     public static final int STATUS_EXCEPTION = 1;
47     public static final int STATUS_NOTOOLSJAR = 2;
48     public static final int STATUS_CANTLOADPLUGINFACTORY = 3;
49     
50     public static final String JavaDoc APT_BATCH_PROCESSOR_PROBLEM_MARKER = PLUGIN_ID + ".marker"; //$NON-NLS-1$
51
/** Marker ID used for build problem, e.g., missing factory jar */
52     public static final String JavaDoc APT_LOADER_PROBLEM_MARKER = PLUGIN_ID + ".buildproblem"; //$NON-NLS-1$
53
/** Marker ID used for configuration problem, e.g generated source folder not on classpath */
54     public static final String JavaDoc APT_CONFIG_PROBLEM_MARKER = PLUGIN_ID + ".configproblem"; //$NON-NLS-1$
55
/** Marker ID used for posting problems during reconcile/build */
56     public static final String JavaDoc APT_COMPILATION_PROBLEM_MARKER = PLUGIN_ID + ".compile.problem"; //$NON-NLS-1$
57

58     private static final SimpleDateFormat JavaDoc TRACE_DATE_FORMAT = new SimpleDateFormat JavaDoc("HH:mm:ss.SSS"); //$NON-NLS-1$
59

60     private static AptPlugin thePlugin = null; // singleton object
61

62     /**
63      * The javax.annotation.processing.Processor class, which is only available on Java 6 and higher.
64      */

65     private static Class JavaDoc<?> _java6ProcessorClass;
66     
67     // Entries are added lazily in getAptProject(), and removed upon
68
// project deletion in deleteAptProject().
69
private static final Map JavaDoc<IJavaProject,AptProject> PROJECT_MAP =
70         new HashMap JavaDoc<IJavaProject,AptProject>();
71
72     // Qualified names of services for which these containers may provide implementations
73
public static final String JavaDoc JAVA5_FACTORY_NAME = "com.sun.mirror.apt.AnnotationProcessorFactory"; //$NON-NLS-1$
74
public static final String JavaDoc JAVA6_FACTORY_NAME = "javax.annotation.processing.Processor"; //$NON-NLS-1$
75

76     public void start(BundleContext context) throws Exception JavaDoc {
77         thePlugin = this;
78         super.start(context);
79         initDebugTracing();
80         // Do we have access to
81

82         try {
83             _java6ProcessorClass = Class.forName(JAVA6_FACTORY_NAME);
84         } catch (Throwable JavaDoc e) {
85             // ignore
86
}
87
88         AptConfig.initialize();
89         // DO NOT load extensions from the start() method. This can cause cycles in class loading
90
// Not to mention it is bad form to load stuff early.
91
// AnnotationProcessorFactoryLoader.getLoader();
92
// register resource-changed listener
93
// TODO: can move this into AptProject.
94
int mask =
95             IResourceChangeEvent.PRE_BUILD |
96             IResourceChangeEvent.PRE_CLOSE |
97             IResourceChangeEvent.PRE_DELETE |
98             IResourceChangeEvent.POST_CHANGE;
99         JavaCore.addPreProcessingResourceChangedListener( new GeneratedResourceChangeListener(), mask );
100         
101         if( DEBUG )
102             trace("registered resource change listener"); //$NON-NLS-1$
103
}
104
105     public void stop(BundleContext context) throws Exception JavaDoc {
106         super.stop(context);
107     }
108     
109     public static AptPlugin getPlugin() {
110         return thePlugin;
111     }
112
113     /**
114      * Log a status message to the platform log. Use this for reporting exceptions.
115      * @param status
116      */

117     public static void log(IStatus status) {
118         thePlugin.getLog().log(status);
119     }
120     
121     /**
122      * Convenience wrapper around log(IStatus), to log an exception
123      * with severity of ERROR.
124      */

125     public static void log(Throwable JavaDoc e, String JavaDoc message) {
126         log(new Status(IStatus.ERROR, PLUGIN_ID, STATUS_EXCEPTION, message, e));
127     }
128     
129     /**
130      * Convenience wrapper around log(IStatus), to log an exception
131      * with severity of WARNING.
132      */

133     public static void logWarning(Throwable JavaDoc e, String JavaDoc message) {
134         log(createWarningStatus(e, message));
135     }
136     
137     /**
138      * Convenience wrapper for rethrowing exceptions as CoreExceptions,
139      * with severity of ERROR.
140      */

141     public static Status createStatus(Throwable JavaDoc e, String JavaDoc message) {
142         return new Status(IStatus.ERROR, PLUGIN_ID, STATUS_EXCEPTION, message, e);
143     }
144     
145     /**
146      * Convenience wrapper for rethrowing exceptions as CoreExceptions,
147      * with severity of WARNING.
148      */

149     public static Status createWarningStatus(Throwable JavaDoc e, String JavaDoc message) {
150         return new Status(IStatus.WARNING, PLUGIN_ID, STATUS_EXCEPTION, message, e);
151     }
152     
153     /**
154      * Convenience wrapper for rethrowing exceptions as CoreExceptions,
155      * with severity of INFO.
156      */

157     public static Status createInfoStatus(Throwable JavaDoc e, String JavaDoc message) {
158         return new Status(IStatus.INFO, PLUGIN_ID, STATUS_EXCEPTION, message, e);
159     }
160     
161     private void initDebugTracing() {
162         String JavaDoc option = Platform.getDebugOption(APT_DEBUG_OPTION);
163         if(option != null) DEBUG = option.equalsIgnoreCase("true") ; //$NON-NLS-1$
164
option = Platform.getDebugOption(APT_DEBUG_GFM_OPTION);
165         if(option != null) DEBUG_GFM = option.equalsIgnoreCase("true") ; //$NON-NLS-1$
166
option = Platform.getDebugOption(APT_DEBUG_GFM_MAPS_OPTION);
167         if(option != null) DEBUG_GFM_MAPS = option.equalsIgnoreCase("true") ; //$NON-NLS-1$
168
}
169     
170     public static void trace(final String JavaDoc msg){
171         if (DEBUG) {
172             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
173             sb.append('[');
174             // SimpleDateFormat is not thread-safe, according to javadoc
175
synchronized(TRACE_DATE_FORMAT) {
176                 sb.append(TRACE_DATE_FORMAT.format(new Date JavaDoc()));
177             }
178             sb.append('-');
179             // Some threads have qualified type names; too long.
180
String JavaDoc threadName = Thread.currentThread().getName();
181             int dot = threadName.lastIndexOf('.');
182             if (dot < 0) {
183                 sb.append(threadName);
184             }
185             else {
186                 sb.append(threadName.substring(dot+1));
187             }
188             sb.append(']');
189             sb.append(msg);
190             System.out.println(sb);
191         }
192     }
193     
194     /**
195      * Convenience method to report an exception in debug trace mode.
196      */

197     public static void trace(String JavaDoc msg, Throwable JavaDoc t) {
198         trace(msg);
199         if (DEBUG) {
200             t.printStackTrace(System.out);
201         }
202     }
203     
204     private static AptProject getAptProject(IJavaProject javaProject, boolean create){
205         synchronized(PROJECT_MAP){
206             AptProject aptProject = PROJECT_MAP.get(javaProject);
207             if (aptProject != null) {
208                 return aptProject;
209             }
210             else{
211                 if( create ){
212                     aptProject = new AptProject(javaProject);
213                     PROJECT_MAP.put(javaProject, aptProject);
214                     return aptProject;
215                 }
216                 else
217                     return null;
218             }
219         }
220     }
221     
222     public static AptProject getAptProject(IJavaProject javaProject) {
223         return getAptProject(javaProject, true);
224     }
225     
226     public static void deleteAptProject(IJavaProject javaProject) {
227         synchronized (PROJECT_MAP) {
228             PROJECT_MAP.remove(javaProject);
229         }
230     }
231
232     /**
233      * True if we are running on a platform that supports Java 6 annotation processing,
234      * that is, if we are running on Java 6 or higher and the org.eclipse.jdt.compiler.apt
235      * plug-in is also present.
236      */

237     public static boolean canRunJava6Processors() {
238         if (_java6ProcessorClass == null)
239             return false;
240         return Platform.getBundle("org.eclipse.jdt.compiler.apt") != null; //$NON-NLS-1$
241
}
242     
243     /**
244      * The javax.annotation.processing.Processor class. This is only available on the
245      * Java 6 or higher platform, so it is loaded via reflection in {@link #start}.
246      */

247     public static Class JavaDoc<?> getJava6ProcessorClass() {
248         return _java6ProcessorClass;
249     }
250
251 }
252
Popular Tags