This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-bcel.git
commit 5a4b4fc17be8df08871649f87a16bdc8054f13ed Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Sun May 1 14:45:53 2022 -0400 Fix SpotBugs [ERROR] High: Found reliance on default encoding --- .../java/org/apache/bcel/util/AttributeHTML.java | 73 +++++++++++----------- src/main/java/org/apache/bcel/util/Class2HTML.java | 21 ++++--- src/main/java/org/apache/bcel/util/CodeHTML.java | 49 ++++++++------- .../java/org/apache/bcel/util/ConstantHTML.java | 39 ++++++------ src/main/java/org/apache/bcel/util/MethodHTML.java | 48 +++++++------- .../org/apache/bcel/util/Class2HTMLTestCase.java | 3 +- 6 files changed, 120 insertions(+), 113 deletions(-) diff --git a/src/main/java/org/apache/bcel/util/AttributeHTML.java b/src/main/java/org/apache/bcel/util/AttributeHTML.java index f44771ab..27728944 100644 --- a/src/main/java/org/apache/bcel/util/AttributeHTML.java +++ b/src/main/java/org/apache/bcel/util/AttributeHTML.java @@ -17,9 +17,9 @@ */ package org.apache.bcel.util; -import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; +import java.nio.charset.Charset; import org.apache.bcel.Const; import org.apache.bcel.classfile.Attribute; @@ -40,31 +40,32 @@ import org.apache.bcel.classfile.Utility; /** * Convert found attributes into HTML file. - * - * */ final class AttributeHTML { private final String class_name; // name of current class - private final PrintWriter file; // file to write to + private final PrintWriter printWriter; // file to write to private int attr_count; private final ConstantHTML constant_html; private final ConstantPool constant_pool; AttributeHTML(final String dir, final String class_name, final ConstantPool constant_pool, - final ConstantHTML constant_html) throws IOException { + final ConstantHTML constant_html, final Charset charset) throws IOException { this.class_name = class_name; this.constant_pool = constant_pool; this.constant_html = constant_html; - file = new PrintWriter(new FileOutputStream(dir + class_name + "_attributes.html")); - file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>"); + printWriter = new PrintWriter(dir + class_name + "_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>"); } void close() { - file.println("</TABLE></BODY></HTML>"); - file.close(); + printWriter.println("</TABLE></BODY></HTML>"); + printWriter.close(); } @@ -87,11 +88,11 @@ final class AttributeHTML { } attr_count++; // Increment number of attributes found so far if (attr_count % 2 == 0) { - file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>"); + printWriter.print("<TR BGCOLOR=\"#C0C0C0\"><TD>"); } else { - file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>"); + printWriter.print("<TR BGCOLOR=\"#A0A0A0\"><TD>"); } - file.println("<H4><A NAME=\"" + anchor + "\">" + attr_count + " " + Const.getAttributeName(tag) + printWriter.println("<H4><A NAME=\"" + anchor + "\">" + attr_count + " " + Const.getAttributeName(tag) + "</A></H4>"); /* Handle different attributes */ @@ -99,7 +100,7 @@ final class AttributeHTML { case Const.ATTR_CODE: final Code c = (Code) attribute; // Some directly printable values - file.print("<UL><LI>Maximum stack size = " + c.getMaxStack() + printWriter.print("<UL><LI>Maximum stack size = " + c.getMaxStack() + "</LI>\n<LI>Number of local variables = " + c.getMaxLocals() + "</LI>\n<LI><A HREF=\"" + class_name + "_code.html#method" + method_number + "\" TARGET=Code>Byte code</A></LI></UL>\n"); @@ -107,63 +108,63 @@ final class AttributeHTML { final CodeException[] ce = c.getExceptionTable(); final int len = ce.length; if (len > 0) { - file.print("<P><B>Exceptions handled</B><UL>"); + printWriter.print("<P><B>Exceptions handled</B><UL>"); for (final CodeException cex : ce) { final int catch_type = cex.getCatchType(); // Index in constant pool - file.print("<LI>"); + printWriter.print("<LI>"); if (catch_type != 0) { - file.print(constant_html.referenceConstant(catch_type)); // Create Link to _cp.html + printWriter.print(constant_html.referenceConstant(catch_type)); // Create Link to _cp.html } else { - file.print("Any Exception"); + printWriter.print("Any Exception"); } - file.print("<BR>(Ranging from lines " + printWriter.print("<BR>(Ranging from lines " + codeLink(cex.getStartPC(), method_number) + " to " + codeLink(cex.getEndPC(), method_number) + ", handled at line " + codeLink(cex.getHandlerPC(), method_number) + ")</LI>"); } - file.print("</UL>"); + printWriter.print("</UL>"); } break; case Const.ATTR_CONSTANT_VALUE: index = ((ConstantValue) attribute).getConstantValueIndex(); // Reference _cp.html - file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index + printWriter.print("<UL><LI><A HREF=\"" + class_name + "_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 - file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index + printWriter.print("<UL><LI><A HREF=\"" + class_name + "_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(); - file.print("<UL>"); + printWriter.print("<UL>"); for (final int indice : indices) { - file.print("<LI><A HREF=\"" + class_name + "_cp.html#cp" + indice + printWriter.print("<LI><A HREF=\"" + class_name + "_cp.html#cp" + indice + "\" TARGET=\"ConstantPool\">Exception class index(" + indice + ")</A>\n"); } - file.print("</UL>\n"); + printWriter.print("</UL>\n"); break; case Const.ATTR_LINE_NUMBER_TABLE: final LineNumber[] line_numbers = ((LineNumberTable) attribute).getLineNumberTable(); // List line number pairs - file.print("<P>"); + printWriter.print("<P>"); for (int i = 0; i < line_numbers.length; i++) { - file.print("(" + line_numbers[i].getStartPC() + ", " + printWriter.print("(" + line_numbers[i].getStartPC() + ", " + line_numbers[i].getLineNumber() + ")"); if (i < line_numbers.length - 1) { - file.print(", "); // breakable + printWriter.print(", "); // breakable } } break; case Const.ATTR_LOCAL_VARIABLE_TABLE: final LocalVariable[] vars = ((LocalVariableTable) attribute).getLocalVariableTable(); // List name, range and type - file.print("<UL>"); + printWriter.print("<UL>"); for (final LocalVariable var : vars) { index = var.getSignatureIndex(); String signature = ((ConstantUtf8) constant_pool.getConstant(index, @@ -171,19 +172,19 @@ final class AttributeHTML { signature = Utility.signatureToString(signature, false); final int start = var.getStartPC(); final int end = start + var.getLength(); - file.println("<LI>" + Class2HTML.referenceType(signature) + " <B>" + printWriter.println("<LI>" + Class2HTML.referenceType(signature) + " <B>" + var.getName() + "</B> in slot %" + var.getIndex() + "<BR>Valid from lines " + "<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + start + "\" TARGET=Code>" + start + "</A> to " + "<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + end + "\" TARGET=Code>" + end + "</A></LI>"); } - file.print("</UL>\n"); + printWriter.print("</UL>\n"); break; case Const.ATTR_INNER_CLASSES: final InnerClass[] classes = ((InnerClasses) attribute).getInnerClasses(); // List inner classes - file.print("<UL>"); + printWriter.print("<UL>"); for (final InnerClass classe : classes) { final String name; final String access; @@ -195,18 +196,18 @@ final class AttributeHTML { name = "<anonymous>"; } access = Utility.accessToString(classe.getInnerAccessFlags()); - file.print("<LI><FONT COLOR=\"#FF0000\">" + access + "</FONT> " + printWriter.print("<LI><FONT COLOR=\"#FF0000\">" + access + "</FONT> " + constant_html.referenceConstant(classe.getInnerClassIndex()) + " in class " + constant_html.referenceConstant(classe.getOuterClassIndex()) + " named " + name + "</LI>\n"); } - file.print("</UL>\n"); + printWriter.print("</UL>\n"); break; default: // Such as Unknown attribute or Deprecated - file.print("<P>" + attribute); + printWriter.print("<P>" + attribute); } - file.println("</TD></TR>"); - file.flush(); + printWriter.println("</TD></TR>"); + printWriter.flush(); } } diff --git a/src/main/java/org/apache/bcel/util/Class2HTML.java b/src/main/java/org/apache/bcel/util/Class2HTML.java index ef56ab14..c987ae89 100644 --- a/src/main/java/org/apache/bcel/util/Class2HTML.java +++ b/src/main/java/org/apache/bcel/util/Class2HTML.java @@ -21,6 +21,8 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.HashSet; import java.util.Set; @@ -186,6 +188,10 @@ public class Class2HTML implements Constants { * @param dir The directory to put the files in */ public Class2HTML(final JavaClass javaClass, final String dir) throws IOException { + this(javaClass, dir, StandardCharsets.UTF_8); + } + + private Class2HTML(final JavaClass javaClass, final String dir, final Charset charset) throws IOException { final Method[] methods = javaClass.getMethods(); this.javaClass = javaClass; this.dir = dir; @@ -198,18 +204,15 @@ public class Class2HTML implements Constants { } else { classPackage = ""; // default package } - final ConstantHTML constantHtml = new ConstantHTML(dir, className, classPackage, methods, - constantPool); - /* Attributes can't be written in one step, so we just open a file - * which will be written consequently. + final ConstantHTML constantHtml = new ConstantHTML(dir, className, classPackage, methods, constantPool, charset); + /* + * Attributes can't be written in one step, so we just open a file which will be written consequently. */ - final AttributeHTML attributeHtml = new AttributeHTML(dir, className, constantPool, - constantHtml); - new MethodHTML(dir, className, methods, javaClass.getFields(), - constantHtml, attributeHtml); + final AttributeHTML attributeHtml = new AttributeHTML(dir, className, constantPool, constantHtml, charset); + new MethodHTML(dir, className, methods, javaClass.getFields(), constantHtml, attributeHtml, charset); // Write main file (with frames, yuk) writeMainHTML(attributeHtml); - new CodeHTML(dir, className, methods, constantPool, constantHtml); + new CodeHTML(dir, className, methods, constantPool, constantHtml, charset); attributeHtml.close(); } diff --git a/src/main/java/org/apache/bcel/util/CodeHTML.java b/src/main/java/org/apache/bcel/util/CodeHTML.java index 543af9a0..4519699a 100644 --- a/src/main/java/org/apache/bcel/util/CodeHTML.java +++ b/src/main/java/org/apache/bcel/util/CodeHTML.java @@ -17,9 +17,9 @@ */ package org.apache.bcel.util; -import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; +import java.nio.charset.Charset; import java.util.BitSet; import org.apache.bcel.Const; @@ -39,33 +39,34 @@ import org.apache.bcel.classfile.Utility; /** * Convert code into HTML file. - * - * */ final class CodeHTML { private static boolean wide; private final String className; // name of current class // private Method[] methods; // Methods to print - private final PrintWriter file; // file to write to + private final PrintWriter printWriter; // file to write to private BitSet gotoSet; private final ConstantPool constantPool; private final ConstantHTML constantHtml; CodeHTML(final String dir, final String class_name, final Method[] methods, final ConstantPool constant_pool, - final ConstantHTML constant_html) throws IOException { + final ConstantHTML constant_html, final Charset charset) throws IOException { this.className = class_name; // this.methods = methods; this.constantPool = constant_pool; this.constantHtml = constant_html; - file = new PrintWriter(new FileOutputStream(dir + class_name + "_code.html")); - file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\">"); + printWriter = new PrintWriter(dir + class_name + "_code.html", charset.name()); + printWriter.print("<HTML><head><meta charset=\""); + printWriter.print(charset.name()); + printWriter.println("\"></head>"); + printWriter.println("<BODY BGCOLOR=\"#C0C0C0\">"); for (int i = 0; i < methods.length; i++) { writeMethod(methods[i], i); } - file.println("</BODY></HTML>"); - file.close(); + printWriter.println("</BODY></HTML>"); + printWriter.close(); } @@ -505,45 +506,45 @@ private final String className; // name of current class access = Utility.replace(access, " ", " "); // Get the method's attributes, the Code Attribute in particular final Attribute[] attributes = method.getAttributes(); - file.print("<P><B><FONT COLOR=\"#FF0000\">" + access + "</FONT> " + "<A NAME=method" + printWriter.print("<P><B><FONT COLOR=\"#FF0000\">" + access + "</FONT> " + "<A NAME=method" + method_number + ">" + Class2HTML.referenceType(type) + "</A> <A HREF=\"" + className + "_methods.html#method" + method_number + "\" TARGET=Methods>" + html_name + "</A>("); for (int i = 0; i < args.length; i++) { - file.print(Class2HTML.referenceType(args[i])); + printWriter.print(Class2HTML.referenceType(args[i])); if (i < args.length - 1) { - file.print(", "); + printWriter.print(", "); } } - file.println(")</B></P>"); + printWriter.println(")</B></P>"); Code c = null; byte[] code = null; if (attributes.length > 0) { - file.print("<H4>Attributes</H4><UL>\n"); + printWriter.print("<H4>Attributes</H4><UL>\n"); for (int i = 0; i < attributes.length; i++) { byte tag = attributes[i].getTag(); if (tag != Const.ATTR_UNKNOWN) { - file.print("<LI><A HREF=\"" + className + "_attributes.html#method" + printWriter.print("<LI><A HREF=\"" + className + "_attributes.html#method" + method_number + "@" + i + "\" TARGET=Attributes>" + Const.getAttributeName(tag) + "</A></LI>\n"); } else { - file.print("<LI>" + attributes[i] + "</LI>"); + printWriter.print("<LI>" + attributes[i] + "</LI>"); } if (tag == Const.ATTR_CODE) { c = (Code) attributes[i]; final Attribute[] attributes2 = c.getAttributes(); code = c.getCode(); - file.print("<UL>"); + printWriter.print("<UL>"); for (int j = 0; j < attributes2.length; j++) { tag = attributes2[j].getTag(); - file.print("<LI><A HREF=\"" + className + "_attributes.html#" + "method" + printWriter.print("<LI><A HREF=\"" + className + "_attributes.html#" + "method" + method_number + "@" + i + "@" + j + "\" TARGET=Attributes>" + Const.getAttributeName(tag) + "</A></LI>\n"); } - file.print("</UL>"); + printWriter.print("</UL>"); } } - file.println("</UL>"); + printWriter.println("</UL>"); } if (code != null) { // No code, an abstract method, e.g. //System.out.println(name + "\n" + Utility.codeToString(code, constantPool, 0, -1)); @@ -552,7 +553,7 @@ private final String className; // name of current class stream.mark(stream.available()); findGotos(stream, c); stream.reset(); - file.println("<TABLE BORDER=0><TR><TH ALIGN=LEFT>Byte<BR>offset</TH>" + printWriter.println("<TABLE BORDER=0><TR><TH ALIGN=LEFT>Byte<BR>offset</TH>" + "<TH ALIGN=LEFT>Instruction</TH><TH ALIGN=LEFT>Argument</TH>"); for (; stream.available() > 0;) { final int offset = stream.getIndex(); @@ -571,12 +572,12 @@ private final String className; // name of current class } else { anchor2 = "" + offset; } - file.println("<TR VALIGN=TOP><TD>" + anchor2 + "</TD><TD>" + anchor + str + "</TR>"); + printWriter.println("<TR VALIGN=TOP><TD>" + anchor2 + "</TD><TD>" + anchor + str + "</TR>"); } } // Mark last line, may be targetted from Attributes window - file.println("<TR><TD> </A></TD></TR>"); - file.println("</TABLE>"); + printWriter.println("<TR><TD> </A></TD></TR>"); + printWriter.println("</TABLE>"); } } } diff --git a/src/main/java/org/apache/bcel/util/ConstantHTML.java b/src/main/java/org/apache/bcel/util/ConstantHTML.java index 5bfaa8da..bc1f9f3b 100644 --- a/src/main/java/org/apache/bcel/util/ConstantHTML.java +++ b/src/main/java/org/apache/bcel/util/ConstantHTML.java @@ -17,9 +17,9 @@ */ package org.apache.bcel.util; -import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; +import java.nio.charset.Charset; import org.apache.bcel.Const; import org.apache.bcel.classfile.Constant; @@ -35,45 +35,46 @@ import org.apache.bcel.classfile.Utility; /** * Convert constant pool into HTML file. - * - * */ final class ConstantHTML { private final String className; // name of current class private final String classPackage; // name of package private final ConstantPool constantPool; // reference to constant pool - private final PrintWriter file; // file to write to + private final PrintWriter printWriter; // file to write to private final String[] constantRef; // String to return for cp[i] private final Constant[] constants; // The constants in the cp private final Method[] methods; ConstantHTML(final String dir, final String class_name, final String class_package, final Method[] methods, - final ConstantPool constant_pool) throws IOException { + final ConstantPool constant_pool, final Charset charset) throws IOException { this.className = class_name; this.classPackage = class_package; this.constantPool = constant_pool; this.methods = methods; constants = constant_pool.getConstantPool(); - file = new PrintWriter(new FileOutputStream(dir + class_name + "_cp.html")); + printWriter = new PrintWriter(dir + class_name + "_cp.html", charset.name()); constantRef = new String[constants.length]; constantRef[0] = "<unknown>"; - file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>"); + printWriter.print("<HTML><head><meta charset=\""); + printWriter.print(charset.name()); + printWriter.println("\"></head>"); + printWriter.println("<BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>"); // Loop through constants, constants[0] is reserved for (int i = 1; i < constants.length; i++) { if (i % 2 == 0) { - file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>"); + printWriter.print("<TR BGCOLOR=\"#C0C0C0\"><TD>"); } else { - file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>"); + printWriter.print("<TR BGCOLOR=\"#A0A0A0\"><TD>"); } if (constants[i] != null) { writeConstant(i); } - file.print("</TD></TR>\n"); + printWriter.print("</TD></TR>\n"); } - file.println("</TABLE></BODY></HTML>"); - file.close(); + printWriter.println("</TABLE></BODY></HTML>"); + printWriter.close(); } @@ -99,7 +100,7 @@ final class ConstantHTML { int name_index; String ref; // The header is always the same - file.println("<H4> <A NAME=cp" + index + ">" + index + "</A> " + Const.getConstantName(tag) + printWriter.println("<H4> <A NAME=cp" + index + ">" + index + "</A> " + Const.getConstantName(tag) + "</H4>"); /* For every constant type get the needed parameters and print them appropiately */ @@ -158,7 +159,7 @@ final class ConstantHTML { + class_index + "\" TARGET=Constants>" + short_method_class + "</A>.<A HREF=\"" + className + "_cp.html#cp" + index + "\" TARGET=ConstantPool>" + html_method_name + "</A> " + arg_types; - file.println("<P><TT>" + ret_type + " " + ref + arg_types + printWriter.println("<P><TT>" + ret_type + " " + ref + arg_types + " </TT>\n<UL>" + "<LI><A HREF=\"#cp" + class_index + "\">Class index(" + class_index + ")</A>\n" + "<LI><A HREF=\"#cp" + name_index + "\">NameAndType index(" + name_index + ")</A></UL>"); @@ -187,7 +188,7 @@ final class ConstantHTML { + "\" TARGET=Constants>" + short_field_class + "</A>.<A HREF=\"" + className + "_cp.html#cp" + index + "\" TARGET=ConstantPool>" + field_name + "</A>"; - file.println("<P><TT>" + ref + "</TT><BR>\n" + "<UL>" + "<LI><A HREF=\"#cp" + printWriter.println("<P><TT>" + ref + "</TT><BR>\n" + "<UL>" + "<LI><A HREF=\"#cp" + class_index + "\">Class(" + class_index + ")</A><BR>\n" + "<LI><A HREF=\"#cp" + name_index + "\">NameAndType(" + name_index + ")</A></UL>"); @@ -203,7 +204,7 @@ final class ConstantHTML { + "</A>"; constantRef[index] = "<A HREF=\"" + className + "_cp.html#cp" + index + "\" TARGET=ConstantPool>" + short_class_name + "</A>"; - file.println("<P><TT>" + ref + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index + printWriter.println("<P><TT>" + ref + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index + ")</A></UL>\n"); break; case Const.CONSTANT_String: @@ -211,7 +212,7 @@ final class ConstantHTML { Const.CONSTANT_String); name_index = c5.getStringIndex(); final String str = Class2HTML.toHTML(constantPool.constantToString(index, tag)); - file.println("<P><TT>" + str + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index + printWriter.println("<P><TT>" + str + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index + ")</A></UL>\n"); break; case Const.CONSTANT_NameAndType: @@ -219,14 +220,14 @@ final class ConstantHTML { Const.CONSTANT_NameAndType); name_index = c6.getNameIndex(); final int signature_index = c6.getSignatureIndex(); - file.println("<P><TT>" + printWriter.println("<P><TT>" + Class2HTML.toHTML(constantPool.constantToString(index, tag)) + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index + ")</A>\n" + "<LI><A HREF=\"#cp" + signature_index + "\">Signature index(" + signature_index + ")</A></UL>\n"); break; default: - file.println("<P><TT>" + Class2HTML.toHTML(constantPool.constantToString(index, tag)) + "</TT>\n"); + printWriter.println("<P><TT>" + Class2HTML.toHTML(constantPool.constantToString(index, tag)) + "</TT>\n"); } // switch } } diff --git a/src/main/java/org/apache/bcel/util/MethodHTML.java b/src/main/java/org/apache/bcel/util/MethodHTML.java index b83220a2..f85f7ece 100644 --- a/src/main/java/org/apache/bcel/util/MethodHTML.java +++ b/src/main/java/org/apache/bcel/util/MethodHTML.java @@ -20,6 +20,7 @@ package org.apache.bcel.util; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; +import java.nio.charset.Charset; import org.apache.bcel.Const; import org.apache.bcel.classfile.Attribute; @@ -31,38 +32,39 @@ 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 file; // file to write to + private final PrintWriter printWriter; // file to write to private final ConstantHTML constantHtml; private final AttributeHTML attribute_html; MethodHTML(final String dir, final String class_name, final Method[] methods, final Field[] fields, - final ConstantHTML constant_html, final AttributeHTML attribute_html) throws IOException { + final ConstantHTML constant_html, final AttributeHTML attribute_html, final Charset charset) throws IOException { this.className = class_name; this.attribute_html = attribute_html; this.constantHtml = constant_html; - file = new PrintWriter(new FileOutputStream(dir + class_name + "_methods.html")); - file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>"); - file.println("<TR><TH ALIGN=LEFT>Access flags</TH><TH ALIGN=LEFT>Type</TH>" + printWriter = new PrintWriter(new FileOutputStream(dir + class_name + "_methods.html")); + 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); } - file.println("</TABLE>"); - file.println("<TABLE BORDER=0><TR><TH ALIGN=LEFT>Access flags</TH>" + 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); } - file.println("</TABLE></BODY></HTML>"); - file.close(); + printWriter.println("</TABLE></BODY></HTML>"); + printWriter.close(); } @@ -77,7 +79,7 @@ final class MethodHTML { String access = Utility.accessToString(field.getAccessFlags()); Attribute[] attributes; access = Utility.replace(access, " ", " "); - file.print("<TR><TD><FONT COLOR=\"#FF0000\">" + access + "</FONT></TD>\n<TD>" + printWriter.print("<TR><TD><FONT COLOR=\"#FF0000\">" + access + "</FONT></TD>\n<TD>" + Class2HTML.referenceType(type) + "</TD><TD><A NAME=\"field" + name + "\">" + name + "</A></TD>"); attributes = field.getAttributes(); @@ -89,12 +91,12 @@ final class MethodHTML { if (attributes[i].getTag() == Const.ATTR_CONSTANT_VALUE) { // Default value final String str = attributes[i].toString(); // Reference attribute in _attributes.html - file.print("<TD>= <A HREF=\"" + className + "_attributes.html#" + name + "@" + i + printWriter.print("<TD>= <A HREF=\"" + className + "_attributes.html#" + name + "@" + i + "\" TARGET=\"Attributes\">" + str + "</TD>\n"); break; } } - file.println("</TR>"); + printWriter.println("</TR>"); } @@ -117,33 +119,33 @@ final class MethodHTML { */ access = Utility.replace(access, " ", " "); html_name = Class2HTML.toHTML(name); - file.print("<TR VALIGN=TOP><TD><FONT COLOR=\"#FF0000\"><A NAME=method" + method_number + printWriter.print("<TR VALIGN=TOP><TD><FONT COLOR=\"#FF0000\"><A NAME=method" + method_number + ">" + access + "</A></FONT></TD>"); - file.print("<TD>" + Class2HTML.referenceType(type) + "</TD><TD>" + "<A HREF=" + className + printWriter.print("<TD>" + Class2HTML.referenceType(type) + "</TD><TD>" + "<A HREF=" + className + "_code.html#method" + method_number + " TARGET=Code>" + html_name + "</A></TD>\n<TD>("); for (int i = 0; i < args.length; i++) { - file.print(Class2HTML.referenceType(args[i])); + printWriter.print(Class2HTML.referenceType(args[i])); if (i < args.length - 1) { - file.print(", "); + printWriter.print(", "); } } - file.print(")</TD></TR>"); + printWriter.print(")</TD></TR>"); // Check for thrown exceptions for (int i = 0; i < attributes.length; i++) { attribute_html.writeAttribute(attributes[i], "method" + method_number + "@" + i, method_number); final byte tag = attributes[i].getTag(); if (tag == Const.ATTR_EXCEPTIONS) { - file.print("<TR VALIGN=TOP><TD COLSPAN=2></TD><TH ALIGN=LEFT>throws</TH><TD>"); + 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++) { - file.print(constantHtml.referenceConstant(exceptions[j])); + printWriter.print(constantHtml.referenceConstant(exceptions[j])); if (j < exceptions.length - 1) { - file.print(", "); + printWriter.print(", "); } } - file.println("</TD></TR>"); + printWriter.println("</TD></TR>"); } else if (tag == Const.ATTR_CODE) { final Attribute[] c_a = ((Code) attributes[i]).getAttributes(); for (int j = 0; j < c_a.length; j++) { diff --git a/src/test/java/org/apache/bcel/util/Class2HTMLTestCase.java b/src/test/java/org/apache/bcel/util/Class2HTMLTestCase.java index 971e91d2..4a9e68fd 100644 --- a/src/test/java/org/apache/bcel/util/Class2HTMLTestCase.java +++ b/src/test/java/org/apache/bcel/util/Class2HTMLTestCase.java @@ -48,8 +48,7 @@ public class Class2HTMLTestCase { @Test public void testReferenceToConstant() { @SuppressWarnings("unused") - final - short referenceToConstant = Class2HTML.AALOAD; + final short referenceToConstant = Class2HTML.AALOAD; } }