KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.internal.compiler.apt.dispatch;
13
14 import java.lang.reflect.Field JavaDoc;
15 import java.nio.charset.Charset JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.LinkedHashMap JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Map JavaDoc;
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 /**
31  * The implementation of ProcessingEnvironment that is used when compilation is
32  * driven by the command line or by the Tool interface. This environment uses
33  * the JavaFileManager provided by the compiler.
34  * @see org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeProcessingEnvImpl
35  */

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 JavaDoc[] commandLineArguments)
44     {
45         super();
46         _compilerOwner = batchCompiler;
47         _compiler = batchCompiler.batchCompiler;
48         _dispatchManager = dispatchManager;
49         Class JavaDoc<?> c = null;
50         try {
51             c = Class.forName("org.eclipse.jdt.internal.compiler.tool.EclipseCompilerImpl"); //$NON-NLS-1$
52
} catch (ClassNotFoundException JavaDoc e) {
53             // ignore
54
}
55         Field JavaDoc field = null;
56         JavaFileManager javaFileManager = null;
57         if (c != null) {
58             try {
59                 field = c.getField("fileManager"); //$NON-NLS-1$
60
} catch (SecurityException JavaDoc e) {
61                 // ignore
62
} catch (IllegalArgumentException JavaDoc e) {
63                 // ignore
64
} catch (NoSuchFieldException JavaDoc e) {
65                 // ignore
66
}
67         }
68         if (field != null) {
69             try {
70                 javaFileManager = (JavaFileManager) field.get(batchCompiler);
71             } catch (IllegalArgumentException JavaDoc e) {
72                 // ignore
73
} catch (IllegalAccessException JavaDoc e) {
74                 // ignore
75
}
76         }
77         if (javaFileManager != null) {
78             _fileManager = javaFileManager;
79         } else {
80             String JavaDoc encoding = (String JavaDoc) batchCompiler.options.get(CompilerOptions.OPTION_Encoding);
81             Charset JavaDoc charset = encoding != null ? Charset.forName(encoding) : null;
82             JavaFileManager manager = new EclipseFileManager(batchCompiler.compilerLocale, charset);
83             ArrayList JavaDoc<String JavaDoc> options = new ArrayList JavaDoc<String JavaDoc>();
84             for (String JavaDoc argument : commandLineArguments) {
85                 options.add(argument);
86             }
87             for (Iterator JavaDoc<String JavaDoc> 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     /**
98      * Parse the -A command line arguments so that they can be delivered to
99      * processors with {@link ProcessingEnvironment#getOptions(). In Sun's Java 6
100      * version of javac, unlike in the Java 5 apt tool, only the -A options are
101      * passed to processors, not the other command line options; that behavior
102      * is repeated here.
103      * @param args the equivalent of the args array from the main() method.
104      * @return a map of key to value, or key to null if there is no value for
105      * a particular key. The "-A" is stripped from the key, so a command-line
106      * argument like "-Afoo=bar" will result in an entry with key "foo" and
107      * value "bar".
108      */

109     private Map JavaDoc<String JavaDoc, String JavaDoc> parseProcessorOptions(String JavaDoc[] args) {
110         Map JavaDoc<String JavaDoc, String JavaDoc> options = new LinkedHashMap JavaDoc<String JavaDoc, String JavaDoc>();
111         for (String JavaDoc arg : args) {
112             if (!arg.startsWith("-A")) { //$NON-NLS-1$
113
continue;
114             }
115             int equals = arg.indexOf('=');
116             if (equals == 2) {
117                 // option begins "-A=" - not valid
118
Exception JavaDoc e = new IllegalArgumentException JavaDoc("-A option must have a key before the equals sign"); //$NON-NLS-1$
119
throw new AbortCompilation(null, e);
120             }
121             if (equals == arg.length() - 1) {
122                 // option ends with "=" - not valid
123
options.put(arg.substring(2, equals), null);
124             } else if (equals == -1) {
125                 // no value
126
options.put(arg.substring(2), null);
127             } else {
128                 // value and key
129
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 JavaDoc
140     public Locale JavaDoc getLocale() {
141         return _compilerOwner.compilerLocale;
142     }
143
144 }
145
Popular Tags