📄 javaclass.java
字号:
package de.fub.bytecode.classfile;import de.fub.bytecode.Constants;import de.fub.bytecode.Repository;import java.io.*;import java.util.StringTokenizer;/** * Represents a Java class, i.e., the data structures, constant pool, * fields, methods and commands contained in a Java .class file. * See <a href="ftp://java.sun.com/docs/specs/">JVM * specification</a> for details. * * @version $Id: JavaClass.java,v 1.6 2001/08/21 14:05:09 ehaase Exp $ * @author <A HREF="http://www.berlin.de/~markus.dahm/">M. Dahm</A> */public class JavaClass extends AccessFlags implements Cloneable, Node { 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 /** * Constructor gets all contents as arguments. * * @param class_name Class name * @param superclass_name 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 * @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) 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); } } /** * Constructor gets all contents as arguments. * * @param class_name Class name * @param superclass_name 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 * @throw IOException */ public void dump(File file) throws IOException { String parent = file.getParent(); if(parent != null) { File dir = new File(parent); if(dir != null) dir.mkdirs(); } dump(new DataOutputStream(new FileOutputStream(file))); } /** * 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); ds.close(); } catch(IOException e) { e.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.close(); } /** * @return Attributes of the class.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -