X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fibex%2Ftool%2FCompiler.java;h=26a1b4bb495ad8945c727db605280dcef0eff035;hb=904771e7a85738982e93f7d10ede273579cf93d6;hp=5b146728b6bbf47790db045c160d5580cf316a4c;hpb=022eeabce326e49a33884713fb51c59e27080cdd;p=org.ibex.tool.git diff --git a/src/java/org/ibex/tool/Compiler.java b/src/java/org/ibex/tool/Compiler.java index 5b14672..26a1b4b 100644 --- a/src/java/org/ibex/tool/Compiler.java +++ b/src/java/org/ibex/tool/Compiler.java @@ -82,17 +82,32 @@ public class Compiler { private ClassLoader loader = ClassLoader.getSystemClassLoader(); private Map loaded = new HashMap(); private PrintWriter out = new PrintWriter(System.out); - private Preprocessor preprocessor = new Preprocessor(null, null, Collections.EMPTY_LIST); + private Preprocessor preprocessor; private Source[] sources; private File builddir = new File("."); - private File sourcedir = new File("."); + private String sourcedir = null; + + private boolean hasErrors; + private PrintWriter warn; + + public Compiler() { + List defs = Collections.EMPTY_LIST; + + String define = System.getProperty("org.ibex.tool.preprocessor.define"); + if (define != null) { + defs = new ArrayList(); + StringTokenizer st = new StringTokenizer(define.toUpperCase(), ","); + while (st.hasMoreTokens()) defs.add(st.nextToken().trim()); + } + + preprocessor = new Preprocessor(null, null, defs); + } - public Compiler() { } public void setBuildDir(String dir) { builddir = new File(dir == null ? "." : dir); } - public void setSourceDir(String dir) { sourcedir = new File(dir == null ? "." : dir); } + public void setSourceDir(String dir) { sourcedir = dir; } /** Pass CompilerOptions.VERSION_1_*. A String of form "1.1", ".2", etc. */ public void setSource(String v) { settings.put(CompilerOptions.OPTION_Source, v); } @@ -104,16 +119,31 @@ public class Compiler { public void compile() { List s = new ArrayList(); - filterSources(s, sourcedir, new char[0][]); + filterSources(s, new File(sourcedir), new char[0][]); if (DEBUG) System.out.println("working with "+s.size() +" sources"); sources = new Source[s.size()]; s.toArray(sources); ICompilationUnit[] units = new ICompilationUnit[s.size()]; - for (int i=0; i < sources.length; i++) units[i] = sources[i].unit; + int u = 0; for (int i=0; i < sources.length; i++) + if (sources[i].compiled == null) units[u++] = sources[i].unit; + if (u != units.length) { + ICompilationUnit[] newu = new ICompilationUnit[u]; + System.arraycopy(units, 0, newu, 0, u); + units = newu; + } + + hasErrors = false; + + StringWriter sw = new StringWriter(); + StringBuffer w = sw.getBuffer(); + warn = new PrintWriter(sw); org.eclipse.jdt.internal.compiler.Compiler jdt = new org.eclipse.jdt.internal.compiler.Compiler( env, policy, settings, results, problems); jdt.compile(units); + + if (!hasErrors) { out.write(w.toString()); out.flush(); } + warn = null; } private final FilenameFilter filterSrcs = new FilenameFilter() { @@ -122,11 +152,72 @@ public class Compiler { private final FileFilter filterDirs = new FileFilter() { public boolean accept(File path) { return path.isDirectory(); } }; + private byte[] bytes(File bfile) { + byte[] bytes = new byte[2 * 1024]; + int pos = 0, in = 0; + try { + InputStream bin = new FileInputStream(bfile); + while ((in = bin.read(bytes, pos, bytes.length - pos)) != -1) { + pos += in; + if (pos == bytes.length) { + byte[] newbytes= new byte[pos * 2]; + System.arraycopy(bytes, 0, newbytes, 0, pos); + bytes = newbytes; + } + } + bin.close(); + } catch (IOException e) { + System.out.println("Error reading class file"); // FIXME + e.printStackTrace(); return null; + } + + if (pos != bytes.length) { + byte[] newbytes= new byte[pos * 2]; + System.arraycopy(bytes, 0, newbytes, 0, pos); + bytes = newbytes; + } + + return bytes; + } private void filterSources(List s, File dir, char[][] pack) { + List bclasses = new ArrayList(); + File bdir = new File(builddir, str(pack, File.separatorChar)); + + // add the java files in this directory File[] ja = dir.listFiles(filterSrcs); - for (int i=0; i < ja.length; i++) - s.add(new Source(ja[i], name(classname(ja[i].getName())), pack)); + for (int i=0; i < ja.length; i++) { + char[] n = name(classname(ja[i].getName())); + Source src = new Source(ja[i], n, pack); + + // skip the file if the build version is newer than the source + String bclass = new String(n); + File bfile = new File(bdir, bclass + ".class"); + if (bfile.lastModified() > ja[i].lastModified()) { + src.compiled = bytes(bfile); + bclasses.add(bclass); + } + + s.add(src); + } + // process the inner classes of any binary files + for (int i=0; i < bclasses.size(); i++) { + final String bc = (String)bclasses.get(i); + final FilenameFilter f = new FilenameFilter() { + public boolean accept(File dir, String name) { + return name.length() > bc.length() && name.startsWith(bc) && + name.charAt(bc.length()) == '$' && name.endsWith(".class"); + } + }; + File[] bfile = bdir.listFiles(f); + for (int j=0; j < bfile.length; j++) { + Source src = new Source(bfile[j], name(classname(bfile[j].getName())), pack); + src.compiled = bytes(bfile[j]); + s.add(src); + } + } + + // add the subdirectories as packages File[] d = dir.listFiles(filterDirs); for (int i=0; i < d.length; i++) { char[][] newpack = new char[pack.length + 1][]; @@ -144,6 +235,7 @@ public class Compiler { File orig; char[] processed = null; byte[] compiled = null; + ClassFileReader reader = null; final ICompilationUnit unit = new ICompilationUnit() { public char[] getMainTypeName() { return n; } @@ -181,8 +273,10 @@ public class Compiler { private Source(File o, char[] n, char[][] p) { orig = o; this.n = n; this.p = p; - try { fileName = orig.getCanonicalPath().toCharArray(); } // FIXME: dont use full path - catch (IOException e) { fileName = orig.getName().toCharArray(); } + String file = sourcedir; + file += File.separatorChar + str(p, File.separatorChar); + file += File.separatorChar + new String(n) + ".java"; + fileName = file.toCharArray(); } } @@ -305,7 +399,7 @@ public class Compiler { for (int i=0; i < nested.length; i++) nested[i] = new LoadedNestedType(classes[i]); - Constructor[] constructors = c.getConstructors(); + Constructor[] constructors = c.getDeclaredConstructors(); Method[] methods = c.getDeclaredMethods(); if (methods.length + constructors.length > 0) { meth = new IBinaryMethod[methods.length + constructors.length]; @@ -354,10 +448,9 @@ public class Compiler { try { Class c = Class.forName(str(p, '.') + '.' + new String(n)); - if (DEBUG) System.out.println("found class: "+ c); + if (DEBUG) System.out.println(" found in classloader: "+ c); IBinaryType b = (IBinaryType)loaded.get(c); if (b == null) loaded.put(c, b = new LoadedClass(c)); - if (DEBUG) System.out.println("returning "+b+", name="+new String(b.getName())); return new NameEnvironmentAnswer(b); } catch (ClassNotFoundException e) {} @@ -365,12 +458,23 @@ public class Compiler { if (p.length > 0 && eq(p[0], "java".toCharArray())) return null; 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)); + for (int i=0; i < sources.length; i++) { + Source s = sources[i]; + if (eq(n, s.n) && eq(p, s.p)) { + if (s.reader != null) { + if (DEBUG) System.out.println(" found reader"); + return new NameEnvironmentAnswer(s.reader); + } + if (s.compiled != null) { + if (DEBUG) System.out.println(" found compiled, new reader"); + s.reader = new ClassFileReader(s.compiled, + s.orig.getName().toCharArray(), true); + return new NameEnvironmentAnswer(s.reader); + } + if (DEBUG) System.out.println(" found unit"); + return new NameEnvironmentAnswer(sources[i].unit); + } + } } catch (ClassFormatException e) { System.out.println("Unexpected ClassFormatException"); // FIXME e.printStackTrace(); @@ -406,18 +510,22 @@ public class Compiler { public void acceptResult(CompilationResult result) { if (DEBUG) System.out.println("got result: "+result); if (result.hasProblems()) { - boolean hasErrors = false; + boolean err = false; IProblem[] p = result.getProblems(); + PrintWriter o; for (int i=0; i < p.length; i++) { - if (p[i].isError()) hasErrors = true; - out.print(p[i].getOriginatingFileName()); - out.print(':'); - out.print(p[i].getSourceLineNumber()); - out.print(':'); - out.println(p[i].getMessage()); + if (p[i].isError()) { o = out; err = true; } + else o = warn; + + o.print(p[i].getOriginatingFileName()); + o.print(':'); + o.print(p[i].getSourceLineNumber()); + o.print(':'); + o.print(p[i].isError() ? " error: " : " warning: "); + o.println(p[i].getMessage()); } out.flush(); - if (hasErrors) return; + if (err) { hasErrors = true; return; } } ClassFile[] c = result.getClassFiles(); @@ -457,6 +565,7 @@ public class Compiler { String delim = name.indexOf('/') == -1 ? "." : "/"; name = name.trim(); if (name.endsWith(".java")) name = name.substring(0, name.length() - 5); + if (name.endsWith(".class")) name = name.substring(0, name.length() - 6); if (name.startsWith(delim)) name = name.substring(1); StringTokenizer st = new StringTokenizer(name, delim);