View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jci.compilers;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Locale;
25  import java.util.Map;
26  import java.util.StringTokenizer;
27  
28  import org.apache.commons.jci.problems.CompilationProblem;
29  import org.apache.commons.jci.readers.ResourceReader;
30  import org.apache.commons.jci.stores.ResourceStore;
31  import org.apache.commons.jci.utils.ConversionUtils;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.eclipse.jdt.core.compiler.IProblem;
35  import org.eclipse.jdt.internal.compiler.ClassFile;
36  import org.eclipse.jdt.internal.compiler.CompilationResult;
37  import org.eclipse.jdt.internal.compiler.Compiler;
38  import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
39  import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
40  import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
41  import org.eclipse.jdt.internal.compiler.IProblemFactory;
42  import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
43  import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
44  import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
45  import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
46  import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
47  import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
48  import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
49  
50  /**
51   * Eclipse compiler implemenation
52   *
53   * @author tcurdt
54   */
55  public final class EclipseJavaCompiler extends AbstractJavaCompiler {
56  
57      private final Log log = LogFactory.getLog(EclipseJavaCompiler.class);
58      private final EclipseJavaCompilerSettings defaultSettings;
59  
60      public EclipseJavaCompiler() {
61          this(new EclipseJavaCompilerSettings());
62      }
63  
64      public EclipseJavaCompiler( final Map<String, String> pSettings ) {
65          defaultSettings = new EclipseJavaCompilerSettings(pSettings);
66      }
67  
68      public EclipseJavaCompiler( final EclipseJavaCompilerSettings pSettings ) {
69          defaultSettings = pSettings;
70      }
71  
72      final class CompilationUnit implements ICompilationUnit {
73  
74          final private String clazzName;
75          final private String fileName;
76          final private char[] typeName;
77          final private char[][] packageName;
78          final private ResourceReader reader;
79  
80          CompilationUnit( final ResourceReader pReader, final String pSourceFile ) {
81              reader = pReader;
82              clazzName = ConversionUtils.convertResourceToClassName(pSourceFile);
83              fileName = pSourceFile;
84              int dot = clazzName.lastIndexOf('.');
85              if (dot > 0) {
86                  typeName = clazzName.substring(dot + 1).toCharArray();
87              } else {
88                  typeName = clazzName.toCharArray();
89              }
90  
91              log.debug("className=" + clazzName);
92              log.debug("fileName=" + fileName);
93              log.debug("typeName=" + new String(typeName));
94  
95              final StringTokenizer izer = new StringTokenizer(clazzName, ".");
96              packageName = new char[izer.countTokens() - 1][];
97              for (int i = 0; i < packageName.length; i++) {
98                  packageName[i] = izer.nextToken().toCharArray();
99                  log.debug("package[" + i + "]=" + new String(packageName[i]));
100             }
101         }
102 
103         public char[] getFileName() {
104             return fileName.toCharArray();
105         }
106 
107         public char[] getContents() {
108             final byte[] content = reader.getBytes(fileName);
109 
110             if (content == null) {
111                 return null;
112                 //throw new RuntimeException("resource " + fileName + " could not be found");
113             }
114 
115             return new String(content).toCharArray();
116         }
117 
118         public char[] getMainTypeName() {
119             return typeName;
120         }
121 
122         public char[][] getPackageName() {
123             return packageName;
124         }
125 
126         public boolean ignoreOptionalProblems() {
127             return false;
128         }
129     }
130 
131 
132     public org.apache.commons.jci.compilers.CompilationResult compile(
133             final String[] pSourceFiles,
134             final ResourceReader pReader,
135             final ResourceStore pStore,
136             final ClassLoader pClassLoader,
137             final JavaCompilerSettings pSettings
138             ) {
139 
140         final Map<String, String> settingsMap = new EclipseJavaCompilerSettings(pSettings).toNativeSettings();
141 
142         final Collection<CompilationProblem> problems = new ArrayList<CompilationProblem>();
143 
144         final ICompilationUnit[] compilationUnits = new ICompilationUnit[pSourceFiles.length];
145         for (int i = 0; i < compilationUnits.length; i++) {
146             final String sourceFile = pSourceFiles[i];
147 
148             if (pReader.isAvailable(sourceFile)) {
149                 compilationUnits[i] = new CompilationUnit(pReader, sourceFile);
150                 log.debug("compiling " + sourceFile);
151             } else {
152                 // log.error("source not found " + sourceFile);
153 
154                 final CompilationProblem problem = new CompilationProblem() {
155 
156                     public int getEndColumn() {
157                         return 0;
158                     }
159 
160                     public int getEndLine() {
161                         return 0;
162                     }
163 
164                     public String getFileName() {
165                         return sourceFile;
166                     }
167 
168                     public String getMessage() {
169                         return "Source " + sourceFile + " could not be found";
170                     }
171 
172                     public int getStartColumn() {
173                         return 0;
174                     }
175 
176                     public int getStartLine() {
177                         return 0;
178                     }
179 
180                     public boolean isError() {
181                         return true;
182                     }
183 
184                     @Override
185                     public String toString() {
186                         return getMessage();
187                     }
188                 };
189 
190                 if (problemHandler != null) {
191                     problemHandler.handle(problem);
192                 }
193 
194                 problems.add(problem);
195             }
196         }
197 
198         if (problems.size() > 0) {
199             final CompilationProblem[] result = new CompilationProblem[problems.size()];
200             problems.toArray(result);
201             return new org.apache.commons.jci.compilers.CompilationResult(result);
202         }
203 
204         final IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems();
205         final IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault());
206         final INameEnvironment nameEnvironment = new INameEnvironment() {
207 
208             public NameEnvironmentAnswer findType( final char[][] pCompoundTypeName ) {
209                 final StringBuilder result = new StringBuilder();
210                 for (int i = 0; i < pCompoundTypeName.length; i++) {
211                     if (i != 0) {
212                         result.append('.');
213                     }
214                     result.append(pCompoundTypeName[i]);
215                 }
216 
217                 //log.debug("finding compoundTypeName=" + result.toString());
218 
219                 return findType(result.toString());
220             }
221 
222             public NameEnvironmentAnswer findType( final char[] pTypeName, final char[][] pPackageName ) {
223                 final StringBuilder result = new StringBuilder();
224                 for (int i = 0; i < pPackageName.length; i++) {
225                     result.append(pPackageName[i]);
226                     result.append('.');
227                 }
228 
229 //                log.debug("finding typeName=" + new String(typeName) + " packageName=" + result.toString());
230 
231                 result.append(pTypeName);
232                 return findType(result.toString());
233             }
234 
235             private NameEnvironmentAnswer findType( final String pClazzName ) {
236 
237                 if (isPackage(pClazzName)) {
238                     return null;
239                 }
240 
241                 log.debug("finding " + pClazzName);
242 
243                 final String resourceName = ConversionUtils.convertClassToResourcePath(pClazzName);
244 
245                 final byte[] clazzBytes = pStore.read(resourceName);
246                 if (clazzBytes != null) {
247                     log.debug("loading from store " + pClazzName);
248 
249                     final char[] fileName = pClazzName.toCharArray();
250                     try {
251                         final ClassFileReader classFileReader = new ClassFileReader(clazzBytes, fileName, true);
252                         return new NameEnvironmentAnswer(classFileReader, null);
253                     } catch (final ClassFormatException e) {
254                         log.error("wrong class format", e);
255                         return null;
256                     }
257                 }
258 
259                 log.debug("not in store " + pClazzName);
260 
261                 final InputStream is = pClassLoader.getResourceAsStream(resourceName);
262                 if (is == null) {
263                     log.debug("class " + pClazzName + " not found");
264                     return null;
265                 }
266 
267                 final byte[] buffer = new byte[8192];
268                 final ByteArrayOutputStream baos = new ByteArrayOutputStream(buffer.length);
269                 int count;
270                 try {
271                     while ((count = is.read(buffer, 0, buffer.length)) > 0) {
272                         baos.write(buffer, 0, count);
273                     }
274                     baos.flush();
275                     final char[] fileName = pClazzName.toCharArray();
276                     final ClassFileReader classFileReader = new ClassFileReader(baos.toByteArray(), fileName, true);
277                     return new NameEnvironmentAnswer(classFileReader, null);
278                 } catch (final IOException e) {
279                     log.error("could not read class", e);
280                     return null;
281                 } catch (final ClassFormatException e) {
282                     log.error("wrong class format", e);
283                     return null;
284                 } finally {
285                     try {
286                         baos.close();
287                     } catch (final IOException oe) {
288                         log.error("could not close output stream", oe);
289                     }
290                     try {
291                         is.close();
292                     } catch (final IOException ie) {
293                         log.error("could not close input stream", ie);
294                     }
295                 }
296             }
297 
298             private boolean isPackage( final String pClazzName ) {
299 
300                 // reject this early as it is cheap
301                 if (pClazzName.contains("-")) { // "-" is not valid in package names
302                     return false;
303                 }
304 
305                 final InputStream is = pClassLoader.getResourceAsStream(ConversionUtils.convertClassToResourcePath(pClazzName));
306                 if (is != null) {
307                     log.debug("found the class for " + pClazzName + "- no package");
308                     try {
309                         is.close();
310                     } catch (final IOException ie) {
311                         log.error("could not close input stream", ie);
312                     } 
313                     return false;
314                 }
315 
316                 // FIXME: this should not be tied to the extension
317                 final String source = pClazzName.replace('.', '/') + ".java";
318                 if (pReader.isAvailable(source)) {
319                     log.debug("found the source " + source + " for " + pClazzName + " - no package ");
320                     return false;
321                 }
322 
323                 /*
324                  * See https://issues.apache.org/jira/browse/JCI-59
325                  * At present, the code assumes that anything else is a package name
326                  * This is wrong, as for example jci.AdditionalTopLevel is not a package name.
327                  * It's not clear how to fix this in general.
328                  * It would seem to need access to the input classpath and/or the generated classes.
329                  */
330                 return true;
331             }
332 
333             public boolean isPackage( char[][] parentPackageName, char[] pPackageName ) {
334                 final StringBuilder result = new StringBuilder();
335                 if (parentPackageName != null) {
336                     for (int i = 0; i < parentPackageName.length; i++) {
337                         if (i != 0) {
338                             result.append('.');
339                         }
340                         result.append(parentPackageName[i]);
341                     }
342                 }
343 
344 //                log.debug("isPackage parentPackageName=" + result.toString() + " packageName=" + new String(packageName));
345 
346                 if (parentPackageName != null && parentPackageName.length > 0) {
347                     result.append('.');
348                 }
349                 result.append(pPackageName);
350                 return isPackage(result.toString());
351             }
352 
353             public void cleanup() {
354                 log.debug("cleanup");
355             }
356         };
357 
358         final ICompilerRequestor compilerRequestor = new ICompilerRequestor() {
359             public void acceptResult( final CompilationResult pResult ) {
360                 if (pResult.hasProblems()) {
361                     for (IProblem iproblem : pResult.getProblems()) {
362                         final CompilationProblem problem = new EclipseCompilationProblem(iproblem);
363                         if (problemHandler != null) {
364                             problemHandler.handle(problem);
365                         }
366                         problems.add(problem);
367                     }
368                 }
369                 if (!pResult.hasErrors()) {
370                     final ClassFile[] clazzFiles = pResult.getClassFiles();
371                     for (ClassFile clazzFile : clazzFiles) {
372                         final char[][] compoundName = clazzFile.getCompoundName();
373                         final StringBuilder clazzName = new StringBuilder();
374                         for (int j = 0; j < compoundName.length; j++) {
375                             if (j != 0) {
376                                 clazzName.append('.');
377                             }
378                             clazzName.append(compoundName[j]);
379                         }
380                         pStore.write(clazzName.toString().replace('.', '/') + ".class", clazzFile.getBytes());
381                     }
382                 }
383             }
384         };
385 
386         final Compiler compiler = new Compiler(nameEnvironment, policy, new CompilerOptions(settingsMap), compilerRequestor, problemFactory);
387 
388         compiler.compile(compilationUnits);
389 
390         final CompilationProblem[] result = new CompilationProblem[problems.size()];
391         problems.toArray(result);
392         return new org.apache.commons.jci.compilers.CompilationResult(result);
393     }
394 
395     public JavaCompilerSettings createDefaultSettings() {
396         return new EclipseJavaCompilerSettings(defaultSettings);
397     }
398 }