+ 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, String srcdir) {
+ 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++) {
+ char[] n = name(classname(ja[i].getName()));
+ Source src = new Source(ja[i], n, pack, srcdir);
+
+ // 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, srcdir);
+ 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][];
+ for (int j=0; j < pack.length; j++) newpack[j] = pack[j];
+ newpack[pack.length] = d[i].getName().toCharArray();
+ filterSources(s, d[i], newpack, srcdir);
+ }
+ }
+
+
+ /** Represents a file to be compiled. */
+ final class Source {
+ char[] fileName;
+ char[] n; char[][] p;
+ File orig;
+ char[] processed = null;
+ byte[] compiled = null;
+ ClassFileReader reader = null;
+
+ final ICompilationUnit unit = new ICompilationUnit() {
+ public char[] getMainTypeName() { return n; }
+ public char[][] getPackageName() { return p; }
+ public char[] getFileName() { return fileName; }
+ 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("IOException: "+e); // FIXME
+ return null;
+ }
+
+ return processed;
+ }
+ };
+
+ private Source(File o, char[] n, char[][] p, String srcdir) {
+ orig = o; this.n = n; this.p = p;
+ String file = srcdir;
+ file += File.separatorChar + str(p, File.separatorChar);
+ file += File.separatorChar + new String(n) + ".java";
+ fileName = file.toCharArray();
+ }
+ }
+
+ // ClassLoader Wrappers ///////////////////////////////////////////////////