package org.apache.bcel.util; import org.apache.bcel.classfile.ConstantString; import org.apache.bcel.classfile.ConstantClass; import org.apache.bcel.classfile.ConstantFieldref; import org.apache.bcel.classfile.ConstantNameAndType; import org.apache.bcel.classfile.Utility; import org.apache.bcel.classfile.ConstantInterfaceMethodref; import org.apache.bcel.classfile.ConstantMethodref; import org.apache.bcel.Const; import java.io.UnsupportedEncodingException; import java.io.FileNotFoundException; import java.nio.charset.Charset; import org.apache.bcel.classfile.Method; import org.apache.bcel.classfile.Constant; import java.io.PrintWriter; import org.apache.bcel.classfile.ConstantPool; final class ConstantHTML { private final String className; private final String classPackage; private final ConstantPool constantPool; private final PrintWriter printWriter; private final String[] constantRef; private final Constant[] constants; private final Method[] methods; ConstantHTML(final String dir, final String className, final String classPackage, final Method[] methods, final ConstantPool constantPool, final Charset charset) throws FileNotFoundException, UnsupportedEncodingException { this.className = className; this.classPackage = classPackage; this.constantPool = constantPool; this.methods = methods; this.constants = constantPool.getConstantPool(); try (PrintWriter newPrintWriter = new PrintWriter(dir + className + "_cp.html", charset.name())) { this.printWriter = newPrintWriter; (this.constantRef = new String[this.constants.length])[0] = "<unknown>"; this.printWriter.print(""); this.printWriter.println(""); for (int i = 1; i < this.constants.length; ++i) { if (i % 2 == 0) { this.printWriter.print("\n"); } this.printWriter.println("
"); } else { this.printWriter.print("
"); } if (this.constants[i] != null) { this.writeConstant(i); } this.printWriter.print("
"); } } private int getMethodNumber(final String str) { for (int i = 0; i < this.methods.length; ++i) { final String cmp = this.methods[i].getName() + this.methods[i].getSignature(); if (cmp.equals(str)) { return i; } } return -1; } String referenceConstant(final int index) { return this.constantRef[index]; } private void writeConstant(final int index) { final byte tag = this.constants[index].getTag(); this.printWriter.println("

" + index + " " + Const.getConstantName(tag) + "

"); switch (tag) { case 10: case 11: { int classIndex; int nameIndex; if (tag == 10) { final ConstantMethodref c = (ConstantMethodref)this.constantPool.getConstant(index, (byte)10, (Class)ConstantMethodref.class); classIndex = c.getClassIndex(); nameIndex = c.getNameAndTypeIndex(); } else { final ConstantInterfaceMethodref c2 = (ConstantInterfaceMethodref)this.constantPool.getConstant(index, (byte)11, (Class)ConstantInterfaceMethodref.class); classIndex = c2.getClassIndex(); nameIndex = c2.getNameAndTypeIndex(); } final String methodName = this.constantPool.constantToString(nameIndex, (byte)12); final String htmlMethodName = Class2HTML.toHTML(methodName); final String methodClass = this.constantPool.constantToString(classIndex, (byte)7); String shortMethodClass = Utility.compactClassName(methodClass); shortMethodClass = Utility.compactClassName(shortMethodClass, this.classPackage + ".", true); final ConstantNameAndType c3 = (ConstantNameAndType)this.constantPool.getConstant(nameIndex, (byte)12, (Class)ConstantNameAndType.class); final String signature = this.constantPool.constantToString(c3.getSignatureIndex(), (byte)1); final String[] args = Utility.methodSignatureArgumentTypes(signature, false); final String type = Utility.methodSignatureReturnType(signature, false); final String retType = Class2HTML.referenceType(type); final StringBuilder buf = new StringBuilder("("); for (int i = 0; i < args.length; ++i) { buf.append(Class2HTML.referenceType(args[i])); if (i < args.length - 1) { buf.append(", "); } } buf.append(")"); final String argTypes = buf.toString(); String ref; if (methodClass.equals(this.className)) { ref = "" + htmlMethodName + ""; } else { ref = "" + shortMethodClass + "." + htmlMethodName; } this.constantRef[index] = retType + " " + shortMethodClass + "." + htmlMethodName + " " + argTypes; this.printWriter.println("

" + retType + " " + ref + argTypes + " \n

"); break; } case 9: { final ConstantFieldref c4 = (ConstantFieldref)this.constantPool.getConstant(index, (byte)9, (Class)ConstantFieldref.class); final int classIndex = c4.getClassIndex(); final int nameIndex = c4.getNameAndTypeIndex(); final String fieldClass = this.constantPool.constantToString(classIndex, (byte)7); String shortFieldClass = Utility.compactClassName(fieldClass); shortFieldClass = Utility.compactClassName(shortFieldClass, this.classPackage + ".", true); final String fieldName = this.constantPool.constantToString(nameIndex, (byte)12); String ref; if (fieldClass.equals(this.className)) { ref = "" + fieldName + ""; } else { ref = "" + shortFieldClass + "." + fieldName + "\n"; } this.constantRef[index] = "" + shortFieldClass + "." + fieldName + ""; this.printWriter.println("

" + ref + "
\n

"); break; } case 7: { final ConstantClass c5 = (ConstantClass)this.constantPool.getConstant(index, (byte)7, (Class)ConstantClass.class); final int nameIndex = c5.getNameIndex(); final String className2 = this.constantPool.constantToString(index, tag); String shortClassName = Utility.compactClassName(className2); shortClassName = Utility.compactClassName(shortClassName, this.classPackage + ".", true); final String ref = "" + shortClassName + ""; this.constantRef[index] = "" + shortClassName + ""; this.printWriter.println("

" + ref + "

\n"); break; } case 8: { final ConstantString c6 = (ConstantString)this.constantPool.getConstant(index, (byte)8, (Class)ConstantString.class); final int nameIndex = c6.getStringIndex(); final String str = Class2HTML.toHTML(this.constantPool.constantToString(index, tag)); this.printWriter.println("

" + str + "

\n"); break; } case 12: { final ConstantNameAndType c7 = (ConstantNameAndType)this.constantPool.getConstant(index, (byte)12, (Class)ConstantNameAndType.class); final int nameIndex = c7.getNameIndex(); final int signatureIndex = c7.getSignatureIndex(); this.printWriter.println("

" + Class2HTML.toHTML(this.constantPool.constantToString(index, tag)) + "

\n"); break; } default: { this.printWriter.println("

" + Class2HTML.toHTML(this.constantPool.constantToString(index, tag)) + "\n"); break; } } } }