MethodHTML.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.util;

  18. import java.io.FileNotFoundException;
  19. import java.io.PrintWriter;
  20. import java.io.UnsupportedEncodingException;
  21. import java.nio.charset.Charset;

  22. import org.apache.bcel.Const;
  23. import org.apache.bcel.classfile.Attribute;
  24. import org.apache.bcel.classfile.Code;
  25. import org.apache.bcel.classfile.ExceptionTable;
  26. import org.apache.bcel.classfile.Field;
  27. import org.apache.bcel.classfile.Method;
  28. import org.apache.bcel.classfile.Utility;

  29. /**
  30.  * Convert methods and fields into HTML file.
  31.  */
  32. final class MethodHTML {

  33.     private final String className; // name of current class
  34.     private final PrintWriter printWriter; // file to write to
  35.     private final ConstantHTML constantHtml;
  36.     private final AttributeHTML attributeHtml;

  37.     MethodHTML(final String dir, final String className, final Method[] methods, final Field[] fields, final ConstantHTML constantHtml,
  38.         final AttributeHTML attributeHtml, final Charset charset) throws FileNotFoundException, UnsupportedEncodingException {
  39.         this.className = className;
  40.         this.attributeHtml = attributeHtml;
  41.         this.constantHtml = constantHtml;
  42.         try (PrintWriter newPrintWriter = new PrintWriter(dir + className + "_methods.html", charset.name())) {
  43.             printWriter = newPrintWriter;
  44.             printWriter.print("<HTML><head><meta charset=\"");
  45.             printWriter.print(charset.name());
  46.             printWriter.println("\"></head>");
  47.             printWriter.println("<BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
  48.             printWriter.println("<TR><TH ALIGN=LEFT>Access&nbsp;flags</TH><TH ALIGN=LEFT>Type</TH>" + "<TH ALIGN=LEFT>Field&nbsp;name</TH></TR>");
  49.             for (final Field field : fields) {
  50.                 writeField(field);
  51.             }
  52.             printWriter.println("</TABLE>");
  53.             printWriter.println("<TABLE BORDER=0><TR><TH ALIGN=LEFT>Access&nbsp;flags</TH>"
  54.                 + "<TH ALIGN=LEFT>Return&nbsp;type</TH><TH ALIGN=LEFT>Method&nbsp;name</TH>" + "<TH ALIGN=LEFT>Arguments</TH></TR>");
  55.             for (int i = 0; i < methods.length; i++) {
  56.                 writeMethod(methods[i], i);
  57.             }
  58.             printWriter.println("</TABLE></BODY></HTML>");
  59.         }
  60.     }

  61.     /**
  62.      * Print field of class.
  63.      *
  64.      * @param field field to print
  65.      */
  66.     private void writeField(final Field field) {
  67.         final String type = Utility.signatureToString(field.getSignature());
  68.         final String name = field.getName();
  69.         String access = Utility.accessToString(field.getAccessFlags());
  70.         Attribute[] attributes;
  71.         access = Utility.replace(access, " ", "&nbsp;");
  72.         printWriter.print("<TR><TD><FONT COLOR=\"#FF0000\">" + access + "</FONT></TD>\n<TD>" + Class2HTML.referenceType(type) + "</TD><TD><A NAME=\"field"
  73.             + name + "\">" + name + "</A></TD>");
  74.         attributes = field.getAttributes();
  75.         // Write them to the Attributes.html file with anchor "<name>[<i>]"
  76.         for (int i = 0; i < attributes.length; i++) {
  77.             attributeHtml.writeAttribute(attributes[i], name + "@" + i);
  78.         }
  79.         for (int i = 0; i < attributes.length; i++) {
  80.             if (attributes[i].getTag() == Const.ATTR_CONSTANT_VALUE) { // Default value
  81.                 final String str = attributes[i].toString();
  82.                 // Reference attribute in _attributes.html
  83.                 printWriter.print("<TD>= <A HREF=\"" + className + "_attributes.html#" + name + "@" + i + "\" TARGET=\"Attributes\">" + str + "</TD>\n");
  84.                 break;
  85.             }
  86.         }
  87.         printWriter.println("</TR>");
  88.     }

  89.     private void writeMethod(final Method method, final int methodNumber) {
  90.         // Get raw signature
  91.         final String signature = method.getSignature();
  92.         // Get array of strings containing the argument types
  93.         final String[] args = Utility.methodSignatureArgumentTypes(signature, false);
  94.         // Get return type string
  95.         final String type = Utility.methodSignatureReturnType(signature, false);
  96.         // Get method name
  97.         final String name = method.getName();
  98.         String htmlName;
  99.         // Get method's access flags
  100.         String access = Utility.accessToString(method.getAccessFlags());
  101.         // Get the method's attributes, the Code Attribute in particular
  102.         final Attribute[] attributes = method.getAttributes();
  103.         /*
  104.          * HTML doesn't like names like <clinit> and spaces are places to break lines. Both we don't want...
  105.          */
  106.         access = Utility.replace(access, " ", "&nbsp;");
  107.         htmlName = Class2HTML.toHTML(name);
  108.         printWriter.print("<TR VALIGN=TOP><TD><FONT COLOR=\"#FF0000\"><A NAME=method" + methodNumber + ">" + access + "</A></FONT></TD>");
  109.         printWriter.print("<TD>" + Class2HTML.referenceType(type) + "</TD><TD>" + "<A HREF=" + className + "_code.html#method" + methodNumber + " TARGET=Code>"
  110.             + htmlName + "</A></TD>\n<TD>(");
  111.         for (int i = 0; i < args.length; i++) {
  112.             printWriter.print(Class2HTML.referenceType(args[i]));
  113.             if (i < args.length - 1) {
  114.                 printWriter.print(", ");
  115.             }
  116.         }
  117.         printWriter.print(")</TD></TR>");
  118.         // Check for thrown exceptions
  119.         for (int i = 0; i < attributes.length; i++) {
  120.             attributeHtml.writeAttribute(attributes[i], "method" + methodNumber + "@" + i, methodNumber);
  121.             final byte tag = attributes[i].getTag();
  122.             if (tag == Const.ATTR_EXCEPTIONS) {
  123.                 printWriter.print("<TR VALIGN=TOP><TD COLSPAN=2></TD><TH ALIGN=LEFT>throws</TH><TD>");
  124.                 final int[] exceptions = ((ExceptionTable) attributes[i]).getExceptionIndexTable();
  125.                 for (int j = 0; j < exceptions.length; j++) {
  126.                     printWriter.print(constantHtml.referenceConstant(exceptions[j]));
  127.                     if (j < exceptions.length - 1) {
  128.                         printWriter.print(", ");
  129.                     }
  130.                 }
  131.                 printWriter.println("</TD></TR>");
  132.             } else if (tag == Const.ATTR_CODE) {
  133.                 final Attribute[] attributeArray = ((Code) attributes[i]).getAttributes();
  134.                 for (int j = 0; j < attributeArray.length; j++) {
  135.                     attributeHtml.writeAttribute(attributeArray[j], "method" + methodNumber + "@" + i + "@" + j, methodNumber);
  136.                 }
  137.             }
  138.         }
  139.     }
  140. }