Browse Source

Unwrap some logic; uniform checks of StringBuilder size

master
Gintas Grigelionis 7 years ago
parent
commit
cda6c91cf8
11 changed files with 95 additions and 102 deletions
  1. +27
    -26
      src/main/org/apache/tools/ant/Target.java
  2. +31
    -35
      src/main/org/apache/tools/ant/dispatch/DispatchUtils.java
  3. +6
    -10
      src/main/org/apache/tools/ant/taskdefs/Ant.java
  4. +5
    -5
      src/main/org/apache/tools/ant/types/Commandline.java
  5. +1
    -1
      src/main/org/apache/tools/ant/types/PropertySet.java
  6. +1
    -1
      src/main/org/apache/tools/ant/util/FileUtils.java
  7. +5
    -5
      src/main/org/apache/tools/ant/util/ResourceUtils.java
  8. +3
    -3
      src/main/org/apache/tools/ant/util/StringTokenizer.java
  9. +10
    -14
      src/main/org/apache/tools/zip/ZipOutputStream.java
  10. +3
    -1
      src/tutorial/tasks-filesets-properties/04-lists/src/Find.java
  11. +3
    -1
      src/tutorial/tasks-filesets-properties/final/src/Find.java

+ 27
- 26
src/main/org/apache/tools/ant/Target.java View File

@@ -145,36 +145,37 @@ public class Target implements TaskContainer {
public static List<String> parseDepends(String depends, public static List<String> parseDepends(String depends,
String targetName, String targetName,
String attributeName) { String attributeName) {
if (depends.isEmpty()) {
return new ArrayList<>();
}

List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
if (depends.length() > 0) {
StringTokenizer tok =
new StringTokenizer(depends, ",", true);
while (tok.hasMoreTokens()) {
String token = tok.nextToken().trim();

// Make sure the dependency is not empty string
if ("".equals(token) || ",".equals(token)) {
StringTokenizer tok = new StringTokenizer(depends, ",", true);
while (tok.hasMoreTokens()) {
String token = tok.nextToken().trim();

// Make sure the dependency is not empty string
if (token.isEmpty() || ",".equals(token)) {
throw new BuildException("Syntax Error: "
+ attributeName
+ " attribute of target \""
+ targetName
+ "\" contains an empty string.");
}

list.add(token);

// Make sure that depends attribute does not
// end in a ,
if (tok.hasMoreTokens()) {
token = tok.nextToken();
if (!tok.hasMoreTokens() || !",".equals(token)) {
throw new BuildException("Syntax Error: " throw new BuildException("Syntax Error: "
+ attributeName + attributeName
+ " attribute of target \""
+ " attribute for target \""
+ targetName + targetName
+ "\" contains an empty string.");
}

list.add(token);

// Make sure that depends attribute does not
// end in a ,
if (tok.hasMoreTokens()) {
token = tok.nextToken();
if (!tok.hasMoreTokens() || !",".equals(token)) {
throw new BuildException("Syntax Error: "
+ attributeName
+ " attribute for target \""
+ targetName
+ "\" ends with a \",\" "
+ "character");
}
+ "\" ends with a \",\" "
+ "character");
} }
} }
} }


+ 31
- 35
src/main/org/apache/tools/ant/dispatch/DispatchUtils.java View File

@@ -52,44 +52,40 @@ public class DispatchUtils {
String mName = null; String mName = null;
try { try {
final String name = dispatchable.getActionParameterName(); final String name = dispatchable.getActionParameterName();
if (name != null && name.trim().length() > 0) {
mName = "get" + name.trim().substring(0, 1).toUpperCase();
if (name.length() > 1) {
mName += name.substring(1);
}
final Class<? extends Dispatchable> c = dispatchable.getClass();
final Method actionM = c.getMethod(mName);
if (actionM != null) {
final Object o = actionM.invoke(dispatchable, (Object[]) null);
if (o != null) {
final String s = o.toString().trim();
if (s.length() > 0) {
methodName = s;
Method executeM = dispatchable.getClass().getMethod(methodName);
if (executeM == null) {
throw new BuildException(
"No public " + methodName + "() in "
+ dispatchable.getClass());
}
executeM.invoke(dispatchable, (Object[]) null);
if (task instanceof UnknownElement) {
((UnknownElement) task).setRealThing(null);
}
} else {
throw new BuildException(
"Dispatchable Task attribute '" + name.trim()
+ "' not set or value is empty.");
}
} else {
throw new BuildException(
"Dispatchable Task attribute '" + name.trim()
+ "' not set or value is empty.");
}
}
} else {
if (name == null || name.trim().isEmpty()) {
throw new BuildException( throw new BuildException(
"Action Parameter Name must not be empty for Dispatchable Task."); "Action Parameter Name must not be empty for Dispatchable Task.");
} }
mName = "get" + name.trim().substring(0, 1).toUpperCase();
if (name.length() > 1) {
mName += name.substring(1);
}
final Class<? extends Dispatchable> c = dispatchable.getClass();
final Method actionM = c.getMethod(mName);
if (actionM != null) {
final Object o = actionM.invoke(dispatchable, (Object[]) null);
if (o == null) {
throw new BuildException(
"Dispatchable Task attribute '" + name.trim()
+ "' not set or value is empty.");
}
methodName = o.toString().trim();
if (methodName.isEmpty()) {
throw new BuildException(
"Dispatchable Task attribute '" + name.trim()
+ "' not set or value is empty.");
}
Method executeM = dispatchable.getClass().getMethod(methodName);
if (executeM == null) {
throw new BuildException(
"No public " + methodName + "() in "
+ dispatchable.getClass());
}
executeM.invoke(dispatchable, (Object[]) null);
if (task instanceof UnknownElement) {
((UnknownElement) task).setRealThing(null);
}
}
} catch (NoSuchMethodException nsme) { } catch (NoSuchMethodException nsme) {
throw new BuildException("No public " + mName + "() in " + task.getClass()); throw new BuildException("No public " + mName + "() in " + task.getClass());
} }


+ 6
- 10
src/main/org/apache/tools/ant/taskdefs/Ant.java View File

@@ -383,19 +383,15 @@ public class Ant extends Task {
String thisAntFile = getProject().getProperty(MagicNames.ANT_FILE); String thisAntFile = getProject().getProperty(MagicNames.ANT_FILE);
// Are we trying to call the target in which we are defined (or // Are we trying to call the target in which we are defined (or
// the build file if this is a top level task)? // the build file if this is a top level task)?
if (thisAntFile != null
&& file.equals(getProject().resolveFile(thisAntFile))
&& getOwningTarget() != null) {

if ("".equals(getOwningTarget().getName())) {
if ("antcall".equals(getTaskName())) {
throw new BuildException(
"antcall must not be used at the top level.");
}
if (thisAntFile != null && file.equals(getProject().resolveFile(thisAntFile))
&& getOwningTarget() != null && getOwningTarget().getName().isEmpty()) {
if ("antcall".equals(getTaskName())) {
throw new BuildException( throw new BuildException(
"antcall must not be used at the top level.");
}
throw new BuildException(
"%s task at the top level must not invoke its own build file.", "%s task at the top level must not invoke its own build file.",
getTaskName()); getTaskName());
}
} }


try { try {


+ 5
- 5
src/main/org/apache/tools/ant/types/Commandline.java View File

@@ -452,11 +452,11 @@ public class Commandline implements Cloneable {
} }
// path containing one or more elements // path containing one or more elements
final StringBuilder result = new StringBuilder(); final StringBuilder result = new StringBuilder();
for (int i = 0; i < line.length; i++) {
if (i > 0) {
for (String l : line) {
if (result.length() > 0) {
result.append(' '); result.append(' ');
} }
result.append(quoteArgument(line[i]));
result.append(quoteArgument(l));
} }
return result.toString(); return result.toString();
} }
@@ -508,7 +508,7 @@ public class Commandline implements Cloneable {
} else if ("\"".equals(nextTok)) { } else if ("\"".equals(nextTok)) {
state = inDoubleQuote; state = inDoubleQuote;
} else if (" ".equals(nextTok)) { } else if (" ".equals(nextTok)) {
if (lastTokenHasBeenQuoted || current.length() != 0) {
if (lastTokenHasBeenQuoted || current.length() > 0) {
result.add(current.toString()); result.add(current.toString());
current.setLength(0); current.setLength(0);
} }
@@ -519,7 +519,7 @@ public class Commandline implements Cloneable {
break; break;
} }
} }
if (lastTokenHasBeenQuoted || current.length() != 0) {
if (lastTokenHasBeenQuoted || current.length() > 0) {
result.add(current.toString()); result.add(current.toString());
} }
if (state == inQuote || state == inDoubleQuote) { if (state == inQuote || state == inDoubleQuote) {


+ 1
- 1
src/main/org/apache/tools/ant/types/PropertySet.java View File

@@ -502,7 +502,7 @@ public class PropertySet extends DataType implements ResourceCollection {
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
TreeMap<String, Object> sorted = new TreeMap<>(getPropertyMap()); TreeMap<String, Object> sorted = new TreeMap<>(getPropertyMap());
for (Entry<String, Object> e : sorted.entrySet()) { for (Entry<String, Object> e : sorted.entrySet()) {
if (b.length() != 0) {
if (b.length() > 0) {
b.append(", "); b.append(", ");
} }
b.append(e.getKey()); b.append(e.getKey());


+ 1
- 1
src/main/org/apache/tools/ant/util/FileUtils.java View File

@@ -710,7 +710,7 @@ public class FileUtils {
String pathComponent = tokenizer.nextToken(); String pathComponent = tokenizer.nextToken();
pathComponent = pathComponent.replace('/', File.separatorChar); pathComponent = pathComponent.replace('/', File.separatorChar);
pathComponent = pathComponent.replace('\\', File.separatorChar); pathComponent = pathComponent.replace('\\', File.separatorChar);
if (path.length() != 0) {
if (path.length() > 0) {
path.append(File.pathSeparatorChar); path.append(File.pathSeparatorChar);
} }
path.append(pathComponent); path.append(pathComponent);


+ 5
- 5
src/main/org/apache/tools/ant/util/ResourceUtils.java View File

@@ -176,8 +176,10 @@ public class ResourceUtils {
final Union result = new Union(); final Union result = new Union();
for (final Resource sr : source) { for (final Resource sr : source) {
String srName = sr.getName(); String srName = sr.getName();
srName = srName == null
? srName : srName.replace('/', File.separatorChar);
if (srName != null) {
srName = srName.replace('/', File.separatorChar);
}



String[] targetnames = null; String[] targetnames = null;
try { try {
@@ -653,7 +655,6 @@ public class ResourceUtils {


final LineTokenizer lineTokenizer = new LineTokenizer(); final LineTokenizer lineTokenizer = new LineTokenizer();
lineTokenizer.setIncludeDelims(true); lineTokenizer.setIncludeDelims(true);
String newline = null;
String line = lineTokenizer.getToken(in); String line = lineTokenizer.getToken(in);
while (line != null) { while (line != null) {
if (line.length() == 0) { if (line.length() == 0) {
@@ -661,8 +662,7 @@ public class ResourceUtils {
// returned with the end of line delimiter // returned with the end of line delimiter
out.newLine(); out.newLine();
} else { } else {
newline = filters.replaceTokens(line);
out.write(newline);
out.write(filters.replaceTokens(line));
} }
line = lineTokenizer.getToken(in); line = lineTokenizer.getToken(in);
} }


+ 3
- 3
src/main/org/apache/tools/ant/util/StringTokenizer.java View File

@@ -104,10 +104,10 @@ public class StringTokenizer extends ProjectComponent implements Tokenizer {
if (inToken) { if (inToken) {
if (isDelim) { if (isDelim) {
if (delimsAreTokens) { if (delimsAreTokens) {
if (word.length() == 0) {
word.append(c);
} else {
if (word.length() > 0) {
pushed = ch; pushed = ch;
} else {
word.append(c);
} }
break; break;
} }


+ 10
- 14
src/main/org/apache/tools/zip/ZipOutputStream.java View File

@@ -1148,20 +1148,16 @@ public class ZipOutputStream extends FilterOutputStream {
} }


String comm = ze.getComment(); String comm = ze.getComment();
if (comm != null && !"".equals(comm)) {

boolean commentEncodable = zipEncoding.canEncode(comm);

if (createUnicodeExtraFields == UnicodeExtraFieldPolicy.ALWAYS
|| !commentEncodable) {
ByteBuffer commentB = getEntryEncoding(ze).encode(comm);
ze.addExtraField(new UnicodeCommentExtraField(comm,
commentB.array(),
commentB.arrayOffset(),
commentB.limit()
- commentB.position())
);
}
if (comm == null || comm.isEmpty()) {
return;
}

if (createUnicodeExtraFields == UnicodeExtraFieldPolicy.ALWAYS
|| !zipEncoding.canEncode(comm)) {
ByteBuffer commentB = getEntryEncoding(ze).encode(comm);
ze.addExtraField(new UnicodeCommentExtraField(comm,
commentB.array(), commentB.arrayOffset(),
commentB.limit() - commentB.position()));
} }
} }




+ 3
- 1
src/tutorial/tasks-filesets-properties/04-lists/src/Find.java View File

@@ -79,7 +79,9 @@ public class Find extends Task {
// create list // create list
StringBuilder list = new StringBuilder(); StringBuilder list = new StringBuilder();
for (String file : foundFiles) { for (String file : foundFiles) {
if (list.length() > 0) list.append(delimiter);
if (list.length() > 0) {
list.append(delimiter);
}
list.append(file); list.append(file);
} }
rv = list.toString(); rv = list.toString();


+ 3
- 1
src/tutorial/tasks-filesets-properties/final/src/Find.java View File

@@ -84,7 +84,9 @@ public class Find extends Task {
// create list // create list
StringBuilder list = new StringBuilder(); StringBuilder list = new StringBuilder();
for (String file : foundFiles) { for (String file : foundFiles) {
if (list.length() > 0) list.append(delimiter);
if (list.length() > 0) {
list.append(delimiter);
}
list.append(file); list.append(file);
} }
rv = list.toString(); rv = list.toString();


Loading…
Cancel
Save