Repository: ant Updated Branches: refs/heads/master 5773626fd -> cda6c91cf
Unwrap some logic; uniform checks of StringBuilder size Project: http://git-wip-us.apache.org/repos/asf/ant/repo Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/cda6c91c Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/cda6c91c Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/cda6c91c Branch: refs/heads/master Commit: cda6c91cf800ba7be7fb479ce8e3264c7ca7c232 Parents: 5773626 Author: Gintas Grigelionis <gin...@apache.org> Authored: Fri Apr 6 12:26:59 2018 +0200 Committer: Gintas Grigelionis <gin...@apache.org> Committed: Fri Apr 6 14:26:54 2018 +0200 ---------------------------------------------------------------------- src/main/org/apache/tools/ant/Target.java | 53 ++++++++-------- .../tools/ant/dispatch/DispatchUtils.java | 66 +++++++++----------- src/main/org/apache/tools/ant/taskdefs/Ant.java | 16 ++--- .../org/apache/tools/ant/types/Commandline.java | 10 +-- .../org/apache/tools/ant/types/PropertySet.java | 2 +- .../org/apache/tools/ant/util/FileUtils.java | 2 +- .../apache/tools/ant/util/ResourceUtils.java | 10 +-- .../apache/tools/ant/util/StringTokenizer.java | 6 +- .../org/apache/tools/zip/ZipOutputStream.java | 24 +++---- .../04-lists/src/Find.java | 4 +- .../final/src/Find.java | 4 +- 11 files changed, 95 insertions(+), 102 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ant/blob/cda6c91c/src/main/org/apache/tools/ant/Target.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/Target.java b/src/main/org/apache/tools/ant/Target.java index a57d3b0..8c5fb8d 100644 --- a/src/main/org/apache/tools/ant/Target.java +++ b/src/main/org/apache/tools/ant/Target.java @@ -145,36 +145,37 @@ public class Target implements TaskContainer { public static List<String> parseDepends(String depends, String targetName, String attributeName) { + if (depends.isEmpty()) { + return 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: " + attributeName - + " attribute of target \"" + + " attribute for 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: " - + attributeName - + " attribute for target \"" - + targetName - + "\" ends with a \",\" " - + "character"); - } + + "\" ends with a \",\" " + + "character"); } } } http://git-wip-us.apache.org/repos/asf/ant/blob/cda6c91c/src/main/org/apache/tools/ant/dispatch/DispatchUtils.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/dispatch/DispatchUtils.java b/src/main/org/apache/tools/ant/dispatch/DispatchUtils.java index edfbd77..efaccf8 100644 --- a/src/main/org/apache/tools/ant/dispatch/DispatchUtils.java +++ b/src/main/org/apache/tools/ant/dispatch/DispatchUtils.java @@ -52,44 +52,40 @@ public class DispatchUtils { String mName = null; try { 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( "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) { throw new BuildException("No public " + mName + "() in " + task.getClass()); } http://git-wip-us.apache.org/repos/asf/ant/blob/cda6c91c/src/main/org/apache/tools/ant/taskdefs/Ant.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/Ant.java b/src/main/org/apache/tools/ant/taskdefs/Ant.java index 1204795..0f68c80 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Ant.java +++ b/src/main/org/apache/tools/ant/taskdefs/Ant.java @@ -383,19 +383,15 @@ public class Ant extends Task { String thisAntFile = getProject().getProperty(MagicNames.ANT_FILE); // Are we trying to call the target in which we are defined (or // 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( + "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.", getTaskName()); - } } try { http://git-wip-us.apache.org/repos/asf/ant/blob/cda6c91c/src/main/org/apache/tools/ant/types/Commandline.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/Commandline.java b/src/main/org/apache/tools/ant/types/Commandline.java index 0ff63f8..ce28160 100644 --- a/src/main/org/apache/tools/ant/types/Commandline.java +++ b/src/main/org/apache/tools/ant/types/Commandline.java @@ -452,11 +452,11 @@ public class Commandline implements Cloneable { } // path containing one or more elements 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(quoteArgument(line[i])); + result.append(quoteArgument(l)); } return result.toString(); } @@ -508,7 +508,7 @@ public class Commandline implements Cloneable { } else if ("\"".equals(nextTok)) { state = inDoubleQuote; } else if (" ".equals(nextTok)) { - if (lastTokenHasBeenQuoted || current.length() != 0) { + if (lastTokenHasBeenQuoted || current.length() > 0) { result.add(current.toString()); current.setLength(0); } @@ -519,7 +519,7 @@ public class Commandline implements Cloneable { break; } } - if (lastTokenHasBeenQuoted || current.length() != 0) { + if (lastTokenHasBeenQuoted || current.length() > 0) { result.add(current.toString()); } if (state == inQuote || state == inDoubleQuote) { http://git-wip-us.apache.org/repos/asf/ant/blob/cda6c91c/src/main/org/apache/tools/ant/types/PropertySet.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/PropertySet.java b/src/main/org/apache/tools/ant/types/PropertySet.java index 5459932..ee25e44 100644 --- a/src/main/org/apache/tools/ant/types/PropertySet.java +++ b/src/main/org/apache/tools/ant/types/PropertySet.java @@ -502,7 +502,7 @@ public class PropertySet extends DataType implements ResourceCollection { StringBuilder b = new StringBuilder(); TreeMap<String, Object> sorted = new TreeMap<>(getPropertyMap()); for (Entry<String, Object> e : sorted.entrySet()) { - if (b.length() != 0) { + if (b.length() > 0) { b.append(", "); } b.append(e.getKey()); http://git-wip-us.apache.org/repos/asf/ant/blob/cda6c91c/src/main/org/apache/tools/ant/util/FileUtils.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java index 92b6953..af76942 100644 --- a/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/src/main/org/apache/tools/ant/util/FileUtils.java @@ -710,7 +710,7 @@ public class FileUtils { String pathComponent = tokenizer.nextToken(); pathComponent = pathComponent.replace('/', File.separatorChar); pathComponent = pathComponent.replace('\\', File.separatorChar); - if (path.length() != 0) { + if (path.length() > 0) { path.append(File.pathSeparatorChar); } path.append(pathComponent); http://git-wip-us.apache.org/repos/asf/ant/blob/cda6c91c/src/main/org/apache/tools/ant/util/ResourceUtils.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/util/ResourceUtils.java b/src/main/org/apache/tools/ant/util/ResourceUtils.java index f54a79f..741ff60 100644 --- a/src/main/org/apache/tools/ant/util/ResourceUtils.java +++ b/src/main/org/apache/tools/ant/util/ResourceUtils.java @@ -176,8 +176,10 @@ public class ResourceUtils { final Union result = new Union(); for (final Resource sr : source) { String srName = sr.getName(); - srName = srName == null - ? srName : srName.replace('/', File.separatorChar); + if (srName != null) { + srName = srName.replace('/', File.separatorChar); + } + String[] targetnames = null; try { @@ -653,7 +655,6 @@ public class ResourceUtils { final LineTokenizer lineTokenizer = new LineTokenizer(); lineTokenizer.setIncludeDelims(true); - String newline = null; String line = lineTokenizer.getToken(in); while (line != null) { if (line.length() == 0) { @@ -661,8 +662,7 @@ public class ResourceUtils { // returned with the end of line delimiter out.newLine(); } else { - newline = filters.replaceTokens(line); - out.write(newline); + out.write(filters.replaceTokens(line)); } line = lineTokenizer.getToken(in); } http://git-wip-us.apache.org/repos/asf/ant/blob/cda6c91c/src/main/org/apache/tools/ant/util/StringTokenizer.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/util/StringTokenizer.java b/src/main/org/apache/tools/ant/util/StringTokenizer.java index 590ec92..35585d1 100644 --- a/src/main/org/apache/tools/ant/util/StringTokenizer.java +++ b/src/main/org/apache/tools/ant/util/StringTokenizer.java @@ -104,10 +104,10 @@ public class StringTokenizer extends ProjectComponent implements Tokenizer { if (inToken) { if (isDelim) { if (delimsAreTokens) { - if (word.length() == 0) { - word.append(c); - } else { + if (word.length() > 0) { pushed = ch; + } else { + word.append(c); } break; } http://git-wip-us.apache.org/repos/asf/ant/blob/cda6c91c/src/main/org/apache/tools/zip/ZipOutputStream.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/zip/ZipOutputStream.java b/src/main/org/apache/tools/zip/ZipOutputStream.java index 297ee36..14dc92c 100644 --- a/src/main/org/apache/tools/zip/ZipOutputStream.java +++ b/src/main/org/apache/tools/zip/ZipOutputStream.java @@ -1148,20 +1148,16 @@ public class ZipOutputStream extends FilterOutputStream { } 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())); } } http://git-wip-us.apache.org/repos/asf/ant/blob/cda6c91c/src/tutorial/tasks-filesets-properties/04-lists/src/Find.java ---------------------------------------------------------------------- diff --git a/src/tutorial/tasks-filesets-properties/04-lists/src/Find.java b/src/tutorial/tasks-filesets-properties/04-lists/src/Find.java index 688c8de..84cda58 100644 --- a/src/tutorial/tasks-filesets-properties/04-lists/src/Find.java +++ b/src/tutorial/tasks-filesets-properties/04-lists/src/Find.java @@ -79,7 +79,9 @@ public class Find extends Task { // create list StringBuilder list = new StringBuilder(); for (String file : foundFiles) { - if (list.length() > 0) list.append(delimiter); + if (list.length() > 0) { + list.append(delimiter); + } list.append(file); } rv = list.toString(); http://git-wip-us.apache.org/repos/asf/ant/blob/cda6c91c/src/tutorial/tasks-filesets-properties/final/src/Find.java ---------------------------------------------------------------------- diff --git a/src/tutorial/tasks-filesets-properties/final/src/Find.java b/src/tutorial/tasks-filesets-properties/final/src/Find.java index 8196c36..7759c74 100644 --- a/src/tutorial/tasks-filesets-properties/final/src/Find.java +++ b/src/tutorial/tasks-filesets-properties/final/src/Find.java @@ -84,7 +84,9 @@ public class Find extends Task { // create list StringBuilder list = new StringBuilder(); for (String file : foundFiles) { - if (list.length() > 0) list.append(delimiter); + if (list.length() > 0) { + list.append(delimiter); + } list.append(file); } rv = list.toString();