001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.support.util;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: import org.eclipse.jdt.internal.compiler.ClassFile;
027: import org.eclipse.jdt.internal.compiler.CompilationResult;
028: import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
029: import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
030: import org.eclipse.jdt.internal.compiler.batch.FileSystem;
031: import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
032: import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
033: import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
034: import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
035:
036: public class JDTCompiler implements ICompilerRequestor {
037:
038: public static ICompilationUnit getUnit(String name, File file)
039: throws Exception {
040:
041: String[] tmp = name.split("[.]");
042: char[][] pack = new char[tmp.length - 1][];
043:
044: for (int i = 0; i < tmp.length - 1; i++) {
045: pack[i] = tmp[i].toCharArray();
046: }
047:
048: ByteArrayOutputStream out = new ByteArrayOutputStream();
049: FileInputStream in = new FileInputStream(file);
050: byte[] buffer = new byte[512];
051: int read;
052: while ((read = in.read(buffer, 0, 512)) >= 0)
053: out.write(buffer, 0, read);
054:
055: ICompilationUnit unit = null;
056:
057: unit = new BasicCompilationUnit(out.toString().toCharArray(),
058: pack, file.getName());
059:
060: out.close();
061: in.close();
062: return unit;
063: }
064:
065: CompilerOptions compilerOption;
066:
067: List<ClassFile> classFiles = new ArrayList<ClassFile>();
068:
069: public void acceptResult(CompilationResult result) {
070: if (result.hasErrors()) {
071: System.err.println(result);
072: }
073:
074: for (ClassFile f : result.getClassFiles()) {
075: classFiles.add(f);
076: }
077: }
078:
079: public List<ClassFile> compile(ICompilationUnit[] units) {
080: org.eclipse.jdt.internal.compiler.Compiler compiler = new org.eclipse.jdt.internal.compiler.Compiler(
081: getLibraryAccess(), getHandlingPolicy(),
082: getCompilerOption(), this , new DefaultProblemFactory());
083: compiler.compile(units);
084: return classFiles;
085: }
086:
087: public CompilerOptions getCompilerOption() {
088: if (compilerOption == null) {
089: compilerOption = new CompilerOptions();
090: compilerOption.sourceLevel = ClassFileConstants.JDK1_5;
091: compilerOption.suppressWarnings = true;
092: }
093: return compilerOption;
094: }
095:
096: private IErrorHandlingPolicy getHandlingPolicy() {
097: return new IErrorHandlingPolicy() {
098: public boolean proceedOnErrors() {
099: return true; // stop if there are some errors
100: }
101:
102: public boolean stopOnFirstError() {
103: return false;
104: }
105: };
106: }
107:
108: FileSystem getLibraryAccess() {
109: String bootpath = System.getProperty("sun.boot.class.path");
110: String classpath = System.getProperty("java.class.path");
111: List<String> lst = new ArrayList<String>();
112: for (String s : bootpath.split(File.pathSeparator)) {
113: File f = new File(s);
114: if (f.exists()) {
115: lst.add(f.getAbsolutePath());
116: }
117: }
118: for (String s : classpath.split(File.pathSeparator)) {
119: File f = new File(s);
120: if (f.exists()) {
121: lst.add(f.getAbsolutePath());
122: }
123: }
124: return new FileSystem(lst.toArray(new String[0]),
125: new String[0], System.getProperty("file.encoding"));
126: }
127:
128: public List<ClassFile> getClassFiles() {
129: return classFiles;
130: }
131:
132: }
|