~ubuntu-branches/ubuntu/natty/aspectj/natty

« back to all changes in this revision

Viewing changes to org.aspectj/modules/bcel-builder/src/org/aspectj/apache/bcel/util/AttributeHTML.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-10-04 16:37:23 UTC
  • mfrom: (1.1.3 upstream) (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20091004163723-ck4y7j7fhjxskkie
Tags: 1.6.6+dfsg-1
* New upstream release.
  - Update 02_use_gjdoc.diff patch
* Update my email address

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.aspectj.apache.bcel.util;
2
 
 
3
 
/* ====================================================================
4
 
 * The Apache Software License, Version 1.1
5
 
 *
6
 
 * Copyright (c) 2001 The Apache Software Foundation.  All rights
7
 
 * reserved.
8
 
 *
9
 
 * Redistribution and use in source and binary forms, with or without
10
 
 * modification, are permitted provided that the following conditions
11
 
 * are met:
12
 
 *
13
 
 * 1. Redistributions of source code must retain the above copyright
14
 
 *    notice, this list of conditions and the following disclaimer.
15
 
 *
16
 
 * 2. Redistributions in binary form must reproduce the above copyright
17
 
 *    notice, this list of conditions and the following disclaimer in
18
 
 *    the documentation and/or other materials provided with the
19
 
 *    distribution.
20
 
 *
21
 
 * 3. The end-user documentation included with the redistribution,
22
 
 *    if any, must include the following acknowledgment:
23
 
 *       "This product includes software developed by the
24
 
 *        Apache Software Foundation (http://www.apache.org/)."
25
 
 *    Alternately, this acknowledgment may appear in the software itself,
26
 
 *    if and wherever such third-party acknowledgments normally appear.
27
 
 *
28
 
 * 4. The names "Apache" and "Apache Software Foundation" and
29
 
 *    "Apache BCEL" must not be used to endorse or promote products
30
 
 *    derived from this software without prior written permission. For
31
 
 *    written permission, please contact apache@apache.org.
32
 
 *
33
 
 * 5. Products derived from this software may not be called "Apache",
34
 
 *    "Apache BCEL", nor may "Apache" appear in their name, without
35
 
 *    prior written permission of the Apache Software Foundation.
36
 
 *
37
 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38
 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39
 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40
 
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41
 
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42
 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43
 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44
 
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45
 
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46
 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47
 
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48
 
 * SUCH DAMAGE.
49
 
 * ====================================================================
50
 
 *
51
 
 * This software consists of voluntary contributions made by many
52
 
 * individuals on behalf of the Apache Software Foundation.  For more
53
 
 * information on the Apache Software Foundation, please see
54
 
 * <http://www.apache.org/>.
55
 
 */
56
 
 
57
 
import java.io.FileOutputStream;
58
 
import java.io.IOException;
59
 
import java.io.PrintWriter;
60
 
 
61
 
import org.aspectj.apache.bcel.classfile.Attribute;
62
 
import org.aspectj.apache.bcel.classfile.Code;
63
 
import org.aspectj.apache.bcel.classfile.CodeException;
64
 
import org.aspectj.apache.bcel.classfile.ConstantPool;
65
 
import org.aspectj.apache.bcel.classfile.ConstantUtf8;
66
 
import org.aspectj.apache.bcel.classfile.ConstantValue;
67
 
import org.aspectj.apache.bcel.classfile.ExceptionTable;
68
 
import org.aspectj.apache.bcel.classfile.InnerClass;
69
 
import org.aspectj.apache.bcel.classfile.InnerClasses;
70
 
import org.aspectj.apache.bcel.classfile.LineNumber;
71
 
import org.aspectj.apache.bcel.classfile.LineNumberTable;
72
 
import org.aspectj.apache.bcel.classfile.LocalVariable;
73
 
import org.aspectj.apache.bcel.classfile.LocalVariableTable;
74
 
import org.aspectj.apache.bcel.classfile.SourceFile;
75
 
import org.aspectj.apache.bcel.classfile.Utility;
76
 
 
77
 
/**
78
 
 * Convert found attributes into HTML file.
79
 
 *
80
 
 * @version $Id: AttributeHTML.java,v 1.5 2007/02/15 14:43:16 aclement Exp $
81
 
 * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
82
 
 * 
83
 
 */
84
 
final class AttributeHTML implements org.aspectj.apache.bcel.Constants {
85
 
  private String       class_name;     // name of current class
86
 
  private PrintWriter  file;                                    // file to write to
87
 
  private int          attr_count = 0;
88
 
  private ConstantHTML constant_html;
89
 
  private ConstantPool constant_pool;
90
 
 
91
 
  AttributeHTML(String dir, String class_name, ConstantPool constant_pool,
92
 
                ConstantHTML constant_html) throws IOException
93
 
  {
94
 
    this.class_name    = class_name;
95
 
    this.constant_pool = constant_pool;
96
 
    this.constant_html = constant_html;
97
 
 
98
 
    file = new PrintWriter(new FileOutputStream(dir + class_name + "_attributes.html"));
99
 
    file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
100
 
  }                                      
101
 
 
102
 
  private final String codeLink(int link, int method_number) {
103
 
    return "<A HREF=\"" + class_name + "_code.html#code" +
104
 
      method_number + "@" + link + "\" TARGET=Code>" +
105
 
      link + "</A>";
106
 
  }
107
 
    
108
 
  final void close() {  
109
 
    file.println("</TABLE></BODY></HTML>");
110
 
    file.close();
111
 
  }
112
 
 
113
 
  final void writeAttribute(Attribute attribute, String anchor) throws IOException {    
114
 
    writeAttribute(attribute, anchor, 0);
115
 
  }
116
 
 
117
 
  final void writeAttribute(Attribute attribute, String anchor, int method_number) throws IOException { 
118
 
    byte         tag = attribute.getTag();
119
 
    int        index;
120
 
 
121
 
    if(tag == ATTR_UNKNOWN) // Don't know what to do about this one
122
 
      return;
123
 
 
124
 
    attr_count++; // Increment number of attributes found so far
125
 
 
126
 
    if(attr_count % 2 == 0)
127
 
      file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>");
128
 
    else
129
 
      file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
130
 
 
131
 
    file.println("<H4><A NAME=\"" + anchor + "\">" + attr_count + " " + ATTRIBUTE_NAMES[tag] + "</A></H4>");
132
 
 
133
 
    /* Handle different attributes
134
 
     */
135
 
    switch(tag) {
136
 
    case ATTR_CODE:
137
 
      Code        c          = (Code)attribute;
138
 
 
139
 
      // Some directly printable values
140
 
      file.print("<UL><LI>Maximum stack size = " + c.getMaxStack() + 
141
 
                 "</LI>\n<LI>Number of local variables = " + 
142
 
                 c.getMaxLocals() + "</LI>\n<LI><A HREF=\"" + class_name +
143
 
                 "_code.html#method" + method_number + "\" TARGET=Code>Byte code</A></LI></UL>\n");
144
 
 
145
 
      // Get handled exceptions and list them
146
 
      CodeException[] ce  = c.getExceptionTable();
147
 
      int             len = ce.length;
148
 
 
149
 
      if(len > 0) {
150
 
        file.print("<P><B>Exceptions handled</B><UL>");
151
 
 
152
 
        for(int i=0; i < len; i++) {
153
 
          int catch_type = ce[i].getCatchType(); // Index in constant pool
154
 
        
155
 
          file.print("<LI>");
156
 
 
157
 
          if(catch_type != 0)
158
 
            file.print(constant_html.referenceConstant(catch_type)); // Create Link to _cp.html
159
 
          else
160
 
            file.print("Any Exception");
161
 
 
162
 
          file.print("<BR>(Ranging from lines " + codeLink(ce[i].getStartPC(), method_number) +
163
 
                     " to " + codeLink(ce[i].getEndPC(), method_number) + ", handled at line " + 
164
 
                     codeLink(ce[i].getHandlerPC(), method_number) + ")</LI>");
165
 
        }
166
 
        file.print("</UL>");
167
 
      }
168
 
      break;
169
 
 
170
 
    case ATTR_CONSTANT_VALUE:
171
 
      index = ((ConstantValue)attribute).getConstantValueIndex();
172
 
 
173
 
      // Reference _cp.html
174
 
      file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index + 
175
 
                 "\" TARGET=\"ConstantPool\">Constant value index(" + index +")</A></UL>\n");
176
 
      break;
177
 
 
178
 
    case ATTR_SOURCE_FILE:
179
 
      index = ((SourceFile)attribute).getSourceFileIndex();
180
 
 
181
 
      // Reference _cp.html
182
 
      file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index + 
183
 
                 "\" TARGET=\"ConstantPool\">Source file index(" + index +")</A></UL>\n");
184
 
      break;
185
 
          
186
 
    case ATTR_EXCEPTIONS:
187
 
      // List thrown exceptions
188
 
      int[] indices = ((ExceptionTable)attribute).getExceptionIndexTable();
189
 
 
190
 
      file.print("<UL>");
191
 
 
192
 
      for(int i=0; i < indices.length; i++)
193
 
        file.print("<LI><A HREF=\"" + class_name + "_cp.html#cp" + indices[i] +
194
 
                   "\" TARGET=\"ConstantPool\">Exception class index(" + indices[i] + ")</A>\n");
195
 
 
196
 
      file.print("</UL>\n");
197
 
      break;
198
 
 
199
 
    case ATTR_LINE_NUMBER_TABLE:
200
 
      LineNumber[] line_numbers =((LineNumberTable)attribute).getLineNumberTable();
201
 
 
202
 
      // List line number pairs
203
 
      file.print("<P>");
204
 
 
205
 
      for(int i=0; i < line_numbers.length; i++) {
206
 
        file.print("(" + line_numbers[i].getStartPC() + ",&nbsp;" + line_numbers[i].getLineNumber() + ")");
207
 
 
208
 
        if(i < line_numbers.length - 1)
209
 
          file.print(", "); // breakable
210
 
      }
211
 
      break;
212
 
 
213
 
    case ATTR_LOCAL_VARIABLE_TABLE:
214
 
      LocalVariable[] vars = ((LocalVariableTable)attribute).getLocalVariableTable();
215
 
 
216
 
      // List name, range and type
217
 
      file.print("<UL>");
218
 
 
219
 
      for(int i=0; i < vars.length; i++) {
220
 
        index = vars[i].getSignatureIndex();
221
 
        String signature = ((ConstantUtf8)constant_pool.getConstant(index, CONSTANT_Utf8)).getBytes();
222
 
        signature = Utility.signatureToString(signature, false);
223
 
        int  start = vars[i].getStartPC();
224
 
        int  end   = (start + vars[i].getLength());
225
 
 
226
 
        file.println("<LI>" + Class2HTML.referenceType(signature) +
227
 
                     "&nbsp;<B>" + vars[i].getName() + "</B> in slot %" + vars[i].getIndex() +
228
 
                     "<BR>Valid from lines " +
229
 
                     "<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + start + "\" TARGET=Code>" +
230
 
                     start + "</A> to " +
231
 
                     "<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + end + "\" TARGET=Code>" +
232
 
                     end + "</A></LI>");
233
 
      }
234
 
      file.print("</UL>\n");
235
 
 
236
 
      break;
237
 
 
238
 
    case ATTR_INNER_CLASSES:
239
 
      InnerClass[] classes = ((InnerClasses)attribute).getInnerClasses();
240
 
 
241
 
      // List inner classes
242
 
      file.print("<UL>");
243
 
 
244
 
      for(int i=0; i < classes.length; i++) {
245
 
        String name, access;
246
 
        
247
 
        index = classes[i].getInnerNameIndex();
248
 
        if(index > 0)
249
 
          name =((ConstantUtf8)constant_pool.getConstant(index, CONSTANT_Utf8)).getBytes();
250
 
        else
251
 
          name = "&lt;anonymous&gt;";
252
 
 
253
 
        access = Utility.accessToString(classes[i].getInnerAccessFlags());
254
 
 
255
 
        file.print("<LI><FONT COLOR=\"#FF0000\">" + access + "</FONT> "+
256
 
                   constant_html.referenceConstant(classes[i].getInnerClassIndex()) +
257
 
                   " in&nbsp;class " + 
258
 
                   constant_html.referenceConstant(classes[i].getOuterClassIndex()) +
259
 
                   " named " + name + "</LI>\n");
260
 
      }
261
 
 
262
 
      file.print("</UL>\n");
263
 
      break;
264
 
 
265
 
    default: // Such as Unknown attribute or Deprecated
266
 
      file.print("<P>" + attribute.toString());
267
 
    }
268
 
 
269
 
    file.println("</TD></TR>");
270
 
    file.flush();
271
 
  }                  
272
 
}