Constructors Summary |
---|
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)Constructor gets all contents as arguments. // directory separator
if(interfaces == null) // Allowed for backward compatibility
interfaces = new int[0];
if(attributes == null)
this.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;
}
}
// Get class name and superclass name
ConstantUtf8 name;
/* 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);
}
|
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)Constructor gets all contents as arguments.
this(class_name_index, superclass_name_index, file_name, major, minor, access_flags,
constant_pool, interfaces, fields, methods, attributes, HEAP);
|
Methods Summary |
---|
static final void | Debug(java.lang.String str)
if(debug)
System.out.println(str);
|
public void | accept(com.sun.org.apache.bcel.internal.classfile.Visitor v)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.
v.visitJavaClass(this);
|
public com.sun.org.apache.bcel.internal.classfile.JavaClass | copy()
JavaClass c = null;
try {
c = (JavaClass)clone();
} catch(CloneNotSupportedException e) {}
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);
return c;
|
public void | dump(java.io.File file)Dump class to a file.
String parent = file.getParent();
if(parent != null) {
File dir = new File(parent);
if(dir != null)
dir.mkdirs();
}
dump(new DataOutputStream(new FileOutputStream(file)));
|
public void | dump(java.lang.String file_name)Dump class to a file named file_name.
dump(new File(file_name));
|
public void | dump(java.io.OutputStream file)Dump Java class to output stream in binary format.
dump(new DataOutputStream(file));
|
public void | dump(java.io.DataOutputStream file)Dump Java class to output stream in binary format.
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.close();
|
public com.sun.org.apache.bcel.internal.classfile.Attribute[] | getAttributes() return attributes;
|
public byte[] | getBytes()
ByteArrayOutputStream s = new ByteArrayOutputStream();
DataOutputStream ds = new DataOutputStream(s);
try {
dump(ds);
ds.close();
} catch(IOException e) { e.printStackTrace(); }
return s.toByteArray();
|
public java.lang.String | getClassName() return class_name;
|
public int | getClassNameIndex() return class_name_index;
|
public com.sun.org.apache.bcel.internal.classfile.ConstantPool | getConstantPool() return constant_pool;
|
public com.sun.org.apache.bcel.internal.classfile.Field[] | getFields() return fields;
|
public java.lang.String | getFileName() return file_name;
|
public java.lang.String[] | getInterfaceNames() return interface_names;
|
public int[] | getInterfaces() return interfaces;
|
public int | getMajor() return major;
|
public com.sun.org.apache.bcel.internal.classfile.Method[] | getMethods() return methods;
|
public int | getMinor() return minor;
|
public java.lang.String | getPackageName() return package_name;
|
public final byte | getSource()
return source;
|
public java.lang.String | getSourceFileName() return source_file_name;
|
public java.lang.String | getSuperclassName() return superclass_name;
|
public int | getSuperclassNameIndex() return superclass_name_index;
|
private static final java.lang.String | indent(java.lang.Object obj)
StringTokenizer tok = new StringTokenizer(obj.toString(), "\n");
StringBuffer buf = new StringBuffer();
while(tok.hasMoreTokens())
buf.append("\t" + tok.nextToken() + "\n");
return buf.toString();
|
public final boolean | instanceOf(com.sun.org.apache.bcel.internal.classfile.JavaClass super_class)
return Repository.instanceOf(this, super_class);
|
public final boolean | isClass()
return (access_flags & Constants.ACC_INTERFACE) == 0;
|
public final boolean | isSuper()
return (access_flags & Constants.ACC_SUPER) != 0;
|
public void | setAttributes(com.sun.org.apache.bcel.internal.classfile.Attribute[] attributes)
// Debugging ... on/off
//String debug = System.getProperty("JavaClass.debug");
//if(debug != null)
// JavaClass.debug = new Boolean(debug).booleanValue();
// Get path separator either / or \ usually
JavaClass.sep = java.io.File.separatorChar;
this.attributes = attributes;
|
public void | setClassName(java.lang.String class_name)
this.class_name = class_name;
|
public void | setClassNameIndex(int class_name_index)
this.class_name_index = class_name_index;
|
public void | setConstantPool(com.sun.org.apache.bcel.internal.classfile.ConstantPool constant_pool)
this.constant_pool = constant_pool;
|
public void | setFields(com.sun.org.apache.bcel.internal.classfile.Field[] fields)
this.fields = fields;
|
public void | setFileName(java.lang.String file_name)Set File name of class, aka SourceFile attribute value
this.file_name = file_name;
|
public void | setInterfaceNames(java.lang.String[] interface_names)
this.interface_names = interface_names;
|
public void | setInterfaces(int[] interfaces)
this.interfaces = interfaces;
|
public void | setMajor(int major)
this.major = major;
|
public void | setMethods(com.sun.org.apache.bcel.internal.classfile.Method[] methods)
this.methods = methods;
|
public void | setMinor(int minor)
this.minor = minor;
|
public void | setSourceFileName(java.lang.String source_file_name)Set absolute path to file this class was read from.
this.source_file_name = source_file_name;
|
public void | setSuperclassName(java.lang.String superclass_name)
this.superclass_name = superclass_name;
|
public void | setSuperclassNameIndex(int superclass_name_index)
this.superclass_name_index = superclass_name_index;
|
public java.lang.String | toString()
String access = Utility.accessToString(access_flags, true);
access = access.equals("")? "" : (access + " ");
StringBuffer buf = new StringBuffer(access +
Utility.classOrInterface(access_flags) +
" " +
class_name + " extends " +
Utility.compactClassName(superclass_name,
false) + '\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" + file_name + '\n");
buf.append("compiled from\t\t" + source_file_name + '\n");
buf.append("compiler version\t" + major + "." + minor + '\n");
buf.append("access flags\t\t" + access_flags + '\n");
buf.append("constant pool\t\t" + constant_pool.getLength() + " entries\n");
buf.append("ACC_SUPER flag\t\t" + isSuper() + "\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" + fields.length + " fields:\n");
for(int i=0; i < fields.length; i++)
buf.append("\t" + fields[i] + '\n");
}
if(methods.length > 0) {
buf.append("\n" + methods.length + " methods:\n");
for(int i=0; i < methods.length; i++)
buf.append("\t" + methods[i] + '\n");
}
return buf.toString();
|