1 11 12 package org.eclipse.jdt.apt.core.internal; 13 14 import java.text.SimpleDateFormat ; 15 import java.util.Date ; 16 import java.util.HashMap ; 17 import java.util.Map ; 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 PLUGIN_ID = "org.eclipse.jdt.apt.core"; 33 public static boolean DEBUG = false; 35 public final static String APT_DEBUG_OPTION = AptPlugin.PLUGIN_ID + "/debug"; public static boolean DEBUG_GFM = false; 37 public final static String APT_DEBUG_GFM_OPTION = AptPlugin.APT_DEBUG_OPTION + "/generatedFiles"; public static boolean DEBUG_GFM_MAPS = false; 39 public final static String APT_DEBUG_GFM_MAPS_OPTION = AptPlugin.APT_DEBUG_OPTION + "/generatedFileMaps"; public static boolean DEBUG_COMPILATION_ENV = false; 41 public final static String APT_COMPILATION_ENV_OPTION = AptPlugin.APT_DEBUG_OPTION + "/compilationEnv"; 43 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 APT_BATCH_PROCESSOR_PROBLEM_MARKER = PLUGIN_ID + ".marker"; 52 public static final String APT_LOADER_PROBLEM_MARKER = PLUGIN_ID + ".buildproblem"; 54 public static final String APT_CONFIG_PROBLEM_MARKER = PLUGIN_ID + ".configproblem"; 56 public static final String APT_COMPILATION_PROBLEM_MARKER = PLUGIN_ID + ".compile.problem"; 58 private static final SimpleDateFormat TRACE_DATE_FORMAT = new SimpleDateFormat ("HH:mm:ss.SSS"); 60 private static AptPlugin thePlugin = null; 62 65 private static Class <?> _java6ProcessorClass; 66 67 private static final Map <IJavaProject,AptProject> PROJECT_MAP = 70 new HashMap <IJavaProject,AptProject>(); 71 72 public static final String JAVA5_FACTORY_NAME = "com.sun.mirror.apt.AnnotationProcessorFactory"; public static final String JAVA6_FACTORY_NAME = "javax.annotation.processing.Processor"; 76 public void start(BundleContext context) throws Exception { 77 thePlugin = this; 78 super.start(context); 79 initDebugTracing(); 80 82 try { 83 _java6ProcessorClass = Class.forName(JAVA6_FACTORY_NAME); 84 } catch (Throwable e) { 85 } 87 88 AptConfig.initialize(); 89 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"); } 104 105 public void stop(BundleContext context) throws Exception { 106 super.stop(context); 107 } 108 109 public static AptPlugin getPlugin() { 110 return thePlugin; 111 } 112 113 117 public static void log(IStatus status) { 118 thePlugin.getLog().log(status); 119 } 120 121 125 public static void log(Throwable e, String message) { 126 log(new Status(IStatus.ERROR, PLUGIN_ID, STATUS_EXCEPTION, message, e)); 127 } 128 129 133 public static void logWarning(Throwable e, String message) { 134 log(createWarningStatus(e, message)); 135 } 136 137 141 public static Status createStatus(Throwable e, String message) { 142 return new Status(IStatus.ERROR, PLUGIN_ID, STATUS_EXCEPTION, message, e); 143 } 144 145 149 public static Status createWarningStatus(Throwable e, String message) { 150 return new Status(IStatus.WARNING, PLUGIN_ID, STATUS_EXCEPTION, message, e); 151 } 152 153 157 public static Status createInfoStatus(Throwable e, String message) { 158 return new Status(IStatus.INFO, PLUGIN_ID, STATUS_EXCEPTION, message, e); 159 } 160 161 private void initDebugTracing() { 162 String option = Platform.getDebugOption(APT_DEBUG_OPTION); 163 if(option != null) DEBUG = option.equalsIgnoreCase("true") ; option = Platform.getDebugOption(APT_DEBUG_GFM_OPTION); 165 if(option != null) DEBUG_GFM = option.equalsIgnoreCase("true") ; option = Platform.getDebugOption(APT_DEBUG_GFM_MAPS_OPTION); 167 if(option != null) DEBUG_GFM_MAPS = option.equalsIgnoreCase("true") ; } 169 170 public static void trace(final String msg){ 171 if (DEBUG) { 172 StringBuffer sb = new StringBuffer (); 173 sb.append('['); 174 synchronized(TRACE_DATE_FORMAT) { 176 sb.append(TRACE_DATE_FORMAT.format(new Date ())); 177 } 178 sb.append('-'); 179 String 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 197 public static void trace(String msg, Throwable 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 237 public static boolean canRunJava6Processors() { 238 if (_java6ProcessorClass == null) 239 return false; 240 return Platform.getBundle("org.eclipse.jdt.compiler.apt") != null; } 242 243 247 public static Class <?> getJava6ProcessorClass() { 248 return _java6ProcessorClass; 249 } 250 251 } 252 | Popular Tags |