From 70da75c5053a3f2e5f7905e2d8bfeb4e998edcec Mon Sep 17 00:00:00 2001 From: Gintas Grigelionis Date: Sun, 20 May 2018 15:53:04 +0200 Subject: More Streams (less StreamUtils) --- src/main/org/apache/tools/ant/ComponentHelper.java | 44 ++++------ .../org/apache/tools/ant/RuntimeConfigurable.java | 7 +- src/main/org/apache/tools/ant/XmlLogger.java | 11 ++- .../tools/ant/attribute/BaseIfAttribute.java | 20 ++--- .../org/apache/tools/ant/taskdefs/Recorder.java | 8 +- .../optional/ejb/BorlandDeploymentTool.java | 8 +- .../taskdefs/optional/ejb/DescriptorHandler.java | 3 +- .../optional/ejb/WeblogicDeploymentTool.java | 99 ++++++++++------------ .../optional/ejb/WebsphereDeploymentTool.java | 16 ++-- 9 files changed, 88 insertions(+), 128 deletions(-) diff --git a/src/main/org/apache/tools/ant/ComponentHelper.java b/src/main/org/apache/tools/ant/ComponentHelper.java index 0011079fd..28fa82486 100644 --- a/src/main/org/apache/tools/ant/ComponentHelper.java +++ b/src/main/org/apache/tools/ant/ComponentHelper.java @@ -34,6 +34,7 @@ import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.Stack; +import java.util.stream.Collectors; import org.apache.tools.ant.launch.Launcher; import org.apache.tools.ant.taskdefs.Definer; @@ -228,7 +229,8 @@ public class ComponentHelper { public void initSubProject(ComponentHelper helper) { // add the types of the parent project @SuppressWarnings("unchecked") - final Hashtable typeTable = (Hashtable) helper.antTypeTable.clone(); + final Hashtable typeTable + = (Hashtable) helper.antTypeTable.clone(); synchronized (antTypeTable) { for (AntTypeDefinition def : typeTable.values()) { antTypeTable.put(def.getName(), def); @@ -239,7 +241,8 @@ public class ComponentHelper { synchronized (this) { checkedNamespaces.addAll(inheritedCheckedNamespace); } - Map> inheritedRestrictedDef = helper.getRestrictedDefinition(); + Map> inheritedRestrictedDef + = helper.getRestrictedDefinition(); synchronized (restrictedDefinitions) { restrictedDefinitions.putAll(inheritedRestrictedDef); } @@ -398,15 +401,11 @@ public class ComponentHelper { synchronized (antTypeTable) { if (rebuildTaskClassDefinitions) { taskClassDefinitions.clear(); - for (Map.Entry e : antTypeTable.entrySet()) { - final Class clazz = e.getValue().getExposedClass(project); - if (clazz == null) { - continue; - } - if (Task.class.isAssignableFrom(clazz)) { - taskClassDefinitions.put(e.getKey(), e.getValue().getTypeClass(project)); - } - } + antTypeTable.entrySet().stream() + .filter(e -> e.getValue().getExposedClass(project) != null + && Task.class.isAssignableFrom(e.getValue().getExposedClass(project))) + .forEach(e -> taskClassDefinitions.put(e.getKey(), + e.getValue().getTypeClass(project))); rebuildTaskClassDefinitions = false; } } @@ -426,15 +425,11 @@ public class ComponentHelper { synchronized (antTypeTable) { if (rebuildTypeClassDefinitions) { typeClassDefinitions.clear(); - for (Map.Entry e : antTypeTable.entrySet()) { - final Class clazz = e.getValue().getExposedClass(project); - if (clazz == null) { - continue; - } - if (!Task.class.isAssignableFrom(clazz)) { - typeClassDefinitions.put(e.getKey(), e.getValue().getTypeClass(project)); - } - } + antTypeTable.entrySet().stream() + .filter(e -> e.getValue().getExposedClass(project) != null + && !Task.class.isAssignableFrom(e.getValue().getExposedClass(project))) + .forEach(e -> typeClassDefinitions.put(e.getKey(), + e.getValue().getTypeClass(project))); rebuildTypeClassDefinitions = false; } } @@ -1077,14 +1072,9 @@ public class ComponentHelper { * @return the (possibly empty) list of definitions */ private List findTypeMatches(String prefix) { - final List result = new ArrayList<>(); synchronized (antTypeTable) { - for (AntTypeDefinition def : antTypeTable.values()) { - if (def.getName().startsWith(prefix)) { - result.add(def); - } - } + return antTypeTable.values().stream().filter(def -> def.getName().startsWith(prefix)) + .collect(Collectors.toList()); } - return result; } } diff --git a/src/main/org/apache/tools/ant/RuntimeConfigurable.java b/src/main/org/apache/tools/ant/RuntimeConfigurable.java index d9964097e..90c34a258 100644 --- a/src/main/org/apache/tools/ant/RuntimeConfigurable.java +++ b/src/main/org/apache/tools/ant/RuntimeConfigurable.java @@ -43,10 +43,6 @@ public class RuntimeConfigurable implements Serializable { /** Serialization version */ private static final long serialVersionUID = 1L; - /** Empty Hashtable. */ - private static final Hashtable EMPTY_HASHTABLE = - new Hashtable<>(0); - /** Name of the element to configure. */ private String elementTag = null; @@ -336,8 +332,7 @@ public class RuntimeConfigurable implements Serializable { * @since Ant 1.6 */ public synchronized Hashtable getAttributeMap() { - return (attributeMap == null) - ? EMPTY_HASHTABLE : new Hashtable<>(attributeMap); + return new Hashtable<>(attributeMap == null ? Collections.emptyMap() : attributeMap); } /** diff --git a/src/main/org/apache/tools/ant/XmlLogger.java b/src/main/org/apache/tools/ant/XmlLogger.java index 96cfc68e2..5adce14f7 100644 --- a/src/main/org/apache/tools/ant/XmlLogger.java +++ b/src/main/org/apache/tools/ant/XmlLogger.java @@ -25,13 +25,13 @@ import java.io.Writer; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Hashtable; +import java.util.Map; import java.util.Stack; 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; @@ -108,16 +108,16 @@ public class XmlLogger implements BuildLogger { private Document doc = builder.newDocument(); /** Mapping for when tasks started (Task to TimedElement). */ - private Hashtable tasks = new Hashtable<>(); + private Map tasks = new Hashtable<>(); /** Mapping for when targets started (Target to TimedElement). */ - private Hashtable targets = new Hashtable<>(); + private Map targets = new Hashtable<>(); /** * Mapping of threads to stacks of elements * (Thread to Stack of TimedElement). */ - private Hashtable> threadStacks = new Hashtable<>(); + private Map> threadStacks = new Hashtable<>(); /** * When the build started. @@ -345,8 +345,7 @@ public class XmlLogger implements BuildLogger { if (element != null) { return element; } - return StreamUtils.enumerationAsStream(tasks.keys()) - .filter(UnknownElement.class::isInstance) + return tasks.keySet().stream().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/attribute/BaseIfAttribute.java b/src/main/org/apache/tools/ant/attribute/BaseIfAttribute.java index 14c22a589..fc861e13a 100644 --- a/src/main/org/apache/tools/ant/attribute/BaseIfAttribute.java +++ b/src/main/org/apache/tools/ant/attribute/BaseIfAttribute.java @@ -19,11 +19,10 @@ package org.apache.tools.ant.attribute; import java.util.HashMap; -import java.util.Hashtable; import java.util.Map; +import java.util.stream.Collectors; import org.apache.tools.ant.ProjectComponent; -import org.apache.tools.ant.RuntimeConfigurable; import org.apache.tools.ant.UnknownElement; @@ -68,17 +67,10 @@ public abstract class BaseIfAttribute * @return a map of attributes. */ protected Map getParams(UnknownElement el) { - Map ret = new HashMap<>(); - RuntimeConfigurable rc = el.getWrapper(); - Hashtable attributes = rc.getAttributeMap(); // This does a copy! - for (Map.Entry entry : attributes.entrySet()) { - String key = entry.getKey(); - if (key.startsWith("ant-attribute:param")) { - int pos = key.lastIndexOf(':'); - ret.put(key.substring(pos + 1), - el.getProject().replaceProperties((String) entry.getValue())); - } - } - return ret; + // this makes a copy! + return el.getWrapper().getAttributeMap().entrySet().stream() + .filter(e -> e.getKey().startsWith("ant-attribute:param")) + .collect(Collectors.toMap(e -> e.getKey().substring(e.getKey().lastIndexOf(':') + 1), + e -> el.getProject().replaceProperties((String) e.getValue()), (a, b) -> b)); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Recorder.java b/src/main/org/apache/tools/ant/taskdefs/Recorder.java index 66152d37d..90410b630 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Recorder.java +++ b/src/main/org/apache/tools/ant/taskdefs/Recorder.java @@ -18,6 +18,7 @@ package org.apache.tools.ant.taskdefs; import java.util.Hashtable; +import java.util.Map; import org.apache.tools.ant.BuildEvent; import org.apache.tools.ant.BuildException; @@ -66,7 +67,7 @@ public class Recorder extends Task implements SubBuildListener { /** Strip task banners if true. */ private boolean emacsMode = false; /** The list of recorder entries. */ - private static Hashtable recorderEntries = new Hashtable<>(); + private static Map recorderEntries = new Hashtable<>(); ////////////////////////////////////////////////////////////////////// // CONSTRUCTORS / INITIALIZERS @@ -304,11 +305,8 @@ public class Recorder extends Task implements SubBuildListener { * * @since Ant 1.7 */ - @SuppressWarnings("unchecked") private void cleanup() { - ((Hashtable) recorderEntries.clone()).entrySet().stream() - .filter(entry -> entry.getValue().getProject() == getProject()) - .forEach(entry -> recorderEntries.remove(entry.getKey())); + recorderEntries.entrySet().removeIf(e -> e.getValue().getProject() == getProject()); getProject().removeBuildListener(this); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ejb/BorlandDeploymentTool.java b/src/main/org/apache/tools/ant/taskdefs/optional/ejb/BorlandDeploymentTool.java index 8894045cf..3db3d5653 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ejb/BorlandDeploymentTool.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ejb/BorlandDeploymentTool.java @@ -16,10 +16,8 @@ * */ - package org.apache.tools.ant.taskdefs.optional.ejb; - import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -31,6 +29,7 @@ import java.util.Collection; import java.util.Hashtable; import java.util.List; import java.util.Map; + import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.ExecTask; @@ -41,7 +40,6 @@ import org.apache.tools.ant.taskdefs.optional.ejb.EjbJar.DTDLocation; import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.Path; - /** * BorlandDeploymentTool is dedicated to the Borland Application Server 4.5 and 4.5.1 * This task generates and compiles the stubs and skeletons for all ejb described into the @@ -60,7 +58,7 @@ import org.apache.tools.ant.types.Path; *
  • version (int) : tell what is the Borland appserver version 4 or 5
  • * * - *
    + *
      *
      *      <ejbjar srcdir="${build.classes}"
      *               basejarname="vsmp"
    @@ -74,7 +72,7 @@ import org.apache.tools.ant.types.Path;
      *          <include name="demo\helper\*.class"/>
      *         </support>
      *     </ejbjar>
    - *
    + *
    * */ public class BorlandDeploymentTool extends GenericDeploymentTool diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ejb/DescriptorHandler.java b/src/main/org/apache/tools/ant/taskdefs/optional/ejb/DescriptorHandler.java index eaef78cf2..4c3ea4951 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ejb/DescriptorHandler.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ejb/DescriptorHandler.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.nio.file.Files; +import java.util.Collections; import java.util.Hashtable; import java.util.Map; @@ -233,7 +234,7 @@ public class DescriptorHandler extends HandlerBase { * @return the map of files */ public Hashtable getFiles() { - return ejbFiles == null ? new Hashtable<>() : ejbFiles; + return new Hashtable<>(ejbFiles == null ? Collections.emptyMap() : ejbFiles); } /** diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java b/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java index c44aea478..5961eca22 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java @@ -21,13 +21,16 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; -import java.util.Enumeration; import java.util.Hashtable; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Vector; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.JarOutputStream; +import java.util.stream.Collectors; +import java.util.zip.ZipEntry; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; @@ -36,7 +39,6 @@ import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.Java; -import org.apache.tools.ant.taskdefs.optional.ejb.EjbJar.DTDLocation; import org.apache.tools.ant.types.Environment; import org.apache.tools.ant.types.Environment.Variable; import org.apache.tools.ant.types.Path; @@ -424,9 +426,7 @@ public class WeblogicDeploymentTool extends GenericDeploymentTool { handler.registerDTD(PUBLICID_WEBLOGIC_EJB510, weblogicDTD); handler.registerDTD(PUBLICID_WEBLOGIC_EJB600, weblogicDTD); - for (DTDLocation dtdLocation : getConfig().dtdLocations) { - handler.registerDTD(dtdLocation.getPublicId(), dtdLocation.getLocation()); - } + getConfig().dtdLocations.forEach(l -> handler.registerDTD(l.getPublicId(), l.getLocation())); return handler; } @@ -685,73 +685,62 @@ public class WeblogicDeploymentTool extends GenericDeploymentTool { genericJar = new JarFile(genericJarFile); wlJar = new JarFile(weblogicJarFile); - Hashtable genericEntries = new Hashtable<>(); - Hashtable wlEntries = new Hashtable<>(); - Hashtable replaceEntries = new Hashtable<>(); + Map replaceEntries = new HashMap<>(); //get the list of generic jar entries - for (Enumeration e = genericJar.entries(); e.hasMoreElements();) { - JarEntry je = e.nextElement(); - genericEntries.put(je.getName().replace('\\', '/'), je); - } + Map genericEntries = genericJar.stream() + .collect(Collectors.toMap(je -> je.getName().replace('\\', '/'), + je -> je, (a, b) -> b)); // get the list of WebLogic jar entries - for (Enumeration e = wlJar.entries(); e.hasMoreElements();) { - JarEntry je = e.nextElement(); - wlEntries.put(je.getName(), je); - } + Map wlEntries = wlJar.stream().collect(Collectors.toMap(ZipEntry::getName, + je -> je, (a, b) -> b)); // Cycle through generic and make sure its in WebLogic genericLoader = getClassLoaderFromJar(genericJarFile); - for (Enumeration e = genericEntries.keys(); e.hasMoreElements();) { - String filepath = e.nextElement(); - - if (wlEntries.containsKey(filepath)) { - // File name/path match - - // Check files see if same - JarEntry genericEntry = genericEntries.get(filepath); - JarEntry wlEntry = wlEntries.get(filepath); + for (String filepath : genericEntries.keySet()) { + if (!wlEntries.containsKey(filepath)) { + // a file doesn't exist rebuild + log("File " + filepath + " not present in weblogic jar", + Project.MSG_VERBOSE); + rebuild = true; + break; + } + // File name/path match + // Check files see if same + JarEntry genericEntry = genericEntries.get(filepath); + JarEntry wlEntry = wlEntries.get(filepath); - if (genericEntry.getCrc() != wlEntry.getCrc() - || genericEntry.getSize() != wlEntry.getSize()) { + if (genericEntry.getCrc() != wlEntry.getCrc() + || genericEntry.getSize() != wlEntry.getSize()) { - if (genericEntry.getName().endsWith(".class")) { - //File are different see if its an object or an interface - String classname - = genericEntry.getName() + if (genericEntry.getName().endsWith(".class")) { + //File are different see if its an object or an interface + String classname = genericEntry.getName() .replace(File.separatorChar, '.') .replace('/', '.'); - classname = classname.substring(0, classname.lastIndexOf(".class")); - - Class genclass = genericLoader.loadClass(classname); - - if (genclass.isInterface()) { - //Interface changed rebuild jar. - log("Interface " + genclass.getName() - + " has changed", Project.MSG_VERBOSE); - rebuild = true; - break; - } - //Object class Changed update it. - replaceEntries.put(filepath, genericEntry); - } else if (!"META-INF/MANIFEST.MF".equals(genericEntry.getName())) { - // it is not the manifest, otherwise we'd ignore it - // File other then class changed rebuild - log("Non class file " + genericEntry.getName() + classname = classname.substring(0, classname.lastIndexOf(".class")); + + Class genclass = genericLoader.loadClass(classname); + + if (genclass.isInterface()) { + //Interface changed rebuild jar. + log("Interface " + genclass.getName() + " has changed", Project.MSG_VERBOSE); rebuild = true; break; } + //Object class Changed update it. + replaceEntries.put(filepath, genericEntry); + } else if (!genericEntry.getName().equals("META-INF/MANIFEST.MF")) { + // it is not the manifest, otherwise we'd ignore it + // File other then class changed rebuild + log("Non class file " + genericEntry.getName() + + " has changed", Project.MSG_VERBOSE); + rebuild = true; + break; } - } else { - // a file doesn't exist rebuild - - log("File " + filepath + " not present in weblogic jar", - Project.MSG_VERBOSE); - rebuild = true; - break; } } 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 cca771651..4a9400758 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 @@ -22,6 +22,8 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.util.Hashtable; +import java.util.HashMap; +import java.util.Map; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.JarOutputStream; @@ -35,7 +37,6 @@ 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,21 +668,18 @@ public class WebsphereDeploymentTool extends GenericDeploymentTool { wasJar = new JarFile(websphereJarFile); //get the list of generic jar entries - Hashtable genericEntries - = StreamUtils.enumerationAsStream(genericJar.entries()) + Map genericEntries = genericJar.stream() .collect(Collectors.toMap(je -> je.getName().replace('\\', '/'), - je -> je, (a, b) -> b, Hashtable::new)); + je -> je, (a, b) -> b)); // get the list of WebSphere jar entries - Hashtable wasEntries - = StreamUtils.enumerationAsStream(wasJar.entries()) - .collect(Collectors.toMap(ZipEntry::getName, - je -> je, (a, b) -> b, Hashtable::new)); + Map wasEntries = wasJar.stream() + .collect(Collectors.toMap(ZipEntry::getName, je -> je, (a, b) -> b)); // Cycle through generic and make sure its in WebSphere genericLoader = getClassLoaderFromJar(genericJarFile); - Hashtable replaceEntries = new Hashtable<>(); + Map replaceEntries = new HashMap<>(); for (String filepath : genericEntries.keySet()) { if (!wasEntries.containsKey(filepath)) { // a file doesn't exist rebuild -- cgit v1.2.1