1. /*
  2. * Copyright 2000-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.DataInputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.util.Vector;
  22. import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ClassCPInfo;
  23. import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ConstantPool;
  24. import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ConstantPoolEntry;
  25. /**
  26. * A ClassFile object stores information about a Java class. The class may
  27. * be read from a DataInputStream.and written to a DataOutputStream. These
  28. * are usually streams from a Java class file or a class file component of a
  29. * Jar file.
  30. *
  31. */
  32. public class ClassFile {
  33. /** The Magic Value that marks the start of a Java class file */
  34. private static final int CLASS_MAGIC = 0xCAFEBABE;
  35. /** This class' constant pool. */
  36. private ConstantPool constantPool;
  37. /** The class name for this class. */
  38. private String className;
  39. /**
  40. * Read the class from a data stream. This method takes an InputStream
  41. * as input and parses the class from the stream. <p>
  42. *
  43. *
  44. *
  45. * @param stream an InputStream from which the class will be read
  46. * @exception IOException if there is a problem reading from the given
  47. * stream.
  48. * @exception ClassFormatError if the class cannot be parsed correctly
  49. */
  50. public void read(InputStream stream) throws IOException, ClassFormatError {
  51. DataInputStream classStream = new DataInputStream(stream);
  52. if (classStream.readInt() != CLASS_MAGIC) {
  53. throw new ClassFormatError("No Magic Code Found "
  54. + "- probably not a Java class file.");
  55. }
  56. // right we have a good looking class file.
  57. /* int minorVersion = */ classStream.readUnsignedShort();
  58. /* int majorVersion = */ classStream.readUnsignedShort();
  59. // read the constant pool in and resolve it
  60. constantPool = new ConstantPool();
  61. constantPool.read(classStream);
  62. constantPool.resolve();
  63. /* int accessFlags = */ classStream.readUnsignedShort();
  64. int thisClassIndex = classStream.readUnsignedShort();
  65. /* int superClassIndex = */ classStream.readUnsignedShort();
  66. ClassCPInfo classInfo
  67. = (ClassCPInfo) constantPool.getEntry(thisClassIndex);
  68. className = classInfo.getClassName();
  69. }
  70. /**
  71. * Get the classes which this class references.
  72. *
  73. * @return a vector of class names which this class references
  74. */
  75. public Vector getClassRefs() {
  76. Vector classRefs = new Vector();
  77. for (int i = 0; i < constantPool.size(); ++i) {
  78. ConstantPoolEntry entry = constantPool.getEntry(i);
  79. if (entry != null
  80. && entry.getTag() == ConstantPoolEntry.CONSTANT_CLASS) {
  81. ClassCPInfo classEntry = (ClassCPInfo) entry;
  82. if (!classEntry.getClassName().equals(className)) {
  83. classRefs.addElement(ClassFileUtils.convertSlashName(classEntry.getClassName()));
  84. }
  85. }
  86. }
  87. return classRefs;
  88. }
  89. /**
  90. * Get the class' fully qualified name in dot format.
  91. *
  92. * @return the class name in dot format (eg. java.lang.Object)
  93. */
  94. public String getFullClassName() {
  95. return ClassFileUtils.convertSlashName(className);
  96. }
  97. }