protected void outputClassFiles(CompilationResult unitResult) { if ((unitResult != null) && (!unitResult.hasErrors() || this.forceOutputGeneration)) { ClassFile[] classFiles = unitResult.getClassFiles(); if (this.outputPath != null) { for (int i = 0, fileCount = classFiles.length; i < fileCount; i++) { // retrieve the key and the corresponding classfile ClassFile classFile = classFiles[i]; String relativeName = new String(classFile.fileName()).replace('/', File.separatorChar) + ".class"; try { org.eclipse.jdt.internal.compiler.util.Util.writeToDisk( true, this.outputPath, relativeName, classFile); } catch (IOException e) { e.printStackTrace(); } } } } }
// Dump classfiles onto disk for all compilation units that where successful // and do not carry a -d none spec, either directly or inherited from Main. @Override public void outputClassFiles(CompilationResult unitResult) { if (!((unitResult == null) || (unitResult.hasErrors() && !this.proceedOnError))) { ClassFile[] classFiles = unitResult.getClassFiles(); boolean generateClasspathStructure = this.fileManager.hasLocation(StandardLocation.CLASS_OUTPUT); String currentDestinationPath = this.destinationPath; File outputLocation = null; if (currentDestinationPath != null) { outputLocation = new File(currentDestinationPath); outputLocation.mkdirs(); } for (int i = 0, fileCount = classFiles.length; i < fileCount; i++) { // retrieve the key and the corresponding classfile ClassFile classFile = classFiles[i]; 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); if (this.compilerOptions.verbose) { EclipseCompilerImpl.this.out.println( Messages.bind( Messages.compilation_write, new String[] { String.valueOf(this.exportedClassFilesCounter + 1), relativeStringName })); } try { JavaFileObject javaFileForOutput = this.fileManager.getJavaFileForOutput( StandardLocation.CLASS_OUTPUT, new String(filename), JavaFileObject.Kind.CLASS, this.javaFileObjectMap.get(unitResult.compilationUnit)); if (generateClasspathStructure) { if (currentDestinationPath != null) { int index = CharOperation.lastIndexOf(File.separatorChar, relativeName); if (index != -1) { File currentFolder = new File(currentDestinationPath, relativeStringName.substring(0, index)); currentFolder.mkdirs(); } } else { // create the subfolfers is necessary // need a way to retrieve the folders to create String path = javaFileForOutput.toUri().getPath(); int index = path.lastIndexOf('/'); if (index != -1) { File file = new File(path.substring(0, index)); file.mkdirs(); } } } OutputStream openOutputStream = javaFileForOutput.openOutputStream(); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(openOutputStream); bufferedOutputStream.write(classFile.header, 0, classFile.headerOffset); bufferedOutputStream.write(classFile.contents, 0, classFile.contentsOffset); bufferedOutputStream.flush(); bufferedOutputStream.close(); } catch (IOException e) { this.logger.logNoClassFileCreated(currentDestinationPath, relativeStringName, e); } this.logger.logClassFile( generateClasspathStructure, currentDestinationPath, relativeStringName); this.exportedClassFilesCounter++; } this.batchCompiler.lookupEnvironment.releaseClassFiles(classFiles); } }
@Override public void onCompilationResult(CompilationResult cr) { Set<String> classesWithErrors = new HashSet<String>(); if (cr.getErrors() != null) { myTracer.push("handling errors", false); for (final CategorizedProblem cp : cr.getErrors()) { String fileName = new String(cp.getOriginatingFileName()); final String fqName = NameUtil.namespaceFromPath( fileName.substring(0, fileName.length() - MPSExtentions.DOT_JAVAFILE.length())); classesWithErrors.add(fqName); SModule containingModule = myContainingModules.get(fqName); assert containingModule != null; JavaFile javaFile = myModuleSources.get(containingModule).getJavaFile(fqName); String messageString = new String(cp.getOriginatingFileName()) + " : " + cp.getMessage(); // final SNode nodeToShow = getNodeByLine(cp, fqName); Object hintObject = new FileWithPosition(javaFile.getFile(), cp.getSourceStart()); String errMsg = messageString + " (line: " + cp.getSourceLineNumber() + ")"; if (cp.isWarning()) { myMessages.add(createMessage(MessageKind.WARNING, errMsg, hintObject)); } else { if (myOutputtedErrors == 0) { myMessages.add(createMessage(MessageKind.ERROR, "Compilation problems", null)); myMessages.add( createMessage( MessageKind.INFORMATION, "Modules: " + myModules.toString() + "\nClasspath: " + myClassPathItems + "\n", null)); } if (myOutputtedErrors < MAX_ERRORS) { myOutputtedErrors++; myMessages.add(createMessage(MessageKind.ERROR, errMsg, hintObject)); } } } myTracer.pop(); myErrorCount += cr.getErrors().length; } myTracer.push("storing files", false); for (ClassFile cf : cr.getClassFiles()) { String fqName = convertCompoundToFqName(cf.getCompoundName()); String containerClassName = fqName; if (containerClassName.contains("$")) { int index = containerClassName.indexOf('$'); containerClassName = containerClassName.substring(0, index); } if (myContainingModules.containsKey(containerClassName)) { SModule m = myContainingModules.get(containerClassName); myChangedModules.add(m); File classesGen = new File(getJavaFacet(m).getClassesGen().getPath()); String packageName = NameUtil.namespaceFromLongName(fqName); File outputDir = new File(classesGen + File.separator + NameUtil.pathFromNamespace(packageName)); if (!outputDir.exists()) { if (!outputDir.mkdirs()) { throw new RuntimeException("Can't create " + outputDir.getPath() + " directory"); } } String className = NameUtil.shortNameFromLongName(fqName); File output = new File(outputDir, className + ".class"); if (!classesWithErrors.contains(containerClassName)) { FileOutputStream os = null; try { os = new FileOutputStream(output); os.write(cf.getBytes()); } catch (IOException e) { String errMsg = "Can't write to " + output.getAbsolutePath(); myMessages.add(createMessage(MessageKind.ERROR, errMsg, null)); } finally { if (os != null) { try { os.close(); } catch (IOException e) { myHandler.handle(createMessage(MessageKind.ERROR, e.toString(), e)); } } } } else { if (output.exists() && !(output.delete())) { String errMsg = "Can't delete " + output.getPath(); myMessages.add(createMessage(MessageKind.ERROR, errMsg, null)); } } } else { String errMsg = "I don't know in which module's output path I should place class file for " + fqName; myMessages.add(createMessage(MessageKind.ERROR, errMsg, null)); } } myTracer.pop(); }