📄 depend.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.taskdefs.optional.depend;import java.io.BufferedReader;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.Enumeration;import java.util.Hashtable;import java.util.Vector;import org.apache.tools.ant.AntClassLoader;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.DirectoryScanner;import org.apache.tools.ant.Project;import org.apache.tools.ant.taskdefs.MatchingTask;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.util.FileUtils;import org.apache.tools.ant.util.depend.DependencyAnalyzer;/** * Generates a dependency file for a given set of classes. * */public class Depend extends MatchingTask { private static final int ONE_SECOND = 1000; /** * A class (struct) user to manage information about a class * */ private static class ClassFileInfo { /** The file where the class file is stored in the file system */ private File absoluteFile; /** The Java class name of this class */ private String className; /** The source File containing this class */ private File sourceFile; /** if user has been warned about this file not having a source file */ private boolean isUserWarned = false; } /** The path where source files exist */ private Path srcPath; /** The path where compiled class files exist. */ private Path destPath; /** 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; /** A map which gives information about a class */ private Hashtable classFileInfoMap; /** * A map which gives the list of jars and classes from the classpath * that a class depends upon */ private Hashtable classpathDependencies; /** The list of classes which are out of date. */ private Hashtable outOfDateClasses; /** * indicates that the dependency relationships should be extended beyond * direct dependencies to include all classes. So if A directly affects * B and B directly affects C, then A indirectly affects C. */ private boolean closure = false; /** * flag to enable warning if we encounter RMI stubs */ private boolean warnOnRmiStubs = true; /** * Flag which controls whether the reversed dependencies should be * dumped to the log */ private boolean dump = false; /** The classpath to look for additional dependencies */ private Path dependClasspath; /** constants used with the cache file */ private static final String CACHE_FILE_NAME = "dependencies.txt"; /** String Used to separate classnames in the dependency file */ private static final String CLASSNAME_PREPEND = "||:"; /** * Set the classpath to be used for this dependency check. * * @param classpath the classpath to be used when checking for * dependencies on elements in the classpath */ public void setClasspath(Path classpath) { if (dependClasspath == null) { dependClasspath = classpath; } else { dependClasspath.append(classpath); } } /** * Gets the classpath to be used for this dependency check. * * @return the current dependency classpath */ public Path getClasspath() { return dependClasspath; } /** * Adds a classpath to be used for this dependency check. * * @return A path object to be configured by Ant */ public Path createClasspath() { if (dependClasspath == null) { dependClasspath = new Path(getProject()); } return dependClasspath.createPath(); } /** * Adds a reference to a classpath defined elsewhere. * * @param r a reference to a path object to be used as the depend * classpath */ public void setClasspathRef(Reference r) { createClasspath().setRefid(r); } /** * Flag to set to true if you want dependency issues with RMI * stubs to appear at warning level. * @param warnOnRmiStubs if true set dependency issues to appear at warning level. * @since Ant1.7 */ public void setWarnOnRmiStubs(boolean warnOnRmiStubs) { this.warnOnRmiStubs = warnOnRmiStubs; } /** * Read the dependencies from cache file * * @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(); BufferedReader in = null; try { in = new BufferedReader(new FileReader(depFile)); String line = null; Vector dependencyList = null; String className = null; int prependLength = CLASSNAME_PREPEND.length(); while ((line = in.readLine()) != null) { if (line.startsWith(CLASSNAME_PREPEND)) { dependencyList = new Vector(); className = line.substring(prependLength); dependencyMap.put(className, dependencyList); } else { dependencyList.addElement(line); } } } finally { if (in != null) { in.close(); } } return dependencyMap; } /** * Write the dependencies to cache file * * @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) throws IOException { if (cache != null) { PrintWriter pw = null; try { cache.mkdirs(); File depFile = new File(cache, CACHE_FILE_NAME); pw = new PrintWriter(new FileWriter(depFile)); Enumeration e = dependencyMap.keys(); while (e.hasMoreElements()) { String className = (String) e.nextElement(); pw.println(CLASSNAME_PREPEND + className); Vector dependencyList = (Vector) dependencyMap.get(className); int size = dependencyList.size(); for (int x = 0; x < size; x++) { pw.println(dependencyList.elementAt(x)); } } } finally { if (pw != null) { pw.close(); } } } } /** * Get the classpath for dependency checking. * * This method removes the dest dirs if it is given from the dependency classpath */ private Path getCheckClassPath() { 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; } } } if (checkPath.length() == 0) { return null; } return new Path(getProject(), checkPath); } /** * Determine the dependencies between classes. Class dependencies are * determined by examining the class references in a class file to other * classes. * * This method sets up the following fields * <ul> * <li>affectedClassMap - the list of classes each class affects</li> * <li>classFileInfoMap - information about each class</li> * <li>classpathDependencies - the list of jars and classes from the * classpath that each class depends upon.</li> * </ul> * * If required, the dependencies are written to the cache. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -