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.
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;
}
}
/* 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.JavaClass[] | getAllInterfaces()Get all interfaces implemented by this JavaClass (transitively).
ClassQueue queue = new ClassQueue();
ClassVector vec = new ClassVector();
queue.enqueue(this);
while(!queue.empty()) {
JavaClass clazz = queue.dequeue();
JavaClass souper = clazz.getSuperClass();
JavaClass[] interfaces = clazz.getInterfaces();
if(clazz.isInterface()) {
vec.addElement(clazz);
} else {
if(souper != null) {
queue.enqueue(souper);
}
}
for(int i = 0; i < interfaces.length; i++) {
queue.enqueue(interfaces[i]);
}
}
return vec.toArray();
|
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);
} catch(IOException e) {
e.printStackTrace();
} finally {
try { ds.close(); } catch(IOException e2) { e2.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 int[] | getInterfaceIndices() return interfaces;
|
public java.lang.String[] | getInterfaceNames() return interface_names;
|
public com.sun.org.apache.bcel.internal.classfile.JavaClass[] | getInterfaces()Get interfaces directly implemented by this JavaClass.
String[] interfaces = getInterfaceNames();
JavaClass[] classes = new JavaClass[interfaces.length];
try {
for(int i = 0; i < interfaces.length; i++) {
classes[i] = repository.loadClass(interfaces[i]);
}
} catch(ClassNotFoundException e) {
System.err.println(e);
return null;
}
return classes;
|
public int | getMajor() return major;
|
public com.sun.org.apache.bcel.internal.classfile.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;
|
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 com.sun.org.apache.bcel.internal.util.Repository | getRepository()Gets the ClassRepository which holds its definition. By default
this is the same as SyntheticRepository.getInstance();
return repository;
|
public final byte | getSource()
return source;
|
public java.lang.String | getSourceFileName() return source_file_name;
|
public com.sun.org.apache.bcel.internal.classfile.JavaClass | getSuperClass()
if("java.lang.Object".equals(getClassName())) {
return null;
}
try {
return repository.loadClass(getSuperclassName());
} catch(ClassNotFoundException e) {
System.err.println(e);
return null;
}
|
public com.sun.org.apache.bcel.internal.classfile.JavaClass[] | getSuperClasses()
JavaClass clazz = this;
ClassVector vec = new ClassVector();
for(clazz = clazz.getSuperClass(); clazz != null;
clazz = clazz.getSuperClass())
{
vec.addElement(clazz);
}
return vec.toArray();
|
public java.lang.String | getSuperclassName() return superclass_name;
|
public int | getSuperclassNameIndex() return superclass_name_index;
|
public boolean | implementationOf(com.sun.org.apache.bcel.internal.classfile.JavaClass inter)
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;
|
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)Equivalent to runtime "instanceof" operator.
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;
|
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 = null, sep = null;
try {
debug = System.getProperty("JavaClass.debug");
// Get path separator either / or \ usually
sep = System.getProperty("file.separator");
}
catch (SecurityException e) {
// falls through
}
if(debug != null)
JavaClass.debug = new Boolean(debug).booleanValue();
if(sep != null)
try {
JavaClass.sep = sep.charAt(0);
} catch(StringIndexOutOfBoundsException e) {} // Never reached
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 | setRepository(com.sun.org.apache.bcel.internal.util.Repository repository)Sets the ClassRepository which loaded the JavaClass.
Should be called immediately after parsing is done.
this.repository = repository;
|
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();
|