tools/java/src/stx/libjava/tools/compiler/CompilerAdapter.java
author Jan Vrany <jan.vrany@fit.cvut.cz>
Mon, 16 Sep 2013 01:04:57 +0100
branchdevelopment
changeset 2733 3d97124aebf5
parent 2732 7d1a1fb5b01a
child 2735 e20dd8496371
permissions -rw-r--r--
Fixes for syntax highlighting. Even full class source is fully parser and indexed as the parser seems to be fast enough (at least at i5 CPU).

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) {
	    return compile(source, true);
	}
	
	/**
     * Compiles classes in given source. The resulting .class files are added
     * to an internal list which can be later retrieved by getClassFiles() - 
     * but only if @param generate is true. 
     * 
     * @param source source code of the class as String.
     * @param generate if false, .class files are not generated (used to check source for errors)   
     * @return true, if compilation succeeded, false otherwise.
     */
	public boolean compile(String source, boolean generate) {
		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());
		}
	}
	

}