1 12 package org.eclipse.jdt.internal.compiler.apt.dispatch; 13 14 import java.lang.reflect.Field ; 15 import java.nio.charset.Charset ; 16 import java.util.ArrayList ; 17 import java.util.Collections ; 18 import java.util.Iterator ; 19 import java.util.LinkedHashMap ; 20 import java.util.Locale ; 21 import java.util.Map ; 22 23 import javax.tools.JavaFileManager; 24 25 import org.eclipse.jdt.internal.compiler.apt.util.EclipseFileManager; 26 import org.eclipse.jdt.internal.compiler.batch.Main; 27 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; 28 import org.eclipse.jdt.internal.compiler.problem.AbortCompilation; 29 30 36 public class BatchProcessingEnvImpl extends BaseProcessingEnvImpl { 37 38 protected final BaseAnnotationProcessorManager _dispatchManager; 39 protected final JavaFileManager _fileManager; 40 protected final Main _compilerOwner; 41 42 public BatchProcessingEnvImpl(BaseAnnotationProcessorManager dispatchManager, Main batchCompiler, 43 String [] commandLineArguments) 44 { 45 super(); 46 _compilerOwner = batchCompiler; 47 _compiler = batchCompiler.batchCompiler; 48 _dispatchManager = dispatchManager; 49 Class <?> c = null; 50 try { 51 c = Class.forName("org.eclipse.jdt.internal.compiler.tool.EclipseCompilerImpl"); } catch (ClassNotFoundException e) { 53 } 55 Field field = null; 56 JavaFileManager javaFileManager = null; 57 if (c != null) { 58 try { 59 field = c.getField("fileManager"); } catch (SecurityException e) { 61 } catch (IllegalArgumentException e) { 63 } catch (NoSuchFieldException e) { 65 } 67 } 68 if (field != null) { 69 try { 70 javaFileManager = (JavaFileManager) field.get(batchCompiler); 71 } catch (IllegalArgumentException e) { 72 } catch (IllegalAccessException e) { 74 } 76 } 77 if (javaFileManager != null) { 78 _fileManager = javaFileManager; 79 } else { 80 String encoding = (String ) batchCompiler.options.get(CompilerOptions.OPTION_Encoding); 81 Charset charset = encoding != null ? Charset.forName(encoding) : null; 82 JavaFileManager manager = new EclipseFileManager(batchCompiler.compilerLocale, charset); 83 ArrayList <String > options = new ArrayList <String >(); 84 for (String argument : commandLineArguments) { 85 options.add(argument); 86 } 87 for (Iterator <String > iterator = options.iterator(); iterator.hasNext(); ) { 88 manager.handleOption(iterator.next(), iterator); 89 } 90 _fileManager = manager; 91 } 92 _processorOptions = Collections.unmodifiableMap(parseProcessorOptions(commandLineArguments)); 93 _filer = new BatchFilerImpl(_dispatchManager, this); 94 _messager = new BatchMessagerImpl(this, _compilerOwner); 95 } 96 97 109 private Map <String , String > parseProcessorOptions(String [] args) { 110 Map <String , String > options = new LinkedHashMap <String , String >(); 111 for (String arg : args) { 112 if (!arg.startsWith("-A")) { continue; 114 } 115 int equals = arg.indexOf('='); 116 if (equals == 2) { 117 Exception e = new IllegalArgumentException ("-A option must have a key before the equals sign"); throw new AbortCompilation(null, e); 120 } 121 if (equals == arg.length() - 1) { 122 options.put(arg.substring(2, equals), null); 124 } else if (equals == -1) { 125 options.put(arg.substring(2), null); 127 } else { 128 options.put(arg.substring(2, equals), arg.substring(equals + 1)); 130 } 131 } 132 return options; 133 } 134 135 public JavaFileManager getFileManager() { 136 return _fileManager; 137 } 138 139 @Override 140 public Locale getLocale() { 141 return _compilerOwner.compilerLocale; 142 } 143 144 } 145 | Popular Tags |