List of usage examples for org.eclipse.jdt.internal.compiler CompilationResult hasProblems
public boolean hasProblems()
From source file:de.plugins.eclipse.depclipse.testcommons.Util.java
License:Open Source License
/** * Returns the compilation errors / warnings for the given CompilationResult. * * @param compilationResult the compilation result * @param showCategory// w w w . j av a 2 s. co m * @param showWarningToken * @return String the problem log */ public static String getProblemLog(CompilationResult compilationResult, boolean showCategory, boolean showWarningToken) { StringBuffer buffer = new StringBuffer(100); if (compilationResult.hasProblems() || compilationResult.hasTasks()) { CategorizedProblem[] problems = compilationResult.getAllProblems(); int count = problems.length; int problemCount = 0; char[] unitSource = compilationResult.compilationUnit.getContents(); for (int i = 0; i < count; i++) { DefaultProblem problem = (DefaultProblem) problems[i]; if (problem != null) { if (problemCount == 0) buffer.append("----------\n"); problemCount++; buffer.append(problemCount + (problem.isError() ? ". ERROR" : ". WARNING")); buffer.append(" in " + new String(problem.getOriginatingFileName()).replace('/', '\\')); try { buffer.append(problem.errorReportSource(unitSource)); buffer.append("\n"); if (showCategory) { String category = problem.getInternalCategoryMessage(); if (category != null) { buffer.append("[@cat:").append(category).append("] "); } } if (showWarningToken) { int irritant = ProblemReporter.getIrritant(problem.getID()); if (irritant != 0) { String warningToken = CompilerOptions.warningTokenFromIrritant(irritant); if (warningToken != null) { buffer.append("[@sup:").append(warningToken).append("] "); } } } buffer.append(problem.getMessage()); buffer.append("\n"); } catch (Exception e) { } buffer.append("----------\n"); } } } return buffer.toString(); }
From source file:io.gige.compiler.internal.CompilerRequestorImpl.java
License:Apache License
@Override public void acceptResult(CompilationResult result) { if (result.hasProblems()) { report(Kind.ERROR, result.getErrors()); }// w w w . j ava 2 s. com if (result.hasTasks()) { report(Kind.NOTE, result.getTasks()); } try { for (ClassFile cf : result.getClassFiles()) { String className = new String(cf.fileName()); JavaFileObject obj = this.manager.getJavaFileForOutput(StandardLocation.CLASS_OUTPUT, className, javax.tools.JavaFileObject.Kind.CLASS, null); try (OutputStream out = obj.openOutputStream()) { out.write(cf.getBytes()); } } } catch (IOException e) { throw new UncheckedIOException(e); } }
From source file:io.takari.maven.plugins.compile.jdt.CompilerJdt.java
License:Open Source License
@Override public void acceptResult(CompilationResult result) { if (result == null) { return; // ah? }//from w w w . java 2s .c o m final String sourceName = new String(result.getFileName()); final File sourceFile = new File(sourceName); Resource<File> input = context.getProcessedSource(sourceFile); // track type references if (result.rootReferences != null && result.qualifiedReferences != null && result.simpleNameReferences != null) { context.setAttribute(input.getResource(), ATTR_REFERENCES, new ReferenceCollection( result.rootReferences, result.qualifiedReferences, result.simpleNameReferences)); } if (result.hasProblems() && (!isLenientProcOnly() || isShowWarnings())) { for (CategorizedProblem problem : result.getProblems()) { MessageSeverity severity = isLenientProcOnly() ? MessageSeverity.WARNING : problem.isError() ? MessageSeverity.ERROR : MessageSeverity.WARNING; input.addMessage(problem.getSourceLineNumber(), ((DefaultProblem) problem).column, problem.getMessage(), severity, null /* cause */); } } try { if (!result.hasErrors() && !isProcOnly()) { for (ClassFile classFile : result.getClassFiles()) { char[] filename = classFile.fileName(); int length = filename.length; char[] relativeName = new char[length + 6]; System.arraycopy(filename, 0, relativeName, 0, length); System.arraycopy(SuffixConstants.SUFFIX_class, 0, relativeName, length, 6); CharOperation.replace(relativeName, '/', File.separatorChar); String relativeStringName = new String(relativeName); writeClassFile(input, relativeStringName, classFile); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // XXX double check affected sources are recompiled when this source has errors }
From source file:ma.glasnost.orika.impl.generator.eclipsejdt.CompilerRequestor.java
License:Apache License
public void acceptResult(CompilationResult result) { boolean hasErrors = false; if (result.hasProblems()) { problems = result.getProblems(); }//ww w . j a v a2 s. com if (!hasErrors) { ClassFile[] classFiles = result.getClassFiles(); for (int i = 0; i < classFiles.length; i++) { ClassFile classFile = classFiles[i]; char[][] compoundName = classFile.getCompoundName(); StringBuilder className = new StringBuilder(); String sep = ""; for (int j = 0; j < compoundName.length; j++) { className.append(sep); className.append(new String(compoundName[j])); sep = "."; } byte[] bytes = classFile.getBytes(); compiledClassFiles.put(className.toString(), bytes); } } }
From source file:org.apache.commons.jci.compilers.EclipseJavaCompiler.java
License:Apache License
public org.apache.commons.jci.compilers.CompilationResult compile(final String[] pSourceFiles, final ResourceReader pReader, final ResourceStore pStore, final ClassLoader pClassLoader, final JavaCompilerSettings pSettings) { final Map<String, String> settingsMap = new EclipseJavaCompilerSettings(pSettings).toNativeSettings(); final Collection<CompilationProblem> problems = new ArrayList<CompilationProblem>(); final ICompilationUnit[] compilationUnits = new ICompilationUnit[pSourceFiles.length]; for (int i = 0; i < compilationUnits.length; i++) { final String sourceFile = pSourceFiles[i]; if (pReader.isAvailable(sourceFile)) { compilationUnits[i] = new CompilationUnit(pReader, sourceFile); log.debug("compiling " + sourceFile); } else {/* w w w.ja v a 2 s. c om*/ // log.error("source not found " + sourceFile); final CompilationProblem problem = new CompilationProblem() { public int getEndColumn() { return 0; } public int getEndLine() { return 0; } public String getFileName() { return sourceFile; } public String getMessage() { return "Source " + sourceFile + " could not be found"; } public int getStartColumn() { return 0; } public int getStartLine() { return 0; } public boolean isError() { return true; } @Override public String toString() { return getMessage(); } }; if (problemHandler != null) { problemHandler.handle(problem); } problems.add(problem); } } if (problems.size() > 0) { final CompilationProblem[] result = new CompilationProblem[problems.size()]; problems.toArray(result); return new org.apache.commons.jci.compilers.CompilationResult(result); } final IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems(); final IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault()); final INameEnvironment nameEnvironment = new INameEnvironment() { public NameEnvironmentAnswer findType(final char[][] pCompoundTypeName) { final StringBuilder result = new StringBuilder(); for (int i = 0; i < pCompoundTypeName.length; i++) { if (i != 0) { result.append('.'); } result.append(pCompoundTypeName[i]); } //log.debug("finding compoundTypeName=" + result.toString()); return findType(result.toString()); } public NameEnvironmentAnswer findType(final char[] pTypeName, final char[][] pPackageName) { final StringBuilder result = new StringBuilder(); for (int i = 0; i < pPackageName.length; i++) { result.append(pPackageName[i]); result.append('.'); } // log.debug("finding typeName=" + new String(typeName) + " packageName=" + result.toString()); result.append(pTypeName); return findType(result.toString()); } private NameEnvironmentAnswer findType(final String pClazzName) { if (isPackage(pClazzName)) { return null; } log.debug("finding " + pClazzName); final String resourceName = ConversionUtils.convertClassToResourcePath(pClazzName); final byte[] clazzBytes = pStore.read(resourceName); if (clazzBytes != null) { log.debug("loading from store " + pClazzName); final char[] fileName = pClazzName.toCharArray(); try { final ClassFileReader classFileReader = new ClassFileReader(clazzBytes, fileName, true); return new NameEnvironmentAnswer(classFileReader, null); } catch (final ClassFormatException e) { log.error("wrong class format", e); return null; } } log.debug("not in store " + pClazzName); final InputStream is = pClassLoader.getResourceAsStream(resourceName); if (is == null) { log.debug("class " + pClazzName + " not found"); return null; } final byte[] buffer = new byte[8192]; final ByteArrayOutputStream baos = new ByteArrayOutputStream(buffer.length); int count; try { while ((count = is.read(buffer, 0, buffer.length)) > 0) { baos.write(buffer, 0, count); } baos.flush(); final char[] fileName = pClazzName.toCharArray(); final ClassFileReader classFileReader = new ClassFileReader(baos.toByteArray(), fileName, true); return new NameEnvironmentAnswer(classFileReader, null); } catch (final IOException e) { log.error("could not read class", e); return null; } catch (final ClassFormatException e) { log.error("wrong class format", e); return null; } finally { try { baos.close(); } catch (final IOException oe) { log.error("could not close output stream", oe); } try { is.close(); } catch (final IOException ie) { log.error("could not close input stream", ie); } } } private boolean isPackage(final String pClazzName) { // reject this early as it is cheap if (pClazzName.contains("-")) { // "-" is not valid in package names return false; } final InputStream is = pClassLoader .getResourceAsStream(ConversionUtils.convertClassToResourcePath(pClazzName)); if (is != null) { log.debug("found the class for " + pClazzName + "- no package"); try { is.close(); } catch (final IOException ie) { log.error("could not close input stream", ie); } return false; } // FIXME: this should not be tied to the extension final String source = pClazzName.replace('.', '/') + ".java"; if (pReader.isAvailable(source)) { log.debug("found the source " + source + " for " + pClazzName + " - no package "); return false; } /* * See https://issues.apache.org/jira/browse/JCI-59 * At present, the code assumes that anything else is a package name * This is wrong, as for example jci.AdditionalTopLevel is not a package name. * It's not clear how to fix this in general. * It would seem to need access to the input classpath and/or the generated classes. */ return true; } public boolean isPackage(char[][] parentPackageName, char[] pPackageName) { final StringBuilder result = new StringBuilder(); if (parentPackageName != null) { for (int i = 0; i < parentPackageName.length; i++) { if (i != 0) { result.append('.'); } result.append(parentPackageName[i]); } } // log.debug("isPackage parentPackageName=" + result.toString() + " packageName=" + new String(packageName)); if (parentPackageName != null && parentPackageName.length > 0) { result.append('.'); } result.append(pPackageName); return isPackage(result.toString()); } public void cleanup() { log.debug("cleanup"); } }; final ICompilerRequestor compilerRequestor = new ICompilerRequestor() { public void acceptResult(final CompilationResult pResult) { if (pResult.hasProblems()) { for (IProblem iproblem : pResult.getProblems()) { final CompilationProblem problem = new EclipseCompilationProblem(iproblem); if (problemHandler != null) { problemHandler.handle(problem); } problems.add(problem); } } if (!pResult.hasErrors()) { final ClassFile[] clazzFiles = pResult.getClassFiles(); for (ClassFile clazzFile : clazzFiles) { final char[][] compoundName = clazzFile.getCompoundName(); final StringBuilder clazzName = new StringBuilder(); for (int j = 0; j < compoundName.length; j++) { if (j != 0) { clazzName.append('.'); } clazzName.append(compoundName[j]); } pStore.write(clazzName.toString().replace('.', '/') + ".class", clazzFile.getBytes()); } } } }; final Compiler compiler = new Compiler(nameEnvironment, policy, new CompilerOptions(settingsMap), compilerRequestor, problemFactory); compiler.compile(compilationUnits); final CompilationProblem[] result = new CompilationProblem[problems.size()]; problems.toArray(result); return new org.apache.commons.jci.compilers.CompilationResult(result); }
From source file:org.apache.tuscany.maven.compiler.CompilerRequestor.java
License:Apache License
public void acceptResult(CompilationResult result) { boolean hasErrors = false; if (result.hasProblems()) { // Convert JDT IProblems into plexus CompilerErrors for (IProblem problem : result.getProblems()) { if (problem.isWarning()) { if (showWarnings) { compilerErrors.add(new CompilerError(new String(problem.getOriginatingFileName()), false, problem.getSourceLineNumber(), problem.getSourceStart(), problem.getSourceLineNumber(), problem.getSourceEnd(), problem.getMessage())); }// ww w. j av a2 s . c om } else if (problem.isError()) { hasErrors = true; compilerErrors.add(new CompilerError(new String(problem.getOriginatingFileName()), true, problem.getSourceLineNumber(), problem.getSourceStart(), problem.getSourceLineNumber(), problem.getSourceEnd(), problem.getMessage())); } } } // Write the class files if (!hasErrors) { ClassFile[] classFiles = result.getClassFiles(); for (ClassFile classFile : classFiles) { // Create file and parent directories StringBuffer className = new StringBuffer(); for (char[] name : classFile.getCompoundName()) { if (className.length() != 0) { className.append('.'); } className.append(name); } File file = new File(outputDirectory, className.toString().replace('.', '/') + ".class"); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } // Write class file contents FileOutputStream fos = null; try { fos = new FileOutputStream(file); fos.write(classFile.getBytes()); } catch (FileNotFoundException e) { throw new IllegalArgumentException(e); } catch (IOException e) { throw new IllegalArgumentException(e); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { } } } } } }
From source file:org.auroraide.server.jci.compilers.EclipseJavaCompiler.java
License:Apache License
public org.apache.commons.jci.compilers.CompilationResult compile(final String[] pSourceFiles, final ResourceReader pReader, final ResourceStore pStore, final ClassLoader pClassLoader, final JavaCompilerSettings pSettings) { final Map<?, ?> settingsMap = ((EclipseJavaCompilerSettings) defaultSettings).getMap(); final Collection<CompilationProblem> problems = new ArrayList<CompilationProblem>(); final ICompilationUnit[] compilationUnits = new ICompilationUnit[pSourceFiles.length]; for (int i = 0; i < compilationUnits.length; i++) { final String sourceFile = pSourceFiles[i]; if (pReader.isAvailable(sourceFile)) { compilationUnits[i] = new CompilationUnit(pReader, sourceFile); log.debug("compiling " + sourceFile); } else {//w w w. j ava 2 s.co m // log.error("source not found " + sourceFile); final CompilationProblem problem = new CompilationProblem() { public int getEndColumn() { return 0; } public int getEndLine() { return 0; } public String getFileName() { return sourceFile; } public String getMessage() { return "Source " + sourceFile + " could not be found"; } public int getStartColumn() { return 0; } public int getStartLine() { return 0; } public boolean isError() { return true; } public String toString() { return getMessage(); } }; if (problemHandler != null) { problemHandler.handle(problem); } problems.add(problem); } } if (problems.size() > 0) { final CompilationProblem[] result = new CompilationProblem[problems.size()]; problems.toArray(result); return new org.apache.commons.jci.compilers.CompilationResult(result); } final IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems(); final IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault()); final INameEnvironment nameEnvironment = new INameEnvironment() { public NameEnvironmentAnswer findType(final char[][] pCompoundTypeName) { final StringBuffer result = new StringBuffer(); for (int i = 0; i < pCompoundTypeName.length; i++) { if (i != 0) { result.append('.'); } result.append(pCompoundTypeName[i]); } //log.debug("finding compoundTypeName=" + result.toString()); return findType(result.toString()); } public NameEnvironmentAnswer findType(final char[] pTypeName, final char[][] pPackageName) { final StringBuffer result = new StringBuffer(); for (int i = 0; i < pPackageName.length; i++) { result.append(pPackageName[i]); result.append('.'); } // log.debug("finding typeName=" + new String(typeName) + " packageName=" + result.toString()); result.append(pTypeName); return findType(result.toString()); } private NameEnvironmentAnswer findType(final String pClazzName) { if (isPackage(pClazzName)) { return null; } log.debug("finding " + pClazzName); final String resourceName = ConversionUtils.convertClassToResourcePath(pClazzName); final byte[] clazzBytes = pStore.read(pClazzName); if (clazzBytes != null) { log.debug("loading from store " + pClazzName); final char[] fileName = pClazzName.toCharArray(); try { final ClassFileReader classFileReader = new ClassFileReader(clazzBytes, fileName, true); return new NameEnvironmentAnswer(classFileReader, null); } catch (final ClassFormatException e) { log.error("wrong class format", e); return null; } } log.debug("not in store " + pClazzName); final InputStream is = pClassLoader.getResourceAsStream(resourceName); if (is == null) { log.debug("class " + pClazzName + " not found"); return null; } final byte[] buffer = new byte[8192]; final ByteArrayOutputStream baos = new ByteArrayOutputStream(buffer.length); int count; try { while ((count = is.read(buffer, 0, buffer.length)) > 0) { baos.write(buffer, 0, count); } baos.flush(); final char[] fileName = pClazzName.toCharArray(); final ClassFileReader classFileReader = new ClassFileReader(baos.toByteArray(), fileName, true); return new NameEnvironmentAnswer(classFileReader, null); } catch (final IOException e) { log.error("could not read class", e); return null; } catch (final ClassFormatException e) { log.error("wrong class format", e); return null; } finally { try { baos.close(); } catch (final IOException oe) { log.error("could not close output stream", oe); } try { is.close(); } catch (final IOException ie) { log.error("could not close input stream", ie); } } } private boolean isPackage(final String pClazzName) { final InputStream is = pClassLoader .getResourceAsStream(ConversionUtils.convertClassToResourcePath(pClazzName)); if (is != null) { log.debug("found the class for " + pClazzName + "- no package"); return false; } // FIXME: this should not be tied to the extension final String source = pClazzName.replace('.', '/') + ".java"; if (pReader.isAvailable(source)) { log.debug("found the source " + source + " for " + pClazzName + " - no package "); return false; } return true; } public boolean isPackage(char[][] parentPackageName, char[] pPackageName) { final StringBuffer result = new StringBuffer(); if (parentPackageName != null) { for (int i = 0; i < parentPackageName.length; i++) { if (i != 0) { result.append('.'); } result.append(parentPackageName[i]); } } // log.debug("isPackage parentPackageName=" + result.toString() + " packageName=" + new String(packageName)); if (parentPackageName != null && parentPackageName.length > 0) { result.append('.'); } result.append(pPackageName); return isPackage(result.toString()); } public void cleanup() { log.debug("cleanup"); } }; final ICompilerRequestor compilerRequestor = new ICompilerRequestor() { public void acceptResult(final CompilationResult pResult) { if (pResult.hasProblems()) { final IProblem[] iproblems = pResult.getProblems(); for (int i = 0; i < iproblems.length; i++) { final IProblem iproblem = iproblems[i]; final CompilationProblem problem = new EclipseCompilationProblem(iproblem); if (problemHandler != null) { problemHandler.handle(problem); } problems.add(problem); } } if (!pResult.hasErrors()) { final ClassFile[] clazzFiles = pResult.getClassFiles(); for (int i = 0; i < clazzFiles.length; i++) { final ClassFile clazzFile = clazzFiles[i]; final char[][] compoundName = clazzFile.getCompoundName(); final StringBuffer clazzName = new StringBuffer(); for (int j = 0; j < compoundName.length; j++) { if (j != 0) { clazzName.append('.'); } clazzName.append(compoundName[j]); } pStore.write(clazzName.toString().replace('.', '/') + ".class", clazzFile.getBytes()); } } } }; final Compiler compiler = new Compiler(nameEnvironment, policy, settingsMap, compilerRequestor, problemFactory, false); compiler.compile(compilationUnits); final CompilationProblem[] result = new CompilationProblem[problems.size()]; problems.toArray(result); return new org.apache.commons.jci.compilers.CompilationResult(result); }
From source file:org.drools.commons.jci.compilers.EclipseJavaCompiler.java
License:Apache License
public org.drools.commons.jci.compilers.CompilationResult compile(final String[] pSourceFiles, final ResourceReader pReader, final ResourceStore pStore, final ClassLoader pClassLoader, final JavaCompilerSettings pSettings) { final Map settingsMap = new EclipseJavaCompilerSettings(pSettings).toNativeSettings(); final Collection problems = new ArrayList(); final ICompilationUnit[] compilationUnits = new ICompilationUnit[pSourceFiles.length]; for (int i = 0; i < compilationUnits.length; i++) { final String sourceFile = pSourceFiles[i]; if (pReader.isAvailable(sourceFile)) { compilationUnits[i] = new CompilationUnit(pReader, sourceFile); } else {//from ww w. jav a 2 s .c om // log.error("source not found " + sourceFile); final CompilationProblem problem = new CompilationProblem() { public int getEndColumn() { return 0; } public int getEndLine() { return 0; } public String getFileName() { return sourceFile; } public String getMessage() { return "Source " + sourceFile + " could not be found"; } public int getStartColumn() { return 0; } public int getStartLine() { return 0; } public boolean isError() { return true; } public String toString() { return getMessage(); } }; if (problemHandler != null) { problemHandler.handle(problem); } problems.add(problem); } } if (problems.size() > 0) { final CompilationProblem[] result = new CompilationProblem[problems.size()]; problems.toArray(result); return new org.drools.commons.jci.compilers.CompilationResult(result); } final IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems(); final IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault()); final INameEnvironment nameEnvironment = new INameEnvironment() { public NameEnvironmentAnswer findType(final char[][] pCompoundTypeName) { final StringBuilder result = new StringBuilder(); for (int i = 0; i < pCompoundTypeName.length; i++) { if (i != 0) { result.append('.'); } result.append(pCompoundTypeName[i]); } //log.debug("finding compoundTypeName=" + result.toString()); return findType(result.toString()); } public NameEnvironmentAnswer findType(final char[] pTypeName, final char[][] pPackageName) { final StringBuilder result = new StringBuilder(); for (int i = 0; i < pPackageName.length; i++) { result.append(pPackageName[i]); result.append('.'); } // log.debug("finding typeName=" + new String(typeName) + " packageName=" + result.toString()); result.append(pTypeName); return findType(result.toString()); } private NameEnvironmentAnswer findType(final String pClazzName) { final String resourceName = ClassUtils.convertClassToResourcePath(pClazzName); final byte[] clazzBytes = pStore.read(resourceName); if (clazzBytes != null) { try { return createNameEnvironmentAnswer(pClazzName, clazzBytes); } catch (final ClassFormatException e) { throw new RuntimeException( "ClassFormatException in loading class '" + pClazzName + "' with JCI."); } } InputStream is = null; ByteArrayOutputStream baos = null; try { is = pClassLoader.getResourceAsStream(resourceName); if (is == null) { return null; } final byte[] buffer = new byte[8192]; baos = new ByteArrayOutputStream(buffer.length); int count; while ((count = is.read(buffer, 0, buffer.length)) > 0) { baos.write(buffer, 0, count); } baos.flush(); return createNameEnvironmentAnswer(pClazzName, baos.toByteArray()); } catch (final IOException e) { throw new RuntimeException("could not read class", e); } catch (final ClassFormatException e) { throw new RuntimeException("wrong class format", e); } finally { try { if (baos != null) { baos.close(); } } catch (final IOException oe) { throw new RuntimeException("could not close output stream", oe); } try { if (is != null) { is.close(); } } catch (final IOException ie) { throw new RuntimeException("could not close input stream", ie); } } } private NameEnvironmentAnswer createNameEnvironmentAnswer(final String pClazzName, final byte[] clazzBytes) throws ClassFormatException { final char[] fileName = pClazzName.toCharArray(); final ClassFileReader classFileReader = new ClassFileReader(clazzBytes, fileName, true); return new NameEnvironmentAnswer(classFileReader, null); } private boolean isSourceAvailable(final String pClazzName, final ResourceReader pReader) { // FIXME: this should not be tied to the extension final String source = pClazzName.replace('.', '/') + ".java"; return pReader.isAvailable(source); } private boolean isPackage(final String pClazzName) { InputStream is = null; try { is = pClassLoader.getResourceAsStream(ClassUtils.convertClassToResourcePath(pClazzName)); return is == null && !isSourceAvailable(pClazzName, pReader); } finally { if (is != null) { try { is.close(); } catch (IOException e) { throw new RuntimeException("Unable to close stream for resource: " + pClazzName); } } } } public boolean isPackage(char[][] parentPackageName, char[] pPackageName) { final StringBuilder result = new StringBuilder(); if (parentPackageName != null) { for (int i = 0; i < parentPackageName.length; i++) { if (i != 0) { result.append('.'); } result.append(parentPackageName[i]); } } // log.debug("isPackage parentPackageName=" + result.toString() + " packageName=" + new String(packageName)); if (parentPackageName != null && parentPackageName.length > 0) { result.append('.'); } result.append(pPackageName); return isPackage(result.toString()); } public void cleanup() { } }; final ICompilerRequestor compilerRequestor = new ICompilerRequestor() { public void acceptResult(final CompilationResult pResult) { if (pResult.hasProblems()) { final IProblem[] iproblems = pResult.getProblems(); for (int i = 0; i < iproblems.length; i++) { final IProblem iproblem = iproblems[i]; final CompilationProblem problem = new EclipseCompilationProblem(iproblem); if (problemHandler != null) { problemHandler.handle(problem); } problems.add(problem); } } if (!pResult.hasErrors()) { final ClassFile[] clazzFiles = pResult.getClassFiles(); for (int i = 0; i < clazzFiles.length; i++) { final ClassFile clazzFile = clazzFiles[i]; final char[][] compoundName = clazzFile.getCompoundName(); final StringBuilder clazzName = new StringBuilder(); for (int j = 0; j < compoundName.length; j++) { if (j != 0) { clazzName.append('.'); } clazzName.append(compoundName[j]); } pStore.write(clazzName.toString().replace('.', '/') + ".class", clazzFile.getBytes()); } } } }; final Compiler compiler = new Compiler(nameEnvironment, policy, settingsMap, compilerRequestor, problemFactory, false); compiler.compile(compilationUnits); final CompilationProblem[] result = new CompilationProblem[problems.size()]; problems.toArray(result); return new org.drools.commons.jci.compilers.CompilationResult(result); }
From source file:org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler.java
License:Apache License
public org.drools.compiler.commons.jci.compilers.CompilationResult compile(final String[] pSourceFiles, final ResourceReader pReader, final ResourceStore pStore, final ClassLoader pClassLoader, final JavaCompilerSettings pSettings) { final Map settingsMap = new EclipseJavaCompilerSettings(pSettings).toNativeSettings(); final Collection problems = new ArrayList(); final ICompilationUnit[] compilationUnits = new ICompilationUnit[pSourceFiles.length]; for (int i = 0; i < compilationUnits.length; i++) { final String sourceFile = pSourceFiles[i]; if (pReader.isAvailable(sourceFile)) { compilationUnits[i] = new CompilationUnit(pReader, sourceFile); } else {//from w w w . ja va2 s. c om // log.error("source not found " + sourceFile); final CompilationProblem problem = new CompilationProblem() { public int getEndColumn() { return 0; } public int getEndLine() { return 0; } public String getFileName() { return sourceFile; } public String getMessage() { return "Source " + sourceFile + " could not be found"; } public int getStartColumn() { return 0; } public int getStartLine() { return 0; } public boolean isError() { return true; } public String toString() { return getMessage(); } }; if (problemHandler != null) { problemHandler.handle(problem); } problems.add(problem); } } if (problems.size() > 0) { final CompilationProblem[] result = new CompilationProblem[problems.size()]; problems.toArray(result); return new org.drools.compiler.commons.jci.compilers.CompilationResult(result); } final IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems(); final IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault()); final INameEnvironment nameEnvironment = new INameEnvironment() { public NameEnvironmentAnswer findType(final char[][] pCompoundTypeName) { final StringBuilder result = new StringBuilder(); for (int i = 0; i < pCompoundTypeName.length; i++) { if (i != 0) { result.append('.'); } result.append(pCompoundTypeName[i]); } //log.debug("finding compoundTypeName=" + result.toString()); return findType(result.toString()); } public NameEnvironmentAnswer findType(final char[] pTypeName, final char[][] pPackageName) { final StringBuilder result = new StringBuilder(); for (int i = 0; i < pPackageName.length; i++) { result.append(pPackageName[i]); result.append('.'); } // log.debug("finding typeName=" + new String(typeName) + " packageName=" + result.toString()); result.append(pTypeName); return findType(result.toString()); } private NameEnvironmentAnswer findType(final String pClazzName) { final String resourceName = ClassUtils.convertClassToResourcePath(pClazzName); final byte[] clazzBytes = pStore.read(resourceName); if (clazzBytes != null) { try { return createNameEnvironmentAnswer(pClazzName, clazzBytes); } catch (final ClassFormatException e) { throw new RuntimeException( "ClassFormatException in loading class '" + pClazzName + "' with JCI."); } } InputStream is = null; ByteArrayOutputStream baos = null; try { is = pClassLoader.getResourceAsStream(resourceName); if (is == null) { return null; } if (ClassUtils.isWindows() || ClassUtils.isOSX()) { // check it really is a class, this issue is due to windows case sensitivity issues for the class org.kie.Process and path org/droosl/process try { pClassLoader.loadClass(pClazzName); } catch (ClassNotFoundException e) { return null; } catch (NoClassDefFoundError e) { return null; } } final byte[] buffer = new byte[8192]; baos = new ByteArrayOutputStream(buffer.length); int count; while ((count = is.read(buffer, 0, buffer.length)) > 0) { baos.write(buffer, 0, count); } baos.flush(); return createNameEnvironmentAnswer(pClazzName, baos.toByteArray()); } catch (final IOException e) { throw new RuntimeException("could not read class", e); } catch (final ClassFormatException e) { throw new RuntimeException("wrong class format", e); } finally { try { if (baos != null) { baos.close(); } } catch (final IOException oe) { throw new RuntimeException("could not close output stream", oe); } try { if (is != null) { is.close(); } } catch (final IOException ie) { throw new RuntimeException("could not close input stream", ie); } } } private NameEnvironmentAnswer createNameEnvironmentAnswer(final String pClazzName, final byte[] clazzBytes) throws ClassFormatException { final char[] fileName = pClazzName.toCharArray(); final ClassFileReader classFileReader = new ClassFileReader(clazzBytes, fileName, true); return new NameEnvironmentAnswer(classFileReader, null); } private boolean isSourceAvailable(final String pClazzName, final ResourceReader pReader) { // FIXME: this should not be tied to the extension final String javaSource = pClazzName.replace('.', '/') + ".java"; final String classSource = pClazzName.replace('.', '/') + ".class"; return pReader.isAvailable(prefix + javaSource) || pReader.isAvailable(prefix + classSource); } private boolean isPackage(final String pClazzName) { InputStream is = null; try { is = pClassLoader.getResourceAsStream(ClassUtils.convertClassToResourcePath(pClazzName)); if (is != null) { if (ClassUtils.isWindows() || ClassUtils.isOSX()) { // check it really is a class, this issue is due to windows case sensitivity issues for the class org.kie.Process and path org/droosl/process try { Class cls = pClassLoader.loadClass(pClazzName); if (cls != null) { return true; } } catch (ClassNotFoundException e) { return true; } catch (NoClassDefFoundError e) { return true; } } } boolean result = is == null && !isSourceAvailable(pClazzName, pReader); return result; } finally { if (is != null) { try { is.close(); } catch (IOException e) { throw new RuntimeException("Unable to close stream for resource: " + pClazzName); } } } } public boolean isPackage(char[][] parentPackageName, char[] pPackageName) { final StringBuilder result = new StringBuilder(); if (parentPackageName != null) { for (int i = 0; i < parentPackageName.length; i++) { if (i != 0) { result.append('.'); } result.append(parentPackageName[i]); } } // log.debug("isPackage parentPackageName=" + result.toString() + " packageName=" + new String(packageName)); if (parentPackageName != null && parentPackageName.length > 0) { result.append('.'); } result.append(pPackageName); return isPackage(result.toString()); } public void cleanup() { } }; final ICompilerRequestor compilerRequestor = new ICompilerRequestor() { public void acceptResult(final CompilationResult pResult) { if (pResult.hasProblems()) { final IProblem[] iproblems = pResult.getProblems(); for (int i = 0; i < iproblems.length; i++) { final IProblem iproblem = iproblems[i]; final CompilationProblem problem = new EclipseCompilationProblem(iproblem); if (problemHandler != null) { problemHandler.handle(problem); } problems.add(problem); } } if (!pResult.hasErrors()) { final ClassFile[] clazzFiles = pResult.getClassFiles(); for (int i = 0; i < clazzFiles.length; i++) { final ClassFile clazzFile = clazzFiles[i]; final char[][] compoundName = clazzFile.getCompoundName(); final StringBuilder clazzName = new StringBuilder(); for (int j = 0; j < compoundName.length; j++) { if (j != 0) { clazzName.append('.'); } clazzName.append(compoundName[j]); } pStore.write(clazzName.toString().replace('.', '/') + ".class", clazzFile.getBytes()); } } } }; final Compiler compiler = new Compiler(nameEnvironment, policy, settingsMap, compilerRequestor, problemFactory, false); compiler.compile(compilationUnits); final CompilationProblem[] result = new CompilationProblem[problems.size()]; problems.toArray(result); return new org.drools.compiler.commons.jci.compilers.CompilationResult(result); }
From source file:org.z2env.impl.components.java.jdt.CompilerRequestorImpl.java
License:Apache License
public void acceptResult(CompilationResult result) { if (result.hasProblems()) { IProblem[] problems = result.getProblems(); for (IProblem problem : problems) { if (problem.isWarning()) { if (logger.isLoggable(Level.FINEST)) { logger.warning(String.format("Compile Warning %s", problemToText(problem))); }/*from www. ja v a 2 s. c om*/ } else if (problem.isError()) { this.hasError = true; logger.severe(String.format("\n\nCompile Error %s\n", problemToText(problem))); } } } if (!this.hasError) { ClassFile[] classFiles = result.getClassFiles(); for (ClassFile classFile : classFiles) { String classFileName = NameEnvironmentImpl.concat(classFile.getCompoundName()) + ".class"; if (logger.isLoggable(Level.FINEST)) { logger.finest("Writing class file " + classFileName + " into " + this.outFolder); } byte[] bytes = classFile.getBytes(); File outFile = new File(this.outFolder, classFileName); if (!outFile.getParentFile().exists()) { outFile.getParentFile().mkdirs(); } FileOutputStream fout = null; try { try { fout = new FileOutputStream(outFile); fout.write(bytes); } finally { fout.close(); } } catch (Exception e) { throw new RuntimeException("Failed to write .class file: " + classFileName, e); } } } }