📄 classfile.java
字号:
* Read in the interfaces from the class file. * * @param iStream the stream on which to read data * * @return int the number of interfaces * implemented by this class * * @exception IOException just pass any IOExceptions up */ private int readInterfaces (DataInputStream iStream) throws IOException { interfacesCount = iStream.readUnsignedShort (); //if there are no interfaces just return if (interfacesCount == 0) return interfacesCount; //allocate space for the interfaces and read them in interfaces = new int[interfacesCount]; for (int lcv=0; lcv < interfacesCount; ++lcv) interfaces[lcv] = iStream.readUnsignedShort (); return interfacesCount; } /** * Read in the fields of this class. * * @param iStream the stream on which to read data * * @return int the number of fields in this class * * @exception IOException just pass any IOExceptions up */ private int readFieldInfo (DataInputStream iStream) throws IOException { //read in the number of fields fieldsCount = iStream.readUnsignedShort (); //allocate space for the fields and read them in fieldInfo = new FieldInfo[fieldsCount]; for (int lcv=0; lcv < fieldsCount; ++lcv) fieldInfo[lcv] = new FieldInfo (iStream, constantPool); return fieldsCount; } /** * Finds the method that contains the line in the source file with the * specified line number. * * @param int a source file line number * * @return MethodInfo if a method contained code at * the specified line number then * the method's MethodInfo is returned * otherwise null */ public MethodInfo lookUpMethodByLineNumber (int lineNumber) { boolean found = false; int index = 0; //loop through all the methods until we find it or run out of methods while ((!found) && (index < methodsCount)) { if (methodInfo[index].containsLine (lineNumber)) found = true; else ++index; } return (found) ? methodInfo[index] : null; } /** * Read in the methods of this class. * * @param iStream the stream on which to read data * * @return int the number of methods in this class * * @exception IOException just pass any IOExceptions up */ private int readMethodInfo (DataInputStream iStream) throws IOException { //read in the number of methods methodsCount = iStream.readUnsignedShort (); //allocate space for the methods and read them in methodInfo = new MethodInfo[methodsCount]; for (int lcv=0; lcv < methodsCount; ++lcv) methodInfo[lcv] = new MethodInfo (iStream, constantPool); return methodsCount; } /** * Read in the attributes of this class. * * @param iStream the stream on which to read data * * @return int the number of attributes of * this class * * @exception IOException just pass any IOExceptions up */ private int readAttributes (DataInputStream iStream) throws IOException { //read in the number of attributes attributesCount = iStream.readUnsignedShort (); //allocate space for the attributes and read them in attributes = new AttributeInfo[attributesCount]; for (int lcv=0; lcv < attributesCount; ++lcv) attributes[lcv] = new AttributeInfo (iStream, constantPool); return attributesCount; } /** * Get a methodinfo given a method name */ public MethodInfo getMethodInfoByName(String methodName, String sig) { for (int i = 0; i < methodsCount; i++) { if ( methodInfo[i].getName().equals(methodName) && methodInfo[i].getSignatureRaw().equals( sig ) ) return methodInfo[i]; } return null; } /** * Get a methodinfo given a method index */ public MethodInfo getMethodInfoByIndex(int methodIndex) { if (methodIndex > methodInfo.length) return null; else return methodInfo[methodIndex]; } /** * Returns a list of MethodInfo objects */ public List getAllMethodInfo() { Vector vector = new Vector(); for ( int i=0; i < methodInfo.length; i++ ) vector.add( methodInfo[ i ] ); return vector; } /** * Returns a list of FieldInfo objects */ public List getAllFieldInfo() { Vector vector = new Vector(); for ( int i=0; i < fieldInfo.length; i++ ) vector.add( fieldInfo[ i ] ); return vector; } /** * Returns a list of class names that are interfaces implemented by * this class */ public List getAllInterfaces() { Vector vector = new Vector(); int nameIndex; ConstantUtf8Info utf8Info; for ( int i=0; i < interfacesCount; i++ ) { nameIndex = ((ConstantClassInfo)constantPool[interfaces[ i ]]).getNameIndex(); utf8Info = (ConstantUtf8Info)constantPool[nameIndex]; Log.LOGN(3, "interface: " + utf8Info.toString()); vector.add(utf8Info.toString()); } return vector; } public String getSuperClass() { if (superClass == OBJECT_SUPER_CLASS_ID) return (""); else { ConstantClassInfo c = (ConstantClassInfo) constantPool[superClass]; int nameIndex = c.getNameIndex (); ConstantUtf8Info utf8Info = (ConstantUtf8Info) constantPool[nameIndex]; return utf8Info.toString(); } } /** * Print the contents of this class file in a nice easy to * read manner to the specified PrintStream * * @param out where to output the contents * of the class file */ public void print (PrintStream out) { ConstantClassInfo c; int nameIndex; ConstantUtf8Info utf8Info; //output the access flags out.println (accessFlags); //get the name of this class out of the constant pool table //and output it c = (ConstantClassInfo) constantPool[thisClass]; nameIndex = c.getNameIndex (); utf8Info = (ConstantUtf8Info) constantPool[nameIndex]; out.println ("This Class:\t\t" + StringParser.parseClassName (utf8Info.toString ())); //get the name of the super class out of the constant pool //table and output it String sc = getSuperClass(); out.print("Superclass: "); if (sc.equals("")) out.println("None"); else out.println(sc); //if this class implements any interfaces look them up in //the constant pool table and output them if (interfacesCount > 0) for (int lcv = 0; lcv < interfacesCount; ++lcv) { c = (ConstantClassInfo) constantPool[interfaces[lcv]]; nameIndex = c.getNameIndex (); utf8Info = (ConstantUtf8Info) constantPool[nameIndex]; out.println ("Interfaces:\t\t" + StringParser.parseClassName (utf8Info.toString ())); } //output the fields of this class file for (int lcv = 0; lcv < fieldsCount; ++lcv) out.println (fieldInfo[lcv].toString ()); //output the methods of this class file for (int lcv = 0; lcv < methodsCount; ++lcv) out.println (methodInfo[lcv].toString ()); //output the attributes of this class file for (int lcv = 0; lcv < attributesCount; ++lcv) out.println (attributes[lcv].toString ()); } public List getVariableTableForMethodIndex(int methodIndex){ MethodInfo mi = null; int i = 0; int attributeCount = 0; List list = null; if( (mi = getMethodInfoByIndex(methodIndex)) != null ){ list = mi.getLocalVariables();// DEBUG// if (list != null) {// for( i = 0; i < list.size();i++)// ((LocalVariable)list.get(i)).print();// }// END DEBUG } return list; } public int getRawAccessFlags() { return (accessFlags.getRawAccessFlags()); } /** * Retrieves the Sourcefile attribute for this method. * * @return SourceFileAttribute this methods code attribute or null * if it does not have one */ public SourceFileAttribute getSourceAttribute () { int attrIndex = 0; boolean found = false; //loop through the attributes until the Source attribute is found or //there are no attributes left while ((!found) && (attrIndex < attributesCount)) { if (attributes[attrIndex].getType () == AttributeInfo.ATTR_SOURCEFILE) found = true; else ++attrIndex; } if (found) {//found the SourceFile attribute so return it AttributeInfo attrInfo = attributes[attrIndex]; return (SourceFileAttribute) attrInfo.getInfo (); } else return null; //didn't find it... just return null } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -