1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.util; 23 24 import java.io.FileOutputStream; 25 import java.io.IOException; 26 import java.io.PrintWriter; 27 28 import com.sun.org.apache.bcel.internal.Const; 29 import com.sun.org.apache.bcel.internal.classfile.Attribute; 30 import com.sun.org.apache.bcel.internal.classfile.Code; 31 import com.sun.org.apache.bcel.internal.classfile.CodeException; 32 import com.sun.org.apache.bcel.internal.classfile.ConstantPool; 33 import com.sun.org.apache.bcel.internal.classfile.ConstantUtf8; 34 import com.sun.org.apache.bcel.internal.classfile.ConstantValue; 35 import com.sun.org.apache.bcel.internal.classfile.ExceptionTable; 36 import com.sun.org.apache.bcel.internal.classfile.InnerClass; 37 import com.sun.org.apache.bcel.internal.classfile.InnerClasses; 38 import com.sun.org.apache.bcel.internal.classfile.LineNumber; 39 import com.sun.org.apache.bcel.internal.classfile.LineNumberTable; 40 import com.sun.org.apache.bcel.internal.classfile.LocalVariable; 41 import com.sun.org.apache.bcel.internal.classfile.LocalVariableTable; 42 import com.sun.org.apache.bcel.internal.classfile.SourceFile; 43 import com.sun.org.apache.bcel.internal.classfile.Utility; 44 45 /** 46 * Convert found attributes into HTML file. 47 * 48 * 49 */ 50 final class AttributeHTML { 51 52 private final String class_name; // name of current class 53 private final PrintWriter file; // file to write to 54 private int attr_count = 0; 55 private final ConstantHTML constant_html; 56 private final ConstantPool constant_pool; 57 58 AttributeHTML(final String dir, final String class_name, final ConstantPool constant_pool, final ConstantHTML constant_html)59 AttributeHTML(final String dir, final String class_name, final ConstantPool constant_pool, 60 final ConstantHTML constant_html) throws IOException { 61 this.class_name = class_name; 62 this.constant_pool = constant_pool; 63 this.constant_html = constant_html; 64 file = new PrintWriter(new FileOutputStream(dir + class_name + "_attributes.html")); 65 file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>"); 66 } 67 68 codeLink( final int link, final int method_number )69 private String codeLink( final int link, final int method_number ) { 70 return "<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + link 71 + "\" TARGET=Code>" + link + "</A>"; 72 } 73 74 close()75 void close() { 76 file.println("</TABLE></BODY></HTML>"); 77 file.close(); 78 } 79 80 writeAttribute( final Attribute attribute, final String anchor )81 void writeAttribute( final Attribute attribute, final String anchor ) { 82 writeAttribute(attribute, anchor, 0); 83 } 84 85 writeAttribute( final Attribute attribute, final String anchor, final int method_number )86 void writeAttribute( final Attribute attribute, final String anchor, final int method_number ) { 87 final byte tag = attribute.getTag(); 88 int index; 89 if (tag == Const.ATTR_UNKNOWN) { 90 return; 91 } 92 attr_count++; // Increment number of attributes found so far 93 if (attr_count % 2 == 0) { 94 file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>"); 95 } else { 96 file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>"); 97 } 98 file.println("<H4><A NAME=\"" + anchor + "\">" + attr_count + " " + Const.getAttributeName(tag) 99 + "</A></H4>"); 100 /* Handle different attributes 101 */ 102 switch (tag) { 103 case Const.ATTR_CODE: 104 final Code c = (Code) attribute; 105 // Some directly printable values 106 file.print("<UL><LI>Maximum stack size = " + c.getMaxStack() 107 + "</LI>\n<LI>Number of local variables = " + c.getMaxLocals() 108 + "</LI>\n<LI><A HREF=\"" + class_name + "_code.html#method" 109 + method_number + "\" TARGET=Code>Byte code</A></LI></UL>\n"); 110 // Get handled exceptions and list them 111 final CodeException[] ce = c.getExceptionTable(); 112 final int len = ce.length; 113 if (len > 0) { 114 file.print("<P><B>Exceptions handled</B><UL>"); 115 for (final CodeException cex : ce) { 116 final int catch_type = cex.getCatchType(); // Index in constant pool 117 file.print("<LI>"); 118 if (catch_type != 0) { 119 file.print(constant_html.referenceConstant(catch_type)); // Create Link to _cp.html 120 } else { 121 file.print("Any Exception"); 122 } 123 file.print("<BR>(Ranging from lines " 124 + codeLink(cex.getStartPC(), method_number) + " to " 125 + codeLink(cex.getEndPC(), method_number) + ", handled at line " 126 + codeLink(cex.getHandlerPC(), method_number) + ")</LI>"); 127 } 128 file.print("</UL>"); 129 } 130 break; 131 case Const.ATTR_CONSTANT_VALUE: 132 index = ((ConstantValue) attribute).getConstantValueIndex(); 133 // Reference _cp.html 134 file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index 135 + "\" TARGET=\"ConstantPool\">Constant value index(" + index 136 + ")</A></UL>\n"); 137 break; 138 case Const.ATTR_SOURCE_FILE: 139 index = ((SourceFile) attribute).getSourceFileIndex(); 140 // Reference _cp.html 141 file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index 142 + "\" TARGET=\"ConstantPool\">Source file index(" + index + ")</A></UL>\n"); 143 break; 144 case Const.ATTR_EXCEPTIONS: 145 // List thrown exceptions 146 final int[] indices = ((ExceptionTable) attribute).getExceptionIndexTable(); 147 file.print("<UL>"); 148 for (final int indice : indices) { 149 file.print("<LI><A HREF=\"" + class_name + "_cp.html#cp" + indice 150 + "\" TARGET=\"ConstantPool\">Exception class index(" + indice 151 + ")</A>\n"); 152 } 153 file.print("</UL>\n"); 154 break; 155 case Const.ATTR_LINE_NUMBER_TABLE: 156 final LineNumber[] line_numbers = ((LineNumberTable) attribute).getLineNumberTable(); 157 // List line number pairs 158 file.print("<P>"); 159 for (int i = 0; i < line_numbers.length; i++) { 160 file.print("(" + line_numbers[i].getStartPC() + ", " 161 + line_numbers[i].getLineNumber() + ")"); 162 if (i < line_numbers.length - 1) { 163 file.print(", "); // breakable 164 } 165 } 166 break; 167 case Const.ATTR_LOCAL_VARIABLE_TABLE: 168 final LocalVariable[] vars = ((LocalVariableTable) attribute).getLocalVariableTable(); 169 // List name, range and type 170 file.print("<UL>"); 171 for (final LocalVariable var : vars) { 172 index = var.getSignatureIndex(); 173 String signature = ((ConstantUtf8) constant_pool.getConstant(index, 174 Const.CONSTANT_Utf8)).getBytes(); 175 signature = Utility.signatureToString(signature, false); 176 final int start = var.getStartPC(); 177 final int end = start + var.getLength(); 178 file.println("<LI>" + Class2HTML.referenceType(signature) + " <B>" 179 + var.getName() + "</B> in slot %" + var.getIndex() 180 + "<BR>Valid from lines " + "<A HREF=\"" + class_name 181 + "_code.html#code" + method_number + "@" + start + "\" TARGET=Code>" 182 + start + "</A> to " + "<A HREF=\"" + class_name + "_code.html#code" 183 + method_number + "@" + end + "\" TARGET=Code>" + end + "</A></LI>"); 184 } 185 file.print("</UL>\n"); 186 break; 187 case Const.ATTR_INNER_CLASSES: 188 final InnerClass[] classes = ((InnerClasses) attribute).getInnerClasses(); 189 // List inner classes 190 file.print("<UL>"); 191 for (final InnerClass classe : classes) { 192 String name; 193 String access; 194 index = classe.getInnerNameIndex(); 195 if (index > 0) { 196 name = ((ConstantUtf8) constant_pool.getConstant(index, Const.CONSTANT_Utf8)) 197 .getBytes(); 198 } else { 199 name = "<anonymous>"; 200 } 201 access = Utility.accessToString(classe.getInnerAccessFlags()); 202 file.print("<LI><FONT COLOR=\"#FF0000\">" + access + "</FONT> " 203 + constant_html.referenceConstant(classe.getInnerClassIndex()) 204 + " in class " 205 + constant_html.referenceConstant(classe.getOuterClassIndex()) 206 + " named " + name + "</LI>\n"); 207 } 208 file.print("</UL>\n"); 209 break; 210 default: // Such as Unknown attribute or Deprecated 211 file.print("<P>" + attribute); 212 } 213 file.println("</TD></TR>"); 214 file.flush(); 215 } 216 } 217