1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.depend;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.util.Enumeration;
  23. import java.util.Hashtable;
  24. import java.util.Vector;
  25. import java.util.zip.ZipEntry;
  26. import java.util.zip.ZipFile;
  27. import org.apache.tools.ant.util.depend.AbstractAnalyzer;
  28. /**
  29. * An analyzer which uses the depend task's bytecode classes to analyze
  30. * dependencies
  31. *
  32. */
  33. public class AntAnalyzer extends AbstractAnalyzer {
  34. /**
  35. * Default constructor
  36. */
  37. public AntAnalyzer() {
  38. }
  39. /**
  40. * Determine the dependencies of the configured root classes.
  41. *
  42. * @param files a vector to be populated with the files which contain
  43. * the dependency classes
  44. * @param classes a vector to be populated with the names of the
  45. * dependency classes.
  46. */
  47. protected void determineDependencies(Vector files, Vector classes) {
  48. // we get the root classes and build up a set of
  49. // classes upon which they depend
  50. Hashtable dependencies = new Hashtable();
  51. Hashtable containers = new Hashtable();
  52. Hashtable toAnalyze = new Hashtable();
  53. for (Enumeration e = getRootClasses(); e.hasMoreElements();) {
  54. String classname = (String) e.nextElement();
  55. toAnalyze.put(classname, classname);
  56. }
  57. int count = 0;
  58. int maxCount = isClosureRequired() ? MAX_LOOPS : 1;
  59. Hashtable analyzedDeps = null;
  60. while (toAnalyze.size() != 0 && count++ < maxCount) {
  61. analyzedDeps = new Hashtable();
  62. for (Enumeration e = toAnalyze.keys(); e.hasMoreElements();) {
  63. String classname = (String) e.nextElement();
  64. dependencies.put(classname, classname);
  65. try {
  66. File container = getClassContainer(classname);
  67. if (container == null) {
  68. continue;
  69. }
  70. containers.put(container, container);
  71. ZipFile zipFile = null;
  72. InputStream inStream = null;
  73. try {
  74. if (container.getName().endsWith(".class")) {
  75. inStream = new FileInputStream(container.getPath());
  76. } else {
  77. zipFile = new ZipFile(container.getPath());
  78. String entryName
  79. = classname.replace('.', '/') + ".class";
  80. ZipEntry entry = new ZipEntry(entryName);
  81. inStream
  82. = zipFile.getInputStream(entry);
  83. }
  84. ClassFile classFile = new ClassFile();
  85. classFile.read(inStream);
  86. Vector dependencyList = classFile.getClassRefs();
  87. Enumeration depEnum = dependencyList.elements();
  88. while (depEnum.hasMoreElements()) {
  89. String dependency = (String) depEnum.nextElement();
  90. analyzedDeps.put(dependency, dependency);
  91. }
  92. } finally {
  93. if (inStream != null) {
  94. inStream.close();
  95. }
  96. if (zipFile != null) {
  97. zipFile.close();
  98. }
  99. }
  100. } catch (IOException ioe) {
  101. // ignore
  102. }
  103. }
  104. toAnalyze.clear();
  105. // now recover all the dependencies collected and add to the list.
  106. Enumeration depsEnum = analyzedDeps.elements();
  107. while (depsEnum.hasMoreElements()) {
  108. String className = (String) depsEnum.nextElement();
  109. if (!dependencies.containsKey(className)) {
  110. toAnalyze.put(className, className);
  111. }
  112. }
  113. }
  114. // pick up the last round of dependencies that were determined
  115. Enumeration depsEnum = analyzedDeps.elements();
  116. while (depsEnum.hasMoreElements()) {
  117. String className = (String) depsEnum.nextElement();
  118. dependencies.put(className, className);
  119. }
  120. files.removeAllElements();
  121. for (Enumeration e = containers.keys(); e.hasMoreElements();) {
  122. files.addElement((File) e.nextElement());
  123. }
  124. classes.removeAllElements();
  125. for (Enumeration e = dependencies.keys(); e.hasMoreElements();) {
  126. classes.addElement((String) e.nextElement());
  127. }
  128. }
  129. /**
  130. * Indicate if this analyzer can determine dependent files.
  131. *
  132. * @return true if the analyzer provides dependency file information.
  133. */
  134. protected boolean supportsFileDependencies() {
  135. return true;
  136. }
  137. }