1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.util;
20
21 import java.io.FileNotFoundException;
22 import java.io.PrintWriter;
23 import java.io.UnsupportedEncodingException;
24 import java.nio.charset.Charset;
25
26 import org.apache.bcel.Const;
27 import org.apache.bcel.classfile.Attribute;
28 import org.apache.bcel.classfile.Code;
29 import org.apache.bcel.classfile.ExceptionTable;
30 import org.apache.bcel.classfile.Field;
31 import org.apache.bcel.classfile.Method;
32 import org.apache.bcel.classfile.Utility;
33
34
35
36
37 final class MethodHTML {
38
39 private final String className;
40 private final PrintWriter printWriter;
41 private final ConstantHTML constantHtml;
42 private final AttributeHTML attributeHtml;
43
44 MethodHTML(final String dir, final String className, final Method[] methods, final Field[] fields, final ConstantHTML constantHtml,
45 final AttributeHTML attributeHtml, final Charset charset) throws FileNotFoundException, UnsupportedEncodingException {
46 this.className = className;
47 this.attributeHtml = attributeHtml;
48 this.constantHtml = constantHtml;
49 try (PrintWriter newPrintWriter = new PrintWriter(dir + className + "_methods.html", charset.name())) {
50 printWriter = newPrintWriter;
51 printWriter.print("<HTML><head><meta charset=\"");
52 printWriter.print(charset.name());
53 printWriter.println("\"></head>");
54 printWriter.println("<BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
55 printWriter.println("<TR><TH ALIGN=LEFT>Access flags</TH><TH ALIGN=LEFT>Type</TH><TH ALIGN=LEFT>Field name</TH></TR>");
56 for (final Field field : fields) {
57 writeField(field);
58 }
59 printWriter.println("</TABLE>");
60 printWriter.println("<TABLE BORDER=0><TR><TH ALIGN=LEFT>Access flags</TH>"
61 + "<TH ALIGN=LEFT>Return type</TH><TH ALIGN=LEFT>Method name</TH><TH ALIGN=LEFT>Arguments</TH></TR>");
62 for (int i = 0; i < methods.length; i++) {
63 writeMethod(methods[i], i);
64 }
65 printWriter.println("</TABLE></BODY></HTML>");
66 }
67 }
68
69
70
71
72
73
74 private void writeField(final Field field) {
75 final String type = Utility.signatureToString(field.getSignature());
76 final String name = field.getName();
77 String access = Utility.accessToString(field.getAccessFlags());
78 final Attribute[] attributes;
79 access = Utility.replace(access, " ", " ");
80 printWriter.print("<TR><TD><FONT COLOR=\"#FF0000\">" + access + "</FONT></TD>\n<TD>" + Class2HTML.referenceType(type) + "</TD><TD><A NAME=\"field"
81 + name + "\">" + name + "</A></TD>");
82 attributes = field.getAttributes();
83
84 for (int i = 0; i < attributes.length; i++) {
85 attributeHtml.writeAttribute(attributes[i], name + "@" + i);
86 }
87 for (int i = 0; i < attributes.length; i++) {
88 if (attributes[i].getTag() == Const.ATTR_CONSTANT_VALUE) {
89 final String str = attributes[i].toString();
90
91 printWriter.print("<TD>= <A HREF=\"" + className + "_attributes.html#" + name + "@" + i + "\" TARGET=\"Attributes\">" + str + "</TD>\n");
92 break;
93 }
94 }
95 printWriter.println("</TR>");
96 }
97
98 private void writeMethod(final Method method, final int methodNumber) {
99
100 final String signature = method.getSignature();
101
102 final String[] args = Utility.methodSignatureArgumentTypes(signature, false);
103
104 final String type = Utility.methodSignatureReturnType(signature, false);
105
106 final String name = method.getName();
107
108 String access = Utility.accessToString(method.getAccessFlags());
109
110 final Attribute[] attributes = method.getAttributes();
111
112
113
114 access = Utility.replace(access, " ", " ");
115 final String htmlName = Class2HTML.toHTML(name);
116 printWriter.print("<TR VALIGN=TOP><TD><FONT COLOR=\"#FF0000\"><A NAME=method" + methodNumber + ">" + access + "</A></FONT></TD>");
117 printWriter.print("<TD>" + Class2HTML.referenceType(type) + "</TD><TD><A HREF=" + className + "_code.html#method" + methodNumber + " TARGET=Code>"
118 + htmlName + "</A></TD>\n<TD>(");
119 for (int i = 0; i < args.length; i++) {
120 printWriter.print(Class2HTML.referenceType(args[i]));
121 if (i < args.length - 1) {
122 printWriter.print(", ");
123 }
124 }
125 printWriter.print(")</TD></TR>");
126
127 for (int i = 0; i < attributes.length; i++) {
128 attributeHtml.writeAttribute(attributes[i], "method" + methodNumber + "@" + i, methodNumber);
129 final byte tag = attributes[i].getTag();
130 if (tag == Const.ATTR_EXCEPTIONS) {
131 printWriter.print("<TR VALIGN=TOP><TD COLSPAN=2></TD><TH ALIGN=LEFT>throws</TH><TD>");
132 final int[] exceptions = ((ExceptionTable) attributes[i]).getExceptionIndexTable();
133 for (int j = 0; j < exceptions.length; j++) {
134 printWriter.print(constantHtml.referenceConstant(exceptions[j]));
135 if (j < exceptions.length - 1) {
136 printWriter.print(", ");
137 }
138 }
139 printWriter.println("</TD></TR>");
140 } else if (tag == Const.ATTR_CODE) {
141 final Attribute[] attributeArray = ((Code) attributes[i]).getAttributes();
142 for (int j = 0; j < attributeArray.length; j++) {
143 attributeHtml.writeAttribute(attributeArray[j], "method" + methodNumber + "@" + i + "@" + j, methodNumber);
144 }
145 }
146 }
147 }
148 }