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;
|
package com.threerings.presents.tools;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.List;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
@@ -47,20 +47,18 @@ public class SourceFile
|
|||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
// slurp our source file into newline separated strings
|
// slurp our source file into newline separated strings
|
||||||
|
_lines = Lists.newArrayList();
|
||||||
BufferedReader bin = new BufferedReader(new FileReader(source));
|
BufferedReader bin = new BufferedReader(new FileReader(source));
|
||||||
ArrayList<String> llist = Lists.newArrayList();
|
|
||||||
String line = null;
|
String line = null;
|
||||||
while ((line = bin.readLine()) != null) {
|
while ((line = bin.readLine()) != null) {
|
||||||
llist.add(line);
|
_lines.add(line);
|
||||||
}
|
}
|
||||||
maybeAddGeneratedImport(llist);
|
|
||||||
_lines = llist.toArray(new String[llist.size()]);
|
|
||||||
bin.close();
|
bin.close();
|
||||||
|
|
||||||
// now determine where to insert our static field declarations and our generated methods
|
// now determine where to insert our static field declarations and our generated methods
|
||||||
int bstart = -1, bend = -1;
|
int bstart = -1, bend = -1;
|
||||||
for (int ii = 0; ii < _lines.length; ii++) {
|
for (int ii = 0, nn = _lines.size(); ii < nn; ii++) {
|
||||||
line = _lines[ii].trim();
|
line = _lines.get(ii).trim();
|
||||||
|
|
||||||
// look for the start of the class body
|
// look for the start of the class body
|
||||||
if (GenUtil.NAME_PATTERN.matcher(line).find()) {
|
if (GenUtil.NAME_PATTERN.matcher(line).find()) {
|
||||||
@@ -69,7 +67,7 @@ public class SourceFile
|
|||||||
} else {
|
} else {
|
||||||
// search down a few lines for the open brace
|
// search down a few lines for the open brace
|
||||||
for (int oo = 1; oo < 10; oo++) {
|
for (int oo = 1; oo < 10; oo++) {
|
||||||
if (get(_lines, ii+oo).trim().endsWith("{")) {
|
if (safeGetLine(ii+oo).trim().endsWith("{")) {
|
||||||
bstart = ii+oo+1;
|
bstart = ii+oo+1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -115,22 +113,14 @@ public class SourceFile
|
|||||||
*/
|
*/
|
||||||
public boolean containsString (String text)
|
public boolean containsString (String text)
|
||||||
{
|
{
|
||||||
for (int ii = 0; ii < _nstart; ii++) {
|
for (int ii = 0, nn = _lines.size(); ii < nn; ii++) {
|
||||||
if (_lines[ii].indexOf(text) != -1) {
|
// don't look inside the autogenerated areas
|
||||||
|
if (!(ii >= _nstart && ii < _nend) && !(ii >= _mstart && ii < _mend) &&
|
||||||
|
_lines.get(ii).contains(text)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int ii = _nend; ii < _mstart; ii++) {
|
return true;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -144,47 +134,50 @@ public class SourceFile
|
|||||||
{
|
{
|
||||||
BufferedWriter bout = new BufferedWriter(new FileWriter(dest));
|
BufferedWriter bout = new BufferedWriter(new FileWriter(dest));
|
||||||
|
|
||||||
|
addOrRemoveGeneratedImport(StringUtil.deNull(fsection).contains("@Generated(") ||
|
||||||
|
StringUtil.deNull(msection).contains("@Generated("));
|
||||||
|
|
||||||
// write the preamble
|
// write the preamble
|
||||||
for (int ii = 0; ii < _nstart; ii++) {
|
for (int ii = 0; ii < _nstart; ii++) {
|
||||||
writeln(bout, _lines[ii]);
|
writeln(bout, _lines.get(ii));
|
||||||
}
|
}
|
||||||
|
|
||||||
// write the field section
|
// write the field section
|
||||||
if (!StringUtil.isBlank(fsection)) {
|
if (!StringUtil.isBlank(fsection)) {
|
||||||
String prev = get(_lines, _nstart-1);
|
String prev = safeGetLine(_nstart-1);
|
||||||
if (!StringUtil.isBlank(prev) && !prev.equals("{")) {
|
if (!StringUtil.isBlank(prev) && !prev.equals("{")) {
|
||||||
bout.newLine();
|
bout.newLine();
|
||||||
}
|
}
|
||||||
writeln(bout, " " + FIELDS_START);
|
writeln(bout, " " + FIELDS_START);
|
||||||
bout.write(fsection);
|
bout.write(fsection);
|
||||||
writeln(bout, " " + FIELDS_END);
|
writeln(bout, " " + FIELDS_END);
|
||||||
if (!StringUtil.isBlank(get(_lines, _nend))) {
|
if (!StringUtil.isBlank(safeGetLine(_nend))) {
|
||||||
bout.newLine();
|
bout.newLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// write the mid-amble
|
// write the mid-amble
|
||||||
for (int ii = _nend; ii < _mstart; ii++) {
|
for (int ii = _nend; ii < _mstart; ii++) {
|
||||||
writeln(bout, _lines[ii]);
|
writeln(bout, _lines.get(ii));
|
||||||
}
|
}
|
||||||
|
|
||||||
// write the method section
|
// write the method section
|
||||||
if (!StringUtil.isBlank(msection)) {
|
if (!StringUtil.isBlank(msection)) {
|
||||||
if (!StringUtil.isBlank(get(_lines, _mstart-1))) {
|
if (!StringUtil.isBlank(safeGetLine(_mstart-1))) {
|
||||||
bout.newLine();
|
bout.newLine();
|
||||||
}
|
}
|
||||||
writeln(bout, " " + METHODS_START);
|
writeln(bout, " " + METHODS_START);
|
||||||
bout.write(msection);
|
bout.write(msection);
|
||||||
writeln(bout, " " + METHODS_END);
|
writeln(bout, " " + METHODS_END);
|
||||||
String next = get(_lines, _mend);
|
String next = safeGetLine(_mend);
|
||||||
if (!StringUtil.isBlank(next) && !next.equals("}")) {
|
if (!StringUtil.isBlank(next) && !next.equals("}")) {
|
||||||
bout.newLine();
|
bout.newLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// write the postamble
|
// write the postamble
|
||||||
for (int ii = _mend; ii < _lines.length; ii++) {
|
for (int ii = _mend, nn = _lines.size(); ii < nn; ii++) {
|
||||||
writeln(bout, _lines[ii]);
|
writeln(bout, _lines.get(ii));
|
||||||
}
|
}
|
||||||
bout.close();
|
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
|
/** Safely gets the <code>index</code>th line, returning the empty string if we exceed the
|
||||||
* length of the array. */
|
* 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. */
|
/** 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;";
|
final String IMPORT = "import javax.annotation.Generated;";
|
||||||
|
|
||||||
int packageLine = -1;
|
int packageLine = -1;
|
||||||
|
int importLine = -1;
|
||||||
int lastJavaImport = -1;
|
int lastJavaImport = -1;
|
||||||
int firstNonJavaImport = -1;
|
int firstNonJavaImport = -1;
|
||||||
for (int ii = 0, nn = lines.size(); ii < nn; ii++) {
|
for (int ii = 0, nn = _lines.size(); ii < nn; ii++) {
|
||||||
String line = lines.get(ii).trim();
|
String line = _lines.get(ii);
|
||||||
if (line.equals(IMPORT)) {
|
if (line.startsWith(IMPORT)) {
|
||||||
return; // we already got one!
|
if (add) {
|
||||||
|
return; // we already got one!
|
||||||
|
}
|
||||||
|
importLine = ii;
|
||||||
|
break;
|
||||||
|
|
||||||
} else if (line.startsWith("package ")) {
|
} else if (line.startsWith("package ")) {
|
||||||
packageLine = ii;
|
packageLine = ii;
|
||||||
@@ -241,20 +239,28 @@ public class SourceFile
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int insertPoint;
|
if (importLine != -1) {
|
||||||
if (lastJavaImport != -1) {
|
// we must be removing, or we'd have already exited
|
||||||
insertPoint = lastJavaImport + 1;
|
_lines.remove(importLine);
|
||||||
|
|
||||||
} else if (firstNonJavaImport != -1) {
|
} else if (!add) {
|
||||||
insertPoint = firstNonJavaImport;
|
return; // it's already not there!
|
||||||
|
|
||||||
} else {
|
} 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 _nstart = -1, _nend = -1;
|
||||||
protected int _mstart = -1, _mend = -1;
|
protected int _mstart = -1, _mend = -1;
|
||||||
|
|||||||
Reference in New Issue
Block a user