tools/java/src/stx/libjava/tools/compiler/CompilerAdapter.java
branchdevelopment
changeset 2728 658220e93dc9
parent 2488 5395660e3366
child 2732 7d1a1fb5b01a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/java/src/stx/libjava/tools/compiler/CompilerAdapter.java	Thu Sep 12 00:24:05 2013 +0100
@@ -0,0 +1,108 @@
+package stx.libjava.tools.compiler;
+
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+import org.eclipse.jdt.internal.compiler.ClassFile;
+import org.eclipse.jdt.internal.compiler.CompilationResult;
+import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
+import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
+import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
+import org.eclipse.jdt.internal.compiler.IProblemFactory;
+import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
+import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
+import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
+import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
+
+import stx.libjava.tools.Source;
+import stx.libjava.tools.environment.Environment;
+
+public class CompilerAdapter implements ICompilerRequestor {
+	protected Environment environment;
+	protected CompilationResult result;
+	
+	public CompilerAdapter() {
+		this(ClassLoader.getSystemClassLoader());
+	}
+	
+	public CompilerAdapter(ClassLoader cl) {
+		environment = new Environment(cl);	
+	}
+	
+	public CompilationResult getResult() {
+		return result;
+	}
+
+	public ClassLoader getClassLoader() {
+		return environment.getClassLoader();
+	}
+	
+	/**
+	 * Compiles classes in given source. The resulting .class files are added
+	 * to an internal list which can be later retrieved by getClassFiles().
+	 * 
+	 * @param source source code of the class as String.   
+	 * @return true, if compilation succeeded, false otherwise.
+	 */
+	public boolean compile(String source) {
+		ICompilerRequestor requestor = this;
+		IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.exitAfterAllProblems();
+	    IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault());
+	    Source[] units = new Source[1];
+	    units[0] = new Source(source);
+	    units[0].setName();
+		
+		org.eclipse.jdt.internal.compiler.Compiler compiler = new org.eclipse.jdt.internal.compiler.Compiler(environment, policy, getDefaultCompilerOptions(), requestor, problemFactory);
+		compiler.compile(units);
+		return getResult().hasErrors();
+	}
+	
+	/**
+	 * Same as compile(String), for compatibility with old interface
+	 * 
+	 * @deprecated
+	 */
+	public boolean compile(String name, String source) {
+	    return compile(source);
+	}
+	
+	/**
+	 * Returns a list of JavaClassFiles that contains results of the compilation. 
+	 * 
+	 * @return resulting class files
+	 */
+	public ClassFile[] getClassFiles() {
+		return getResult().getClassFiles();
+	}
+	
+	public static Map<String, Object> getDefaultCompilerSettings() {
+		String javaSpecVersion = System.getProperty("java.specification.version");
+        Map<String, Object> settings = new HashMap<String, Object>();
+        settings.put(CompilerOptions.OPTION_Source, javaSpecVersion);
+        settings.put(CompilerOptions.OPTION_TargetPlatform, javaSpecVersion);
+        settings.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.IGNORE);
+        return settings;
+	}
+
+    public static CompilerOptions getDefaultCompilerOptions() {
+    	return new CompilerOptions(getDefaultCompilerSettings());
+    }
+
+	@Override
+	public void acceptResult(CompilationResult result) {
+		this.result = result;
+		for (ClassFile cf : this.result.getClassFiles()) {
+			StringBuilder sb = new StringBuilder();
+			for (int i = 0; i < cf.getCompoundName().length; i++) {
+				sb.append(cf.getCompoundName()[i]);
+				if (i < cf.getCompoundName().length - 1) {
+					sb.append('.');
+				}
+			}
+			environment.types.put(sb.toString(), cf.getBytes());
+		}
+	}
+	
+
+}