package org.apache.bcel.util;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.ExceptionTable;
import org.apache.bcel.classfile.Attribute;
import org.apache.bcel.classfile.Utility;
import java.io.UnsupportedEncodingException;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
import org.apache.bcel.classfile.Field;
import org.apache.bcel.classfile.Method;
import java.io.PrintWriter;
final class MethodHTML
{
private final String className;
private final PrintWriter printWriter;
private final ConstantHTML constantHtml;
private final AttributeHTML attributeHtml;
MethodHTML(final String dir, final String className, final Method[] methods, final Field[] fields, final ConstantHTML constantHtml, final AttributeHTML attributeHtml, final Charset charset) throws FileNotFoundException, UnsupportedEncodingException {
this.className = className;
this.attributeHtml = attributeHtml;
this.constantHtml = constantHtml;
try (PrintWriter newPrintWriter = new PrintWriter(dir + className + "_methods.html", charset.name())) {
(this.printWriter = newPrintWriter).print("
");
this.printWriter.println("");
this.printWriter.println("Access flags | Type | Field name |
");
for (final Field field : fields) {
this.writeField(field);
}
this.printWriter.println("
");
this.printWriter.println("Access flags | Return type | Method name | Arguments |
");
for (int i = 0; i < methods.length; ++i) {
this.writeMethod(methods[i], i);
}
this.printWriter.println("
");
}
}
private void writeField(final Field field) {
final String type = Utility.signatureToString(field.getSignature());
final String name = field.getName();
String access = Utility.accessToString(field.getAccessFlags());
access = Utility.replace(access, " ", " ");
this.printWriter.print("" + access + " | \n" + Class2HTML.referenceType(type) + " | " + name + " | ");
final Attribute[] attributes = field.getAttributes();
for (int i = 0; i < attributes.length; ++i) {
this.attributeHtml.writeAttribute(attributes[i], name + "@" + i);
}
for (int i = 0; i < attributes.length; ++i) {
if (attributes[i].getTag() == 1) {
final String str = attributes[i].toString();
this.printWriter.print("= " + str + " | \n");
break;
}
}
this.printWriter.println("
");
}
private void writeMethod(final Method method, final int methodNumber) {
final String signature = method.getSignature();
final String[] args = Utility.methodSignatureArgumentTypes(signature, false);
final String type = Utility.methodSignatureReturnType(signature, false);
final String name = method.getName();
String access = Utility.accessToString(method.getAccessFlags());
final Attribute[] attributes = method.getAttributes();
access = Utility.replace(access, " ", " ");
final String htmlName = Class2HTML.toHTML(name);
this.printWriter.print("" + access + " | ");
this.printWriter.print("" + Class2HTML.referenceType(type) + " | " + htmlName + " | \n(");
for (int i = 0; i < args.length; ++i) {
this.printWriter.print(Class2HTML.referenceType(args[i]));
if (i < args.length - 1) {
this.printWriter.print(", ");
}
}
this.printWriter.print(") |
");
for (int i = 0; i < attributes.length; ++i) {
this.attributeHtml.writeAttribute(attributes[i], "method" + methodNumber + "@" + i, methodNumber);
final byte tag = attributes[i].getTag();
if (tag == 3) {
this.printWriter.print(" | throws | ");
final int[] exceptions = ((ExceptionTable)attributes[i]).getExceptionIndexTable();
for (int j = 0; j < exceptions.length; ++j) {
this.printWriter.print(this.constantHtml.referenceConstant(exceptions[j]));
if (j < exceptions.length - 1) {
this.printWriter.print(", ");
}
}
this.printWriter.println(" |
");
}
else if (tag == 2) {
final Attribute[] attributeArray = ((Code)attributes[i]).getAttributes();
for (int j = 0; j < attributeArray.length; ++j) {
this.attributeHtml.writeAttribute(attributeArray[j], "method" + methodNumber + "@" + i + "@" + j, methodNumber);
}
}
}
}
}