http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/Depend.java ---------------------------------------------------------------------- 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 f46af0a..c08b089 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 @@ -23,10 +23,18 @@ import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.io.PrintWriter; import java.net.URL; +import java.util.ArrayList; import java.util.Enumeration; -import java.util.Hashtable; -import java.util.Vector; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Stream; import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.BuildException; @@ -37,6 +45,9 @@ import org.apache.tools.ant.taskdefs.rmic.DefaultRmicAdapter; import org.apache.tools.ant.taskdefs.rmic.WLRmic; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Reference; +import org.apache.tools.ant.types.ResourceCollection; +import org.apache.tools.ant.types.resources.Difference; +import org.apache.tools.ant.types.resources.FileProvider; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.depend.DependencyAnalyzer; @@ -74,26 +85,23 @@ public class Depend extends MatchingTask { /** The directory which contains the dependency cache. */ private File cache; - /** The list of source paths derived from the srcPath field. */ - private String[] srcPathList; - /** * A map which gives for every class a list of the class which it * affects. */ - private Hashtable affectedClassMap; + private Map<String, Map<String, ClassFileInfo>> affectedClassMap; /** A map which gives information about a class */ - private Hashtable classFileInfoMap; + private Map<String, ClassFileInfo> classFileInfoMap; /** * A map which gives the list of jars and classes from the classpath * that a class depends upon */ - private Hashtable classpathDependencies; + private Map<String, Set<File>> classpathDependencies; /** The list of classes which are out of date. */ - private Hashtable outOfDateClasses; + private Map<String, String> outOfDateClasses; /** * indicates that the dependency relationships should be extended beyond @@ -182,29 +190,24 @@ public class Depend extends MatchingTask { * @return a collection of class dependencies * @exception IOException if the dependency file cannot be read */ - private Hashtable readCachedDependencies(File depFile) throws IOException { - Hashtable dependencyMap = new Hashtable(); + private Map<String, List<String>> readCachedDependencies(File depFile) throws IOException { + Map<String, List<String>> dependencyMap = new HashMap<>(); - BufferedReader in = null; - try { - in = new BufferedReader(new FileReader(depFile)); - String line = null; - Vector dependencyList = null; - String className = null; - int prependLength = CLASSNAME_PREPEND.length(); + int prependLength = CLASSNAME_PREPEND.length(); + + try (BufferedReader in = new BufferedReader(new FileReader(depFile))) { + List<String> dependencyList = null; + String line; while ((line = in.readLine()) != null) { if (line.startsWith(CLASSNAME_PREPEND)) { - dependencyList = new Vector(); - className = line.substring(prependLength); - dependencyMap.put(className, dependencyList); + String className = line.substring(prependLength); + dependencyList = dependencyMap.computeIfAbsent(className, + k -> new ArrayList<>()); } else if (dependencyList != null) { - dependencyList.addElement(line); + dependencyList.add(line); } } - } finally { - FileUtils.close(in); } - return dependencyMap; } @@ -214,32 +217,18 @@ public class Depend extends MatchingTask { * @param dependencyMap the map of dependencies to be written out. * @exception IOException if the dependency file cannot be written out. */ - private void writeCachedDependencies(Hashtable dependencyMap) + private void writeCachedDependencies(Map<String, List<String>> dependencyMap) throws IOException { if (cache != null) { - BufferedWriter pw = null; - try { - cache.mkdirs(); - File depFile = new File(cache, CACHE_FILE_NAME); - - pw = new BufferedWriter(new FileWriter(depFile)); - Enumeration e = dependencyMap.keys(); - while (e.hasMoreElements()) { - String className = (String) e.nextElement(); - - pw.write(CLASSNAME_PREPEND + className); - pw.newLine(); - - Vector dependencyList - = (Vector) dependencyMap.get(className); - int size = dependencyList.size(); - for (int x = 0; x < size; x++) { - pw.write(String.valueOf(dependencyList.elementAt(x))); - pw.newLine(); - } + cache.mkdirs(); + File depFile = new File(cache, CACHE_FILE_NAME); + try (PrintWriter pw = + new PrintWriter(new BufferedWriter(new FileWriter(depFile)))) { + for (Map.Entry<String, List<String>> e : dependencyMap + .entrySet()) { + pw.printf("%s%s%n", CLASSNAME_PREPEND, e.getKey()); + e.getValue().forEach(pw::println); } - } finally { - FileUtils.close(pw); } } } @@ -253,28 +242,16 @@ public class Depend extends MatchingTask { if (dependClasspath == null) { return null; } - - String[] destPathElements = destPath.list(); - String[] classpathElements = dependClasspath.list(); - String checkPath = ""; - for (int i = 0; i < classpathElements.length; ++i) { - String element = classpathElements[i]; - boolean inDestPath = false; - for (int j = 0; j < destPathElements.length && !inDestPath; ++j) { - inDestPath = destPathElements[j].equals(element); - } - if (!inDestPath) { - if (checkPath.length() == 0) { - checkPath = element; - } else { - checkPath += ":" + element; - } - } - } - - Path p = null; - if (checkPath.length() > 0) { - p = new Path(getProject(), checkPath); + Difference diff = new Difference(); + diff.add(destPath); + diff.add(dependClasspath); + + Path p; + if (diff.isEmpty()) { + p = null; + } else { + p = new Path(getProject()); + p.add(diff); } log("Classpath without dest dir is " + p, Project.MSG_DEBUG); @@ -300,11 +277,11 @@ public class Depend extends MatchingTask { * files cannot be read or written */ private void determineDependencies() throws IOException { - affectedClassMap = new Hashtable(); - classFileInfoMap = new Hashtable(); + affectedClassMap = new HashMap<>(); + classFileInfoMap = new HashMap<>(); boolean cacheDirty = false; - Hashtable dependencyMap = new Hashtable(); + Map<String, List<String>> dependencyMap = new HashMap<>(); File cacheFile = null; boolean cacheFileExists = true; long cacheLastModified = Long.MAX_VALUE; @@ -318,13 +295,11 @@ public class Depend extends MatchingTask { dependencyMap = readCachedDependencies(cacheFile); } } - Enumeration classfileEnum = getClassFiles(destPath).elements(); - while (classfileEnum.hasMoreElements()) { - ClassFileInfo info = (ClassFileInfo) classfileEnum.nextElement(); + for (ClassFileInfo info : getClassFiles()) { log("Adding class info for " + info.className, Project.MSG_DEBUG); classFileInfoMap.put(info.className, info); - Vector dependencyList = null; + List<String> dependencyList = null; if (cache != null) { // try to read the dependency info from the map if it is @@ -333,7 +308,7 @@ public class Depend extends MatchingTask { && cacheLastModified > info.absoluteFile.lastModified()) { // depFile exists and is newer than the class file // need to get dependency list from the map. - dependencyList = (Vector) dependencyMap.get(info.className); + dependencyList = dependencyMap.get(info.className); } } @@ -343,11 +318,11 @@ public class Depend extends MatchingTask { analyzer.addRootClass(info.className); analyzer.addClassPath(destPath); analyzer.setClosure(false); - dependencyList = new Vector(); - Enumeration depEnum = analyzer.getClassDependencies(); + dependencyList = new ArrayList<>(); + Enumeration<String> depEnum = analyzer.getClassDependencies(); while (depEnum.hasMoreElements()) { - Object o = depEnum.nextElement(); - dependencyList.addElement(o); + String o = depEnum.nextElement(); + dependencyList.add(o); log("Class " + info.className + " depends on " + o, Project.MSG_DEBUG); } @@ -357,18 +332,10 @@ public class Depend extends MatchingTask { // This class depends on each class in the dependency list. For each // one of those, add this class into their affected classes list - Enumeration depEnum = dependencyList.elements(); - while (depEnum.hasMoreElements()) { - String dependentClass = (String) depEnum.nextElement(); - - Hashtable affectedClasses - = (Hashtable) affectedClassMap.get(dependentClass); - if (affectedClasses == null) { - affectedClasses = new Hashtable(); - affectedClassMap.put(dependentClass, affectedClasses); - } - - affectedClasses.put(info.className, info); + for (String dependentClass : dependencyList) { + affectedClassMap + .computeIfAbsent(dependentClass, k -> new HashMap<>()) + .put(info.className, info); log(dependentClass + " affects " + info.className, Project.MSG_DEBUG); } @@ -378,21 +345,19 @@ public class Depend extends MatchingTask { Path checkPath = getCheckClassPath(); if (checkPath != null) { // now determine which jars each class depends upon - classpathDependencies = new Hashtable(); + classpathDependencies = new HashMap<>(); try (AntClassLoader loader = getProject().createClassLoader(checkPath)) { - Hashtable classpathFileCache = new Hashtable(); + Map<String, Object> classpathFileCache = new HashMap<>(); Object nullFileMarker = new Object(); - for (Enumeration e = dependencyMap.keys(); e.hasMoreElements();) { - String className = (String) e.nextElement(); + for (Map.Entry<String, List<String>> e : dependencyMap.entrySet()) { + String className = e.getKey(); log("Determining classpath dependencies for " + className, Project.MSG_DEBUG); - Vector dependencyList = (Vector) dependencyMap.get(className); - Hashtable dependencies = new Hashtable(); + List<String> dependencyList = e.getValue(); + Set<File> dependencies = new HashSet<>(); classpathDependencies.put(className, dependencies); - Enumeration e2 = dependencyList.elements(); - while (e2.hasMoreElements()) { - String dependency = (String) e2.nextElement(); + for (String dependency : dependencyList) { log("Looking for " + dependency, Project.MSG_DEBUG); Object classpathFileObject = classpathFileCache.get(dependency); @@ -405,22 +370,23 @@ public class Depend extends MatchingTask { = loader.getResource(dependency.replace('.', '/') + ".class"); log("URL is " + classURL, Project.MSG_DEBUG); if (classURL != null) { - if (classURL.getProtocol().equals("jar")) { + if ("jar".equals(classURL.getProtocol())) { String jarFilePath = classURL.getFile(); int classMarker = jarFilePath.indexOf('!'); jarFilePath = jarFilePath.substring(0, classMarker); if (jarFilePath.startsWith("file:")) { classpathFileObject = new File( - FileUtils.getFileUtils().fromURI(jarFilePath)); + FileUtils.getFileUtils() + .fromURI(jarFilePath)); } else { throw new IOException( - "Bizarre nested path in jar: protocol: " - + jarFilePath); + "Bizarre nested path in jar: protocol: " + + jarFilePath); } - } else if (classURL.getProtocol().equals("file")) { + } else if ("file".equals(classURL.getProtocol())) { classpathFileObject = new File( - FileUtils.getFileUtils() - .fromURI(classURL.toExternalForm())); + FileUtils.getFileUtils().fromURI( + classURL.toExternalForm())); } log("Class " + className + " depends on " + classpathFileObject @@ -437,7 +403,7 @@ public class Depend extends MatchingTask { File jarFile = (File) classpathFileObject; log("Adding a classpath dependency on " + jarFile, Project.MSG_DEBUG); - dependencies.put(jarFile, jarFile); + dependencies.add(jarFile); } } } @@ -460,11 +426,9 @@ public class Depend extends MatchingTask { */ private int deleteAllAffectedFiles() { int count = 0; - for (Enumeration e = outOfDateClasses.elements(); e.hasMoreElements();) { - String className = (String) e.nextElement(); + for (String className : outOfDateClasses.keySet()) { count += deleteAffectedFiles(className); - ClassFileInfo classInfo - = (ClassFileInfo) classFileInfoMap.get(className); + ClassFileInfo classInfo = classFileInfoMap.get(className); if (classInfo != null && classInfo.absoluteFile.exists()) { if (classInfo.sourceFile == null) { warnOutOfDateButNotDeleted(classInfo, className, className); @@ -487,14 +451,13 @@ public class Depend extends MatchingTask { private int deleteAffectedFiles(String className) { int count = 0; - Hashtable affectedClasses = (Hashtable) affectedClassMap.get(className); + Map<String, ClassFileInfo> affectedClasses = affectedClassMap.get(className); if (affectedClasses == null) { return count; } - for (Enumeration e = affectedClasses.keys(); e.hasMoreElements();) { - String affectedClass = (String) e.nextElement(); - ClassFileInfo affectedClassInfo - = (ClassFileInfo) affectedClasses.get(affectedClass); + for (Map.Entry<String, ClassFileInfo> e : affectedClasses.entrySet()) { + String affectedClass = e.getKey(); + ClassFileInfo affectedClassInfo = e.getValue(); if (!affectedClassInfo.absoluteFile.exists()) { continue; @@ -516,16 +479,16 @@ public class Depend extends MatchingTask { // without closure we may delete an inner class but not the // top level class which would not trigger a recompile. - if (affectedClass.indexOf("$") == -1) { + if (affectedClass.indexOf('$') == -1) { continue; } // need to delete the main class String topLevelClassName - = affectedClass.substring(0, affectedClass.indexOf("$")); + = affectedClass.substring(0, affectedClass.indexOf('$')); log("Top level class = " + topLevelClassName, Project.MSG_VERBOSE); ClassFileInfo topLevelClassInfo - = (ClassFileInfo) classFileInfoMap.get(topLevelClassName); + = classFileInfoMap.get(topLevelClassName); if (topLevelClassInfo != null && topLevelClassInfo.absoluteFile.exists()) { log("Deleting file " @@ -569,8 +532,8 @@ public class Depend extends MatchingTask { log("The class " + affectedClass + " in file " + affectedClassInfo.absoluteFile.getPath() + " is out of date due to " + className - + " but has not been deleted because its source file" - + " could not be determined", level); + + " but has not been deleted because its source file could not be determined", + level); affectedClassInfo.isUserWarned = true; } @@ -598,75 +561,48 @@ public class Depend extends MatchingTask { log("Reverse Dependency Dump for " + affectedClassMap.size() + " classes:", Project.MSG_DEBUG); - Enumeration classEnum = affectedClassMap.keys(); - while (classEnum.hasMoreElements()) { - String className = (String) classEnum.nextElement(); + affectedClassMap.forEach((className, affectedClasses) -> { log(" Class " + className + " affects:", Project.MSG_DEBUG); - Hashtable affectedClasses - = (Hashtable) affectedClassMap.get(className); - Enumeration affectedClassEnum = affectedClasses.keys(); - while (affectedClassEnum.hasMoreElements()) { - String affectedClass = (String) affectedClassEnum.nextElement(); - ClassFileInfo info - = (ClassFileInfo) affectedClasses.get(affectedClass); - log(" " + affectedClass + " in " - + info.absoluteFile.getPath(), Project.MSG_DEBUG); - } - } + affectedClasses.forEach((affectedClass, info) -> log( + " " + affectedClass + " in " + info.absoluteFile.getPath(), + Project.MSG_DEBUG)); + }); if (classpathDependencies != null) { log("Classpath file dependencies (Forward):", Project.MSG_DEBUG); - Enumeration classpathEnum = classpathDependencies.keys(); - while (classpathEnum.hasMoreElements()) { - String className = (String) classpathEnum.nextElement(); + classpathDependencies.forEach((className, dependencies) -> { log(" Class " + className + " depends on:", Project.MSG_DEBUG); - Hashtable dependencies - = (Hashtable) classpathDependencies.get(className); - - Enumeration classpathFileEnum = dependencies.elements(); - while (classpathFileEnum.hasMoreElements()) { - File classpathFile = (File) classpathFileEnum.nextElement(); - log(" " + classpathFile.getPath(), Project.MSG_DEBUG); - } - } + dependencies.forEach(f -> log(" " + f.getPath(), Project.MSG_DEBUG)); + }); } } private void determineOutOfDateClasses() { - outOfDateClasses = new Hashtable(); - for (int i = 0; i < srcPathList.length; i++) { - File srcDir = getProject().resolveFile(srcPathList[i]); - if (srcDir.exists()) { - DirectoryScanner ds = this.getDirectoryScanner(srcDir); - String[] files = ds.getIncludedFiles(); - scanDir(srcDir, files); - } - } + outOfDateClasses = new HashMap<>(); + directories(srcPath).forEach(srcDir -> { + DirectoryScanner ds = this.getDirectoryScanner(srcDir); + scanDir(srcDir, ds.getIncludedFiles()); + }); // now check classpath file dependencies if (classpathDependencies == null) { return; } - Enumeration classpathDepsEnum = classpathDependencies.keys(); - while (classpathDepsEnum.hasMoreElements()) { - String className = (String) classpathDepsEnum.nextElement(); + for (Map.Entry<String, Set<File>> e : classpathDependencies.entrySet()) { + String className = e.getKey(); if (outOfDateClasses.containsKey(className)) { continue; } - ClassFileInfo info - = (ClassFileInfo) classFileInfoMap.get(className); + ClassFileInfo info = classFileInfoMap.get(className); // if we have no info about the class - it may have been deleted already and we // are using cached info. if (info != null) { - Hashtable dependencies - = (Hashtable) classpathDependencies.get(className); - for (Enumeration e2 = dependencies.elements(); e2.hasMoreElements();) { - File classpathFile = (File) e2.nextElement(); - if (classpathFile.lastModified() - > info.absoluteFile.lastModified()) { + for (File classpathFile : e.getValue()) { + if (classpathFile.lastModified() > info.absoluteFile + .lastModified()) { log("Class " + className + " is out of date with respect to " + classpathFile, Project.MSG_DEBUG); @@ -683,6 +619,7 @@ public class Depend extends MatchingTask { * * @exception BuildException Thrown in case of an unrecoverable error. */ + @Override public void execute() throws BuildException { try { long start = System.currentTimeMillis(); @@ -691,8 +628,7 @@ public class Depend extends MatchingTask { getLocation()); } - srcPathList = srcPath.list(); - if (srcPathList.length == 0) { + if (!directories(srcPath).findAny().isPresent()) { throw new BuildException("srcdir attribute must be non-empty", getLocation()); } @@ -702,8 +638,8 @@ public class Depend extends MatchingTask { } if (cache != null && cache.exists() && !cache.isDirectory()) { - throw new BuildException("The cache, if specified, must " - + "point to a directory"); + throw new BuildException( + "The cache, if specified, must point to a directory"); } if (cache != null && !cache.exists()) { @@ -743,50 +679,38 @@ public class Depend extends MatchingTask { * checked. */ protected void scanDir(File srcDir, String[] files) { - - for (int i = 0; i < files.length; i++) { - File srcFile = new File(srcDir, files[i]); - if (files[i].endsWith(".java")) { + for (String f : files) { + File srcFile = new File(srcDir, f); + if (f.endsWith(".java")) { String filePath = srcFile.getPath(); String className = filePath.substring(srcDir.getPath().length() + 1, filePath.length() - ".java".length()); className = ClassFileUtils.convertSlashName(className); ClassFileInfo info - = (ClassFileInfo) classFileInfoMap.get(className); + = classFileInfoMap.get(className); if (info == null) { // there was no class file. add this class to the list outOfDateClasses.put(className, className); - } else { - if (srcFile.lastModified() - > info.absoluteFile.lastModified()) { - outOfDateClasses.put(className, className); - } + } else if (srcFile.lastModified() > info.absoluteFile + .lastModified()) { + outOfDateClasses.put(className, className); } } } } - /** * Get the list of class files we are going to analyse. * - * @param classLocations a path structure containing all the directories - * where classes can be found. * @return a vector containing the classes to analyse. */ - private Vector getClassFiles(Path classLocations) { + private List<ClassFileInfo> getClassFiles() { // break the classLocations into its components. - String[] classLocationsList = classLocations.list(); + List<ClassFileInfo> classFileList = new ArrayList<>(); - Vector classFileList = new Vector(); - - for (int i = 0; i < classLocationsList.length; ++i) { - File dir = new File(classLocationsList[i]); - if (dir.isDirectory()) { - addClassFiles(classFileList, dir, dir); - } - } + directories(destPath) + .forEach(dir -> addClassFiles(classFileList, dir, dir)); return classFileList; } @@ -800,21 +724,17 @@ public class Depend extends MatchingTask { */ private File findSourceFile(String classname, File sourceFileKnownToExist) { String sourceFilename; - int innerIndex = classname.indexOf("$"); + int innerIndex = classname.indexOf('$'); if (innerIndex != -1) { sourceFilename = classname.substring(0, innerIndex) + ".java"; } else { sourceFilename = classname + ".java"; } - // search the various source path entries - for (int i = 0; i < srcPathList.length; ++i) { - File sourceFile = new File(srcPathList[i], sourceFilename); - if (sourceFile.equals(sourceFileKnownToExist) || sourceFile.exists()) { - return sourceFile; - } - } - return null; + return directories(srcPath) + .map(d -> new File(d, sourceFilename)).filter(Predicate + .<File> isEqual(sourceFileKnownToExist).or(File::exists)) + .findFirst().orElse(null); } /** @@ -829,36 +749,34 @@ public class Depend extends MatchingTask { * the absolute class name from the relative position in the * source tree */ - private void addClassFiles(Vector classFileList, File dir, File root) { - String[] filesInDir = dir.list(); + private void addClassFiles(List<ClassFileInfo> classFileList, File dir, File root) { + File[] children = dir.listFiles(); - if (filesInDir == null) { + if (children == null) { return; } - int length = filesInDir.length; int rootLength = root.getPath().length(); File sourceFileKnownToExist = null; // speed optimization - for (int i = 0; i < length; ++i) { - File file = new File(dir, filesInDir[i]); - if (filesInDir[i].endsWith(".class")) { + for (File file : children) { + if (file.getName().endsWith(".class")) { ClassFileInfo info = new ClassFileInfo(); info.absoluteFile = file; - String relativeName = file.getPath().substring( - rootLength + 1, - file.getPath().length() - ".class".length()); + + String relativeName = file.getPath().substring(rootLength + 1, + file.getPath().length() - ".class".length()); + info.className = ClassFileUtils.convertSlashName(relativeName); - info.sourceFile = sourceFileKnownToExist = findSourceFile( - relativeName, sourceFileKnownToExist); - classFileList.addElement(info); + info.sourceFile = sourceFileKnownToExist = + findSourceFile(relativeName, sourceFileKnownToExist); + classFileList.add(info); } else { addClassFiles(classFileList, file, root); } } } - /** * Set the directories path to find the Java source files. * @@ -907,5 +825,10 @@ public class Depend extends MatchingTask { public void setDump(boolean dump) { this.dump = dump; } -} + private Stream<File> directories(ResourceCollection rc) { + return rc.stream().map(r -> r.as(FileProvider.class)) + .filter(Objects::nonNull).map(FileProvider::getFile) + .filter(File::isDirectory); + } +}
http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/DirectoryIterator.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/DirectoryIterator.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/DirectoryIterator.java index 4401f1c..7137918 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/DirectoryIterator.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/DirectoryIterator.java @@ -19,11 +19,14 @@ package org.apache.tools.ant.taskdefs.optional.depend; import java.io.File; import java.io.IOException; +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.Collections; +import java.util.Deque; +import java.util.Iterator; +import java.util.List; import java.io.InputStream; import java.nio.file.Files; -import java.util.Enumeration; -import java.util.Stack; -import java.util.Vector; /** * An iterator which iterates through the contents of a java directory. The @@ -37,7 +40,7 @@ public class DirectoryIterator implements ClassFileIterator { * This is a stack of current iterators supporting the depth first * traversal of the directory tree. */ - private Stack enumStack; + private Deque<Iterator<File>> enumStack; /** * The current directory iterator. As directories encounter lower level @@ -46,7 +49,7 @@ public class DirectoryIterator implements ClassFileIterator { * directory. This implements a depth first traversal of the directory * namespace. */ - private Enumeration currentEnum; + private Iterator<File> currentIterator; /** * Creates a directory iterator. The directory iterator is created to @@ -64,12 +67,8 @@ public class DirectoryIterator implements ClassFileIterator { public DirectoryIterator(File rootDirectory, boolean changeInto) throws IOException { super(); - - enumStack = new Stack(); - - Vector filesInRoot = getDirectoryEntries(rootDirectory); - - currentEnum = filesInRoot.elements(); + enumStack = new ArrayDeque<>(); + currentIterator = getDirectoryEntries(rootDirectory).iterator(); } /** @@ -80,21 +79,12 @@ public class DirectoryIterator implements ClassFileIterator { * @return a vector containing File objects for each entry in the * directory. */ - private Vector getDirectoryEntries(File directory) { - Vector files = new Vector(); - - // File[] filesInDir = directory.listFiles(); - String[] filesInDir = directory.list(); - - if (filesInDir != null) { - int length = filesInDir.length; - - for (int i = 0; i < length; ++i) { - files.addElement(new File(directory, filesInDir[i])); - } + private List<File> getDirectoryEntries(File directory) { + File[] filesInDir = directory.listFiles(); + if (filesInDir == null) { + return Collections.emptyList(); } - - return files; + return Arrays.asList(filesInDir); } /** @@ -111,25 +101,25 @@ public class DirectoryIterator implements ClassFileIterator { * * @return the next ClassFile in the iteration. */ + @Override public ClassFile getNextClassFile() { ClassFile nextElement = null; try { while (nextElement == null) { - if (currentEnum.hasMoreElements()) { - File element = (File) currentEnum.nextElement(); + if (currentIterator.hasNext()) { + File element = currentIterator.next(); if (element.isDirectory()) { // push the current iterator onto the stack and then // iterate through this directory. - enumStack.push(currentEnum); + enumStack.push(currentIterator); - Vector files = getDirectoryEntries(element); + List<File> files = getDirectoryEntries(element); - currentEnum = files.elements(); + currentIterator = files.iterator(); } else { - // we have a file. create a stream for it try (InputStream inFileStream = Files.newInputStream(element.toPath())) { @@ -145,13 +135,11 @@ public class DirectoryIterator implements ClassFileIterator { } } } + } else // this iterator is exhausted. Can we pop one off the stack + if (enumStack.isEmpty()) { + break; } else { - // this iterator is exhausted. Can we pop one off the stack - if (enumStack.empty()) { - break; - } else { - currentEnum = (Enumeration) enumStack.pop(); - } + currentIterator = enumStack.pop(); } } } catch (IOException e) { @@ -162,4 +150,3 @@ public class DirectoryIterator implements ClassFileIterator { } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/JarFileIterator.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/JarFileIterator.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/JarFileIterator.java index c468b95..cc2d60d 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/JarFileIterator.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/JarFileIterator.java @@ -41,7 +41,6 @@ public class JarFileIterator implements ClassFileIterator { */ public JarFileIterator(InputStream stream) throws IOException { super(); - jarStream = new ZipInputStream(stream); } @@ -50,6 +49,7 @@ public class JarFileIterator implements ClassFileIterator { * * @return a ClassFile object describing the class from the jar */ + @Override public ClassFile getNextClassFile() { ZipEntry jarEntry; ClassFile nextElement = null; @@ -69,7 +69,6 @@ public class JarFileIterator implements ClassFileIterator { nextElement = javaClass; } else { - jarEntry = jarStream.getNextEntry(); } } @@ -83,9 +82,7 @@ public class JarFileIterator implements ClassFileIterator { throw new BuildException("Problem reading JAR file: " + text); } - return nextElement; } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ClassCPInfo.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ClassCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ClassCPInfo.java index 8abbfc8..a870fc7 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ClassCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ClassCPInfo.java @@ -54,6 +54,7 @@ public class ClassCPInfo extends ConstantPoolEntry { * @exception IOException thrown if there is a problem reading the entry * from the stream. */ + @Override public void read(DataInputStream cpStream) throws IOException { index = cpStream.readUnsignedShort(); className = "unresolved"; @@ -64,6 +65,7 @@ public class ClassCPInfo extends ConstantPoolEntry { * * @return string representation of this constant pool entry */ + @Override public String toString() { return "Class Constant Pool Entry for " + className + "[" + index + "]"; } @@ -74,6 +76,7 @@ public class ClassCPInfo extends ConstantPoolEntry { * @param constantPool the constant pool with which to resolve the * class. */ + @Override public void resolve(ConstantPool constantPool) { className = ((Utf8CPInfo) constantPool.getEntry(index)).getValue(); @@ -90,4 +93,3 @@ public class ClassCPInfo extends ConstantPoolEntry { } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantCPInfo.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantCPInfo.java index 6103422..1865c07 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantCPInfo.java @@ -60,4 +60,3 @@ public abstract class ConstantCPInfo extends ConstantPoolEntry { } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantPool.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantPool.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantPool.java index 9bec0ed..ec7ed64 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantPool.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantPool.java @@ -23,6 +23,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.IntStream; /** * The constant pool of a Java class. The constant pool is a collection of @@ -343,16 +345,11 @@ public class ConstantPool { * * @return the constant pool entries as strings */ + @Override public String toString() { - StringBuilder sb = new StringBuilder("\n"); - final int size = entries.size(); - - for (int i = 0; i < size; ++i) { - sb.append('[').append(i).append("] = ").append(getEntry(i)).append('\n'); - } - - return sb.toString(); + return IntStream.range(0, entries.size()) + .mapToObj(i -> String.format("[%d] = %s", i, getEntry(i))) + .collect(Collectors.joining("\n", "\n", "\n")); } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantPoolEntry.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantPoolEntry.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantPoolEntry.java index 26a0d09..b639fa9 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantPoolEntry.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantPoolEntry.java @@ -115,72 +115,57 @@ public abstract class ConstantPoolEntry { */ public static ConstantPoolEntry readEntry(DataInputStream cpStream) throws IOException { - ConstantPoolEntry cpInfo = null; int cpTag = cpStream.readUnsignedByte(); + ConstantPoolEntry cpInfo; switch (cpTag) { case CONSTANT_UTF8: cpInfo = new Utf8CPInfo(); - break; case CONSTANT_INTEGER: cpInfo = new IntegerCPInfo(); - break; case CONSTANT_FLOAT: cpInfo = new FloatCPInfo(); - break; case CONSTANT_LONG: cpInfo = new LongCPInfo(); - break; case CONSTANT_DOUBLE: cpInfo = new DoubleCPInfo(); - break; case CONSTANT_CLASS: cpInfo = new ClassCPInfo(); - break; case CONSTANT_STRING: cpInfo = new StringCPInfo(); - break; case CONSTANT_FIELDREF: cpInfo = new FieldRefCPInfo(); - break; case CONSTANT_METHODREF: cpInfo = new MethodRefCPInfo(); - break; case CONSTANT_INTERFACEMETHODREF: cpInfo = new InterfaceMethodRefCPInfo(); - break; case CONSTANT_NAMEANDTYPE: cpInfo = new NameAndTypeCPInfo(); - break; case CONSTANT_METHODHANDLE: cpInfo = new MethodHandleCPInfo(); - break; case CONSTANT_METHODTYPE: cpInfo = new MethodTypeCPInfo(); - break; case CONSTANT_INVOKEDYNAMIC: cpInfo = new InvokeDynamicCPInfo(); - break; default: throw new ClassFormatError("Invalid Constant Pool entry Type " + cpTag); } - cpInfo.read(cpStream); return cpInfo; @@ -239,4 +224,3 @@ public abstract class ConstantPoolEntry { } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/DoubleCPInfo.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/DoubleCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/DoubleCPInfo.java index a21c0d6..5f28999 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/DoubleCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/DoubleCPInfo.java @@ -41,8 +41,9 @@ public class DoubleCPInfo extends ConstantCPInfo { * @exception IOException if there is a problem reading the entry from the * stream. */ + @Override public void read(DataInputStream cpStream) throws IOException { - setValue(new Double(cpStream.readDouble())); + setValue(Double.valueOf(cpStream.readDouble())); } /** @@ -50,9 +51,9 @@ public class DoubleCPInfo extends ConstantCPInfo { * * @return the string representation of this constant pool entry. */ + @Override public String toString() { return "Double Constant Pool Entry: " + getValue(); } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/FieldRefCPInfo.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/FieldRefCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/FieldRefCPInfo.java index 06c0925..1367b62 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/FieldRefCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/FieldRefCPInfo.java @@ -49,6 +49,7 @@ public class FieldRefCPInfo extends ConstantPoolEntry { * @exception IOException if there is a problem reading the entry from * the stream. */ + @Override public void read(DataInputStream cpStream) throws IOException { classIndex = cpStream.readUnsignedShort(); nameAndTypeIndex = cpStream.readUnsignedShort(); @@ -61,6 +62,7 @@ public class FieldRefCPInfo extends ConstantPoolEntry { * @param constantPool the constant pool of which this entry is a member * and against which this entry is to be resolved. */ + @Override public void resolve(ConstantPool constantPool) { ClassCPInfo fieldClass = (ClassCPInfo) constantPool.getEntry(classIndex); @@ -85,18 +87,14 @@ public class FieldRefCPInfo extends ConstantPoolEntry { * * @return the string representation of this constant pool entry. */ + @Override public String toString() { - String value; - if (isResolved()) { - value = "Field : Class = " + fieldClassName + ", name = " - + fieldName + ", type = " + fieldType; - } else { - value = "Field : Class index = " + classIndex - + ", name and type index = " + nameAndTypeIndex; + return "Field : Class = " + fieldClassName + ", name = " + fieldName + + ", type = " + fieldType; } - - return value; + return "Field : Class index = " + classIndex + + ", name and type index = " + nameAndTypeIndex; } /** @@ -127,4 +125,3 @@ public class FieldRefCPInfo extends ConstantPoolEntry { } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/FloatCPInfo.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/FloatCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/FloatCPInfo.java index 532b672..d9bf465 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/FloatCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/FloatCPInfo.java @@ -39,8 +39,9 @@ public class FloatCPInfo extends ConstantCPInfo { * @exception IOException if there is a problem reading the entry from * the stream. */ + @Override public void read(DataInputStream cpStream) throws IOException { - setValue(new Float(cpStream.readFloat())); + setValue(Float.valueOf(cpStream.readFloat())); } /** @@ -48,9 +49,9 @@ public class FloatCPInfo extends ConstantCPInfo { * * @return the string representation of this constant pool entry. */ + @Override public String toString() { return "Float Constant Pool Entry: " + getValue(); } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/IntegerCPInfo.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/IntegerCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/IntegerCPInfo.java index 3beaa8c..a3ecdaa 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/IntegerCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/IntegerCPInfo.java @@ -39,8 +39,9 @@ public class IntegerCPInfo extends ConstantCPInfo { * @exception IOException if there is a problem reading the entry from * the stream. */ + @Override public void read(DataInputStream cpStream) throws IOException { - setValue(new Integer(cpStream.readInt())); + setValue(Integer.valueOf(cpStream.readInt())); } /** @@ -48,9 +49,9 @@ public class IntegerCPInfo extends ConstantCPInfo { * * @return the string representation of this constant pool entry. */ + @Override public String toString() { return "Integer Constant Pool Entry: " + getValue(); } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/InterfaceMethodRefCPInfo.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/InterfaceMethodRefCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/InterfaceMethodRefCPInfo.java index fbc23c1..4f7246c 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/InterfaceMethodRefCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/InterfaceMethodRefCPInfo.java @@ -55,6 +55,7 @@ public class InterfaceMethodRefCPInfo extends ConstantPoolEntry { * @exception IOException if there is a problem reading the entry from * the stream. */ + @Override public void read(DataInputStream cpStream) throws IOException { classIndex = cpStream.readUnsignedShort(); nameAndTypeIndex = cpStream.readUnsignedShort(); @@ -67,6 +68,7 @@ public class InterfaceMethodRefCPInfo extends ConstantPoolEntry { * @param constantPool the constant pool of which this entry is a member * and against which this entry is to be resolved. */ + @Override public void resolve(ConstantPool constantPool) { ClassCPInfo interfaceMethodClass = (ClassCPInfo) constantPool.getEntry(classIndex); @@ -91,19 +93,16 @@ public class InterfaceMethodRefCPInfo extends ConstantPoolEntry { * * @return the string representation of this constant pool entry. */ + @Override public String toString() { - String value; - if (isResolved()) { - value = "InterfaceMethod : Class = " + interfaceMethodClassName + return "InterfaceMethod : Class = " + interfaceMethodClassName + ", name = " + interfaceMethodName + ", type = " + interfaceMethodType; - } else { - value = "InterfaceMethod : Class index = " + classIndex - + ", name and type index = " + nameAndTypeIndex; - } + } + return "InterfaceMethod : Class index = " + classIndex + + ", name and type index = " + nameAndTypeIndex; - return value; } /** @@ -134,4 +133,3 @@ public class InterfaceMethodRefCPInfo extends ConstantPoolEntry { } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/InvokeDynamicCPInfo.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/InvokeDynamicCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/InvokeDynamicCPInfo.java index 3795db7..176b99a 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/InvokeDynamicCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/InvokeDynamicCPInfo.java @@ -32,7 +32,7 @@ public class InvokeDynamicCPInfo extends ConstantCPInfo { private int nameAndTypeIndex; /** the name and type CP info pointed to */ private NameAndTypeCPInfo nameAndTypeCPInfo; - /** */ + /** Constructor. */ public InvokeDynamicCPInfo() { super(CONSTANT_INVOKEDYNAMIC, 1); @@ -46,6 +46,7 @@ public class InvokeDynamicCPInfo extends ConstantCPInfo { * @exception java.io.IOException if there is a problem reading the entry from * the stream. */ + @Override public void read(DataInputStream cpStream) throws IOException { bootstrapMethodAttrIndex = cpStream.readUnsignedShort(); nameAndTypeIndex = cpStream.readUnsignedShort(); @@ -56,16 +57,14 @@ public class InvokeDynamicCPInfo extends ConstantCPInfo { * * @return the string representation of this constant pool entry. */ + @Override public String toString() { - String value; if (isResolved()) { - value = "Name = " + nameAndTypeCPInfo.getName() + ", type = " + nameAndTypeCPInfo.getType(); - } else { - value = "BootstrapMethodAttrIndex inx = " + bootstrapMethodAttrIndex - + "NameAndType index = " + nameAndTypeIndex; + return "Name = " + nameAndTypeCPInfo.getName() + ", type = " + + nameAndTypeCPInfo.getType(); } - - return value; + return "BootstrapMethodAttrIndex inx = " + bootstrapMethodAttrIndex + + "NameAndType index = " + nameAndTypeIndex; } /** * Resolve this constant pool entry with respect to its dependents in @@ -74,6 +73,7 @@ public class InvokeDynamicCPInfo extends ConstantCPInfo { * @param constantPool the constant pool of which this entry is a member * and against which this entry is to be resolved. */ + @Override public void resolve(ConstantPool constantPool) { nameAndTypeCPInfo = (NameAndTypeCPInfo) constantPool.getEntry(nameAndTypeIndex); @@ -82,4 +82,3 @@ public class InvokeDynamicCPInfo extends ConstantCPInfo { } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/LongCPInfo.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/LongCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/LongCPInfo.java index e854f04..9e57a39 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/LongCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/LongCPInfo.java @@ -39,8 +39,9 @@ public class LongCPInfo extends ConstantCPInfo { * @exception IOException if there is a problem reading the entry from * the stream. */ + @Override public void read(DataInputStream cpStream) throws IOException { - setValue(new Long(cpStream.readLong())); + setValue(Long.valueOf(cpStream.readLong())); } /** @@ -48,9 +49,9 @@ public class LongCPInfo extends ConstantCPInfo { * * @return the string representation of this constant pool entry. */ + @Override public String toString() { return "Long Constant Pool Entry: " + getValue(); } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodHandleCPInfo.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodHandleCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodHandleCPInfo.java index e11e3aa..e304d41 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodHandleCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodHandleCPInfo.java @@ -36,22 +36,23 @@ public class MethodHandleCPInfo extends ConstantPoolEntry { * signature of the method */ private int nameAndTypeIndex; + public enum ReferenceKind { - REF_getField(1), - REF_getStatic(2), - REF_putField(3), - REF_putStatic(4), - REF_invokeVirtual(5), - REF_invokeStatic(6), - REF_invokeSpecial(7), - REF_newInvokeSpecial(8), - REF_invokeInterface(9); - private final int referenceKind; - ReferenceKind(int referenceKind) { - this.referenceKind = referenceKind; - } + REF_getField, + REF_getStatic, + REF_putField, + REF_putStatic, + REF_invokeVirtual, + REF_invokeStatic, + REF_invokeSpecial, + REF_newInvokeSpecial, + REF_invokeInterface; + public int value() { + return ordinal() + 1; + } } + /** Constructor. */ public MethodHandleCPInfo() { super(CONSTANT_METHODHANDLE, 1); @@ -65,9 +66,9 @@ public class MethodHandleCPInfo extends ConstantPoolEntry { * @exception java.io.IOException if there is a problem reading the entry from * the stream. */ + @Override public void read(DataInputStream cpStream) throws IOException { referenceKind = ReferenceKind.values()[cpStream.readUnsignedByte() - 1]; - referenceIndex = cpStream.readUnsignedShort(); } @@ -76,17 +77,13 @@ public class MethodHandleCPInfo extends ConstantPoolEntry { * * @return the string representation of this constant pool entry. */ + @Override public String toString() { - String value; - if (isResolved()) { - value = "MethodHandle : " + reference.toString(); - } else { - value = "MethodHandle : Reference kind = " + referenceKind - + "Reference index = " + referenceIndex; + return "MethodHandle : " + reference.toString(); } - - return value; + return "MethodHandle : Reference kind = " + referenceKind + + "Reference index = " + referenceIndex; } /** @@ -96,12 +93,11 @@ public class MethodHandleCPInfo extends ConstantPoolEntry { * @param constantPool the constant pool of which this entry is a member * and against which this entry is to be resolved. */ + @Override public void resolve(ConstantPool constantPool) { reference = constantPool.getEntry(referenceIndex); reference.resolve(constantPool); super.resolve(constantPool); } - } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodRefCPInfo.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodRefCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodRefCPInfo.java index 6b33521..b90169f 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodRefCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodRefCPInfo.java @@ -52,6 +52,7 @@ public class MethodRefCPInfo extends ConstantPoolEntry { * @exception IOException if there is a problem reading the entry from * the stream. */ + @Override public void read(DataInputStream cpStream) throws IOException { classIndex = cpStream.readUnsignedShort(); nameAndTypeIndex = cpStream.readUnsignedShort(); @@ -62,18 +63,14 @@ public class MethodRefCPInfo extends ConstantPoolEntry { * * @return the string representation of this constant pool entry. */ + @Override public String toString() { - String value; - if (isResolved()) { - value = "Method : Class = " + methodClassName + ", name = " - + methodName + ", type = " + methodType; - } else { - value = "Method : Class index = " + classIndex - + ", name and type index = " + nameAndTypeIndex; + return "Method : Class = " + methodClassName + ", name = " + + methodName + ", type = " + methodType; } - - return value; + return "Method : Class index = " + classIndex + + ", name and type index = " + nameAndTypeIndex; } /** @@ -83,6 +80,7 @@ public class MethodRefCPInfo extends ConstantPoolEntry { * @param constantPool the constant pool of which this entry is a member * and against which this entry is to be resolved. */ + @Override public void resolve(ConstantPool constantPool) { ClassCPInfo methodClass = (ClassCPInfo) constantPool.getEntry(classIndex); @@ -130,4 +128,3 @@ public class MethodRefCPInfo extends ConstantPoolEntry { } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodTypeCPInfo.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodTypeCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodTypeCPInfo.java index d3c35ce..7b6f9a6 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodTypeCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodTypeCPInfo.java @@ -30,6 +30,7 @@ public class MethodTypeCPInfo extends ConstantCPInfo { private int methodDescriptorIndex; /** the value of the method descriptor pointed to */ private String methodDescriptor; + /** Constructor. */ public MethodTypeCPInfo() { super(CONSTANT_METHODTYPE, 1); @@ -63,6 +64,7 @@ public class MethodTypeCPInfo extends ConstantCPInfo { methodDescriptor = methodClass.getValue(); super.resolve(constantPool); } + /** * Print a readable version of the constant pool entry. * @@ -70,13 +72,10 @@ public class MethodTypeCPInfo extends ConstantCPInfo { */ @Override public String toString() { - if (!isResolved()) { - return "MethodDescriptorIndex: " + methodDescriptorIndex; - } else { + if (isResolved()) { return "MethodDescriptor: " + methodDescriptor; - } + return "MethodDescriptorIndex: " + methodDescriptorIndex; } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/NameAndTypeCPInfo.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/NameAndTypeCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/NameAndTypeCPInfo.java index 47f454d..761808b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/NameAndTypeCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/NameAndTypeCPInfo.java @@ -25,6 +25,20 @@ import java.io.IOException; * */ public class NameAndTypeCPInfo extends ConstantPoolEntry { + /** the name component of this entry */ + private String name; + /** the type component of this entry */ + private String type; + /** + * the index into the constant pool at which the name component's string + * value is stored + */ + private int nameIndex; + /** + * the index into the constant pool where the type descriptor string is + * stored. + */ + private int descriptorIndex; /** Constructor. */ public NameAndTypeCPInfo() { @@ -39,6 +53,7 @@ public class NameAndTypeCPInfo extends ConstantPoolEntry { * @exception IOException if there is a problem reading the entry from * the stream. */ + @Override public void read(DataInputStream cpStream) throws IOException { nameIndex = cpStream.readUnsignedShort(); descriptorIndex = cpStream.readUnsignedShort(); @@ -49,17 +64,13 @@ public class NameAndTypeCPInfo extends ConstantPoolEntry { * * @return the string representation of this constant pool entry. */ + @Override public String toString() { - String value; - if (isResolved()) { - value = "Name = " + name + ", type = " + type; - } else { - value = "Name index = " + nameIndex - + ", descriptor index = " + descriptorIndex; + return "Name = " + name + ", type = " + type; } - - return value; + return "Name index = " + nameIndex + ", descriptor index = " + + descriptorIndex; } /** @@ -69,6 +80,7 @@ public class NameAndTypeCPInfo extends ConstantPoolEntry { * @param constantPool the constant pool of which this entry is a member * and against which this entry is to be resolved. */ + @Override public void resolve(ConstantPool constantPool) { name = ((Utf8CPInfo) constantPool.getEntry(nameIndex)).getValue(); type = ((Utf8CPInfo) constantPool.getEntry(descriptorIndex)).getValue(); @@ -94,19 +106,4 @@ public class NameAndTypeCPInfo extends ConstantPoolEntry { return type; } - /** the name component of this entry */ - private String name; - /** the type component of this entry */ - private String type; - /** - * the index into the constant pool at which the name component's string - * value is stored - */ - private int nameIndex; - /** - * the index into the constant pool where the type descriptor string is - * stored. - */ - private int descriptorIndex; } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/StringCPInfo.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/StringCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/StringCPInfo.java index bc9ee24..7503403 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/StringCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/StringCPInfo.java @@ -26,6 +26,8 @@ import java.io.IOException; * */ public class StringCPInfo extends ConstantCPInfo { + /** the index into the constant pool containing the string's content */ + private int index; /** Constructor. */ public StringCPInfo() { @@ -40,9 +42,9 @@ public class StringCPInfo extends ConstantCPInfo { * @exception IOException if there is a problem reading the entry from * the stream. */ + @Override public void read(DataInputStream cpStream) throws IOException { index = cpStream.readUnsignedShort(); - setValue("unresolved"); } @@ -51,6 +53,7 @@ public class StringCPInfo extends ConstantCPInfo { * * @return the string representation of this constant pool entry. */ + @Override public String toString() { return "String Constant Pool Entry for " + getValue() + "[" + index + "]"; @@ -63,12 +66,11 @@ public class StringCPInfo extends ConstantCPInfo { * @param constantPool the constant pool of which this entry is a member * and against which this entry is to be resolved. */ + @Override public void resolve(ConstantPool constantPool) { setValue(((Utf8CPInfo) constantPool.getEntry(index)).getValue()); super.resolve(constantPool); } - /** the index into the constant pool containing the string's content */ - private int index; } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/Utf8CPInfo.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/Utf8CPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/Utf8CPInfo.java index 5471ccd..724bc9f 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/Utf8CPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/Utf8CPInfo.java @@ -41,6 +41,7 @@ public class Utf8CPInfo extends ConstantPoolEntry { * @exception IOException if there is a problem reading the entry from * the stream. */ + @Override public void read(DataInputStream cpStream) throws IOException { value = cpStream.readUTF(); } @@ -50,6 +51,7 @@ public class Utf8CPInfo extends ConstantPoolEntry { * * @return the string representation of this constant pool entry. */ + @Override public String toString() { return "UTF8 Value = " + value; } @@ -64,4 +66,3 @@ public class Utf8CPInfo extends ConstantPoolEntry { } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/ejb/BorlandDeploymentTool.java ---------------------------------------------------------------------- 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 4eefaeb..c1e5799 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 @@ -26,16 +26,18 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Collection; import java.util.Hashtable; -import java.util.Iterator; -import java.util.Vector; - +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; import org.apache.tools.ant.taskdefs.Execute; import org.apache.tools.ant.taskdefs.ExecuteStreamHandler; import org.apache.tools.ant.taskdefs.Java; +import org.apache.tools.ant.taskdefs.optional.ejb.EjbJar.DTDLocation; import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.Path; @@ -78,19 +80,18 @@ import org.apache.tools.ant.types.Path; public class BorlandDeploymentTool extends GenericDeploymentTool implements ExecuteStreamHandler { /** Borland 1.1 ejb id */ - public static final String PUBLICID_BORLAND_EJB - = "-//Inprise Corporation//DTD Enterprise JavaBeans 1.1//EN"; + public static final String PUBLICID_BORLAND_EJB = + "-//Inprise Corporation//DTD Enterprise JavaBeans 1.1//EN"; - protected static final String DEFAULT_BAS45_EJB11_DTD_LOCATION - = "/com/inprise/j2ee/xml/dtds/ejb-jar.dtd"; + protected static final String DEFAULT_BAS45_EJB11_DTD_LOCATION = + "/com/inprise/j2ee/xml/dtds/ejb-jar.dtd"; - protected static final String DEFAULT_BAS_DTD_LOCATION - = "/com/inprise/j2ee/xml/dtds/ejb-inprise.dtd"; + protected static final String DEFAULT_BAS_DTD_LOCATION = + "/com/inprise/j2ee/xml/dtds/ejb-inprise.dtd"; protected static final String BAS_DD = "ejb-inprise.xml"; protected static final String BES_DD = "ejb-borland.xml"; - /** Java2iiop executable **/ protected static final String JAVA2IIOP = "java2iiop"; @@ -114,13 +115,13 @@ public class BorlandDeploymentTool extends GenericDeploymentTool /** Borland Enterprise Server = version 5 */ static final int BES = 5; + /** Borland Application Server or Inprise Application Server = version 4 */ static final int BAS = 4; /** borland appserver version 4 or 5 */ private int version = BAS; - /** * Instance variable that determines whether it is necessary to verify the * produced jar @@ -128,7 +129,7 @@ public class BorlandDeploymentTool extends GenericDeploymentTool private boolean verify = true; private String verifyArgs = ""; - private Hashtable genfiles = new Hashtable(); + private Map<String, File> genfiles = new Hashtable<>(); /** * set the debug mode for java2iiop (default false) @@ -146,7 +147,6 @@ public class BorlandDeploymentTool extends GenericDeploymentTool this.verify = verify; } - /** * Setter used to store the suffix for the generated borland jar file. * @param inString the string to use as the suffix. @@ -155,7 +155,6 @@ public class BorlandDeploymentTool extends GenericDeploymentTool this.jarSuffix = inString; } - /** * sets some additional args to send to verify command * @param args additional command line parameters @@ -173,7 +172,6 @@ public class BorlandDeploymentTool extends GenericDeploymentTool this.borlandDTD = inString; } - /** * setter used to store whether the task will include the generate client task. * (see : BorlandGenerateClient task) @@ -200,7 +198,6 @@ public class BorlandDeploymentTool extends GenericDeploymentTool this.java2iioparams = params; } - /** * Get the borland descriptor handler. * @param srcDir the source directory. @@ -209,8 +206,9 @@ public class BorlandDeploymentTool extends GenericDeploymentTool protected DescriptorHandler getBorlandDescriptorHandler(final File srcDir) { DescriptorHandler handler = new DescriptorHandler(getTask(), srcDir) { + @Override protected void processElement() { - if (currentElement.equals("type-storage")) { + if ("type-storage".equals(currentElement)) { // Get the filename of vendor specific descriptor String fileNameWithMETA = currentText; //trim the META_INF\ off of the file name @@ -226,8 +224,7 @@ public class BorlandDeploymentTool extends GenericDeploymentTool handler.registerDTD(PUBLICID_BORLAND_EJB, borlandDTD == null ? DEFAULT_BAS_DTD_LOCATION : borlandDTD); - for (Iterator i = getConfig().dtdLocations.iterator(); i.hasNext();) { - EjbJar.DTDLocation dtdLocation = (EjbJar.DTDLocation) i.next(); + for (DTDLocation dtdLocation : getConfig().dtdLocations) { handler.registerDTD(dtdLocation.getPublicId(), dtdLocation.getLocation()); } return handler; @@ -239,14 +236,15 @@ public class BorlandDeploymentTool extends GenericDeploymentTool * @param ejbFiles the map to add the files to. * @param ddPrefix the prefix to use. */ - protected void addVendorFiles(Hashtable ejbFiles, String ddPrefix) { + @Override + protected void addVendorFiles(Hashtable<String, File> ejbFiles, String ddPrefix) { //choose the right vendor DD if (!(version == BES || version == BAS)) { throw new BuildException("version " + version + " is not supported"); } - String dd = (version == BES ? BES_DD : BAS_DD); + String dd = (version == BES) ? BES_DD : BAS_DD; log("vendor file : " + ddPrefix + dd, Project.MSG_DEBUG); @@ -266,6 +264,7 @@ public class BorlandDeploymentTool extends GenericDeploymentTool * Get the vendor specific name of the Jar that will be output. The modification date * of this jar will be checked against the dependent bean classes. */ + @Override File getVendorOutputJarFile(String baseName) { return new File(getDestDir(), baseName + jarSuffix); } @@ -294,8 +293,7 @@ public class BorlandDeploymentTool extends GenericDeploymentTool private void verifyBorlandJarV5(File sourceJar) { log("verify BES " + sourceJar, Project.MSG_INFO); try { - ExecTask execTask = null; - execTask = new ExecTask(getTask()); + ExecTask execTask = new ExecTask(getTask()); execTask.setDir(new File(".")); execTask.setExecutable("iastool"); //classpath @@ -315,9 +313,7 @@ public class BorlandDeploymentTool extends GenericDeploymentTool execTask.execute(); } catch (Exception e) { // Have to catch this because of the semantics of calling main() - String msg = "Exception while calling generateclient Details: " - + e.toString(); - throw new BuildException(msg, e); + throw new BuildException("Exception while calling generateclient Details: ", e); } } @@ -354,7 +350,6 @@ public class BorlandDeploymentTool extends GenericDeploymentTool } } - /** * Generate the client jar corresponding to the jar file passed as parameter * the method uses the BorlandGenerateClient task. @@ -364,7 +359,7 @@ public class BorlandDeploymentTool extends GenericDeploymentTool getTask().getProject().addTaskDefinition("internal_bas_generateclient", org.apache.tools.ant.taskdefs.optional.ejb.BorlandGenerateClient.class); - org.apache.tools.ant.taskdefs.optional.ejb.BorlandGenerateClient gentask = null; + BorlandGenerateClient gentask; log("generate client for " + sourceJar, Project.MSG_INFO); try { Project project = getTask().getProject(); @@ -381,9 +376,7 @@ public class BorlandDeploymentTool extends GenericDeploymentTool gentask.execute(); } catch (Exception e) { //TO DO : delete the file if it is not a valid file. - String msg = "Exception while calling " + VERIFY + " Details: " - + e.toString(); - throw new BuildException(msg, e); + throw new BuildException("Exception while calling " + VERIFY, e); } } @@ -392,10 +385,8 @@ public class BorlandDeploymentTool extends GenericDeploymentTool * Add all the generate class file into the ejb files * @param ithomes : iterator on home class */ - private void buildBorlandStubs(Iterator ithomes) { - Execute execTask = null; - - execTask = new Execute(this); + private void buildBorlandStubs(Collection<String> ithomes) { + Execute execTask = new Execute(this); Project project = getTask().getProject(); execTask.setAntRun(project); execTask.setWorkingDirectory(project.getBaseDir()); @@ -419,16 +410,14 @@ public class BorlandDeploymentTool extends GenericDeploymentTool commandline.createArgument().setLine(java2iioparams); } - //root dir commandline.createArgument().setValue("-root_dir"); commandline.createArgument().setValue(getConfig().srcDir.getAbsolutePath()); //compiling order commandline.createArgument().setValue("-compile"); //add the home class - while (ithomes.hasNext()) { - commandline.createArgument().setValue(ithomes.next().toString()); - } + ithomes.stream().map(Object::toString) + .forEach(v -> commandline.createArgument().setValue(v)); try { log("Calling java2iiop", Project.MSG_VERBOSE); @@ -436,11 +425,11 @@ public class BorlandDeploymentTool extends GenericDeploymentTool execTask.setCommandline(commandline.getCommandline()); int result = execTask.execute(); if (Execute.isFailure(result)) { - String msg = "Failed executing java2iiop (ret code is " - + result + ")"; - throw new BuildException(msg, getTask().getLocation()); + throw new BuildException( + "Failed executing java2iiop (ret code is " + result + ")", + getTask().getLocation()); } - } catch (java.io.IOException e) { + } catch (IOException e) { log("java2iiop exception :" + e.getMessage(), Project.MSG_ERR); throw new BuildException(e, getTask().getLocation()); } @@ -456,13 +445,13 @@ public class BorlandDeploymentTool extends GenericDeploymentTool * @param publicId the id to use. * @throws BuildException if there is an error. */ - protected void writeJar(String baseName, File jarFile, Hashtable files, String publicId) + @Override + protected void writeJar(String baseName, File jarFile, Hashtable<String, File> files, String publicId) throws BuildException { //build the home classes list. - Vector homes = new Vector(); - Iterator it = files.keySet().iterator(); - while (it.hasNext()) { - String clazz = (String) it.next(); + List<String> homes = new ArrayList<>(); + + for (String clazz : files.keySet()) { if (clazz.endsWith("Home.class")) { //remove .class extension String home = toClass(clazz); @@ -471,7 +460,7 @@ public class BorlandDeploymentTool extends GenericDeploymentTool } } - buildBorlandStubs(homes.iterator()); + buildBorlandStubs(homes); //add the gen files to the collection files.putAll(genfiles); @@ -494,9 +483,8 @@ public class BorlandDeploymentTool extends GenericDeploymentTool */ private String toClass(String filename) { //remove the .class - String classname = filename.substring(0, filename.lastIndexOf(".class")); - classname = classname.replace('\\', '.'); - return classname; + return filename.substring(0, filename.lastIndexOf(".class")) + .replace('\\', '.').replace('/', '.'); } /** @@ -505,28 +493,35 @@ public class BorlandDeploymentTool extends GenericDeploymentTool */ private String toClassFile(String filename) { //remove the .class - String classfile = filename.substring(0, filename.lastIndexOf(".java")); - classfile = classfile + ".class"; - return classfile; + return filename.replaceFirst("\\.java$", ".class"); } // implementation of org.apache.tools.ant.taskdefs.ExecuteStreamHandler interface /** {@inheritDoc}. */ - public void start() throws IOException { } + @Override + public void start() throws IOException { + } + /** {@inheritDoc}. */ - public void stop() { } + @Override + public void stop() { + } + /** {@inheritDoc}. */ - public void setProcessInputStream(OutputStream param1) throws IOException { } + @Override + public void setProcessInputStream(OutputStream param1) throws IOException { + } /** * Set the output stream of the process. * @param is the input stream. * @throws IOException if there is an error. */ + @Override public void setProcessOutputStream(InputStream is) throws IOException { - try { - BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + try (BufferedReader reader = + new BufferedReader(new InputStreamReader(is))) { String javafile; while ((javafile = reader.readLine()) != null) { if (javafile.endsWith(".java")) { @@ -536,10 +531,8 @@ public class BorlandDeploymentTool extends GenericDeploymentTool genfiles.put(key, new File(classfile)); } } - reader.close(); } catch (Exception e) { - String msg = "Exception while parsing java2iiop output. Details: " + e.toString(); - throw new BuildException(msg, e); + throw new BuildException("Exception while parsing java2iiop output.", e); } } @@ -548,6 +541,7 @@ public class BorlandDeploymentTool extends GenericDeploymentTool * @param is the input stream. * @throws IOException if there is an error. */ + @Override public void setProcessErrorStream(InputStream is) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String s = reader.readLine(); @@ -556,4 +550,3 @@ public class BorlandDeploymentTool extends GenericDeploymentTool } } } -
