forked from apache/commons-bcel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttributeHTML.java
More file actions
192 lines (183 loc) · 9.04 KB
/
Copy pathAttributeHTML.java
File metadata and controls
192 lines (183 loc) · 9.04 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* 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.Closeable;
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.CodeException;
import org.apache.bcel.classfile.ConstantPool;
import org.apache.bcel.classfile.ConstantValue;
import org.apache.bcel.classfile.ExceptionTable;
import org.apache.bcel.classfile.InnerClass;
import org.apache.bcel.classfile.InnerClasses;
import org.apache.bcel.classfile.LineNumber;
import org.apache.bcel.classfile.LineNumberTable;
import org.apache.bcel.classfile.LocalVariableTable;
import org.apache.bcel.classfile.SourceFile;
import org.apache.bcel.classfile.Utility;
/**
* Convert found attributes into HTML file.
*/
final class AttributeHTML implements Closeable {
private final String className; // name of current class
private final PrintWriter printWriter; // file to write to
private int attrCount;
private final ConstantHTML constantHtml;
private final ConstantPool constantPool;
AttributeHTML(final String dir, final String className, final ConstantPool constantPool, final ConstantHTML constantHtml, final Charset charset)
throws FileNotFoundException, UnsupportedEncodingException {
this.className = className;
this.constantPool = constantPool;
this.constantHtml = constantHtml;
printWriter = new PrintWriter(dir + className + "_attributes.html", charset.name());
printWriter.print("<HTML><head><meta charset=\"");
printWriter.print(charset.name());
printWriter.println("\"></head>");
printWriter.println("<BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
}
@Override
public void close() {
printWriter.println("</TABLE></BODY></HTML>");
printWriter.close();
}
private String codeLink(final int link, final int methodNumber) {
return "<A HREF=\"" + className + "_code.html#code" + methodNumber + "@" + link + "\" TARGET=Code>" + link + "</A>";
}
void writeAttribute(final Attribute attribute, final String anchor) {
writeAttribute(attribute, anchor, 0);
}
void writeAttribute(final Attribute attribute, final String anchor, final int methodNumber) {
final byte tag = attribute.getTag();
int index;
if (tag == Const.ATTR_UNKNOWN) {
return;
}
attrCount++; // Increment number of attributes found so far
if (attrCount % 2 == 0) {
printWriter.print("<TR BGCOLOR=\"#C0C0C0\"><TD>");
} else {
printWriter.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
}
printWriter.println("<H4><A NAME=\"" + anchor + "\">" + attrCount + " " + Const.getAttributeName(tag) + "</A></H4>");
/*
* Handle different attributes
*/
switch (tag) {
case Const.ATTR_CODE:
final Code c = (Code) attribute;
// Some directly printable values
printWriter.print("<UL><LI>Maximum stack size = " + c.getMaxStack() + "</LI>\n<LI>Number of local variables = " + c.getMaxLocals()
+ "</LI>\n<LI><A HREF=\"" + className + "_code.html#method" + methodNumber + "\" TARGET=Code>Byte code</A></LI></UL>\n");
// Get handled exceptions and list them
final CodeException[] ce = c.getExceptionTable();
final int len = ce.length;
if (len > 0) {
printWriter.print("<P><B>Exceptions handled</B><UL>");
for (final CodeException cex : ce) {
final int catchType = cex.getCatchType(); // Index in constant pool
printWriter.print("<LI>");
if (catchType != 0) {
printWriter.print(constantHtml.referenceConstant(catchType)); // Create Link to _cp.html
} else {
printWriter.print("Any Exception");
}
printWriter.print("<BR>(Ranging from lines " + codeLink(cex.getStartPC(), methodNumber) + " to " + codeLink(cex.getEndPC(), methodNumber)
+ ", handled at line " + codeLink(cex.getHandlerPC(), methodNumber) + ")</LI>");
}
printWriter.print("</UL>");
}
break;
case Const.ATTR_CONSTANT_VALUE:
index = ((ConstantValue) attribute).getConstantValueIndex();
// Reference _cp.html
printWriter
.print("<UL><LI><A HREF=\"" + className + "_cp.html#cp" + index + "\" TARGET=\"ConstantPool\">Constant value index(" + index + ")</A></UL>\n");
break;
case Const.ATTR_SOURCE_FILE:
index = ((SourceFile) attribute).getSourceFileIndex();
// Reference _cp.html
printWriter
.print("<UL><LI><A HREF=\"" + className + "_cp.html#cp" + index + "\" TARGET=\"ConstantPool\">Source file index(" + index + ")</A></UL>\n");
break;
case Const.ATTR_EXCEPTIONS:
// List thrown exceptions
final int[] indices = ((ExceptionTable) attribute).getExceptionIndexTable();
printWriter.print("<UL>");
for (final int indice : indices) {
printWriter
.print("<LI><A HREF=\"" + className + "_cp.html#cp" + indice + "\" TARGET=\"ConstantPool\">Exception class index(" + indice + ")</A>\n");
}
printWriter.print("</UL>\n");
break;
case Const.ATTR_LINE_NUMBER_TABLE:
final LineNumber[] lineNumbers = ((LineNumberTable) attribute).getLineNumberTable();
// List line number pairs
printWriter.print("<P>");
for (int i = 0; i < lineNumbers.length; i++) {
printWriter.print("(" + lineNumbers[i].getStartPC() + ", " + lineNumbers[i].getLineNumber() + ")");
if (i < lineNumbers.length - 1) {
printWriter.print(", "); // breakable
}
}
break;
case Const.ATTR_LOCAL_VARIABLE_TABLE:
// List name, range and type
printWriter.print("<UL>");
((LocalVariableTable) attribute).forEach(var -> {
final int sigIdx = var.getSignatureIndex();
String signature = constantPool.getConstantUtf8(sigIdx).getBytes();
signature = Utility.signatureToString(signature, false);
final int start = var.getStartPC();
final int end = start + var.getLength();
printWriter.println("<LI>" + Class2HTML.referenceType(signature) + " <B>" + Class2HTML.toHTML(var.getName()) + "</B> in slot %" + var.getIndex()
+ "<BR>Valid from lines <A HREF=\"" + className + "_code.html#code" + methodNumber + "@" + start + "\" TARGET=Code>" + start
+ "</A> to <A HREF=\"" + className + "_code.html#code" + methodNumber + "@" + end + "\" TARGET=Code>" + end + "</A></LI>");
});
printWriter.print("</UL>\n");
break;
case Const.ATTR_INNER_CLASSES:
// List inner classes
printWriter.print("<UL>");
for (final InnerClass clazz : ((InnerClasses) attribute).getInnerClasses()) {
final String name;
final String access;
index = clazz.getInnerNameIndex();
if (index > 0) {
name = Class2HTML.toHTML(constantPool.getConstantUtf8(index).getBytes());
} else {
name = "<anonymous>";
}
access = Utility.accessToString(clazz.getInnerAccessFlags());
printWriter.print("<LI><FONT COLOR=\"#FF0000\">" + access + "</FONT> " + constantHtml.referenceConstant(clazz.getInnerClassIndex())
+ " in class " + constantHtml.referenceConstant(clazz.getOuterClassIndex()) + " named " + name + "</LI>\n");
}
printWriter.print("</UL>\n");
break;
default: // Such as Unknown attribute or Deprecated
printWriter.print("<P>" + attribute);
}
printWriter.println("</TD></TR>");
printWriter.flush();
}
}