When generating dobj files, add or remove the Generated import depending
on whether we need it. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6055 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -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<String> 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 <code>index</code>th 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<String> 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<String> _lines;
|
||||
|
||||
protected int _nstart = -1, _nend = -1;
|
||||
protected int _mstart = -1, _mend = -1;
|
||||
|
||||
Reference in New Issue
Block a user