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