forked from apache/commons-bcel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodHTML.java
More file actions
148 lines (141 loc) · 7.1 KB
/
Copy pathMethodHTML.java
File metadata and controls
148 lines (141 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.bcel.util;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import org.apache.bcel.Const;
import org.apache.bcel.classfile.Attribute;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.ExceptionTable;
import org.apache.bcel.classfile.Field;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.classfile.Utility;
/**
* Convert methods and fields into HTML file.
*/
final class MethodHTML {
private final String className; // name of current class
private final PrintWriter printWriter; // file to write to
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())) {
printWriter = newPrintWriter;
printWriter.print("<HTML><head><meta charset=\"");
printWriter.print(charset.name());
printWriter.println("\"></head>");
printWriter.println("<BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
printWriter.println("<TR><TH ALIGN=LEFT>Access flags</TH><TH ALIGN=LEFT>Type</TH><TH ALIGN=LEFT>Field name</TH></TR>");
for (final Field field : fields) {
writeField(field);
}
printWriter.println("</TABLE>");
printWriter.println("<TABLE BORDER=0><TR><TH ALIGN=LEFT>Access flags</TH>"
+ "<TH ALIGN=LEFT>Return type</TH><TH ALIGN=LEFT>Method name</TH><TH ALIGN=LEFT>Arguments</TH></TR>");
for (int i = 0; i < methods.length; i++) {
writeMethod(methods[i], i);
}
printWriter.println("</TABLE></BODY></HTML>");
}
}
/**
* Print field of class.
*
* @param field field to print.
*/
private void writeField(final Field field) {
final String type = Utility.signatureToString(field.getSignature());
final String name = field.getName();
String access = Utility.accessToString(field.getAccessFlags());
final Attribute[] attributes;
access = Utility.replace(access, " ", " ");
printWriter.print("<TR><TD><FONT COLOR=\"#FF0000\">" + access + "</FONT></TD>\n<TD>" + Class2HTML.referenceType(type) + "</TD><TD><A NAME=\"field"
+ name + "\">" + Class2HTML.toHTML(name) + "</A></TD>");
attributes = field.getAttributes();
// Write them to the Attributes.html file with anchor "<name>[<i>]"
for (int i = 0; i < attributes.length; i++) {
attributeHtml.writeAttribute(attributes[i], name + "@" + i);
}
for (int i = 0; i < attributes.length; i++) {
if (attributes[i].getTag() == Const.ATTR_CONSTANT_VALUE) { // Default value
final String str = attributes[i].toString();
// Reference attribute in _attributes.html
printWriter.print("<TD>= <A HREF=\"" + className + "_attributes.html#" + name + "@" + i + "\" TARGET=\"Attributes\">" + str + "</TD>\n");
break;
}
}
printWriter.println("</TR>");
}
private void writeMethod(final Method method, final int methodNumber) {
// Get raw signature
final String signature = method.getSignature();
// Get array of strings containing the argument types
final String[] args = Utility.methodSignatureArgumentTypes(signature, false);
// Get return type string
final String type = Utility.methodSignatureReturnType(signature, false);
// Get method name
final String name = method.getName();
// Get method's access flags
String access = Utility.accessToString(method.getAccessFlags());
// Get the method's attributes, the Code Attribute in particular
final Attribute[] attributes = method.getAttributes();
/*
* HTML doesn't like names like <clinit> and spaces are places to break lines. Both we don't want...
*/
access = Utility.replace(access, " ", " ");
final String htmlName = Class2HTML.toHTML(name);
printWriter.print("<TR VALIGN=TOP><TD><FONT COLOR=\"#FF0000\"><A NAME=method" + methodNumber + ">" + access + "</A></FONT></TD>");
printWriter.print("<TD>" + Class2HTML.referenceType(type) + "</TD><TD><A HREF=" + className + "_code.html#method" + methodNumber + " TARGET=Code>"
+ htmlName + "</A></TD>\n<TD>(");
for (int i = 0; i < args.length; i++) {
printWriter.print(Class2HTML.referenceType(args[i]));
if (i < args.length - 1) {
printWriter.print(", ");
}
}
printWriter.print(")</TD></TR>");
// Check for thrown exceptions
for (int i = 0; i < attributes.length; i++) {
attributeHtml.writeAttribute(attributes[i], "method" + methodNumber + "@" + i, methodNumber);
final byte tag = attributes[i].getTag();
if (tag == Const.ATTR_EXCEPTIONS) {
printWriter.print("<TR VALIGN=TOP><TD COLSPAN=2></TD><TH ALIGN=LEFT>throws</TH><TD>");
final int[] exceptions = ((ExceptionTable) attributes[i]).getExceptionIndexTable();
for (int j = 0; j < exceptions.length; j++) {
printWriter.print(constantHtml.referenceConstant(exceptions[j]));
if (j < exceptions.length - 1) {
printWriter.print(", ");
}
}
printWriter.println("</TD></TR>");
} else if (tag == Const.ATTR_CODE) {
final Attribute[] attributeArray = ((Code) attributes[i]).getAttributes();
for (int j = 0; j < attributeArray.length; j++) {
attributeHtml.writeAttribute(attributeArray[j], "method" + methodNumber + "@" + i + "@" + j, methodNumber);
}
}
}
}
}