📄 javaclass.java
字号:
/**
* @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
String debug = System.getProperty("JavaClass.debug");
if(debug != null)
JavaClass.debug = new Boolean(debug).booleanValue();
// 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(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();
}
private static final String indent(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();
}
/**
* @return deep copy of this class
*/
public 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 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 teh super class
*/
public final boolean instanceOf(JavaClass super_class) {
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 clazz is an implementation of interface inter
*/
public boolean implementationOf(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;
}
/**
* @return the superclass for this JavaClass object, or null if this
* is java.lang.Object
*/
public JavaClass getSuperClass() {
if("java.lang.Object".equals(getClassName())) {
return null;
}
try {
return repository.loadClass(getSuperclassName());
} catch(ClassNotFoundException e) {
System.err.println(e);
return null;
}
}
/**
* @return list of super classes of this class in ascending order, i.e.,
* java.lang.Object is always the last element
*/
public JavaClass[] getSuperClasses() {
JavaClass clazz = this;
ClassVector vec = new ClassVector();
for(clazz = clazz.getSuperClass(); clazz != null;
clazz = clazz.getSuperClass())
{
vec.addElement(clazz);
}
return vec.toArray();
}
/**
* Get interfaces directly implemented by this JavaClass.
*/
public JavaClass[] getInterfaces() {
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;
}
/**
* Get all interfaces implemented by this JavaClass (transitively).
*/
public JavaClass[] getAllInterfaces() {
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();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -