--- /dev/null
+package org.ibex.tool;
+
+import java.util.*;
+import java.io.*;
+
+import java.lang.reflect.*;
+
+import org.eclipse.jdt.core.compiler.IProblem;
+import org.eclipse.jdt.internal.compiler.ClassFile;
+import org.eclipse.jdt.internal.compiler.CompilationResult;
+//import org.eclipse.jdt.internal.compiler.Compiler;
+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.classfmt.ClassFileReader;
+import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
+import org.eclipse.jdt.internal.compiler.env.IBinaryField;
+import org.eclipse.jdt.internal.compiler.env.IBinaryMethod;
+import org.eclipse.jdt.internal.compiler.env.IBinaryNestedType;
+import org.eclipse.jdt.internal.compiler.env.IBinaryType;
+import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
+import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
+import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
+import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
+import org.eclipse.jdt.internal.compiler.impl.Constant;
+import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
+
+public class Compiler {
+
+ private ClassLoader loader = ClassLoader.getSystemClassLoader();
+ private Map loaded = new HashMap();
+ private Writer out = new PrintWriter(System.out);
+ private Preprocessor preprocessor = new Preprocessor(null, null, Collections.EMPTY_LIST);
+
+ private Source[] sources;
+ private ICompilationUnit[] units;
+
+
+ public static void main(String[] args) {
+ new Compiler(args);
+ }
+
+ public Compiler(String[] files) {
+ sources = new Source[files.length];
+ units = new ICompilationUnit[files.length];
+
+ for (int i=0; i < files.length; i++) {
+ char[][] n = classname(files[i]);
+ sources[i] = new Source(new File(files[i]), name(n), pack(n));
+ units[i] = sources[i].unit;
+ }
+
+ org.eclipse.jdt.internal.compiler.Compiler jdt =
+ new org.eclipse.jdt.internal.compiler.Compiler(
+ env, policy, settings, results, problems);
+ jdt.compile(units);
+ }
+
+
+ // Compiler Parameters ////////////////////////////////////////////////////
+
+ final static class LoadedNestedType implements IBinaryNestedType {
+ private Class c;
+ LoadedNestedType(Class c) { this.c = c; }
+ public char[] getName() { return name(c); }
+ public char[] getEnclosingTypeName() { return name(c.getDeclaringClass()); }
+ public int getModifiers() { return c.getModifiers(); }
+ }
+
+ final static class LoadedField implements IBinaryField {
+ private Field f;
+ LoadedField(Field f) { this.f = f; }
+ public Constant getConstant() { return Constant.NotAConstant; }// FIXME
+ public char[] getTypeName() { return typeName(f.getType()); }
+ public char[] getName() { return f.getName().toCharArray(); }
+ public int getModifiers() { return f.getModifiers(); }
+ }
+
+ private static char[] typeName(Class p) {
+ StringBuffer sb = new StringBuffer();
+ typeName(p, sb);
+ return sb.toString().toCharArray();
+ }
+ private static void typeName(Class p, StringBuffer sb) {
+ String name = p.getName();
+ switch (name.charAt(0)) {
+ case 'B': case 'C': case 'D': case 'F': case 'I':
+ case 'J': case 'S': case 'Z': case 'V': case '[':
+ sb.append(name); break;
+
+ case 'b': if (name.equals("boolean")) { sb.append('Z'); break; }
+ if (name.equals("byte")) { sb.append('B'); break; }
+ case 'c': if (name.equals("char")) { sb.append('C'); break; }
+ case 'd': if (name.equals("double")) { sb.append('D'); break; }
+ case 'f': if (name.equals("float")) { sb.append('F'); break; }
+ case 'i': if (name.equals("int")) { sb.append('I'); break; }
+ case 'l': if (name.equals("long")) { sb.append('J'); break; }
+ case 's': if (name.equals("short")) { sb.append('S'); break; }
+ case 'v': if (name.equals("void")) { sb.append('V'); break; }
+ default:
+ sb.append('L'); sb.append(name(p)); sb.append(';');
+ }
+ }
+ private static char[] descriptor(Class[] p, Class r) {
+ StringBuffer sb = new StringBuffer();
+ sb.append('(');
+ if (p != null) for (int i=0; i < p.length; i++) typeName(p[i], sb);
+ sb.append(')');
+ if (r != null) typeName(r, sb);
+ return sb.toString().toCharArray();
+ }
+
+ final static class LoadedConstructor implements IBinaryMethod {
+ private Constructor c;
+ private char[][] ex;
+ private char[] desc;
+
+ LoadedConstructor(Constructor c) {
+ this.c = c;
+
+ Class[] exs = c.getExceptionTypes();
+ ex = new char[exs.length][];
+ for (int i=0; i < ex.length; i++) ex[i] = name(exs[i]);
+
+ desc = descriptor(c.getParameterTypes(), null);
+ }
+ public int getModifiers() { return c.getModifiers(); }
+ public char[] getSelector() { return c.getName().toCharArray(); }
+ public boolean isConstructor() { return true; }
+ public boolean isClinit() { return false; }
+ public char[][] getArgumentNames() { return null; }
+ public char[][] getExceptionTypeNames() { return ex; }
+ public char[] getMethodDescriptor() { return desc; }
+ }
+
+ final static class LoadedMethod implements IBinaryMethod {
+ private Method m;
+ private char[][] ex;
+ private char[] desc;
+
+ LoadedMethod(Method m) {
+ this.m = m;
+
+ Class[] exs = m.getExceptionTypes();
+ ex = new char[exs.length][];
+ for (int i=0; i < ex.length; i++) ex[i] = name(exs[i]);
+
+ desc = descriptor(m.getParameterTypes(), m.getReturnType());
+ }
+ public int getModifiers() { return m.getModifiers(); }
+ public char[] getSelector() { return m.getName().toCharArray(); }
+ public boolean isConstructor() { return false; }
+ public boolean isClinit() { return false; }
+ public char[][] getArgumentNames() { return null; } // FIXME: does this do anything cool?
+ public char[][] getExceptionTypeNames() { return ex; }
+ public char[] getMethodDescriptor() { return desc; }
+ }
+
+ final static class LoadedClass implements IBinaryType {
+ private Class c;
+ private IBinaryField[] f;
+ private char[][] inf;
+ private IBinaryNestedType[] nested;
+ private IBinaryMethod[] meth = null;
+
+ LoadedClass(Class c) {
+ this.c = c;
+
+ Field[] fields = c.getFields();
+ f = new IBinaryField[fields.length];
+ for (int i=0; i < f.length; i++) f[i] = new LoadedField(fields[i]);
+
+ Class[] interfaces = c.getInterfaces();
+ inf = new char[interfaces.length][];
+ for (int i=0; i < inf.length; i++) inf[i] = name(interfaces[i]);
+
+ Class[] classes = c.getClasses();
+ nested = new IBinaryNestedType[classes.length];
+ for (int i=0; i < nested.length; i++)
+ nested[i] = new LoadedNestedType(classes[i]);
+
+ Constructor[] constructors = c.getConstructors();
+ Method[] methods = c.getDeclaredMethods();
+ if (methods.length + constructors.length > 0) {
+ meth = new IBinaryMethod[methods.length + constructors.length];
+ int i=0;
+ for (int j=0; j < methods.length; j++)
+ meth[i++] = new LoadedMethod(methods[j]);
+ for (int j=0; j < constructors.length; j++)
+ meth[i++] = new LoadedConstructor(constructors[j]);
+ }
+
+ }
+
+ public char[] getName() { return name(c); }
+ public char[] getEnclosingTypeName() { return name(c.getDeclaringClass()); }
+ public char[] getSuperclassName() { return name(c.getSuperclass()); }
+ public IBinaryField[] getFields() { return f; }
+ public char[][] getInterfaceNames() { return inf; }
+ public IBinaryNestedType[] getMemberTypes() { return nested; }
+ public IBinaryMethod[] getMethods() { return meth; }
+ public int getModifiers() { return c.getModifiers(); }
+
+ public boolean isBinaryType() { return true; }
+ public boolean isClass() { return true; }
+ public boolean isInterface() { return c.isInterface(); }
+ public boolean isAnonymous() { return false; }
+ public boolean isLocal() { return false; } // FIXME
+ public boolean isMember() { return false; } // FIXME
+ public char[] sourceFileName() { return null; }
+ public char[] getFileName() { return null; }
+ }
+
+
+ private final INameEnvironment env = new INameEnvironment() {
+ public NameEnvironmentAnswer findType(char[][] c) { return findType(name(c), pack(c)); }
+ public NameEnvironmentAnswer findType(char[] n, char[][] p) {
+ try {
+ Class c = Class.forName(str(p, '.') + '.' + new String(n));
+ IBinaryType b = (IBinaryType)loaded.get(c);
+ if (b == null) loaded.put(c, b = new LoadedClass(c));
+ return new NameEnvironmentAnswer(b);
+ } catch (ClassNotFoundException e) {}
+
+ try {
+ for (int i=0; i < sources.length; i++)
+ if (eq(n, sources[i].n) && eq(p, sources[i].p))
+ return sources[i].compiled == null ?
+ new NameEnvironmentAnswer(sources[i].unit) :
+ new NameEnvironmentAnswer(new ClassFileReader(sources[i].compiled,
+ sources[i].orig.getName().toCharArray(), true));
+ } catch (ClassFormatException e) {
+ System.out.println("FIXME unexpected ClassFormatException");
+ e.printStackTrace();
+ }
+ return null;
+ }
+ public boolean isPackage(char[][] parent, char[] name) {
+ String parentName = str(parent, '/');
+ for (int i=0; i < sources.length; i++)
+ if (eq(name, sources[i].n) && eq(parent, sources[i].p)) return false;
+ return
+ loader.getResource(parentName + ".class") == null &&
+ loader.getResource(parentName + '/' + name + ".class") == null;
+ }
+ public void cleanup() {}
+ };
+
+ private final IErrorHandlingPolicy policy =
+ DefaultErrorHandlingPolicies.proceedWithAllProblems();
+
+ private final Map settings = new HashMap();
+ {
+ settings.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.GENERATE);
+ settings.put(CompilerOptions.OPTION_SourceFileAttribute, CompilerOptions.GENERATE);
+ settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_3);
+ settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_1);
+ };
+
+ private final ICompilerRequestor results = new ICompilerRequestor() {
+ public void acceptResult(CompilationResult result) {
+ if (result.hasProblems()) {
+ boolean hasErrors = false;
+ IProblem[] p = result.getProblems();
+ try {
+ for (int i=0; i < p.length; i++) {
+ if (p[i].isError()) hasErrors = true;
+ out.write(p[i].getOriginatingFileName());
+ out.write(':');
+ out.write(p[i].getSourceLineNumber());
+ out.write(':');
+ out.write(p[i].getMessage());
+ }
+ } catch (IOException e) {
+ System.out.println("FIXME severe: IOException on out");
+ e.printStackTrace();
+ }
+ if (hasErrors) return;
+ }
+
+ ClassFile[] c = result.getClassFiles();
+ for (int i=0; i < c.length; i++) {
+ try {
+ String name = str(c[i].getCompoundName(), '/') + ".class";
+ OutputStream o = new BufferedOutputStream(
+ new FileOutputStream(new File(name)));
+ o.write(c[i].getBytes());
+ o.close();
+ } catch (IOException e) {
+ System.out.println("FIXME: IOException writing class");
+ e.printStackTrace();
+ }
+ }
+ }
+ };
+
+ private final IProblemFactory problems = new DefaultProblemFactory();
+
+ private final class Source {
+ char[] n; char[][] p;
+ File orig;
+ char[] processed = null;
+ byte[] compiled = null;
+
+ ICompilationUnit unit = new ICompilationUnit() {
+ public char[] getMainTypeName() { return n; }
+ public char[][] getPackageName() { return p; }
+ public char[] getFileName() { return orig.getName().toCharArray(); }
+ public char[] getContents() {
+ if (processed != null) return processed;
+
+ try {
+ Reader r = new InputStreamReader(new BufferedInputStream(
+ new FileInputStream(orig)));
+ StringWriter w = new StringWriter();
+ Vector err;
+
+ synchronized (preprocessor) {
+ preprocessor.setReader(r);
+ preprocessor.setWriter(w);
+ err = preprocessor.process();
+ }
+
+ if (err.size() > 0) {
+ System.out.println("Preprocessor Errors, "+err); // FIXME
+ return null;
+ }
+
+ processed = w.toString().toCharArray();
+ } catch (IOException e) {
+ System.out.println("FIXME: IOException: "+e); // FIXME
+ return null;
+ }
+
+ return processed;
+ }
+ };
+
+ private Source(File o, char[] n, char[][] p) {
+ orig = o; this.n = n; this.p = p; }
+ }
+
+ // Helper Functiosn ///////////////////////////////////////////////////////
+
+ /** Convert source file path into class name. */
+ private char[][] classname(String name) {
+ String delim = name.indexOf('/') == -1 ? "." : "/";
+ name = name.trim();
+ if (name.endsWith(".java")) name = name.substring(0, name.length() - 5);
+ if (name.startsWith(delim)) name = name.substring(1);
+
+ StringTokenizer st = new StringTokenizer(name, delim);
+ char[][] n = new char[st.countTokens()][];
+ for (int i=0; st.hasMoreTokens(); i++) n[i] = st.nextToken().toCharArray();
+ return n;
+ }
+
+ private static String str(char[][] name, char delim) {
+ StringBuffer b = new StringBuffer();
+ if (name != null) for (int i=0; i < name.length; i++)
+ if (name[i] != null) { b.append(delim); b.append(name[i]); }
+ if (b.length() > 0) b.deleteCharAt(0);
+ return b.toString();
+ }
+
+ private static char[][] pack(char[][] c) {
+ char[][] p = new char[c.length - 1][];
+ for (int i=0; i < p.length; i++) p[i] = c[i];
+ return p;
+ }
+
+ private static char[] name(char[][] c) { return c[c.length - 1]; }
+
+ private static boolean eq(char[][] c1, char[][] c2) {
+ if (c1.length != c2.length) return false;
+ for (int i=0; i < c1.length; i++) if (!eq(c1[i], c2[i])) return false;
+ return true;
+ }
+
+ private static boolean eq(char[] c1, char[] c2) {
+ if (c1.length != c2.length) return false;
+ for (int i=0; i < c1.length; i++) if (c1[i] != c2[i]) return false;
+ return true;
+ }
+
+ private static char[] name(Class c) {
+ return c == null ? null : c.getName().replace('.', '/').toCharArray();
+ }
+}