1. /*
  2. * Copyright 2001-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.types.optional.depend;
  18. import java.util.Enumeration;
  19. import java.util.Vector;
  20. import org.apache.tools.ant.DirectoryScanner;
  21. import org.apache.tools.ant.Project;
  22. import org.apache.tools.ant.types.FileSet;
  23. /**
  24. * A ClassfileSet is a FileSet, that enlists all classes that depend on a
  25. * certain set of root classes.
  26. *
  27. * A ClassfileSet extends FileSets. The
  28. * nested FileSet attribute provides the domain, that is used for searching
  29. * for dependent classes
  30. *
  31. */
  32. public class ClassfileSet extends FileSet {
  33. /**
  34. * The list of root classes for this class file set. These are the
  35. * classes which must be included in the fileset and which are the
  36. * starting point for the dependency search.
  37. */
  38. private Vector rootClasses = new Vector();
  39. /**
  40. * The list of filesets which contain root classes
  41. */
  42. private Vector rootFileSets = new Vector();
  43. /**
  44. * Inner class used to contain info about root classes
  45. */
  46. public static class ClassRoot {
  47. /** The name of the root class */
  48. private String rootClass;
  49. /**
  50. * Set the root class name
  51. *
  52. * @param name the name of the root class
  53. */
  54. public void setClassname(String name) {
  55. this.rootClass = name;
  56. }
  57. /**
  58. * Get the name of the root class
  59. *
  60. * @return the name of the root class.
  61. */
  62. public String getClassname() {
  63. return rootClass;
  64. }
  65. }
  66. /**
  67. * Default constructor
  68. */
  69. public ClassfileSet() {
  70. }
  71. /**
  72. * Add a fileset to which contains a collection of root classes used to
  73. * drive the search from classes
  74. *
  75. * @param rootFileSet a root file set to be used to search for dependent
  76. * classes
  77. */
  78. public void addRootFileset(FileSet rootFileSet) {
  79. rootFileSets.addElement(rootFileSet);
  80. }
  81. /**
  82. * Create a ClassfileSet from another ClassfileSet
  83. *
  84. * @param s the other classfileset
  85. */
  86. protected ClassfileSet(ClassfileSet s) {
  87. super(s);
  88. rootClasses = (Vector) s.rootClasses.clone();
  89. }
  90. /**
  91. * Set the root class attribute
  92. *
  93. * @param rootClass the name of the root class.
  94. */
  95. public void setRootClass(String rootClass) {
  96. rootClasses.addElement(rootClass);
  97. }
  98. /**
  99. * Return the DirectoryScanner associated with this FileSet.
  100. *
  101. * @param p the project used to resolve dirs, etc.
  102. *
  103. * @return a dependency scanner.
  104. */
  105. public DirectoryScanner getDirectoryScanner(Project p) {
  106. if (isReference()) {
  107. return getRef(p).getDirectoryScanner(p);
  108. }
  109. Vector allRootClasses = (Vector) rootClasses.clone();
  110. for (Enumeration e = rootFileSets.elements(); e.hasMoreElements();) {
  111. FileSet additionalRootSet = (FileSet) e.nextElement();
  112. DirectoryScanner additionalScanner
  113. = additionalRootSet.getDirectoryScanner(p);
  114. String[] files = additionalScanner.getIncludedFiles();
  115. for (int i = 0; i < files.length; ++i) {
  116. if (files[i].endsWith(".class")) {
  117. String classFilePath
  118. = files[i].substring(0, files[i].length() - 6);
  119. String className
  120. = classFilePath.replace('/', '.').replace('\\', '.');
  121. allRootClasses.addElement(className);
  122. }
  123. }
  124. }
  125. DirectoryScanner parentScanner = super.getDirectoryScanner(p);
  126. DependScanner scanner = new DependScanner(parentScanner);
  127. scanner.setBasedir(getDir(p));
  128. scanner.setRootClasses(allRootClasses);
  129. scanner.scan();
  130. return scanner;
  131. }
  132. /**
  133. * Add a nested root class definition to this class file set
  134. *
  135. * @param root the configured class root.
  136. */
  137. public void addConfiguredRoot(ClassRoot root) {
  138. rootClasses.addElement(root.getClassname());
  139. }
  140. /**
  141. * Clone this data type.
  142. *
  143. * @return a clone of the class file set
  144. */
  145. public Object clone() {
  146. if (isReference()) {
  147. return new ClassfileSet((ClassfileSet) getRef(getProject()));
  148. } else {
  149. return new ClassfileSet(this);
  150. }
  151. }
  152. }