Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/JavaClass.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/JavaClass.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/JavaClass.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/JavaClass.java Wed Mar 15 03:31:56 2006
@@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- */
+ */
package org.apache.bcel.classfile;
-
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
@@ -47,851 +46,821 @@
* @author <A HREF="mailto:[email protected]">M. Dahm</A>
*/
public class JavaClass extends AccessFlags implements Cloneable, Node, Comparable {
- private String file_name;
- private String package_name;
- private String source_file_name = "<Unknown>";
- private int class_name_index;
- private int superclass_name_index;
- private String class_name;
- private String superclass_name;
- private int major, minor; // Compiler version
- private ConstantPool constant_pool; // Constant pool
- private int[] interfaces; // implemented interfaces
- private String[] interface_names;
- private Field[] fields; // Fields, i.e., variables of class
- private Method[] methods; // methods defined in the class
- private Attribute[] attributes; // attributes defined in the class
- private byte source = HEAP; // Generated in memory
-
- public static final byte HEAP = 1;
- public static final byte FILE = 2;
- public static final byte ZIP = 3;
-
- static boolean debug = false; // Debugging on/off
- static char sep = '/'; // directory separator
-
- private static BCELComparator _cmp = new BCELComparator() {
- public boolean equals(Object o1, Object o2) {
- JavaClass THIS = (JavaClass)o1;
- JavaClass THAT = (JavaClass)o2;
-
- return THIS.getClassName().equals(THAT.getClassName());
- }
-
- public int hashCode(Object o) {
- JavaClass THIS = (JavaClass)o;
- return THIS.getClassName().hashCode();
- }
- };
-
- /**
- * In cases where we go ahead and create something,
- * use the default SyntheticRepository, because we
- * don't know any better.
- */
- private transient org.apache.bcel.util.Repository repository =
- SyntheticRepository.getInstance();
-
- /**
- * Constructor gets all contents as arguments.
- *
- * @param class_name_index Index into constant pool referencing a
- * ConstantClass that represents this class.
- * @param superclass_name_index Index into constant pool referencing a
- * ConstantClass that represents this class's superclass.
- * @param file_name File name
- * @param major Major compiler version
- * @param minor Minor compiler version
- * @param access_flags Access rights defined by bit flags
- * @param constant_pool Array of constants
- * @param interfaces Implemented interfaces
- * @param fields Class fields
- * @param methods Class methods
- * @param attributes Class attributes
- * @param source Read from file or generated in memory?
- */
- public JavaClass(
- int class_name_index,
- int superclass_name_index,
- String file_name,
- int major,
- int minor,
- int access_flags,
- ConstantPool constant_pool,
- int[] interfaces,
- Field[] fields,
- Method[] methods,
- Attribute[] attributes,
- byte source) {
- if (interfaces == null) // Allowed for backward compatibility
- interfaces = new int[0];
- if (attributes == null)
- attributes = new Attribute[0];
- if (fields == null)
- fields = new Field[0];
- if (methods == null)
- methods = new Method[0];
-
- this.class_name_index = class_name_index;
- this.superclass_name_index = superclass_name_index;
- this.file_name = file_name;
- this.major = major;
- this.minor = minor;
- this.access_flags = access_flags;
- this.constant_pool = constant_pool;
- this.interfaces = interfaces;
- this.fields = fields;
- this.methods = methods;
- this.attributes = attributes;
- this.source = source;
-
- // Get source file name if available
- for (int i = 0; i < attributes.length; i++) {
- if (attributes[i] instanceof SourceFile) {
- source_file_name = ((SourceFile)attributes[i]).getSourceFileName();
- break;
- }
- }
-
- /* According to the specification the following entries must be of type
- * `ConstantClass' but we check that anyway via the
- * `ConstPool.getConstant' method.
- */
- class_name =
- constant_pool.getConstantString(
- class_name_index,
- Constants.CONSTANT_Class);
- class_name = Utility.compactClassName(class_name, false);
-
- int index = class_name.lastIndexOf('.');
- if (index < 0)
- package_name = "";
- else
- package_name = class_name.substring(0, index);
-
- if (superclass_name_index > 0) {
- // May be zero -> class is java.lang.Object
- superclass_name =
- constant_pool.getConstantString(
- superclass_name_index,
- Constants.CONSTANT_Class);
- superclass_name = Utility.compactClassName(superclass_name, false);
- } else
- superclass_name = "java.lang.Object";
-
- interface_names = new String[interfaces.length];
- for (int i = 0; i < interfaces.length; i++) {
- String str =
- constant_pool.getConstantString(
- interfaces[i],
- Constants.CONSTANT_Class);
- interface_names[i] = Utility.compactClassName(str, false);
- }
- }
-
- /**
- * Constructor gets all contents as arguments.
- *
- * @param class_name_index Class name
- * @param superclass_name_index Superclass name
- * @param file_name File name
- * @param major Major compiler version
- * @param minor Minor compiler version
- * @param access_flags Access rights defined by bit flags
- * @param constant_pool Array of constants
- * @param interfaces Implemented interfaces
- * @param fields Class fields
- * @param methods Class methods
- * @param attributes Class attributes
- */
- public JavaClass(
- int class_name_index,
- int superclass_name_index,
- String file_name,
- int major,
- int minor,
- int access_flags,
- ConstantPool constant_pool,
- int[] interfaces,
- Field[] fields,
- Method[] methods,
- Attribute[] attributes) {
- this(
- class_name_index,
- superclass_name_index,
- file_name,
- major,
- minor,
- access_flags,
- constant_pool,
- interfaces,
- fields,
- methods,
- attributes,
- HEAP);
- }
-
- /**
- * Called by objects that are traversing the nodes of the tree implicitely
- * defined by the contents of a Java class. I.e., the hierarchy of methods,
- * fields, attributes, etc. spawns a tree of objects.
- *
- * @param v Visitor object
- */
- public void accept(Visitor v) {
- v.visitJavaClass(this);
- }
-
- /* Print debug information depending on `JavaClass.debug'
- */
- static final void Debug(String str) {
- if (debug)
- System.out.println(str);
- }
-
- /**
- * Dump class to a file.
- *
- * @param file Output file
- * @throws IOException
- */
- public void dump(File file) throws IOException {
- String parent = file.getParent();
-
- if (parent != null) {
- File dir = new File(parent);
- dir.mkdirs();
- }
-
- DataOutputStream dos = null;
- try {
- dos = new DataOutputStream(new FileOutputStream(file));
- dump(dos);
- }
- finally {
- if (dos != null)
- dos.close();
- }
- }
-
- /**
- * Dump class to a file named file_name.
- *
- * @param _file_name Output file name
- * @exception IOException
- */
- public void dump(String _file_name) throws IOException {
- dump(new File(_file_name));
- }
-
- /**
- * @return class in binary format
- */
- public byte[] getBytes() {
- ByteArrayOutputStream s = new ByteArrayOutputStream();
- DataOutputStream ds = new DataOutputStream(s);
-
- try {
- dump(ds);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- ds.close();
- } catch (IOException e2) {
- e2.printStackTrace();
- }
- }
-
- return s.toByteArray();
- }
-
- /**
- * Dump Java class to output stream in binary format.
- *
- * @param file Output stream
- * @exception IOException
- */
- public void dump(OutputStream file) throws IOException {
- dump(new DataOutputStream(file));
- }
-
- /**
- * Dump Java class to output stream in binary format.
- *
- * @param file Output stream
- * @exception IOException
- */
- public void dump(DataOutputStream file) throws IOException {
- file.writeInt(0xcafebabe);
- file.writeShort(minor);
- file.writeShort(major);
-
- constant_pool.dump(file);
-
- file.writeShort(access_flags);
- file.writeShort(class_name_index);
- file.writeShort(superclass_name_index);
-
- file.writeShort(interfaces.length);
- for (int i = 0; i < interfaces.length; i++)
- file.writeShort(interfaces[i]);
-
- file.writeShort(fields.length);
- for (int i = 0; i < fields.length; i++)
- fields[i].dump(file);
-
- file.writeShort(methods.length);
- for (int i = 0; i < methods.length; i++)
- methods[i].dump(file);
-
- if (attributes != null) {
- file.writeShort(attributes.length);
- for (int i = 0; i < attributes.length; i++)
- attributes[i].dump(file);
- } else
- file.writeShort(0);
- file.flush();
- }
-
- /**
- * @return Attributes of the class.
- */
- public Attribute[] getAttributes() {
- return attributes;
- }
-
- /**
- * @return Class name.
- */
- public String getClassName() {
- return class_name;
- }
-
- /**
- * @return Package name.
- */
- public String getPackageName() {
- return package_name;
- }
-
- /**
- * @return Class name index.
- */
- public int getClassNameIndex() {
- return class_name_index;
- }
-
- /**
- * @return Constant pool.
- */
- public ConstantPool getConstantPool() {
- return constant_pool;
- }
-
- /**
- * @return Fields, i.e., variables of the class. Like the JVM spec
- * mandates for the classfile format, these fields are those specific to
- * this class, and not those of the superclass or superinterfaces.
- */
- public Field[] getFields() {
- return fields;
- }
-
- /**
- * @return File name of class, aka SourceFile attribute value
- */
- public String getFileName() {
- return file_name;
- }
-
- /**
- * @return Names of implemented interfaces.
- */
- public String[] getInterfaceNames() {
- return interface_names;
- }
-
- /**
- * @return Indices in constant pool of implemented interfaces.
- */
- public int[] getInterfaceIndices() {
- return interfaces;
- }
-
- /**
- * @return Major number of class file version.
- */
- public int getMajor() {
- return major;
- }
-
- /**
- * @return Methods of the class.
- */
- public Method[] getMethods() {
- return methods;
- }
-
- /**
- * @return A org.apache.bcel.classfile.Method corresponding to
- * java.lang.reflect.Method if any
- */
- public Method getMethod(java.lang.reflect.Method m) {
- for (int i = 0; i < methods.length; i++) {
- Method method = methods[i];
-
- if (m.getName().equals(method.getName())
- && (m.getModifiers() == method.getModifiers())
- && Type.getSignature(m).equals(method.getSignature())) {
- return method;
- }
- }
-
- return null;
- }
-
- /**
- * @return Minor number of class file version.
- */
- public int getMinor() {
- return minor;
- }
-
- /**
- * @return sbsolute path to file where this class was read from
- */
- public String getSourceFileName() {
- return source_file_name;
- }
-
- /**
- * @return Superclass name.
- */
- public String getSuperclassName() {
- return superclass_name;
- }
-
- /**
- * @return Class name index.
- */
- public int getSuperclassNameIndex() {
- return superclass_name_index;
- }
-
- static {
- // Debugging ... on/off
- debug = Boolean.getBoolean("JavaClass.debug");
-
- // Get path separator either / or \ usually
- String _sep = System.getProperty("file.separator");
-
- if (_sep != null)
- try {
- JavaClass.sep = _sep.charAt(0);
- } catch (StringIndexOutOfBoundsException e) {
- } // Never reached
- }
-
- /**
- * @param attributes .
- */
- public void setAttributes(Attribute[] attributes) {
- this.attributes = attributes;
- }
-
- /**
- * @param class_name .
- */
- public void setClassName(String class_name) {
- this.class_name = class_name;
- }
-
- /**
- * @param class_name_index .
- */
- public void setClassNameIndex(int class_name_index) {
- this.class_name_index = class_name_index;
- }
-
- /**
- * @param constant_pool .
- */
- public void setConstantPool(ConstantPool constant_pool) {
- this.constant_pool = constant_pool;
- }
-
- /**
- * @param fields .
- */
- public void setFields(Field[] fields) {
- this.fields = fields;
- }
-
- /**
- * Set File name of class, aka SourceFile attribute value
- */
- public void setFileName(String file_name) {
- this.file_name = file_name;
- }
-
- /**
- * @param interface_names .
- */
- public void setInterfaceNames(String[] interface_names) {
- this.interface_names = interface_names;
- }
-
- /**
- * @param interfaces .
- */
- public void setInterfaces(int[] interfaces) {
- this.interfaces = interfaces;
- }
-
- /**
- * @param major .
- */
- public void setMajor(int major) {
- this.major = major;
- }
-
- /**
- * @param methods .
- */
- public void setMethods(Method[] methods) {
- this.methods = methods;
- }
-
- /**
- * @param minor .
- */
- public void setMinor(int minor) {
- this.minor = minor;
- }
-
- /**
- * Set absolute path to file this class was read from.
- */
- public void setSourceFileName(String source_file_name) {
- this.source_file_name = source_file_name;
- }
-
- /**
- * @param superclass_name .
- */
- public void setSuperclassName(String superclass_name) {
- this.superclass_name = superclass_name;
- }
-
- /**
- * @param superclass_name_index .
- */
- public void setSuperclassNameIndex(int superclass_name_index) {
- this.superclass_name_index = superclass_name_index;
- }
-
- /**
- * @return String representing class contents.
- */
- public String toString() {
- String access = Utility.accessToString(access_flags, true);
- access = access.equals("") ? "" : (access + " ");
-
- StringBuffer buf =
- new StringBuffer(128);
- buf.append(access)
- .append(Utility.classOrInterface(access_flags))
- .append(" ")
- .append(class_name)
- .append(" extends ")
- .append(Utility.compactClassName(superclass_name, false))
- .append('\n');
- int size = interfaces.length;
-
- if (size > 0) {
- buf.append("implements\t\t");
-
- for (int i = 0; i < size; i++) {
- buf.append(interface_names[i]);
- if (i < size - 1)
- buf.append(", ");
- }
-
- buf.append('\n');
- }
-
- buf.append("filename\t\t").append(file_name).append('\n');
- buf.append("compiled from\t\t").append(source_file_name).append('\n');
- buf.append("compiler version\t").append(major).append(".").append(minor).append('\n');
- buf.append("access flags\t\t").append(access_flags).append('\n');
- buf.append("constant pool\t\t").append(constant_pool.getLength()).append(" entries\n");
- buf.append("ACC_SUPER flag\t\t").append(isSuper()).append("\n");
-
- if (attributes.length > 0) {
- buf.append("\nAttribute(s):\n");
- for (int i = 0; i < attributes.length; i++)
- buf.append(indent(attributes[i]));
- }
-
- if (fields.length > 0) {
- buf.append("\n").append(fields.length).append(" fields:\n");
- for (int i = 0; i < fields.length; i++)
- buf.append("\t").append(fields[i]).append('\n');
- }
-
- if (methods.length > 0) {
- buf.append("\n").append(methods.length).append(" methods:\n");
- for (int i = 0; i < methods.length; i++)
- buf.append("\t").append(methods[i]).append('\n');
- }
-
- return buf.toString();
- }
-
- private static final String indent(Object obj) {
- StringTokenizer tok = new StringTokenizer(obj.toString(), "\n");
- StringBuffer buf = new StringBuffer();
-
- while (tok.hasMoreTokens())
- buf.append("\t").append(tok.nextToken()).append("\n");
-
- return buf.toString();
- }
-
- /**
- * @return deep copy of this class
- */
- public JavaClass copy() {
- JavaClass c = null;
-
- try {
- c = (JavaClass)clone();
-
- c.constant_pool = constant_pool.copy();
- c.interfaces = (int[])interfaces.clone();
- c.interface_names = (String[])interface_names.clone();
-
- c.fields = new Field[fields.length];
- for (int i = 0; i < fields.length; i++)
- c.fields[i] = fields[i].copy(c.constant_pool);
-
- c.methods = new Method[methods.length];
- for (int i = 0; i < methods.length; i++)
- c.methods[i] = methods[i].copy(c.constant_pool);
-
- c.attributes = new Attribute[attributes.length];
- for (int i = 0; i < attributes.length; i++)
- c.attributes[i] = attributes[i].copy(c.constant_pool);
- } catch (CloneNotSupportedException e) {
- }
-
- return c;
- }
-
- public final boolean isSuper() {
- return (access_flags & Constants.ACC_SUPER) != 0;
- }
-
- public final boolean isClass() {
- return (access_flags & Constants.ACC_INTERFACE) == 0;
- }
-
- /** @return returns either HEAP (generated), FILE, or ZIP
- */
- public final byte getSource() {
- return source;
- }
-
- /********************* New repository functionality *********************/
-
- /**
- * Gets the ClassRepository which holds its definition. By default
- * this is the same as SyntheticRepository.getInstance();
- */
- public org.apache.bcel.util.Repository getRepository() {
- return repository;
- }
-
- /**
- * Sets the ClassRepository which loaded the JavaClass.
- * Should be called immediately after parsing is done.
- */
- public void setRepository(org.apache.bcel.util.Repository repository) {
- this.repository = repository;
- }
-
- /** Equivalent to runtime "instanceof" operator.
- *
- * @return true if this JavaClass is derived from the super class
- * @throws ClassNotFoundException if superclasses or superinterfaces
- * of this object can't be found
- */
- public final boolean instanceOf(JavaClass super_class)
- throws ClassNotFoundException {
-
- if (this.equals(super_class))
- return true;
-
- JavaClass[] super_classes = getSuperClasses();
-
- for (int i = 0; i < super_classes.length; i++) {
- if (super_classes[i].equals(super_class)) {
- return true;
- }
- }
-
- if (super_class.isInterface()) {
- return implementationOf(super_class);
- }
-
- return false;
- }
-
- /**
- * @return true, if this class is an implementation of interface inter
- * @throws ClassNotFoundException if superclasses or superinterfaces
- * of this class can't be found
- */
- public boolean implementationOf(JavaClass inter)
- throws ClassNotFoundException {
-
- if (!inter.isInterface()) {
- throw new IllegalArgumentException(
- inter.getClassName() + " is no interface");
- }
-
- if (this.equals(inter)) {
- return true;
- }
-
- JavaClass[] super_interfaces = getAllInterfaces();
-
- for (int i = 0; i < super_interfaces.length; i++) {
- if (super_interfaces[i].equals(inter)) {
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * @return the superclass for this JavaClass object, or null if this
- * is java.lang.Object
- * @throws ClassNotFoundException if the superclass can't be found
- */
- public JavaClass getSuperClass() throws ClassNotFoundException {
- if ("java.lang.Object".equals(getClassName())) {
- return null;
- }
-
- return repository.loadClass(getSuperclassName());
- }
-
- /**
- * @return list of super classes of this class in ascending order, i.e.,
- * java.lang.Object is always the last element
- * @throws ClassNotFoundException if any of the superclasses can't be found
- */
- public JavaClass[] getSuperClasses() throws ClassNotFoundException {
- JavaClass clazz = this;
- List allSuperClasses = new ArrayList();
-
- for (clazz = clazz.getSuperClass();
- clazz != null;
- clazz = clazz.getSuperClass()) {
- allSuperClasses.add(clazz);
- }
-
- return (JavaClass[])allSuperClasses.toArray(new JavaClass[allSuperClasses.size()]);
- }
-
- /**
- * Get interfaces directly implemented by this JavaClass.
- */
- public JavaClass[] getInterfaces() throws ClassNotFoundException {
- String[] _interfaces = getInterfaceNames();
- JavaClass[] classes = new JavaClass[_interfaces.length];
-
- for (int i = 0; i < _interfaces.length; i++) {
- classes[i] = repository.loadClass(_interfaces[i]);
- }
-
- return classes;
- }
-
- /**
- * Get all interfaces implemented by this JavaClass (transitively).
- */
- public JavaClass[] getAllInterfaces() throws ClassNotFoundException {
- ClassQueue queue = new ClassQueue();
- Set allInterfaces = new TreeSet();
-
- queue.enqueue(this);
-
- while (!queue.empty()) {
- JavaClass clazz = queue.dequeue();
-
- JavaClass souper = clazz.getSuperClass();
- JavaClass[] _interfaces = clazz.getInterfaces();
-
- if (clazz.isInterface()) {
- allInterfaces.add(clazz);
- } else {
- if (souper != null) {
- queue.enqueue(souper);
- }
- }
-
- for (int i = 0; i < _interfaces.length; i++) {
- queue.enqueue(_interfaces[i]);
- }
- }
-
- return (JavaClass[])allInterfaces.toArray(new JavaClass[allInterfaces.size()]);
- }
-
- /**
- * @return Comparison strategy object
- */
- public static BCELComparator getComparator() {
- return _cmp;
- }
-
- /**
- * @param comparator Comparison strategy object
- */
- public static void setComparator(BCELComparator comparator) {
- _cmp = comparator;
- }
-
- /**
- * Return value as defined by given BCELComparator strategy.
- * By default two JavaClass objects are said to be equal when
- * their class names are equal.
- *
- * @see java.lang.Object#equals(java.lang.Object)
- */
- public boolean equals(Object obj) {
- return _cmp.equals(this, obj);
- }
-
- /**
- * Return the natural ordering of two JavaClasses.
- * This ordering is based on the class name
- */
- public int compareTo(Object obj) {
- return getClassName().compareTo(((JavaClass)obj).getClassName());
- }
-
- /**
- * Return value as defined by given BCELComparator strategy.
- * By default return the hashcode of the class name.
- *
- * @see java.lang.Object#hashCode()
- */
- public int hashCode() {
- return _cmp.hashCode(this);
- }
+
+ private String file_name;
+ private String package_name;
+ private String source_file_name = "<Unknown>";
+ private int class_name_index;
+ private int superclass_name_index;
+ private String class_name;
+ private String superclass_name;
+ private int major, minor; // Compiler version
+ private ConstantPool constant_pool; // Constant pool
+ private int[] interfaces; // implemented interfaces
+ private String[] interface_names;
+ private Field[] fields; // Fields, i.e., variables of class
+ private Method[] methods; // methods defined in the class
+ private Attribute[] attributes; // attributes defined in the class
+ private byte source = HEAP; // Generated in memory
+ public static final byte HEAP = 1;
+ public static final byte FILE = 2;
+ public static final byte ZIP = 3;
+ static boolean debug = false; // Debugging on/off
+ static char sep = '/'; // directory separator
+ private static BCELComparator _cmp = new BCELComparator() {
+
+ public boolean equals( Object o1, Object o2 ) {
+ JavaClass THIS = (JavaClass) o1;
+ JavaClass THAT = (JavaClass) o2;
+ return THIS.getClassName().equals(THAT.getClassName());
+ }
+
+
+ public int hashCode( Object o ) {
+ JavaClass THIS = (JavaClass) o;
+ return THIS.getClassName().hashCode();
+ }
+ };
+ /**
+ * In cases where we go ahead and create something,
+ * use the default SyntheticRepository, because we
+ * don't know any better.
+ */
+ private transient org.apache.bcel.util.Repository repository = SyntheticRepository
+ .getInstance();
+
+
+ /**
+ * Constructor gets all contents as arguments.
+ *
+ * @param class_name_index Index into constant pool referencing a
+ * ConstantClass that represents this class.
+ * @param superclass_name_index Index into constant pool referencing a
+ * ConstantClass that represents this class's superclass.
+ * @param file_name File name
+ * @param major Major compiler version
+ * @param minor Minor compiler version
+ * @param access_flags Access rights defined by bit flags
+ * @param constant_pool Array of constants
+ * @param interfaces Implemented interfaces
+ * @param fields Class fields
+ * @param methods Class methods
+ * @param attributes Class attributes
+ * @param source Read from file or generated in memory?
+ */
+ public JavaClass(int class_name_index, int superclass_name_index, String file_name, int major,
+ int minor, int access_flags, ConstantPool constant_pool, int[] interfaces,
+ Field[] fields, Method[] methods, Attribute[] attributes, byte source) {
+ if (interfaces == null) {
+ interfaces = new int[0];
+ }
+ if (attributes == null) {
+ attributes = new Attribute[0];
+ }
+ if (fields == null) {
+ fields = new Field[0];
+ }
+ if (methods == null) {
+ methods = new Method[0];
+ }
+ this.class_name_index = class_name_index;
+ this.superclass_name_index = superclass_name_index;
+ this.file_name = file_name;
+ this.major = major;
+ this.minor = minor;
+ this.access_flags = access_flags;
+ this.constant_pool = constant_pool;
+ this.interfaces = interfaces;
+ this.fields = fields;
+ this.methods = methods;
+ this.attributes = attributes;
+ this.source = source;
+ // Get source file name if available
+ for (int i = 0; i < attributes.length; i++) {
+ if (attributes[i] instanceof SourceFile) {
+ source_file_name = ((SourceFile) attributes[i]).getSourceFileName();
+ break;
+ }
+ }
+ /* According to the specification the following entries must be of type
+ * `ConstantClass' but we check that anyway via the
+ * `ConstPool.getConstant' method.
+ */
+ class_name = constant_pool.getConstantString(class_name_index, Constants.CONSTANT_Class);
+ class_name = Utility.compactClassName(class_name, false);
+ int index = class_name.lastIndexOf('.');
+ if (index < 0) {
+ package_name = "";
+ } else {
+ package_name = class_name.substring(0, index);
+ }
+ if (superclass_name_index > 0) {
+ // May be zero -> class is java.lang.Object
+ superclass_name = constant_pool.getConstantString(superclass_name_index,
+ Constants.CONSTANT_Class);
+ superclass_name = Utility.compactClassName(superclass_name, false);
+ } else {
+ superclass_name = "java.lang.Object";
+ }
+ interface_names = new String[interfaces.length];
+ for (int i = 0; i < interfaces.length; i++) {
+ String str = constant_pool.getConstantString(interfaces[i], Constants.CONSTANT_Class);
+ interface_names[i] = Utility.compactClassName(str, false);
+ }
+ }
+
+
+ /**
+ * Constructor gets all contents as arguments.
+ *
+ * @param class_name_index Class name
+ * @param superclass_name_index Superclass name
+ * @param file_name File name
+ * @param major Major compiler version
+ * @param minor Minor compiler version
+ * @param access_flags Access rights defined by bit flags
+ * @param constant_pool Array of constants
+ * @param interfaces Implemented interfaces
+ * @param fields Class fields
+ * @param methods Class methods
+ * @param attributes Class attributes
+ */
+ public JavaClass(int class_name_index, int superclass_name_index, String file_name, int major,
+ int minor, int access_flags, ConstantPool constant_pool, int[] interfaces,
+ Field[] fields, Method[] methods, Attribute[] attributes) {
+ this(class_name_index, superclass_name_index, file_name, major, minor, access_flags,
+ constant_pool, interfaces, fields, methods, attributes, HEAP);
+ }
+
+
+ /**
+ * Called by objects that are traversing the nodes of the tree implicitely
+ * defined by the contents of a Java class. I.e., the hierarchy of methods,
+ * fields, attributes, etc. spawns a tree of objects.
+ *
+ * @param v Visitor object
+ */
+ public void accept( Visitor v ) {
+ v.visitJavaClass(this);
+ }
+
+
+ /* Print debug information depending on `JavaClass.debug'
+ */
+ static final void Debug( String str ) {
+ if (debug) {
+ System.out.println(str);
+ }
+ }
+
+
+ /**
+ * Dump class to a file.
+ *
+ * @param file Output file
+ * @throws IOException
+ */
+ public void dump( File file ) throws IOException {
+ String parent = file.getParent();
+ if (parent != null) {
+ File dir = new File(parent);
+ dir.mkdirs();
+ }
+ DataOutputStream dos = null;
+ try {
+ dos = new DataOutputStream(new FileOutputStream(file));
+ dump(dos);
+ } finally {
+ if (dos != null) {
+ dos.close();
+ }
+ }
+ }
+
+
+ /**
+ * Dump class to a file named file_name.
+ *
+ * @param _file_name Output file name
+ * @exception IOException
+ */
+ public void dump( String _file_name ) throws IOException {
+ dump(new File(_file_name));
+ }
+
+
+ /**
+ * @return class in binary format
+ */
+ public byte[] getBytes() {
+ ByteArrayOutputStream s = new ByteArrayOutputStream();
+ DataOutputStream ds = new DataOutputStream(s);
+ try {
+ dump(ds);
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ ds.close();
+ } catch (IOException e2) {
+ e2.printStackTrace();
+ }
+ }
+ return s.toByteArray();
+ }
+
+
+ /**
+ * Dump Java class to output stream in binary format.
+ *
+ * @param file Output stream
+ * @exception IOException
+ */
+ public void dump( OutputStream file ) throws IOException {
+ dump(new DataOutputStream(file));
+ }
+
+
+ /**
+ * Dump Java class to output stream in binary format.
+ *
+ * @param file Output stream
+ * @exception IOException
+ */
+ public void dump( DataOutputStream file ) throws IOException {
+ file.writeInt(0xcafebabe);
+ file.writeShort(minor);
+ file.writeShort(major);
+ constant_pool.dump(file);
+ file.writeShort(access_flags);
+ file.writeShort(class_name_index);
+ file.writeShort(superclass_name_index);
+ file.writeShort(interfaces.length);
+ for (int i = 0; i < interfaces.length; i++) {
+ file.writeShort(interfaces[i]);
+ }
+ file.writeShort(fields.length);
+ for (int i = 0; i < fields.length; i++) {
+ fields[i].dump(file);
+ }
+ file.writeShort(methods.length);
+ for (int i = 0; i < methods.length; i++) {
+ methods[i].dump(file);
+ }
+ if (attributes != null) {
+ file.writeShort(attributes.length);
+ for (int i = 0; i < attributes.length; i++) {
+ attributes[i].dump(file);
+ }
+ } else {
+ file.writeShort(0);
+ }
+ file.flush();
+ }
+
+
+ /**
+ * @return Attributes of the class.
+ */
+ public Attribute[] getAttributes() {
+ return attributes;
+ }
+
+
+ /**
+ * @return Class name.
+ */
+ public String getClassName() {
+ return class_name;
+ }
+
+
+ /**
+ * @return Package name.
+ */
+ public String getPackageName() {
+ return package_name;
+ }
+
+
+ /**
+ * @return Class name index.
+ */
+ public int getClassNameIndex() {
+ return class_name_index;
+ }
+
+
+ /**
+ * @return Constant pool.
+ */
+ public ConstantPool getConstantPool() {
+ return constant_pool;
+ }
+
+
+ /**
+ * @return Fields, i.e., variables of the class. Like the JVM spec
+ * mandates for the classfile format, these fields are those specific to
+ * this class, and not those of the superclass or superinterfaces.
+ */
+ public Field[] getFields() {
+ return fields;
+ }
+
+
+ /**
+ * @return File name of class, aka SourceFile attribute value
+ */
+ public String getFileName() {
+ return file_name;
+ }
+
+
+ /**
+ * @return Names of implemented interfaces.
+ */
+ public String[] getInterfaceNames() {
+ return interface_names;
+ }
+
+
+ /**
+ * @return Indices in constant pool of implemented interfaces.
+ */
+ public int[] getInterfaceIndices() {
+ return interfaces;
+ }
+
+
+ /**
+ * @return Major number of class file version.
+ */
+ public int getMajor() {
+ return major;
+ }
+
+
+ /**
+ * @return Methods of the class.
+ */
+ public Method[] getMethods() {
+ return methods;
+ }
+
+
+ /**
+ * @return A org.apache.bcel.classfile.Method corresponding to
+ * java.lang.reflect.Method if any
+ */
+ public Method getMethod( java.lang.reflect.Method m ) {
+ for (int i = 0; i < methods.length; i++) {
+ Method method = methods[i];
+ if (m.getName().equals(method.getName()) && (m.getModifiers() == method.getModifiers())
+ && Type.getSignature(m).equals(method.getSignature())) {
+ return method;
+ }
+ }
+ return null;
+ }
+
+
+ /**
+ * @return Minor number of class file version.
+ */
+ public int getMinor() {
+ return minor;
+ }
+
+
+ /**
+ * @return sbsolute path to file where this class was read from
+ */
+ public String getSourceFileName() {
+ return source_file_name;
+ }
+
+
+ /**
+ * @return Superclass name.
+ */
+ public String getSuperclassName() {
+ return superclass_name;
+ }
+
+
+ /**
+ * @return Class name index.
+ */
+ public int getSuperclassNameIndex() {
+ return superclass_name_index;
+ }
+
+ static {
+ // Debugging ... on/off
+ debug = Boolean.getBoolean("JavaClass.debug");
+ // Get path separator either / or \ usually
+ String _sep = System.getProperty("file.separator");
+ if (_sep != null) {
+ try {
+ JavaClass.sep = _sep.charAt(0);
+ } catch (StringIndexOutOfBoundsException e) {
+ } // Never reached
+ }
+ }
+
+
+ /**
+ * @param attributes .
+ */
+ public void setAttributes( Attribute[] attributes ) {
+ this.attributes = attributes;
+ }
+
+
+ /**
+ * @param class_name .
+ */
+ public void setClassName( String class_name ) {
+ this.class_name = class_name;
+ }
+
+
+ /**
+ * @param class_name_index .
+ */
+ public void setClassNameIndex( int class_name_index ) {
+ this.class_name_index = class_name_index;
+ }
+
+
+ /**
+ * @param constant_pool .
+ */
+ public void setConstantPool( ConstantPool constant_pool ) {
+ this.constant_pool = constant_pool;
+ }
+
+
+ /**
+ * @param fields .
+ */
+ public void setFields( Field[] fields ) {
+ this.fields = fields;
+ }
+
+
+ /**
+ * Set File name of class, aka SourceFile attribute value
+ */
+ public void setFileName( String file_name ) {
+ this.file_name = file_name;
+ }
+
+
+ /**
+ * @param interface_names .
+ */
+ public void setInterfaceNames( String[] interface_names ) {
+ this.interface_names = interface_names;
+ }
+
+
+ /**
+ * @param interfaces .
+ */
+ public void setInterfaces( int[] interfaces ) {
+ this.interfaces = interfaces;
+ }
+
+
+ /**
+ * @param major .
+ */
+ public void setMajor( int major ) {
+ this.major = major;
+ }
+
+
+ /**
+ * @param methods .
+ */
+ public void setMethods( Method[] methods ) {
+ this.methods = methods;
+ }
+
+
+ /**
+ * @param minor .
+ */
+ public void setMinor( int minor ) {
+ this.minor = minor;
+ }
+
+
+ /**
+ * Set absolute path to file this class was read from.
+ */
+ public void setSourceFileName( String source_file_name ) {
+ this.source_file_name = source_file_name;
+ }
+
+
+ /**
+ * @param superclass_name .
+ */
+ public void setSuperclassName( String superclass_name ) {
+ this.superclass_name = superclass_name;
+ }
+
+
+ /**
+ * @param superclass_name_index .
+ */
+ public void setSuperclassNameIndex( int superclass_name_index ) {
+ this.superclass_name_index = superclass_name_index;
+ }
+
+
+ /**
+ * @return String representing class contents.
+ */
+ public String toString() {
+ String access = Utility.accessToString(access_flags, true);
+ access = access.equals("") ? "" : (access + " ");
+ StringBuffer buf = new StringBuffer(128);
+ buf.append(access).append(Utility.classOrInterface(access_flags)).append(" ").append(
+ class_name).append(" extends ").append(
+ Utility.compactClassName(superclass_name, false)).append('\n');
+ int size = interfaces.length;
+ if (size > 0) {
+ buf.append("implements\t\t");
+ for (int i = 0; i < size; i++) {
+ buf.append(interface_names[i]);
+ if (i < size - 1) {
+ buf.append(", ");
+ }
+ }
+ buf.append('\n');
+ }
+ buf.append("filename\t\t").append(file_name).append('\n');
+ buf.append("compiled from\t\t").append(source_file_name).append('\n');
+ buf.append("compiler version\t").append(major).append(".").append(minor).append('\n');
+ buf.append("access flags\t\t").append(access_flags).append('\n');
+ buf.append("constant pool\t\t").append(constant_pool.getLength()).append(" entries\n");
+ buf.append("ACC_SUPER flag\t\t").append(isSuper()).append("\n");
+ if (attributes.length > 0) {
+ buf.append("\nAttribute(s):\n");
+ for (int i = 0; i < attributes.length; i++) {
+ buf.append(indent(attributes[i]));
+ }
+ }
+ if (fields.length > 0) {
+ buf.append("\n").append(fields.length).append(" fields:\n");
+ for (int i = 0; i < fields.length; i++) {
+ buf.append("\t").append(fields[i]).append('\n');
+ }
+ }
+ if (methods.length > 0) {
+ buf.append("\n").append(methods.length).append(" methods:\n");
+ for (int i = 0; i < methods.length; i++) {
+ buf.append("\t").append(methods[i]).append('\n');
+ }
+ }
+ return buf.toString();
+ }
+
+
+ private static final String indent( Object obj ) {
+ StringTokenizer tok = new StringTokenizer(obj.toString(), "\n");
+ StringBuffer buf = new StringBuffer();
+ while (tok.hasMoreTokens()) {
+ buf.append("\t").append(tok.nextToken()).append("\n");
+ }
+ return buf.toString();
+ }
+
+
+ /**
+ * @return deep copy of this class
+ */
+ public JavaClass copy() {
+ JavaClass c = null;
+ try {
+ c = (JavaClass) clone();
+ c.constant_pool = constant_pool.copy();
+ c.interfaces = (int[]) interfaces.clone();
+ c.interface_names = (String[]) interface_names.clone();
+ c.fields = new Field[fields.length];
+ for (int i = 0; i < fields.length; i++) {
+ c.fields[i] = fields[i].copy(c.constant_pool);
+ }
+ c.methods = new Method[methods.length];
+ for (int i = 0; i < methods.length; i++) {
+ c.methods[i] = methods[i].copy(c.constant_pool);
+ }
+ c.attributes = new Attribute[attributes.length];
+ for (int i = 0; i < attributes.length; i++) {
+ c.attributes[i] = attributes[i].copy(c.constant_pool);
+ }
+ } catch (CloneNotSupportedException e) {
+ }
+ return c;
+ }
+
+
+ public final boolean isSuper() {
+ return (access_flags & Constants.ACC_SUPER) != 0;
+ }
+
+
+ public final boolean isClass() {
+ return (access_flags & Constants.ACC_INTERFACE) == 0;
+ }
+
+
+ /** @return returns either HEAP (generated), FILE, or ZIP
+ */
+ public final byte getSource() {
+ return source;
+ }
+
+
+ /********************* New repository functionality *********************/
+ /**
+ * Gets the ClassRepository which holds its definition. By default
+ * this is the same as SyntheticRepository.getInstance();
+ */
+ public org.apache.bcel.util.Repository getRepository() {
+ return repository;
+ }
+
+
+ /**
+ * Sets the ClassRepository which loaded the JavaClass.
+ * Should be called immediately after parsing is done.
+ */
+ public void setRepository( org.apache.bcel.util.Repository repository ) {
+ this.repository = repository;
+ }
+
+
+ /** Equivalent to runtime "instanceof" operator.
+ *
+ * @return true if this JavaClass is derived from the super class
+ * @throws ClassNotFoundException if superclasses or superinterfaces
+ * of this object can't be found
+ */
+ public final boolean instanceOf( JavaClass super_class ) throws ClassNotFoundException {
+ if (this.equals(super_class)) {
+ return true;
+ }
+ JavaClass[] super_classes = getSuperClasses();
+ for (int i = 0; i < super_classes.length; i++) {
+ if (super_classes[i].equals(super_class)) {
+ return true;
+ }
+ }
+ if (super_class.isInterface()) {
+ return implementationOf(super_class);
+ }
+ return false;
+ }
+
+
+ /**
+ * @return true, if this class is an implementation of interface inter
+ * @throws ClassNotFoundException if superclasses or superinterfaces
+ * of this class can't be found
+ */
+ public boolean implementationOf( JavaClass inter ) throws ClassNotFoundException {
+ if (!inter.isInterface()) {
+ throw new IllegalArgumentException(inter.getClassName() + " is no interface");
+ }
+ if (this.equals(inter)) {
+ return true;
+ }
+ JavaClass[] super_interfaces = getAllInterfaces();
+ for (int i = 0; i < super_interfaces.length; i++) {
+ if (super_interfaces[i].equals(inter)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+
+ /**
+ * @return the superclass for this JavaClass object, or null if this
+ * is java.lang.Object
+ * @throws ClassNotFoundException if the superclass can't be found
+ */
+ public JavaClass getSuperClass() throws ClassNotFoundException {
+ if ("java.lang.Object".equals(getClassName())) {
+ return null;
+ }
+ return repository.loadClass(getSuperclassName());
+ }
+
+
+ /**
+ * @return list of super classes of this class in ascending order, i.e.,
+ * java.lang.Object is always the last element
+ * @throws ClassNotFoundException if any of the superclasses can't be found
+ */
+ public JavaClass[] getSuperClasses() throws ClassNotFoundException {
+ JavaClass clazz = this;
+ List allSuperClasses = new ArrayList();
+ for (clazz = clazz.getSuperClass(); clazz != null; clazz = clazz.getSuperClass()) {
+ allSuperClasses.add(clazz);
+ }
+ return (JavaClass[]) allSuperClasses.toArray(new JavaClass[allSuperClasses.size()]);
+ }
+
+
+ /**
+ * Get interfaces directly implemented by this JavaClass.
+ */
+ public JavaClass[] getInterfaces() throws ClassNotFoundException {
+ String[] _interfaces = getInterfaceNames();
+ JavaClass[] classes = new JavaClass[_interfaces.length];
+ for (int i = 0; i < _interfaces.length; i++) {
+ classes[i] = repository.loadClass(_interfaces[i]);
+ }
+ return classes;
+ }
+
+
+ /**
+ * Get all interfaces implemented by this JavaClass (transitively).
+ */
+ public JavaClass[] getAllInterfaces() throws ClassNotFoundException {
+ ClassQueue queue = new ClassQueue();
+ Set allInterfaces = new TreeSet();
+ queue.enqueue(this);
+ while (!queue.empty()) {
+ JavaClass clazz = queue.dequeue();
+ JavaClass souper = clazz.getSuperClass();
+ JavaClass[] _interfaces = clazz.getInterfaces();
+ if (clazz.isInterface()) {
+ allInterfaces.add(clazz);
+ } else {
+ if (souper != null) {
+ queue.enqueue(souper);
+ }
+ }
+ for (int i = 0; i < _interfaces.length; i++) {
+ queue.enqueue(_interfaces[i]);
+ }
+ }
+ return (JavaClass[]) allInterfaces.toArray(new JavaClass[allInterfaces.size()]);
+ }
+
+
+ /**
+ * @return Comparison strategy object
+ */
+ public static BCELComparator getComparator() {
+ return _cmp;
+ }
+
+
+ /**
+ * @param comparator Comparison strategy object
+ */
+ public static void setComparator( BCELComparator comparator ) {
+ _cmp = comparator;
+ }
+
+
+ /**
+ * Return value as defined by given BCELComparator strategy.
+ * By default two JavaClass objects are said to be equal when
+ * their class names are equal.
+ *
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ public boolean equals( Object obj ) {
+ return _cmp.equals(this, obj);
+ }
+
+
+ /**
+ * Return the natural ordering of two JavaClasses.
+ * This ordering is based on the class name
+ */
+ public int compareTo( Object obj ) {
+ return getClassName().compareTo(((JavaClass) obj).getClassName());
+ }
+
+
+ /**
+ * Return value as defined by given BCELComparator strategy.
+ * By default return the hashcode of the class name.
+ *
+ * @see java.lang.Object#hashCode()
+ */
+ public int hashCode() {
+ return _cmp.hashCode(this);
+ }
}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/LineNumber.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/LineNumber.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/LineNumber.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/LineNumber.java Wed Mar 15 03:31:56 2006
@@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- */
+ */
package org.apache.bcel.classfile;
-
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
@@ -32,98 +31,111 @@
* @see LineNumberTable
*/
public final class LineNumber implements Cloneable, Node, Serializable {
- private int start_pc; // Program Counter (PC) corresponds to line
- private int line_number; // number in source file
- /**
- * Initialize from another object.
- */
- public LineNumber(LineNumber c) {
- this(c.getStartPC(), c.getLineNumber());
- }
-
- /**
- * Construct object from file stream.
- * @param file Input stream
- * @throws IOException
- */
- LineNumber(DataInputStream file) throws IOException
- {
- this(file.readUnsignedShort(), file.readUnsignedShort());
- }
-
- /**
- * @param start_pc Program Counter (PC) corresponds to
- * @param line_number line number in source file
- */
- public LineNumber(int start_pc, int line_number)
- {
- this.start_pc = start_pc;
- this.line_number = line_number;
- }
-
- /**
- * Called by objects that are traversing the nodes of the tree implicitely
- * defined by the contents of a Java class. I.e., the hierarchy of methods,
- * fields, attributes, etc. spawns a tree of objects.
- *
- * @param v Visitor object
- */
- public void accept(Visitor v) {
- v.visitLineNumber(this);
- }
-
- /**
- * Dump line number/pc pair to file stream in binary format.
- *
- * @param file Output file stream
- * @throws IOException
- */
- public final void dump(DataOutputStream file) throws IOException
- {
- file.writeShort(start_pc);
- file.writeShort(line_number);
-
- }
- /**
- * @return Corresponding source line
- */
- public final int getLineNumber() { return line_number; }
-
- /**
- * @return PC in code
- */
- public final int getStartPC() { return start_pc; }
-
- /**
- * @param line_number the source line number
- */
- public final void setLineNumber(int line_number) {
- this.line_number = line_number;
- }
-
- /**
- * @param start_pc the pc for this line number
- */
- public final void setStartPC(int start_pc) {
- this.start_pc = start_pc;
- }
-
- /**
- * @return String representation
- */
- public final String toString() {
- return "LineNumber(" + start_pc + ", " + line_number + ")";
- }
-
- /**
- * @return deep copy of this object
- */
- public LineNumber copy() {
- try {
- return (LineNumber)clone();
- } catch(CloneNotSupportedException e) {}
+ private int start_pc; // Program Counter (PC) corresponds to line
+ private int line_number; // number in source file
+
- return null;
- }
+ /**
+ * Initialize from another object.
+ */
+ public LineNumber(LineNumber c) {
+ this(c.getStartPC(), c.getLineNumber());
+ }
+
+
+ /**
+ * Construct object from file stream.
+ * @param file Input stream
+ * @throws IOException
+ */
+ LineNumber(DataInputStream file) throws IOException {
+ this(file.readUnsignedShort(), file.readUnsignedShort());
+ }
+
+
+ /**
+ * @param start_pc Program Counter (PC) corresponds to
+ * @param line_number line number in source file
+ */
+ public LineNumber(int start_pc, int line_number) {
+ this.start_pc = start_pc;
+ this.line_number = line_number;
+ }
+
+
+ /**
+ * Called by objects that are traversing the nodes of the tree implicitely
+ * defined by the contents of a Java class. I.e., the hierarchy of methods,
+ * fields, attributes, etc. spawns a tree of objects.
+ *
+ * @param v Visitor object
+ */
+ public void accept( Visitor v ) {
+ v.visitLineNumber(this);
+ }
+
+
+ /**
+ * Dump line number/pc pair to file stream in binary format.
+ *
+ * @param file Output file stream
+ * @throws IOException
+ */
+ public final void dump( DataOutputStream file ) throws IOException {
+ file.writeShort(start_pc);
+ file.writeShort(line_number);
+ }
+
+
+ /**
+ * @return Corresponding source line
+ */
+ public final int getLineNumber() {
+ return line_number;
+ }
+
+
+ /**
+ * @return PC in code
+ */
+ public final int getStartPC() {
+ return start_pc;
+ }
+
+
+ /**
+ * @param line_number the source line number
+ */
+ public final void setLineNumber( int line_number ) {
+ this.line_number = line_number;
+ }
+
+
+ /**
+ * @param start_pc the pc for this line number
+ */
+ public final void setStartPC( int start_pc ) {
+ this.start_pc = start_pc;
+ }
+
+
+ /**
+ * @return String representation
+ */
+ public final String toString() {
+ return "LineNumber(" + start_pc + ", " + line_number + ")";
+ }
+
+
+ /**
+ * @return deep copy of this object
+ */
+ public LineNumber copy() {
+ try {
+ return (LineNumber) clone();
+ } catch (CloneNotSupportedException e) {
+ }
+ return null;
+ }
}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/LineNumberTable.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/LineNumberTable.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/LineNumberTable.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/LineNumberTable.java Wed Mar 15 03:31:56 2006
@@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- */
+ */
package org.apache.bcel.classfile;
-
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
@@ -33,175 +32,177 @@
* @see LineNumber
*/
public final class LineNumberTable extends Attribute {
- private int line_number_table_length;
- private LineNumber[] line_number_table; // Table of line/numbers pairs
- /*
- * Initialize from another object. Note that both objects use the same
- * references (shallow copy). Use copy() for a physical copy.
- */
- public LineNumberTable(LineNumberTable c) {
- this(c.getNameIndex(), c.getLength(), c.getLineNumberTable(),
- c.getConstantPool());
- }
-
- /*
- * @param name_index Index of name
- * @param length Content length in bytes
- * @param line_number_table Table of line/numbers pairs
- * @param constant_pool Array of constants
- */
- public LineNumberTable(int name_index, int length,
- LineNumber[] line_number_table,
- ConstantPool constant_pool)
- {
- super(Constants.ATTR_LINE_NUMBER_TABLE, name_index, length, constant_pool);
- setLineNumberTable(line_number_table);
- }
-
- /**
- * Construct object from file stream.
- * @param name_index Index of name
- * @param length Content length in bytes
- * @param file Input stream
- * @param constant_pool Array of constants
- * @throws IOException
- */
- LineNumberTable(int name_index, int length, DataInputStream file,
- ConstantPool constant_pool) throws IOException
- {
- this(name_index, length, (LineNumber[])null, constant_pool);
- line_number_table_length = (file.readUnsignedShort());
- line_number_table = new LineNumber[line_number_table_length];
-
- for(int i=0; i < line_number_table_length; i++)
- line_number_table[i] = new LineNumber(file);
- }
- /**
- * Called by objects that are traversing the nodes of the tree implicitely
- * defined by the contents of a Java class. I.e., the hierarchy of methods,
- * fields, attributes, etc. spawns a tree of objects.
- *
- * @param v Visitor object
- */
- public void accept(Visitor v) {
- v.visitLineNumberTable(this);
- }
- /**
- * Dump line number table attribute to file stream in binary format.
- *
- * @param file Output file stream
- * @throws IOException
- */
- public final void dump(DataOutputStream file) throws IOException
- {
- super.dump(file);
- file.writeShort(line_number_table_length);
- for(int i=0; i < line_number_table_length; i++)
- line_number_table[i].dump(file);
- }
-
- /**
- * @return Array of (pc offset, line number) pairs.
- */
- public final LineNumber[] getLineNumberTable() { return line_number_table; }
-
- /**
- * @param line_number_table the line number entries for this table
- */
- public final void setLineNumberTable(LineNumber[] line_number_table) {
- this.line_number_table = line_number_table;
-
- line_number_table_length = (line_number_table == null)? 0 :
- line_number_table.length;
- }
-
- /**
- * @return String representation.
- */
- public final String toString() {
- StringBuffer buf = new StringBuffer();
- StringBuffer line = new StringBuffer();
- String newLine = System.getProperty("line.separator", "\n");
-
- for(int i=0; i < line_number_table_length; i++) {
- line.append(line_number_table[i].toString());
-
- if(i < line_number_table_length - 1) {
- line.append(", ");
- }
-
- if(line.length() > 72) {
- line.append(newLine);
- buf.append(line.toString());
- line.setLength(0);
- }
- }
-
- buf.append(line);
-
- return buf.toString();
- }
-
- /**
- * Map byte code positions to source code lines.
- *
- * @param pos byte code offset
- * @return corresponding line in source code
- */
- public int getSourceLine(int pos) {
- int l = 0, r = line_number_table_length-1;
-
- if(r < 0) // array is empty
- return -1;
-
- int min_index = -1, min=-1;
-
- /* Do a binary search since the array is ordered.
- */
- do {
- int i = (l + r) / 2;
- int j = line_number_table[i].getStartPC();
-
- if(j == pos)
- return line_number_table[i].getLineNumber();
- else if(pos < j) // else constrain search area
- r = i - 1;
- else // pos > j
- l = i + 1;
-
- /* If exact match can't be found (which is the most common case)
- * return the line number that corresponds to the greatest index less
- * than pos.
- */
- if(j < pos && j > min) {
- min = j;
- min_index = i;
- }
- } while(l <= r);
-
- /* It's possible that we did not find any valid entry for the bytecode
- * offset we were looking for.
- */
- if (min_index < 0)
- return -1;
-
- return line_number_table[min_index].getLineNumber();
- }
-
- /**
- * @return deep copy of this attribute
- */
- public Attribute copy(ConstantPool _constant_pool) {
- LineNumberTable c = (LineNumberTable)clone();
-
- c.line_number_table = new LineNumber[line_number_table_length];
- for(int i=0; i < line_number_table_length; i++)
- c.line_number_table[i] = line_number_table[i].copy();
-
- c.constant_pool = _constant_pool;
- return c;
- }
+ private int line_number_table_length;
+ private LineNumber[] line_number_table; // Table of line/numbers pairs
+
+
+ /*
+ * Initialize from another object. Note that both objects use the same
+ * references (shallow copy). Use copy() for a physical copy.
+ */
+ public LineNumberTable(LineNumberTable c) {
+ this(c.getNameIndex(), c.getLength(), c.getLineNumberTable(), c.getConstantPool());
+ }
+
+
+ /*
+ * @param name_index Index of name
+ * @param length Content length in bytes
+ * @param line_number_table Table of line/numbers pairs
+ * @param constant_pool Array of constants
+ */
+ public LineNumberTable(int name_index, int length, LineNumber[] line_number_table,
+ ConstantPool constant_pool) {
+ super(Constants.ATTR_LINE_NUMBER_TABLE, name_index, length, constant_pool);
+ setLineNumberTable(line_number_table);
+ }
+
+
+ /**
+ * Construct object from file stream.
+ * @param name_index Index of name
+ * @param length Content length in bytes
+ * @param file Input stream
+ * @param constant_pool Array of constants
+ * @throws IOException
+ */
+ LineNumberTable(int name_index, int length, DataInputStream file, ConstantPool constant_pool)
+ throws IOException {
+ this(name_index, length, (LineNumber[]) null, constant_pool);
+ line_number_table_length = (file.readUnsignedShort());
+ line_number_table = new LineNumber[line_number_table_length];
+ for (int i = 0; i < line_number_table_length; i++) {
+ line_number_table[i] = new LineNumber(file);
+ }
+ }
+
+
+ /**
+ * Called by objects that are traversing the nodes of the tree implicitely
+ * defined by the contents of a Java class. I.e., the hierarchy of methods,
+ * fields, attributes, etc. spawns a tree of objects.
+ *
+ * @param v Visitor object
+ */
+ public void accept( Visitor v ) {
+ v.visitLineNumberTable(this);
+ }
+
+
+ /**
+ * Dump line number table attribute to file stream in binary format.
+ *
+ * @param file Output file stream
+ * @throws IOException
+ */
+ public final void dump( DataOutputStream file ) throws IOException {
+ super.dump(file);
+ file.writeShort(line_number_table_length);
+ for (int i = 0; i < line_number_table_length; i++) {
+ line_number_table[i].dump(file);
+ }
+ }
+
+
+ /**
+ * @return Array of (pc offset, line number) pairs.
+ */
+ public final LineNumber[] getLineNumberTable() {
+ return line_number_table;
+ }
+
+
+ /**
+ * @param line_number_table the line number entries for this table
+ */
+ public final void setLineNumberTable( LineNumber[] line_number_table ) {
+ this.line_number_table = line_number_table;
+ line_number_table_length = (line_number_table == null) ? 0 : line_number_table.length;
+ }
+
+
+ /**
+ * @return String representation.
+ */
+ public final String toString() {
+ StringBuffer buf = new StringBuffer();
+ StringBuffer line = new StringBuffer();
+ String newLine = System.getProperty("line.separator", "\n");
+ for (int i = 0; i < line_number_table_length; i++) {
+ line.append(line_number_table[i].toString());
+ if (i < line_number_table_length - 1) {
+ line.append(", ");
+ }
+ if (line.length() > 72) {
+ line.append(newLine);
+ buf.append(line.toString());
+ line.setLength(0);
+ }
+ }
+ buf.append(line);
+ return buf.toString();
+ }
+
+
+ /**
+ * Map byte code positions to source code lines.
+ *
+ * @param pos byte code offset
+ * @return corresponding line in source code
+ */
+ public int getSourceLine( int pos ) {
+ int l = 0, r = line_number_table_length - 1;
+ if (r < 0) {
+ return -1;
+ }
+ int min_index = -1, min = -1;
+ /* Do a binary search since the array is ordered.
+ */
+ do {
+ int i = (l + r) / 2;
+ int j = line_number_table[i].getStartPC();
+ if (j == pos) {
+ return line_number_table[i].getLineNumber();
+ } else if (pos < j) {
+ r = i - 1;
+ } else {
+ l = i + 1;
+ }
+ /* If exact match can't be found (which is the most common case)
+ * return the line number that corresponds to the greatest index less
+ * than pos.
+ */
+ if (j < pos && j > min) {
+ min = j;
+ min_index = i;
+ }
+ } while (l <= r);
+ /* It's possible that we did not find any valid entry for the bytecode
+ * offset we were looking for.
+ */
+ if (min_index < 0) {
+ return -1;
+ }
+ return line_number_table[min_index].getLineNumber();
+ }
+
- public final int getTableLength() { return line_number_table_length; }
+ /**
+ * @return deep copy of this attribute
+ */
+ public Attribute copy( ConstantPool _constant_pool ) {
+ LineNumberTable c = (LineNumberTable) clone();
+ c.line_number_table = new LineNumber[line_number_table_length];
+ for (int i = 0; i < line_number_table_length; i++) {
+ c.line_number_table[i] = line_number_table[i].copy();
+ }
+ c.constant_pool = _constant_pool;
+ return c;
+ }
+
+
+ public final int getTableLength() {
+ return line_number_table_length;
+ }
}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/LocalVariable.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/LocalVariable.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/LocalVariable.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/LocalVariable.java Wed Mar 15 03:31:56 2006
@@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- */
+ */
package org.apache.bcel.classfile;
-
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
@@ -31,195 +30,219 @@
* @author <A HREF="mailto:[email protected]">M. Dahm</A>
* @see LocalVariableTable
*/
-public final class LocalVariable
- implements Constants, Cloneable, Node, Serializable
-{
- private int start_pc; // Range in which the variable is valid
- private int length;
- private int name_index; // Index in constant pool of variable name
- private int signature_index; // Index of variable signature
- private int index; /* Variable is `index'th local variable on
- * this method's frame.
- */
-
- private ConstantPool constant_pool;
-
- /**
- * Initialize from another object. Note that both objects use the same
- * references (shallow copy). Use copy() for a physical copy.
- */
- public LocalVariable(LocalVariable c) {
- this(c.getStartPC(), c.getLength(), c.getNameIndex(),
- c.getSignatureIndex(), c.getIndex(), c.getConstantPool());
- }
-
- /**
- * Construct object from file stream.
- * @param file Input stream
- * @throws IOException
- */
- LocalVariable(DataInputStream file, ConstantPool constant_pool)
- throws IOException
- {
- this(file.readUnsignedShort(), file.readUnsignedShort(),
- file.readUnsignedShort(), file.readUnsignedShort(),
- file.readUnsignedShort(), constant_pool);
- }
-
- /**
- * @param start_pc Range in which the variable
- * @param length ... is valid
- * @param name_index Index in constant pool of variable name
- * @param signature_index Index of variable's signature
- * @param index Variable is `index'th local variable on the method's frame
- * @param constant_pool Array of constants
- */
- public LocalVariable(int start_pc, int length, int name_index,
- int signature_index, int index,
- ConstantPool constant_pool)
- {
- this.start_pc = start_pc;
- this.length = length;
- this.name_index = name_index;
- this.signature_index = signature_index;
- this.index = index;
- this.constant_pool = constant_pool;
- }
-
- /**
- * Called by objects that are traversing the nodes of the tree implicitely
- * defined by the contents of a Java class. I.e., the hierarchy of methods,
- * fields, attributes, etc. spawns a tree of objects.
- *
- * @param v Visitor object
- */
- public void accept(Visitor v) {
- v.visitLocalVariable(this);
- }
-
- /**
- * Dump local variable to file stream in binary format.
- *
- * @param file Output file stream
- * @throws IOException
- */
- public final void dump(DataOutputStream file) throws IOException
- {
- file.writeShort(start_pc);
- file.writeShort(length);
- file.writeShort(name_index);
- file.writeShort(signature_index);
- file.writeShort(index);
- }
-
- /**
- * @return Constant pool used by this object.
- */
- public final ConstantPool getConstantPool() { return constant_pool; }
-
- /**
- * @return Variable is valid within getStartPC() .. getStartPC()+getLength()
- */
- public final int getLength() { return length; }
-
- /**
- * @return Variable name.
- */
- public final String getName() {
- ConstantUtf8 c;
-
- c = (ConstantUtf8)constant_pool.getConstant(name_index, CONSTANT_Utf8);
- return c.getBytes();
- }
-
- /**
- * @return Index in constant pool of variable name.
- */
- public final int getNameIndex() { return name_index; }
-
- /**
- * @return Signature.
- */
- public final String getSignature() {
- ConstantUtf8 c;
- c = (ConstantUtf8)constant_pool.getConstant(signature_index,
- CONSTANT_Utf8);
- return c.getBytes();
- }
-
- /**
- * @return Index in constant pool of variable signature.
- */
- public final int getSignatureIndex() { return signature_index; }
-
- /**
- * @return index of register where variable is stored
- */
- public final int getIndex() { return index; }
-
- /**
- * @return Start of range where he variable is valid
- */
- public final int getStartPC() { return start_pc; }
-
- /**
- * @param constant_pool Constant pool to be used for this object.
- */
- public final void setConstantPool(ConstantPool constant_pool) {
- this.constant_pool = constant_pool;
- }
-
- /**
- * @param length the length of this local variable
- */
- public final void setLength(int length) {
- this.length = length;
- }
-
- /**
- * @param name_index the index into the constant pool for the name of this variable
- */
- public final void setNameIndex(int name_index) {
- this.name_index = name_index;
- }
-
- /**
- * @param signature_index the index into the constant pool for the signature of this variable
- */
- public final void setSignatureIndex(int signature_index) {
- this.signature_index = signature_index;
- }
-
- /**
- * @param index the index in the local variable table of this variable
- */
- public final void setIndex(int index) { this.index = index; }
-
- /**
- * @param start_pc Specify range where the local variable is valid.
- */
- public final void setStartPC(int start_pc) {
- this.start_pc = start_pc;
- }
-
- /**
- * @return string representation.
- */
- public final String toString() {
- String name = getName(), signature = Utility.signatureToString(getSignature());
-
- return "LocalVariable(start_pc = " + start_pc + ", length = " + length +
- ", index = " + index + ":" + signature + " " + name + ")";
- }
-
- /**
- * @return deep copy of this object
- */
- public LocalVariable copy() {
- try {
- return (LocalVariable)clone();
- } catch(CloneNotSupportedException e) {}
+public final class LocalVariable implements Constants, Cloneable, Node, Serializable {
- return null;
- }
+ private int start_pc; // Range in which the variable is valid
+ private int length;
+ private int name_index; // Index in constant pool of variable name
+ private int signature_index; // Index of variable signature
+ private int index; /* Variable is `index'th local variable on
+ * this method's frame.
+ */
+ private ConstantPool constant_pool;
+
+
+ /**
+ * Initialize from another object. Note that both objects use the same
+ * references (shallow copy). Use copy() for a physical copy.
+ */
+ public LocalVariable(LocalVariable c) {
+ this(c.getStartPC(), c.getLength(), c.getNameIndex(), c.getSignatureIndex(), c.getIndex(),
+ c.getConstantPool());
+ }
+
+
+ /**
+ * Construct object from file stream.
+ * @param file Input stream
+ * @throws IOException
+ */
+ LocalVariable(DataInputStream file, ConstantPool constant_pool) throws IOException {
+ this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), file
+ .readUnsignedShort(), file.readUnsignedShort(), constant_pool);
+ }
+
+
+ /**
+ * @param start_pc Range in which the variable
+ * @param length ... is valid
+ * @param name_index Index in constant pool of variable name
+ * @param signature_index Index of variable's signature
+ * @param index Variable is `index'th local variable on the method's frame
+ * @param constant_pool Array of constants
+ */
+ public LocalVariable(int start_pc, int length, int name_index, int signature_index, int index,
+ ConstantPool constant_pool) {
+ this.start_pc = start_pc;
+ this.length = length;
+ this.name_index = name_index;
+ this.signature_index = signature_index;
+ this.index = index;
+ this.constant_pool = constant_pool;
+ }
+
+
+ /**
+ * Called by objects that are traversing the nodes of the tree implicitely
+ * defined by the contents of a Java class. I.e., the hierarchy of methods,
+ * fields, attributes, etc. spawns a tree of objects.
+ *
+ * @param v Visitor object
+ */
+ public void accept( Visitor v ) {
+ v.visitLocalVariable(this);
+ }
+
+
+ /**
+ * Dump local variable to file stream in binary format.
+ *
+ * @param file Output file stream
+ * @throws IOException
+ */
+ public final void dump( DataOutputStream file ) throws IOException {
+ file.writeShort(start_pc);
+ file.writeShort(length);
+ file.writeShort(name_index);
+ file.writeShort(signature_index);
+ file.writeShort(index);
+ }
+
+
+ /**
+ * @return Constant pool used by this object.
+ */
+ public final ConstantPool getConstantPool() {
+ return constant_pool;
+ }
+
+
+ /**
+ * @return Variable is valid within getStartPC() .. getStartPC()+getLength()
+ */
+ public final int getLength() {
+ return length;
+ }
+
+
+ /**
+ * @return Variable name.
+ */
+ public final String getName() {
+ ConstantUtf8 c;
+ c = (ConstantUtf8) constant_pool.getConstant(name_index, CONSTANT_Utf8);
+ return c.getBytes();
+ }
+
+
+ /**
+ * @return Index in constant pool of variable name.
+ */
+ public final int getNameIndex() {
+ return name_index;
+ }
+
+
+ /**
+ * @return Signature.
+ */
+ public final String getSignature() {
+ ConstantUtf8 c;
+ c = (ConstantUtf8) constant_pool.getConstant(signature_index, CONSTANT_Utf8);
+ return c.getBytes();
+ }
+
+
+ /**
+ * @return Index in constant pool of variable signature.
+ */
+ public final int getSignatureIndex() {
+ return signature_index;
+ }
+
+
+ /**
+ * @return index of register where variable is stored
+ */
+ public final int getIndex() {
+ return index;
+ }
+
+
+ /**
+ * @return Start of range where he variable is valid
+ */
+ public final int getStartPC() {
+ return start_pc;
+ }
+
+
+ /**
+ * @param constant_pool Constant pool to be used for this object.
+ */
+ public final void setConstantPool( ConstantPool constant_pool ) {
+ this.constant_pool = constant_pool;
+ }
+
+
+ /**
+ * @param length the length of this local variable
+ */
+ public final void setLength( int length ) {
+ this.length = length;
+ }
+
+
+ /**
+ * @param name_index the index into the constant pool for the name of this variable
+ */
+ public final void setNameIndex( int name_index ) {
+ this.name_index = name_index;
+ }
+
+
+ /**
+ * @param signature_index the index into the constant pool for the signature of this variable
+ */
+ public final void setSignatureIndex( int signature_index ) {
+ this.signature_index = signature_index;
+ }
+
+
+ /**
+ * @param index the index in the local variable table of this variable
+ */
+ public final void setIndex( int index ) {
+ this.index = index;
+ }
+
+
+ /**
+ * @param start_pc Specify range where the local variable is valid.
+ */
+ public final void setStartPC( int start_pc ) {
+ this.start_pc = start_pc;
+ }
+
+
+ /**
+ * @return string representation.
+ */
+ public final String toString() {
+ String name = getName(), signature = Utility.signatureToString(getSignature());
+ return "LocalVariable(start_pc = " + start_pc + ", length = " + length + ", index = "
+ + index + ":" + signature + " " + name + ")";
+ }
+
+
+ /**
+ * @return deep copy of this object
+ */
+ public LocalVariable copy() {
+ try {
+ return (LocalVariable) clone();
+ } catch (CloneNotSupportedException e) {
+ }
+ return null;
+ }
}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/LocalVariableTable.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/LocalVariableTable.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/LocalVariableTable.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/LocalVariableTable.java Wed Mar 15 03:31:56 2006
@@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- */
+ */
package org.apache.bcel.classfile;
-
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
@@ -32,158 +31,167 @@
* @see LocalVariable
*/
public class LocalVariableTable extends Attribute {
- private int local_variable_table_length; // Table of local
- private LocalVariable[] local_variable_table; // variables
- /**
- * Initialize from another object. Note that both objects use the same
- * references (shallow copy). Use copy() for a physical copy.
- */
- public LocalVariableTable(LocalVariableTable c) {
- this(c.getNameIndex(), c.getLength(), c.getLocalVariableTable(),
- c.getConstantPool());
- }
-
- /**
- * @param name_index Index in constant pool to `LocalVariableTable'
- * @param length Content length in bytes
- * @param local_variable_table Table of local variables
- * @param constant_pool Array of constants
- */
- public LocalVariableTable(int name_index, int length,
- LocalVariable[] local_variable_table,
- ConstantPool constant_pool)
- {
- super(Constants.ATTR_LOCAL_VARIABLE_TABLE, name_index, length, constant_pool);
- setLocalVariableTable(local_variable_table);
- }
-
- /**
- * Construct object from file stream.
- * @param name_index Index in constant pool
- * @param length Content length in bytes
- * @param file Input stream
- * @param constant_pool Array of constants
- * @throws IOException
- */
- LocalVariableTable(int name_index, int length, DataInputStream file,
- ConstantPool constant_pool) throws IOException
- {
- this(name_index, length, (LocalVariable[])null, constant_pool);
-
- local_variable_table_length = (file.readUnsignedShort());
- local_variable_table = new LocalVariable[local_variable_table_length];
-
- for(int i=0; i < local_variable_table_length; i++)
- local_variable_table[i] = new LocalVariable(file, constant_pool);
- }
-
- /**
- * Called by objects that are traversing the nodes of the tree implicitely
- * defined by the contents of a Java class. I.e., the hierarchy of methods,
- * fields, attributes, etc. spawns a tree of objects.
- *
- * @param v Visitor object
- */
- public void accept(Visitor v) {
- v.visitLocalVariableTable(this);
- }
-
- /**
- * Dump local variable table attribute to file stream in binary format.
- *
- * @param file Output file stream
- * @throws IOException
- */
- public final void dump(DataOutputStream file) throws IOException
- {
- super.dump(file);
- file.writeShort(local_variable_table_length);
- for(int i=0; i < local_variable_table_length; i++)
- local_variable_table[i].dump(file);
- }
-
- /**
- * @return Array of local variables of method.
- */
- public final LocalVariable[] getLocalVariableTable() {
- return local_variable_table;
- }
-
- /**
- * @return first matching variable using index
- *
- * @param index the variable slot
- *
- * @return the first LocalVariable that matches the slot or null if not found
- *
- * @deprecated since 5.2 because multiple variables can share the
- * same slot, use getLocalVariable(int index, int pc) instead.
- */
- public final LocalVariable getLocalVariable(int index) {
- for(int i=0; i < local_variable_table_length; i++)
- if(local_variable_table[i].getIndex() == index)
- return local_variable_table[i];
-
- return null;
- }
-
- /**
- * @return matching variable using index when variable is used at supplied pc
- *
- * @param index the variable slot
- * @param pc the current pc that this variable is alive
- *
- * @return the LocalVariable that matches or null if not found
- */
- public final LocalVariable getLocalVariable(int index, int pc) {
- for(int i=0; i < local_variable_table_length; i++)
- if(local_variable_table[i].getIndex() == index) {
- int start_pc = local_variable_table[i].getStartPC();
- int end_pc = start_pc + local_variable_table[i].getLength();
- if ((pc >= start_pc) && (pc < end_pc))
- return local_variable_table[i];
- }
-
- return null;
- }
-
- public final void setLocalVariableTable(LocalVariable[] local_variable_table)
- {
- this.local_variable_table = local_variable_table;
- local_variable_table_length = (local_variable_table == null)? 0 :
- local_variable_table.length;
- }
-
- /**
- * @return String representation.
- */
- public final String toString() {
- StringBuffer buf = new StringBuffer("");
-
- for(int i=0; i < local_variable_table_length; i++) {
- buf.append(local_variable_table[i].toString());
-
- if(i < local_variable_table_length - 1)
- buf.append('\n');
- }
-
- return buf.toString();
- }
-
- /**
- * @return deep copy of this attribute
- */
- public Attribute copy(ConstantPool _constant_pool) {
- LocalVariableTable c = (LocalVariableTable)clone();
-
- c.local_variable_table = new LocalVariable[local_variable_table_length];
- for(int i=0; i < local_variable_table_length; i++)
- c.local_variable_table[i] = local_variable_table[i].copy();
-
- c.constant_pool = _constant_pool;
- return c;
- }
+ private int local_variable_table_length; // Table of local
+ private LocalVariable[] local_variable_table; // variables
+
+
+ /**
+ * Initialize from another object. Note that both objects use the same
+ * references (shallow copy). Use copy() for a physical copy.
+ */
+ public LocalVariableTable(LocalVariableTable c) {
+ this(c.getNameIndex(), c.getLength(), c.getLocalVariableTable(), c.getConstantPool());
+ }
+
+
+ /**
+ * @param name_index Index in constant pool to `LocalVariableTable'
+ * @param length Content length in bytes
+ * @param local_variable_table Table of local variables
+ * @param constant_pool Array of constants
+ */
+ public LocalVariableTable(int name_index, int length, LocalVariable[] local_variable_table,
+ ConstantPool constant_pool) {
+ super(Constants.ATTR_LOCAL_VARIABLE_TABLE, name_index, length, constant_pool);
+ setLocalVariableTable(local_variable_table);
+ }
- public final int getTableLength() { return local_variable_table_length; }
+
+ /**
+ * Construct object from file stream.
+ * @param name_index Index in constant pool
+ * @param length Content length in bytes
+ * @param file Input stream
+ * @param constant_pool Array of constants
+ * @throws IOException
+ */
+ LocalVariableTable(int name_index, int length, DataInputStream file, ConstantPool constant_pool)
+ throws IOException {
+ this(name_index, length, (LocalVariable[]) null, constant_pool);
+ local_variable_table_length = (file.readUnsignedShort());
+ local_variable_table = new LocalVariable[local_variable_table_length];
+ for (int i = 0; i < local_variable_table_length; i++) {
+ local_variable_table[i] = new LocalVariable(file, constant_pool);
+ }
+ }
+
+
+ /**
+ * Called by objects that are traversing the nodes of the tree implicitely
+ * defined by the contents of a Java class. I.e., the hierarchy of methods,
+ * fields, attributes, etc. spawns a tree of objects.
+ *
+ * @param v Visitor object
+ */
+ public void accept( Visitor v ) {
+ v.visitLocalVariableTable(this);
+ }
+
+
+ /**
+ * Dump local variable table attribute to file stream in binary format.
+ *
+ * @param file Output file stream
+ * @throws IOException
+ */
+ public final void dump( DataOutputStream file ) throws IOException {
+ super.dump(file);
+ file.writeShort(local_variable_table_length);
+ for (int i = 0; i < local_variable_table_length; i++) {
+ local_variable_table[i].dump(file);
+ }
+ }
+
+
+ /**
+ * @return Array of local variables of method.
+ */
+ public final LocalVariable[] getLocalVariableTable() {
+ return local_variable_table;
+ }
+
+
+ /**
+ * @return first matching variable using index
+ *
+ * @param index the variable slot
+ *
+ * @return the first LocalVariable that matches the slot or null if not found
+ *
+ * @deprecated since 5.2 because multiple variables can share the
+ * same slot, use getLocalVariable(int index, int pc) instead.
+ */
+ public final LocalVariable getLocalVariable( int index ) {
+ for (int i = 0; i < local_variable_table_length; i++) {
+ if (local_variable_table[i].getIndex() == index) {
+ return local_variable_table[i];
+ }
+ }
+ return null;
+ }
+
+
+ /**
+ * @return matching variable using index when variable is used at supplied pc
+ *
+ * @param index the variable slot
+ * @param pc the current pc that this variable is alive
+ *
+ * @return the LocalVariable that matches or null if not found
+ */
+ public final LocalVariable getLocalVariable( int index, int pc ) {
+ for (int i = 0; i < local_variable_table_length; i++) {
+ if (local_variable_table[i].getIndex() == index) {
+ int start_pc = local_variable_table[i].getStartPC();
+ int end_pc = start_pc + local_variable_table[i].getLength();
+ if ((pc >= start_pc) && (pc < end_pc)) {
+ return local_variable_table[i];
+ }
+ }
+ }
+ return null;
+ }
+
+
+ public final void setLocalVariableTable( LocalVariable[] local_variable_table ) {
+ this.local_variable_table = local_variable_table;
+ local_variable_table_length = (local_variable_table == null)
+ ? 0
+ : local_variable_table.length;
+ }
+
+
+ /**
+ * @return String representation.
+ */
+ public final String toString() {
+ StringBuffer buf = new StringBuffer("");
+ for (int i = 0; i < local_variable_table_length; i++) {
+ buf.append(local_variable_table[i].toString());
+ if (i < local_variable_table_length - 1) {
+ buf.append('\n');
+ }
+ }
+ return buf.toString();
+ }
+
+
+ /**
+ * @return deep copy of this attribute
+ */
+ public Attribute copy( ConstantPool _constant_pool ) {
+ LocalVariableTable c = (LocalVariableTable) clone();
+ c.local_variable_table = new LocalVariable[local_variable_table_length];
+ for (int i = 0; i < local_variable_table_length; i++) {
+ c.local_variable_table[i] = local_variable_table[i].copy();
+ }
+ c.constant_pool = _constant_pool;
+ return c;
+ }
+
+
+ public final int getTableLength() {
+ return local_variable_table_length;
+ }
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.