diff --git a/src/java/com/threerings/presents/tools/SourceFile.java b/src/java/com/threerings/presents/tools/SourceFile.java index 81fee76a9..2d826f35c 100644 --- a/src/java/com/threerings/presents/tools/SourceFile.java +++ b/src/java/com/threerings/presents/tools/SourceFile.java @@ -21,7 +21,7 @@ package com.threerings.presents.tools; -import java.util.ArrayList; +import java.util.List; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -47,20 +47,18 @@ public class SourceFile throws IOException { // slurp our source file into newline separated strings + _lines = Lists.newArrayList(); BufferedReader bin = new BufferedReader(new FileReader(source)); - ArrayList llist = Lists.newArrayList(); String line = null; while ((line = bin.readLine()) != null) { - llist.add(line); + _lines.add(line); } - maybeAddGeneratedImport(llist); - _lines = llist.toArray(new String[llist.size()]); bin.close(); // now determine where to insert our static field declarations and our generated methods int bstart = -1, bend = -1; - for (int ii = 0; ii < _lines.length; ii++) { - line = _lines[ii].trim(); + for (int ii = 0, nn = _lines.size(); ii < nn; ii++) { + line = _lines.get(ii).trim(); // look for the start of the class body if (GenUtil.NAME_PATTERN.matcher(line).find()) { @@ -69,7 +67,7 @@ public class SourceFile } else { // search down a few lines for the open brace for (int oo = 1; oo < 10; oo++) { - if (get(_lines, ii+oo).trim().endsWith("{")) { + if (safeGetLine(ii+oo).trim().endsWith("{")) { bstart = ii+oo+1; break; } @@ -115,22 +113,14 @@ public class SourceFile */ public boolean containsString (String text) { - for (int ii = 0; ii < _nstart; ii++) { - if (_lines[ii].indexOf(text) != -1) { + for (int ii = 0, nn = _lines.size(); ii < nn; ii++) { + // don't look inside the autogenerated areas + if (!(ii >= _nstart && ii < _nend) && !(ii >= _mstart && ii < _mend) && + _lines.get(ii).contains(text)) { return true; } } - for (int ii = _nend; ii < _mstart; ii++) { - if (_lines[ii].indexOf(text) != -1) { - return true; - } - } - for (int ii = _mend; ii < _lines.length; ii++) { - if (_lines[ii].indexOf(text) != -1) { - return true; - } - } - return false; + return true; } /** @@ -144,47 +134,50 @@ public class SourceFile { BufferedWriter bout = new BufferedWriter(new FileWriter(dest)); + addOrRemoveGeneratedImport(StringUtil.deNull(fsection).contains("@Generated(") || + StringUtil.deNull(msection).contains("@Generated(")); + // write the preamble for (int ii = 0; ii < _nstart; ii++) { - writeln(bout, _lines[ii]); + writeln(bout, _lines.get(ii)); } // write the field section if (!StringUtil.isBlank(fsection)) { - String prev = get(_lines, _nstart-1); + String prev = safeGetLine(_nstart-1); if (!StringUtil.isBlank(prev) && !prev.equals("{")) { bout.newLine(); } writeln(bout, " " + FIELDS_START); bout.write(fsection); writeln(bout, " " + FIELDS_END); - if (!StringUtil.isBlank(get(_lines, _nend))) { + if (!StringUtil.isBlank(safeGetLine(_nend))) { bout.newLine(); } } // write the mid-amble for (int ii = _nend; ii < _mstart; ii++) { - writeln(bout, _lines[ii]); + writeln(bout, _lines.get(ii)); } // write the method section if (!StringUtil.isBlank(msection)) { - if (!StringUtil.isBlank(get(_lines, _mstart-1))) { + if (!StringUtil.isBlank(safeGetLine(_mstart-1))) { bout.newLine(); } writeln(bout, " " + METHODS_START); bout.write(msection); writeln(bout, " " + METHODS_END); - String next = get(_lines, _mend); + String next = safeGetLine(_mend); if (!StringUtil.isBlank(next) && !next.equals("}")) { bout.newLine(); } } // write the postamble - for (int ii = _mend; ii < _lines.length; ii++) { - writeln(bout, _lines[ii]); + for (int ii = _mend, nn = _lines.size(); ii < nn; ii++) { + writeln(bout, _lines.get(ii)); } bout.close(); } @@ -202,9 +195,9 @@ public class SourceFile /** Safely gets the indexth line, returning the empty string if we exceed the * length of the array. */ - protected String get (String[] lines, int index) + protected String safeGetLine (int index) { - return (index < lines.length) ? lines[index] : ""; + return (index < _lines.size()) ? _lines.get(index) : ""; } /** Helper function for writing a string and a newline to a writer. */ @@ -216,19 +209,24 @@ public class SourceFile } /** - * Add an import for "@Generated", if needed. + * Add or remove an import for "@Generated", if needed. */ - protected void maybeAddGeneratedImport (ArrayList lines) + protected void addOrRemoveGeneratedImport (boolean add) { final String IMPORT = "import javax.annotation.Generated;"; int packageLine = -1; + int importLine = -1; int lastJavaImport = -1; int firstNonJavaImport = -1; - for (int ii = 0, nn = lines.size(); ii < nn; ii++) { - String line = lines.get(ii).trim(); - if (line.equals(IMPORT)) { - return; // we already got one! + for (int ii = 0, nn = _lines.size(); ii < nn; ii++) { + String line = _lines.get(ii); + if (line.startsWith(IMPORT)) { + if (add) { + return; // we already got one! + } + importLine = ii; + break; } else if (line.startsWith("package ")) { packageLine = ii; @@ -241,20 +239,28 @@ public class SourceFile } } - int insertPoint; - if (lastJavaImport != -1) { - insertPoint = lastJavaImport + 1; + if (importLine != -1) { + // we must be removing, or we'd have already exited + _lines.remove(importLine); - } else if (firstNonJavaImport != -1) { - insertPoint = firstNonJavaImport; + } else if (!add) { + return; // it's already not there! } else { - insertPoint = packageLine + 1; + importLine = (lastJavaImport != -1) ? lastJavaImport + 1 + : ((firstNonJavaImport != -1) ? firstNonJavaImport : packageLine + 1); + _lines.add(importLine, IMPORT); } - lines.add(insertPoint, IMPORT); + + // the import line is always above these other lines, so they can be adjusted wholesale + int adjustment = add ? 1 : -1; + _nstart += adjustment; + _nend += adjustment; + _mstart += adjustment; + _mend += adjustment; } - protected String[] _lines; + protected List _lines; protected int _nstart = -1, _nend = -1; protected int _mstart = -1, _mend = -1;