diff options
Diffstat (limited to 'src/main/org/apache/tools/ant')
27 files changed, 151 insertions, 163 deletions
diff --git a/src/main/org/apache/tools/ant/Main.java b/src/main/org/apache/tools/ant/Main.java index 115290631..25202c917 100644 --- a/src/main/org/apache/tools/ant/Main.java +++ b/src/main/org/apache/tools/ant/Main.java @@ -47,7 +47,7 @@ import org.apache.tools.ant.property.ResolvePropertyMap; import org.apache.tools.ant.util.ClasspathUtils; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.ProxySetup; - +import org.apache.tools.ant.util.StreamUtils; /** * Command line entry point into Ant. This class is entered via the @@ -1266,7 +1266,7 @@ public class Main implements AntMain { } msg.append(eol); if (!dependencies.isEmpty() && dependencies.elementAt(i).hasMoreElements()) { - msg.append(Collections.list(dependencies.elementAt(i)).stream() + msg.append(StreamUtils.enumerationAsStream(dependencies.elementAt(i)) .collect(Collectors.joining(", ", " depends on: ", eol))); } } diff --git a/src/main/org/apache/tools/ant/ProjectHelperRepository.java b/src/main/org/apache/tools/ant/ProjectHelperRepository.java index 8bb0cb2e3..b1c48beb5 100644 --- a/src/main/org/apache/tools/ant/ProjectHelperRepository.java +++ b/src/main/org/apache/tools/ant/ProjectHelperRepository.java @@ -32,6 +32,7 @@ import java.util.stream.Stream; import org.apache.tools.ant.helper.ProjectHelper2; import org.apache.tools.ant.types.Resource; import org.apache.tools.ant.util.LoaderUtils; +import org.apache.tools.ant.util.StreamUtils; /** * Repository of {@link ProjectHelper} found in the classpath or via @@ -77,8 +78,7 @@ public class ProjectHelperRepository { private void collectProjectHelpers() { // First, try the system property - Constructor<? extends ProjectHelper> projectHelper = getProjectHelperBySystemProperty(); - registerProjectHelper(projectHelper); + registerProjectHelper(getProjectHelperBySystemProperty()); // A JDK1.3 'service' (like in JAXP). That will plug a helper // automatically if in CLASSPATH, with the right META-INF/services. @@ -88,17 +88,14 @@ public class ProjectHelperRepository { for (URL resource : Collections.list(classLoader.getResources(ProjectHelper.SERVICE_ID))) { URLConnection conn = resource.openConnection(); conn.setUseCaches(false); - projectHelper = - getProjectHelperByService(conn.getInputStream()); - registerProjectHelper(projectHelper); + registerProjectHelper(getProjectHelperByService(conn.getInputStream())); } } InputStream systemResource = ClassLoader.getSystemResourceAsStream(ProjectHelper.SERVICE_ID); if (systemResource != null) { - projectHelper = getProjectHelperByService(systemResource); - registerProjectHelper(projectHelper); + registerProjectHelper(getProjectHelperByService(systemResource)); } } catch (Exception e) { System.err.println("Unable to load ProjectHelper from service " @@ -250,20 +247,19 @@ public class ProjectHelperRepository { * @return the first ProjectHelper that fit the requirement (never <code>null</code>). */ public ProjectHelper getProjectHelperForBuildFile(Resource buildFile) throws BuildException { - for (Iterator<ProjectHelper> it = getHelpers(); it.hasNext();) { - ProjectHelper helper = it.next(); - if (helper.canParseBuildFile(buildFile)) { - if (DEBUG) { - System.out.println("ProjectHelper " - + helper.getClass().getName() - + " selected for the build file " - + buildFile); - } - return helper; - } + ProjectHelper ph = StreamUtils.iteratorAsStream(getHelpers()) + .filter(helper -> helper.canParseBuildFile(buildFile)) + .findFirst().orElse(null); + + if (ph == null) { + throw new BuildException("BUG: at least the ProjectHelper2 should " + + "have supported the file " + buildFile); + } + if (DEBUG) { + System.out.println("ProjectHelper " + ph.getClass().getName() + + " selected for the build file " + buildFile); } - throw new BuildException("BUG: at least the ProjectHelper2 should " - + "have supported the file " + buildFile); + return ph; } /** @@ -274,20 +270,19 @@ public class ProjectHelperRepository { * @return the first ProjectHelper that fit the requirement (never <code>null</code>). */ public ProjectHelper getProjectHelperForAntlib(Resource antlib) throws BuildException { - for (Iterator<ProjectHelper> it = getHelpers(); it.hasNext();) { - ProjectHelper helper = it.next(); - if (helper.canParseAntlibDescriptor(antlib)) { - if (DEBUG) { - System.out.println("ProjectHelper " - + helper.getClass().getName() - + " selected for the antlib " - + antlib); - } - return helper; - } + ProjectHelper ph = StreamUtils.iteratorAsStream(getHelpers()) + .filter(helper -> helper.canParseAntlibDescriptor(antlib)) + .findFirst().orElse(null); + + if (ph == null) { + throw new BuildException("BUG: at least the ProjectHelper2 should " + + "have supported the file " + antlib); + } + if (DEBUG) { + System.out.println("ProjectHelper " + ph.getClass().getName() + + " selected for the antlib " + antlib); } - throw new BuildException("BUG: at least the ProjectHelper2 should " - + "have supported the file " + antlib); + return ph; } /** diff --git a/src/main/org/apache/tools/ant/XmlLogger.java b/src/main/org/apache/tools/ant/XmlLogger.java index 789b9fd1a..96cfc68e2 100644 --- a/src/main/org/apache/tools/ant/XmlLogger.java +++ b/src/main/org/apache/tools/ant/XmlLogger.java @@ -24,7 +24,6 @@ import java.io.PrintStream; import java.io.Writer; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.Collections; import java.util.Hashtable; import java.util.Stack; @@ -32,6 +31,7 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.tools.ant.util.DOMElementWriter; +import org.apache.tools.ant.util.StreamUtils; import org.apache.tools.ant.util.StringUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -345,7 +345,8 @@ public class XmlLogger implements BuildLogger { if (element != null) { return element; } - return Collections.list(tasks.keys()).stream().filter(UnknownElement.class::isInstance) + return StreamUtils.enumerationAsStream(tasks.keys()) + .filter(UnknownElement.class::isInstance) .filter(key -> ((UnknownElement) key).getTask() == task).findFirst() .map(key -> tasks.get(key)).orElse(null); } diff --git a/src/main/org/apache/tools/ant/taskdefs/Ant.java b/src/main/org/apache/tools/ant/taskdefs/Ant.java index b09448c76..f9079ab7e 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Ant.java +++ b/src/main/org/apache/tools/ant/taskdefs/Ant.java @@ -25,7 +25,6 @@ import java.lang.reflect.Method; import java.nio.file.Files; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; @@ -33,7 +32,6 @@ import java.util.Set; import java.util.Vector; import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.BuildListener; import org.apache.tools.ant.DefaultLogger; import org.apache.tools.ant.MagicNames; import org.apache.tools.ant.Main; @@ -197,10 +195,7 @@ public class Ant extends Task { private void initializeProject() { newProject.setInputHandler(getProject().getInputHandler()); - Iterator<BuildListener> iter = getBuildListeners(); - while (iter.hasNext()) { - newProject.addBuildListener(iter.next()); - } + getProject().getBuildListeners().forEach(bl -> newProject.addBuildListener(bl)); if (output != null) { File outfile; @@ -749,13 +744,6 @@ public class Ant extends Task { } /** - * @since Ant 1.6.2 - */ - private Iterator<BuildListener> getBuildListeners() { - return getProject().getBuildListeners().iterator(); - } - - /** * Helper class that implements the nested <reference> * element of <ant> and <antcall>. */ diff --git a/src/main/org/apache/tools/ant/taskdefs/DependSet.java b/src/main/org/apache/tools/ant/taskdefs/DependSet.java index 6fb946337..45c82749d 100644 --- a/src/main/org/apache/tools/ant/taskdefs/DependSet.java +++ b/src/main/org/apache/tools/ant/taskdefs/DependSet.java @@ -38,6 +38,7 @@ import org.apache.tools.ant.types.resources.comparators.Reverse; import org.apache.tools.ant.types.resources.selectors.Exists; import org.apache.tools.ant.types.resources.selectors.Not; import org.apache.tools.ant.types.resources.selectors.ResourceSelector; +import org.apache.tools.ant.util.StreamUtils; /** * Examines and removes out of date target files. If any of the target files @@ -263,18 +264,8 @@ public class DependSet extends MatchingTask { } private Resource getXest(ResourceCollection rc, ResourceComparator c) { - Iterator<Resource> i = rc.iterator(); - if (!i.hasNext()) { - return null; - } - Resource xest = i.next(); - while (i.hasNext()) { - Resource next = i.next(); - if (c.compare(xest, next) < 0) { - xest = next; - } - } - return xest; + return StreamUtils.iteratorAsStream(rc.iterator()) + .min(c::compare).orElse(null); } private Resource getOldest(ResourceCollection rc) { diff --git a/src/main/org/apache/tools/ant/taskdefs/Execute.java b/src/main/org/apache/tools/ant/taskdefs/Execute.java index f9b2ebe27..913ba36a4 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Execute.java +++ b/src/main/org/apache/tools/ant/taskdefs/Execute.java @@ -28,7 +28,6 @@ import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; -import java.util.Map.Entry; import java.util.Vector; import org.apache.tools.ant.BuildException; @@ -177,9 +176,7 @@ public class Execute { @Deprecated public static synchronized Vector<String> getProcEnvironment() { Vector<String> v = new Vector<>(); - for (Entry<String, String> entry : getEnvironmentVariables().entrySet()) { - v.add(entry.getKey() + "=" + entry.getValue()); - } + getEnvironmentVariables().forEach((key, value) -> v.add(key + "=" + value)); return v; } diff --git a/src/main/org/apache/tools/ant/taskdefs/Expand.java b/src/main/org/apache/tools/ant/taskdefs/Expand.java index 82c80f488..3b62ea820 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Expand.java +++ b/src/main/org/apache/tools/ant/taskdefs/Expand.java @@ -24,8 +24,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.file.Files; -import java.util.Collections; import java.util.Date; +import java.util.Enumeration; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -189,7 +189,9 @@ public class Expand extends Task { } try (ZipFile zf = new ZipFile(srcF, encoding, scanForUnicodeExtraFields)) { boolean empty = true; - for (ZipEntry ze : Collections.list(zf.getEntries())) { + Enumeration<ZipEntry> entries = zf.getEntries(); + while (entries.hasMoreElements()) { + ZipEntry ze = entries.nextElement(); empty = false; InputStream is = null; log("extracting " + ze.getName(), Project.MSG_DEBUG); diff --git a/src/main/org/apache/tools/ant/taskdefs/Jar.java b/src/main/org/apache/tools/ant/taskdefs/Jar.java index 64c658d3f..f83532df2 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Jar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Jar.java @@ -55,6 +55,7 @@ import org.apache.tools.ant.types.ResourceCollection; import org.apache.tools.ant.types.ZipFileSet; import org.apache.tools.ant.types.spi.Service; import org.apache.tools.ant.util.FileUtils; +import org.apache.tools.ant.util.StreamUtils; import org.apache.tools.zip.JarMarker; import org.apache.tools.zip.ZipExtraField; import org.apache.tools.zip.ZipOutputStream; @@ -321,18 +322,17 @@ public class Jar extends Zip { */ private Manifest getManifestFromJar(File jarFile) throws IOException { try (ZipFile zf = new ZipFile(jarFile)) { - // must not use getEntry as "well behaving" applications // must accept the manifest in any capitalization - for (ZipEntry ze : Collections.list(zf.entries())) { - if (MANIFEST_NAME.equalsIgnoreCase(ze.getName())) { - try (InputStreamReader isr = - new InputStreamReader(zf.getInputStream(ze), "UTF-8")) { - return getManifest(isr); - } - } + ZipEntry ze = StreamUtils.enumerationAsStream(zf.entries()) + .filter(entry -> MANIFEST_NAME.equalsIgnoreCase(entry.getName())) + .findFirst().orElse(null); + if (ze == null) { + return null; + } + try (InputStreamReader isr = new InputStreamReader(zf.getInputStream(ze), "UTF-8")) { + return getManifest(isr); } - return null; } } @@ -351,7 +351,7 @@ public class Jar extends Zip { private boolean jarHasIndex(File jarFile) throws IOException { try (ZipFile zf = new ZipFile(jarFile)) { - return Collections.list(zf.entries()).stream() + return StreamUtils.enumerationAsStream(zf.entries()) .anyMatch(ze -> INDEX_NAME.equalsIgnoreCase(ze.getName())); } } @@ -524,9 +524,8 @@ public class Jar extends Zip { private void writeManifest(ZipOutputStream zOut, Manifest manifest) throws IOException { - for (String warning : Collections.list(manifest.getWarnings())) { - log("Manifest warning: " + warning, Project.MSG_WARN); - } + StreamUtils.enumerationAsStream(manifest.getWarnings()) + .forEach(warning -> log("Manifest warning: " + warning, Project.MSG_WARN)); zipDir((Resource) null, zOut, "META-INF/", ZipFileSet.DEFAULT_DIR_MODE, JAR_MARKER); @@ -1064,7 +1063,7 @@ public class Jar extends Zip { throws IOException { try (org.apache.tools.zip.ZipFile zf = new org.apache.tools.zip.ZipFile(file, "utf-8")) { Set<String> dirSet = new HashSet<>(); - for (org.apache.tools.zip.ZipEntry ze : Collections.list(zf.getEntries())) { + StreamUtils.enumerationAsStream(zf.getEntries()).forEach(ze -> { String name = ze.getName(); if (ze.isDirectory()) { dirSet.add(name); @@ -1077,7 +1076,7 @@ public class Jar extends Zip { // well. dirSet.add(name.substring(0, name.lastIndexOf('/') + 1)); } - } + }); dirs.addAll(dirSet); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Manifest.java b/src/main/org/apache/tools/ant/taskdefs/Manifest.java index 70eb4fcca..ef6dc882c 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Manifest.java +++ b/src/main/org/apache/tools/ant/taskdefs/Manifest.java @@ -26,7 +26,6 @@ import java.io.PrintWriter; import java.io.Reader; import java.io.StringWriter; import java.io.UnsupportedEncodingException; -import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; import java.util.LinkedHashMap; @@ -39,6 +38,7 @@ import java.util.stream.Collectors; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.util.FileUtils; +import org.apache.tools.ant.util.StreamUtils; /** * Holds the data of a jar manifest. @@ -676,7 +676,7 @@ public class Manifest { public Object clone() { Section cloned = new Section(); cloned.setName(name); - Collections.list(getAttributeKeys()).stream() + StreamUtils.enumerationAsStream(getAttributeKeys()) .map(key -> new Attribute(getAttribute(key).getName(), getAttribute(key).getValue())).forEach(cloned::storeAttribute); return cloned; @@ -932,8 +932,7 @@ public class Manifest { for (String sectionName : Collections.list(other.getSectionNames())) { Section ourSection = sections.get(sectionName); - Section otherSection - = other.sections.get(sectionName); + Section otherSection = other.sections.get(sectionName); if (ourSection == null) { if (otherSection != null) { addConfiguredSection((Section) otherSection.clone()); @@ -1020,7 +1019,7 @@ public class Manifest { */ public Enumeration<String> getWarnings() { // create a vector and add in the warnings for the main section - List<String> warnings = new ArrayList<>(Collections.list(mainSection.getWarnings())); + List<String> warnings = Collections.list(mainSection.getWarnings()); // add in the warnings for all the sections sections.values().stream().map(section -> Collections.list(section.getWarnings())) diff --git a/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java b/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java index 69fa7870f..d90a04952 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/ManifestTask.java @@ -25,12 +25,12 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.nio.charset.Charset; import java.nio.file.Files; -import java.util.Collections; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.EnumeratedAttribute; +import org.apache.tools.ant.util.StreamUtils; /** * Creates a manifest file for inclusion in a JAR, Ant task wrapper @@ -112,7 +112,7 @@ public class ManifestTask extends Task { */ public void addConfiguredSection(Manifest.Section section) throws ManifestException { - Collections.list(section.getAttributeKeys()).stream() + StreamUtils.enumerationAsStream(section.getAttributeKeys()) .map(section::getAttribute).forEach(this::checkAttribute); nestedManifest.addConfiguredSection(section); } @@ -241,7 +241,7 @@ public class ManifestTask extends Task { } // look for and print warnings - Collections.list(nestedManifest.getWarnings()) + StreamUtils.enumerationAsStream(nestedManifest.getWarnings()) .forEach(e -> log("Manifest warning: " + e, Project.MSG_WARN)); try { if ("update".equals(mode.getValue()) && manifestFile.exists()) { diff --git a/src/main/org/apache/tools/ant/taskdefs/Recorder.java b/src/main/org/apache/tools/ant/taskdefs/Recorder.java index cb9b4d88a..8a7d8213a 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Recorder.java +++ b/src/main/org/apache/tools/ant/taskdefs/Recorder.java @@ -307,13 +307,9 @@ public class Recorder extends Task implements SubBuildListener { */ @SuppressWarnings("unchecked") private void cleanup() { - Hashtable<String, RecorderEntry> entries - = (Hashtable<String, RecorderEntry>) recorderEntries.clone(); - for (Map.Entry<String, RecorderEntry> entry : entries.entrySet()) { - if (entry.getValue().getProject() == getProject()) { - recorderEntries.remove(entry.getKey()); - } - } + ((Hashtable<String, RecorderEntry>) recorderEntries.clone()).entrySet().stream() + .filter(entry -> entry.getValue().getProject() == getProject()) + .forEach(entry -> recorderEntries.remove(entry.getKey())); getProject().removeBuildListener(this); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Replace.java b/src/main/org/apache/tools/ant/taskdefs/Replace.java index 5adcd9969..2cfaa8bb1 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Replace.java +++ b/src/main/org/apache/tools/ant/taskdefs/Replace.java @@ -45,6 +45,7 @@ import org.apache.tools.ant.types.resources.FileProvider; import org.apache.tools.ant.types.resources.FileResource; import org.apache.tools.ant.types.resources.Union; import org.apache.tools.ant.util.FileUtils; +import org.apache.tools.ant.util.StreamUtils; /** * Replaces all occurrences of one or more string tokens with given @@ -511,14 +512,12 @@ public class Replace extends MatchingTask { try { if (replaceFilterResource != null) { - Properties props = getProperties(replaceFilterResource); - Iterator<Object> e = getOrderedIterator(props); - while (e.hasNext()) { - String tok = e.next().toString(); + final Properties properties = getProperties(replaceFilterResource); + StreamUtils.iteratorAsStream(getOrderedIterator(properties)).forEach(tok -> { Replacefilter replaceFilter = createReplacefilter(); replaceFilter.setToken(tok); - replaceFilter.setValue(props.getProperty(tok)); - } + replaceFilter.setValue(properties.getProperty(tok)); + }); } validateAttributes(); @@ -936,10 +935,9 @@ public class Replace extends MatchingTask { * * @param props Properties */ - private Iterator<Object> getOrderedIterator(Properties props) { - List<Object> keys = new ArrayList<>(props.keySet()); - keys.sort(Comparator.comparingInt(o -> Objects.toString(o, "").length()) - .reversed()); + private Iterator<String> getOrderedIterator(Properties props) { + List<String> keys = new ArrayList<>(props.stringPropertyNames()); + keys.sort(Comparator.<String>comparingInt(s -> s.length()).reversed()); return keys.iterator(); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/And.java b/src/main/org/apache/tools/ant/taskdefs/condition/And.java index 8fc5c6c52..c98fc1a02 100644 --- a/src/main/org/apache/tools/ant/taskdefs/condition/And.java +++ b/src/main/org/apache/tools/ant/taskdefs/condition/And.java @@ -18,9 +18,8 @@ package org.apache.tools.ant.taskdefs.condition; -import java.util.Collections; - import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.util.StreamUtils; /** * <and> condition container. @@ -38,7 +37,7 @@ public class And extends ConditionBase implements Condition { */ @Override public boolean eval() throws BuildException { - return Collections.list(getConditions()).stream().allMatch(Condition::eval); + return StreamUtils.enumerationAsStream(getConditions()).allMatch(Condition::eval); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/IsSigned.java b/src/main/org/apache/tools/ant/taskdefs/condition/IsSigned.java index 199abe15a..7e16b5a2a 100644 --- a/src/main/org/apache/tools/ant/taskdefs/condition/IsSigned.java +++ b/src/main/org/apache/tools/ant/taskdefs/condition/IsSigned.java @@ -19,12 +19,12 @@ package org.apache.tools.ant.taskdefs.condition; import java.io.File; import java.io.IOException; -import java.util.Collections; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.ManifestTask; import org.apache.tools.ant.types.DataType; +import org.apache.tools.ant.util.StreamUtils; import org.apache.tools.zip.ZipFile; /** @@ -72,7 +72,7 @@ public class IsSigned extends DataType implements Condition { throws IOException { try (ZipFile jarFile = new ZipFile(zipFile)) { if (null == name) { - return Collections.list(jarFile.getEntries()).stream() + return StreamUtils.enumerationAsStream(jarFile.getEntries()) .anyMatch(e -> e.getName().startsWith(SIG_START) && e.getName().endsWith(SIG_END)); } name = replaceInvalidChars(name); diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/Or.java b/src/main/org/apache/tools/ant/taskdefs/condition/Or.java index 32caaa8a7..65c64be4b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/condition/Or.java +++ b/src/main/org/apache/tools/ant/taskdefs/condition/Or.java @@ -21,6 +21,7 @@ package org.apache.tools.ant.taskdefs.condition; import java.util.Collections; import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.util.StreamUtils; /** * <or> condition container. @@ -38,7 +39,7 @@ public class Or extends ConditionBase implements Condition { */ @Override public boolean eval() throws BuildException { - return Collections.list(getConditions()).stream().anyMatch(Condition::eval); + return StreamUtils.enumerationAsStream(getConditions()).anyMatch(Condition::eval); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/Xor.java b/src/main/org/apache/tools/ant/taskdefs/condition/Xor.java index a771e955a..7e31633e8 100644 --- a/src/main/org/apache/tools/ant/taskdefs/condition/Xor.java +++ b/src/main/org/apache/tools/ant/taskdefs/condition/Xor.java @@ -20,6 +20,7 @@ package org.apache.tools.ant.taskdefs.condition; import java.util.Collections; import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.util.StreamUtils; /** * The <tt>Xor</tt> condition type to exclusive or operations. @@ -36,13 +37,9 @@ public class Xor extends ConditionBase implements Condition { */ @Override public boolean eval() throws BuildException { - // initial state is false. - boolean state = false; - for (Condition c : Collections.list(getConditions())) { - // every condition is xored against the previous one - state ^= c.eval(); - } - return state; + // initial state is false + return StreamUtils.enumerationAsStream(getConditions()).map(Condition::eval) + .reduce((a, b) -> a ^ b).orElse(Boolean.FALSE); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java b/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java index 01fe812e0..ebfd50103 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java @@ -28,7 +28,6 @@ import java.lang.reflect.Field; import java.net.URL; import java.nio.file.Files; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.Hashtable; import java.util.List; @@ -64,6 +63,7 @@ import org.apache.tools.ant.types.resources.URLProvider; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.JAXPUtils; import org.apache.tools.ant.util.JavaEnvUtils; +import org.apache.tools.ant.util.StreamUtils; import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -619,7 +619,7 @@ public class TraXLiaison implements XSLTLiaison4, ErrorListener, XSLTLoggerAware if (factory != null) { setFactory(factory.getName()); // configure factory attributes - Collections.list(factory.getAttributes()) + StreamUtils.enumerationAsStream(factory.getAttributes()) .forEach(attr -> setAttribute(attr.getName(), attr.getValue())); factory.getFeatures() .forEach(feature -> setFeature(feature.getName(), feature.getValue())); @@ -633,7 +633,7 @@ public class TraXLiaison implements XSLTLiaison4, ErrorListener, XSLTLoggerAware } // configure output properties - Collections.list(xsltTask.getOutputProperties()) + StreamUtils.enumerationAsStream(xsltTask.getOutputProperties()) .forEach(prop -> setOutputProperty(prop.getName(), prop.getValue())); suppressWarnings = xsltTask.getSuppressWarnings(); diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/Depend.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/Depend.java index 4be59b5c6..f72da99b1 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/Depend.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/Depend.java @@ -322,7 +322,7 @@ public class Depend extends MatchingTask { analyzer.addRootClass(info.className); analyzer.addClassPath(destPath); analyzer.setClosure(false); - dependencyList = new ArrayList<>(Collections.list(analyzer.getClassDependencies())); + dependencyList = Collections.list(analyzer.getClassDependencies()); dependencyList.forEach(o -> log("Class " + info.className + " depends on " + o, Project.MSG_DEBUG)); cacheDirty = true; diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java b/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java index eed937724..cca771651 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java @@ -21,7 +21,6 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; -import java.util.Collections; import java.util.Hashtable; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -36,6 +35,7 @@ import org.apache.tools.ant.taskdefs.Java; import org.apache.tools.ant.types.Environment; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.util.FileUtils; +import org.apache.tools.ant.util.StreamUtils; /** * WebSphere deployment tool that augments the ejbjar task. @@ -667,13 +667,16 @@ public class WebsphereDeploymentTool extends GenericDeploymentTool { wasJar = new JarFile(websphereJarFile); //get the list of generic jar entries - Hashtable<String, JarEntry> genericEntries = Collections.list(genericJar.entries()).stream() + Hashtable<String, JarEntry> genericEntries + = StreamUtils.enumerationAsStream(genericJar.entries()) .collect(Collectors.toMap(je -> je.getName().replace('\\', '/'), je -> je, (a, b) -> b, Hashtable::new)); // get the list of WebSphere jar entries - Hashtable<String, JarEntry> wasEntries = Collections.list(wasJar.entries()).stream() - .collect(Collectors.toMap(ZipEntry::getName, je -> je, (a, b) -> b, Hashtable::new)); + Hashtable<String, JarEntry> wasEntries + = StreamUtils.enumerationAsStream(wasJar.entries()) + .collect(Collectors.toMap(ZipEntry::getName, + je -> je, (a, b) -> b, Hashtable::new)); // Cycle through generic and make sure its in WebSphere genericLoader = getClassLoaderFromJar(genericJarFile); diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/jlink/jlink.java b/src/main/org/apache/tools/ant/taskdefs/optional/jlink/jlink.java index 234b8c44c..96728acdb 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/jlink/jlink.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/jlink/jlink.java @@ -28,7 +28,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.Collections; +import java.util.Enumeration; import java.util.List; import java.util.Vector; import java.util.zip.CRC32; @@ -207,7 +207,9 @@ public class jlink { return; } try (ZipFile zipf = new ZipFile(f)) { - for (ZipEntry inputEntry : Collections.list(zipf.entries())) { + Enumeration<? extends ZipEntry> entries = zipf.entries(); + while (entries.hasMoreElements()) { + ZipEntry inputEntry = entries.nextElement(); //Ignore manifest entries. They're bound to cause conflicts between //files that are being merged. User should supply their own //manifest file when doing the merge. diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/Enumerations.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/Enumerations.java index da76dc862..649754b4e 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/Enumerations.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/Enumerations.java @@ -54,8 +54,8 @@ public final class Enumerations { * @param <T> object type * @param enums the array of enumerations. * @return the enumeration over the array of enumerations. - * @deprecated Stream.concat(Collections.list ( one).stream(), Collections.list(two).stream()) - * .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::enumeration)) + * @deprecated use Stream.concat(Collections.list(one).stream(), Collections.list(two).stream()) + * .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::enumeration)) */ @Deprecated @SafeVarargs diff --git a/src/main/org/apache/tools/ant/types/PropertySet.java b/src/main/org/apache/tools/ant/types/PropertySet.java index a4239461c..1ba772a02 100644 --- a/src/main/org/apache/tools/ant/types/PropertySet.java +++ b/src/main/org/apache/tools/ant/types/PropertySet.java @@ -24,7 +24,6 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Optional; import java.util.Properties; import java.util.Set; @@ -494,17 +493,8 @@ public class PropertySet extends DataType implements ResourceCollection { return getRef().toString(); } dieOnCircularReference(); - StringBuilder b = new StringBuilder(); - TreeMap<String, Object> sorted = new TreeMap<>(getPropertyMap()); - for (Entry<String, Object> e : sorted.entrySet()) { - if (b.length() > 0) { - b.append(", "); - } - b.append(e.getKey()); - b.append("="); - b.append(e.getValue()); - } - return b.toString(); + return new TreeMap<>(getPropertyMap()).entrySet().stream() + .map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(", ")); } /** diff --git a/src/main/org/apache/tools/ant/types/ZipScanner.java b/src/main/org/apache/tools/ant/types/ZipScanner.java index 566715936..f43fa7c65 100644 --- a/src/main/org/apache/tools/ant/types/ZipScanner.java +++ b/src/main/org/apache/tools/ant/types/ZipScanner.java @@ -20,14 +20,13 @@ package org.apache.tools.ant.types; import java.io.File; import java.io.IOException; -import java.util.Collections; import java.util.Map; import java.util.zip.ZipException; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.resources.FileProvider; import org.apache.tools.ant.types.resources.ZipResource; -import org.apache.tools.zip.ZipEntry; +import org.apache.tools.ant.util.StreamUtils; import org.apache.tools.zip.ZipFile; /** @@ -62,7 +61,7 @@ public class ZipScanner extends ArchiveScanner { "Only file provider resources are supported")); try (ZipFile zf = new ZipFile(srcFile, encoding)) { - for (ZipEntry entry : Collections.list(zf.getEntries())) { + StreamUtils.enumerationAsStream(zf.getEntries()).forEach(entry -> { Resource r = new ZipResource(srcFile, encoding, entry); String name = entry.getName(); if (entry.isDirectory()) { @@ -77,7 +76,7 @@ public class ZipScanner extends ArchiveScanner { matchFileEntries.put(name, r); } } - } + }); } catch (ZipException ex) { throw new BuildException("Problem reading " + srcFile, ex); } catch (IOException ex) { diff --git a/src/main/org/apache/tools/ant/types/optional/depend/DependScanner.java b/src/main/org/apache/tools/ant/types/optional/depend/DependScanner.java index 413ae39f7..b014c0f7b 100644 --- a/src/main/org/apache/tools/ant/types/optional/depend/DependScanner.java +++ b/src/main/org/apache/tools/ant/types/optional/depend/DependScanner.java @@ -18,7 +18,6 @@ package org.apache.tools.ant.types.optional.depend; import java.io.File; -import java.util.Collections; import java.util.Set; import java.util.Vector; import java.util.stream.Collectors; @@ -27,9 +26,9 @@ import java.util.stream.Stream; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.util.StreamUtils; import org.apache.tools.ant.util.depend.DependencyAnalyzer; - /** * DirectoryScanner for finding class dependencies. */ @@ -126,14 +125,11 @@ public class DependScanner extends DirectoryScanner { Set<String> parentSet = Stream.of(parentScanner.getIncludedFiles()) .collect(Collectors.toSet()); - for (String classname : Collections.list(analyzer.getClassDependencies())) { - String filename = classname.replace('.', File.separatorChar) + ".class"; - File depFile = new File(basedir, filename); - if (depFile.exists() && parentSet.contains(filename)) { - // This is included - included.addElement(filename); - } - } + // This is included + StreamUtils.enumerationAsStream(analyzer.getClassDependencies()) + .map(cName -> cName.replace('.', File.separatorChar) + ".class") + .filter(fName -> new File(basedir, fName).exists() && parentSet.contains(fName)) + .forEach(fName -> included.addElement(fName)); } /** diff --git a/src/main/org/apache/tools/ant/util/CollectionUtils.java b/src/main/org/apache/tools/ant/util/CollectionUtils.java index c516fe324..33f98885c 100644 --- a/src/main/org/apache/tools/ant/util/CollectionUtils.java +++ b/src/main/org/apache/tools/ant/util/CollectionUtils.java @@ -83,7 +83,7 @@ public class CollectionUtils { // don't need the opposite check as the Dictionaries have the // same size, so we've also covered all keys of d2 already. - return Collections.list(d1.keys()).stream() + return StreamUtils.enumerationAsStream(d1.keys()) .allMatch(key -> d1.get(key).equals(d2.get(key))); } @@ -113,7 +113,7 @@ public class CollectionUtils { @Deprecated public static <K, V> void putAll(Dictionary<? super K, ? super V> m1, Dictionary<? extends K, ? extends V> m2) { - Collections.list(m2.keys()).forEach(key -> m1.put(key, m2.get(key))); + StreamUtils.enumerationAsStream(m2.keys()).forEach(key -> m1.put(key, m2.get(key))); } /** @@ -210,7 +210,7 @@ public class CollectionUtils { * @param <T> element type * @return the collection * @since Ant 1.8.0 - * @deprecated instantiate a list an use forEachRemaining(list::add) + * @deprecated instantiate a list and use forEachRemaining(list::add) */ @Deprecated public static <T> Collection<T> asCollection(final Iterator<? extends T> iter) { diff --git a/src/main/org/apache/tools/ant/util/StreamUtils.java b/src/main/org/apache/tools/ant/util/StreamUtils.java new file mode 100644 index 000000000..f64f4120a --- /dev/null +++ b/src/main/org/apache/tools/ant/util/StreamUtils.java @@ -0,0 +1,33 @@ +package org.apache.tools.ant.util; + +import java.util.Enumeration; +import java.util.Spliterator; +import java.util.Spliterators; +import java.util.function.Consumer; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +public class StreamUtils { + /** + * Turn Enumeration into a Stream + * + * @param <T> Enumeration type + * @param e Enumeration + * @return Stream + */ + public static <T> Stream<T> enumerationAsStream(Enumeration<T> e) { + return StreamSupport.stream( + new Spliterators.AbstractSpliterator<T>(Long.MAX_VALUE, Spliterator.ORDERED) { + public boolean tryAdvance(Consumer<? super T> action) { + if (e.hasMoreElements()) { + action.accept(e.nextElement()); + return true; + } + return false; + } + public void forEachRemaining(Consumer<? super T> action) { + while(e.hasMoreElements()) action.accept(e.nextElement()); + } + }, false); + } +} diff --git a/src/main/org/apache/tools/ant/util/depend/bcel/FullAnalyzer.java b/src/main/org/apache/tools/ant/util/depend/bcel/FullAnalyzer.java index 8b6357df9..aac7cbe7e 100644 --- a/src/main/org/apache/tools/ant/util/depend/bcel/FullAnalyzer.java +++ b/src/main/org/apache/tools/ant/util/depend/bcel/FullAnalyzer.java @@ -16,6 +16,7 @@ * */ package org.apache.tools.ant.util.depend.bcel; + import java.io.File; import java.io.IOException; import java.util.Collections; @@ -27,6 +28,7 @@ import org.apache.bcel.classfile.ClassParser; import org.apache.bcel.classfile.DescendingVisitor; import org.apache.bcel.classfile.JavaClass; import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.util.StreamUtils; import org.apache.tools.ant.util.depend.AbstractAnalyzer; /** @@ -102,7 +104,7 @@ public class FullAnalyzer extends AbstractAnalyzer { toAnalyze.clear(); // now recover all the dependencies collected and add to the list. - Collections.list(dependencyVisitor.getDependencies()).stream() + StreamUtils.enumerationAsStream(dependencyVisitor.getDependencies()) .filter(className -> !dependencies.contains(className)) .forEach(toAnalyze::add); } |