Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/util/ConstantHTML.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/util/ConstantHTML.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/util/ConstantHTML.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/util/ConstantHTML.java Wed Mar 15 03:31:56 2006
@@ -1,4 +1,3 @@
-
/*
* Copyright 2000-2004 The Apache Software Foundation
*
@@ -14,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- */
+ */
package org.apache.bcel.util;
-
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
@@ -40,197 +38,197 @@
*
*/
final class ConstantHTML implements org.apache.bcel.Constants {
- private String class_name; // name of current class
- private String class_package; // name of package
- private ConstantPool constant_pool; // reference to constant pool
- private PrintWriter file; // file to write to
- private String[] constant_ref; // String to return for cp[i]
- private Constant[] constants; // The constants in the cp
- private Method[] methods;
-
- ConstantHTML(String dir, String class_name, String class_package, Method[] methods,
- ConstantPool constant_pool) throws IOException
- {
- this.class_name = class_name;
- this.class_package = class_package;
- this.constant_pool = constant_pool;
- this.methods = methods;
- constants = constant_pool.getConstantPool();
- file = new PrintWriter(new FileOutputStream(dir + class_name + "_cp.html"));
- constant_ref = new String[constants.length];
- constant_ref[0] = "<unknown>";
-
- file.println("<HTML><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>");
- else
- file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
-
- if(constants[i] != null)
- writeConstant(i);
-
- file.print("</TD></TR>\n");
+
+ private String class_name; // name of current class
+ private String class_package; // name of package
+ private ConstantPool constant_pool; // reference to constant pool
+ private PrintWriter file; // file to write to
+ private String[] constant_ref; // String to return for cp[i]
+ private Constant[] constants; // The constants in the cp
+ private Method[] methods;
+
+
+ ConstantHTML(String dir, String class_name, String class_package, Method[] methods,
+ ConstantPool constant_pool) throws IOException {
+ this.class_name = class_name;
+ this.class_package = class_package;
+ this.constant_pool = constant_pool;
+ this.methods = methods;
+ constants = constant_pool.getConstantPool();
+ file = new PrintWriter(new FileOutputStream(dir + class_name + "_cp.html"));
+ constant_ref = new String[constants.length];
+ constant_ref[0] = "<unknown>";
+ file.println("<HTML><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>");
+ } else {
+ file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
+ }
+ if (constants[i] != null) {
+ writeConstant(i);
+ }
+ file.print("</TD></TR>\n");
+ }
+ file.println("</TABLE></BODY></HTML>");
+ file.close();
+ }
+
+
+ String referenceConstant( int index ) {
+ return constant_ref[index];
+ }
+
+
+ private void writeConstant( int index ) {
+ byte tag = constants[index].getTag();
+ int class_index, name_index;
+ String ref;
+ // The header is always the same
+ file.println("<H4> <A NAME=cp" + index + ">" + index + "</A> " + CONSTANT_NAMES[tag]
+ + "</H4>");
+ /* For every constant type get the needed parameters and print them appropiately
+ */
+ switch (tag) {
+ case CONSTANT_InterfaceMethodref:
+ case CONSTANT_Methodref:
+ // Get class_index and name_and_type_index, depending on type
+ if (tag == CONSTANT_Methodref) {
+ ConstantMethodref c = (ConstantMethodref) constant_pool.getConstant(index,
+ CONSTANT_Methodref);
+ class_index = c.getClassIndex();
+ name_index = c.getNameAndTypeIndex();
+ } else {
+ ConstantInterfaceMethodref c1 = (ConstantInterfaceMethodref) constant_pool
+ .getConstant(index, CONSTANT_InterfaceMethodref);
+ class_index = c1.getClassIndex();
+ name_index = c1.getNameAndTypeIndex();
+ }
+ // Get method name and its class
+ String method_name = constant_pool.constantToString(name_index,
+ CONSTANT_NameAndType);
+ String html_method_name = Class2HTML.toHTML(method_name);
+ // Partially compacted class name, i.e., / -> .
+ String method_class = constant_pool.constantToString(class_index, CONSTANT_Class);
+ String short_method_class = Utility.compactClassName(method_class); // I.e., remove java.lang.
+ short_method_class = Utility.compactClassName(method_class); // I.e., remove java.lang.
+ short_method_class = Utility.compactClassName(short_method_class, class_package
+ + ".", true); // Remove class package prefix
+ // Get method signature
+ ConstantNameAndType c2 = (ConstantNameAndType) constant_pool.getConstant(
+ name_index, CONSTANT_NameAndType);
+ String signature = constant_pool.constantToString(c2.getSignatureIndex(),
+ CONSTANT_Utf8);
+ // Get array of strings containing the argument types
+ String[] args = Utility.methodSignatureArgumentTypes(signature, false);
+ // Get return type string
+ String type = Utility.methodSignatureReturnType(signature, false);
+ String ret_type = Class2HTML.referenceType(type);
+ StringBuffer buf = new StringBuffer("(");
+ for (int i = 0; i < args.length; i++) {
+ buf.append(Class2HTML.referenceType(args[i]));
+ if (i < args.length - 1) {
+ buf.append(", ");
+ }
+ }
+ buf.append(")");
+ String arg_types = buf.toString();
+ if (method_class.equals(class_name)) {
+ ref = "<A HREF=\"" + class_name + "_code.html#method"
+ + getMethodNumber(method_name + signature) + "\" TARGET=Code>"
+ + html_method_name + "</A>";
+ } else {
+ ref = "<A HREF=\"" + method_class + ".html" + "\" TARGET=_top>"
+ + short_method_class + "</A>." + html_method_name;
+ }
+ constant_ref[index] = ret_type + " <A HREF=\"" + class_name + "_cp.html#cp"
+ + class_index + "\" TARGET=Constants>" + short_method_class
+ + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" + index
+ + "\" TARGET=ConstantPool>" + html_method_name + "</A> " + arg_types;
+ file.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>");
+ break;
+ case CONSTANT_Fieldref:
+ // Get class_index and name_and_type_index
+ ConstantFieldref c3 = (ConstantFieldref) constant_pool.getConstant(index,
+ CONSTANT_Fieldref);
+ class_index = c3.getClassIndex();
+ name_index = c3.getNameAndTypeIndex();
+ // Get method name and its class (compacted)
+ String field_class = constant_pool.constantToString(class_index, CONSTANT_Class);
+ String short_field_class = Utility.compactClassName(field_class); // I.e., remove java.lang.
+ short_field_class = Utility.compactClassName(short_field_class,
+ class_package + ".", true); // Remove class package prefix
+ String field_name = constant_pool
+ .constantToString(name_index, CONSTANT_NameAndType);
+ if (field_class.equals(class_name)) {
+ ref = "<A HREF=\"" + field_class + "_methods.html#field" + field_name
+ + "\" TARGET=Methods>" + field_name + "</A>";
+ } else {
+ ref = "<A HREF=\"" + field_class + ".html\" TARGET=_top>" + short_field_class
+ + "</A>." + field_name + "\n";
+ }
+ constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + class_index
+ + "\" TARGET=Constants>" + short_field_class + "</A>.<A HREF=\""
+ + class_name + "_cp.html#cp" + index + "\" TARGET=ConstantPool>"
+ + field_name + "</A>";
+ file.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>");
+ break;
+ case CONSTANT_Class:
+ ConstantClass c4 = (ConstantClass) constant_pool.getConstant(index, CONSTANT_Class);
+ name_index = c4.getNameIndex();
+ String class_name2 = constant_pool.constantToString(index, tag); // / -> .
+ String short_class_name = Utility.compactClassName(class_name2); // I.e., remove java.lang.
+ short_class_name = Utility.compactClassName(short_class_name, class_package + ".",
+ true); // Remove class package prefix
+ ref = "<A HREF=\"" + class_name2 + ".html\" TARGET=_top>" + short_class_name
+ + "</A>";
+ constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + index
+ + "\" TARGET=ConstantPool>" + short_class_name + "</A>";
+ file.println("<P><TT>" + ref + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index
+ + "\">Name index(" + name_index + ")</A></UL>\n");
+ break;
+ case CONSTANT_String:
+ ConstantString c5 = (ConstantString) constant_pool.getConstant(index,
+ CONSTANT_String);
+ name_index = c5.getStringIndex();
+ String str = Class2HTML.toHTML(constant_pool.constantToString(index, tag));
+ file.println("<P><TT>" + str + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index
+ + "\">Name index(" + name_index + ")</A></UL>\n");
+ break;
+ case CONSTANT_NameAndType:
+ ConstantNameAndType c6 = (ConstantNameAndType) constant_pool.getConstant(index,
+ CONSTANT_NameAndType);
+ name_index = c6.getNameIndex();
+ int signature_index = c6.getSignatureIndex();
+ file.println("<P><TT>"
+ + Class2HTML.toHTML(constant_pool.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(constant_pool.constantToString(index, tag))
+ + "</TT>\n");
+ } // switch
}
-
- file.println("</TABLE></BODY></HTML>");
- file.close();
- }
-
- String referenceConstant(int index) {
- return constant_ref[index];
- }
-
- private void writeConstant(int index) {
- byte tag = constants[index].getTag();
- int class_index, name_index;
- String ref;
-
- // The header is always the same
- file.println("<H4> <A NAME=cp" + index + ">" + index + "</A> " + CONSTANT_NAMES[tag] + "</H4>");
-
- /* For every constant type get the needed parameters and print them appropiately
- */
- switch(tag) {
- case CONSTANT_InterfaceMethodref:
- case CONSTANT_Methodref:
- // Get class_index and name_and_type_index, depending on type
- if(tag == CONSTANT_Methodref) {
- ConstantMethodref c = (ConstantMethodref)constant_pool.getConstant(index, CONSTANT_Methodref);
- class_index = c.getClassIndex();
- name_index = c.getNameAndTypeIndex();
- }
- else {
- ConstantInterfaceMethodref c1 = (ConstantInterfaceMethodref)constant_pool.getConstant(index, CONSTANT_InterfaceMethodref);
- class_index = c1.getClassIndex();
- name_index = c1.getNameAndTypeIndex();
- }
-
- // Get method name and its class
- String method_name = constant_pool.constantToString(name_index, CONSTANT_NameAndType);
- String html_method_name = Class2HTML.toHTML(method_name);
-
- // Partially compacted class name, i.e., / -> .
- String method_class = constant_pool.constantToString(class_index, CONSTANT_Class);
- String short_method_class = Utility.compactClassName(method_class); // I.e., remove java.lang.
- short_method_class = Utility.compactClassName(method_class); // I.e., remove java.lang.
- short_method_class = Utility.compactClassName(short_method_class, class_package + ".", true); // Remove class package prefix
-
- // Get method signature
- ConstantNameAndType c2 = (ConstantNameAndType)constant_pool.getConstant(name_index, CONSTANT_NameAndType);
- String signature = constant_pool.constantToString(c2.getSignatureIndex(), CONSTANT_Utf8);
- // Get array of strings containing the argument types
- String[] args = Utility.methodSignatureArgumentTypes(signature, false);
-
- // Get return type string
- String type = Utility.methodSignatureReturnType(signature, false);
- String ret_type = Class2HTML.referenceType(type);
-
- StringBuffer buf = new StringBuffer("(");
- for(int i=0; i < args.length; i++) {
- buf.append(Class2HTML.referenceType(args[i]));
- if(i < args.length - 1)
- buf.append(", ");
- }
- buf.append(")");
-
- String arg_types = buf.toString();
-
- if(method_class.equals(class_name)) // Method is local to class
- ref = "<A HREF=\"" + class_name + "_code.html#method" + getMethodNumber(method_name + signature) +
- "\" TARGET=Code>" + html_method_name + "</A>";
- else
- ref = "<A HREF=\"" + method_class + ".html" + "\" TARGET=_top>" + short_method_class +
- "</A>." + html_method_name;
-
- constant_ref[index] = ret_type + " <A HREF=\"" + class_name + "_cp.html#cp" + class_index +
- "\" TARGET=Constants>" +
- short_method_class + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" +
- index + "\" TARGET=ConstantPool>" + html_method_name + "</A> " + arg_types;
-
- file.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>");
- break;
-
- case CONSTANT_Fieldref:
- // Get class_index and name_and_type_index
- ConstantFieldref c3 = (ConstantFieldref)constant_pool.getConstant(index, CONSTANT_Fieldref);
- class_index = c3.getClassIndex();
- name_index = c3.getNameAndTypeIndex();
-
- // Get method name and its class (compacted)
- String field_class = constant_pool.constantToString(class_index, CONSTANT_Class);
- String short_field_class = Utility.compactClassName(field_class); // I.e., remove java.lang.
- short_field_class = Utility.compactClassName(short_field_class, class_package + ".", true); // Remove class package prefix
-
- String field_name = constant_pool.constantToString(name_index, CONSTANT_NameAndType);
-
- if(field_class.equals(class_name)) // Field is local to class
- ref = "<A HREF=\"" + field_class + "_methods.html#field" +
- field_name + "\" TARGET=Methods>" + field_name + "</A>";
- else
- ref = "<A HREF=\"" + field_class + ".html\" TARGET=_top>" +
- short_field_class + "</A>." + field_name + "\n";
-
- constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + class_index + "\" TARGET=Constants>" +
- short_field_class + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" +
- index + "\" TARGET=ConstantPool>" + field_name + "</A>";
-
- file.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>");
- break;
-
- case CONSTANT_Class:
- ConstantClass c4 = (ConstantClass)constant_pool.getConstant(index, CONSTANT_Class);
- name_index = c4.getNameIndex();
- String class_name2 = constant_pool.constantToString(index, tag); // / -> .
- String short_class_name = Utility.compactClassName(class_name2); // I.e., remove java.lang.
- short_class_name = Utility.compactClassName(short_class_name, class_package + ".", true); // Remove class package prefix
-
- ref = "<A HREF=\"" + class_name2 + ".html\" TARGET=_top>" + short_class_name + "</A>";
- constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + index +
- "\" TARGET=ConstantPool>" + short_class_name + "</A>";
-
- file.println("<P><TT>" + ref + "</TT><UL>" +
- "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index + ")</A></UL>\n");
- break;
-
- case CONSTANT_String:
- ConstantString c5 = (ConstantString)constant_pool.getConstant(index, CONSTANT_String);
- name_index = c5.getStringIndex();
-
- String str = Class2HTML.toHTML(constant_pool.constantToString(index, tag));
-
- file.println("<P><TT>" + str + "</TT><UL>" +
- "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index + ")</A></UL>\n");
- break;
-
- case CONSTANT_NameAndType:
- ConstantNameAndType c6 = (ConstantNameAndType)constant_pool.getConstant(index, CONSTANT_NameAndType);
- name_index = c6.getNameIndex();
- int signature_index = c6.getSignatureIndex();
-
- file.println("<P><TT>" + Class2HTML.toHTML(constant_pool.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(constant_pool.constantToString(index, tag)) + "</TT>\n");
- } // switch
- }
-
- private final int getMethodNumber(String str) {
- for(int i=0; i < methods.length; i++) {
- String cmp = methods[i].getName() + methods[i].getSignature();
- if(cmp.equals(str))
- return i;
+
+
+ private final int getMethodNumber( String str ) {
+ for (int i = 0; i < methods.length; i++) {
+ String cmp = methods[i].getName() + methods[i].getSignature();
+ if (cmp.equals(str)) {
+ return i;
+ }
+ }
+ return -1;
}
- return -1;
- }
}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/util/InstructionFinder.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/util/InstructionFinder.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/util/InstructionFinder.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/util/InstructionFinder.java Wed Mar 15 03:31:56 2006
@@ -65,422 +65,408 @@
* @see InstructionList
*/
public class InstructionFinder {
- private static final int OFFSET = 32767; // char + OFFSET is
- // outside of
- // LATIN-1
- private static final int NO_OPCODES = 256; // Potential number,
- // some are not used
-
- private static final Map map = new HashMap(); // Map<String,Pattern>
-
- private InstructionList il;
- private String il_string; // instruction list
- // as string
- private InstructionHandle[] handles; // map instruction
-
- // list to array
-
- /**
- * @param il
- * instruction list to search for given patterns
- */
- public InstructionFinder(InstructionList il) {
- this.il = il;
- reread();
- }
-
- /**
- * Reread the instruction list, e.g., after you've altered the list upon a
- * match.
- */
- public final void reread() {
- int size = il.getLength();
- char[] buf = new char[size]; // Create a string with length equal to il
- // length
- handles = il.getInstructionHandles();
-
- // Map opcodes to characters
- for (int i = 0; i < size; i++)
- buf[i] = makeChar(handles[i].getInstruction().getOpcode());
-
- il_string = new String(buf);
- }
-
- /**
- * Map symbolic instruction names like "getfield" to a single character.
- *
- * @param pattern
- * instruction pattern in lower case
- * @return encoded string for a pattern such as "BranchInstruction".
- */
- private static final String mapName(String pattern) {
- String result = (String) map.get(pattern);
-
- if (result != null)
- return result;
-
- for (short i = 0; i < NO_OPCODES; i++)
- if (pattern.equals(Constants.OPCODE_NAMES[i]))
- return "" + makeChar(i);
-
- throw new RuntimeException("Instruction unknown: " + pattern);
- }
-
- /**
- * Replace symbolic names of instructions with the appropiate character and
- * remove all white space from string. Meta characters such as +, * are
- * ignored.
- *
- * @param pattern
- * The pattern to compile
- * @return translated regular expression string
- */
- private static final String compilePattern(String pattern) {
- //Bug: 38787 - Instructions are assumed to be english, to avoid odd Locale issues
- String lower = pattern.toLowerCase(Locale.ENGLISH);
- StringBuffer buf = new StringBuffer();
- int size = pattern.length();
-
- for (int i = 0; i < size; i++) {
- char ch = lower.charAt(i);
-
- if (Character.isLetterOrDigit(ch)) {
- StringBuffer name = new StringBuffer();
-
- while ((Character.isLetterOrDigit(ch) || ch == '_') && i < size) {
- name.append(ch);
-
- if (++i < size)
- ch = lower.charAt(i);
- else
- break;
- }
-
- i--;
-
- buf.append(mapName(name.toString()));
- } else if (!Character.isWhitespace(ch))
- buf.append(ch);
- }
-
- return buf.toString();
- }
-
- /**
- * @return the matched piece of code as an array of instruction (handles)
- */
- private InstructionHandle[] getMatch(int matched_from, int match_length) {
- InstructionHandle[] match = new InstructionHandle[match_length];
- System.arraycopy(handles, matched_from, match, 0, match_length);
-
- return match;
- }
-
- /**
- * Search for the given pattern in the instruction list. You can search for
- * any valid opcode via its symbolic name, e.g. "istore". You can also use a
- * super class or an interface name to match a whole set of instructions, e.g.
- * "BranchInstruction" or "LoadInstruction". "istore" is also an alias for all
- * "istore_x" instructions. Additional aliases are "if" for "ifxx", "if_icmp"
- * for "if_icmpxx", "if_acmp" for "if_acmpxx".
- *
- * Consecutive instruction names must be separated by white space which will
- * be removed during the compilation of the pattern.
- *
- * For the rest the usual pattern matching rules for regular expressions
- * apply.
- * <P>
- * Example pattern:
- *
- * <pre>
- * search("BranchInstruction NOP ((IfInstruction|GOTO)+ ISTORE Instruction)*");
- * </pre>
- *
- * <p>
- * If you alter the instruction list upon a match such that other matching
- * areas are affected, you should call reread() to update the finder and call
- * search() again, because the matches are cached.
- *
- * @param pattern
- * the instruction pattern to search for, where case is ignored
- * @param from
- * where to start the search in the instruction list
- * @param constraint
- * optional CodeConstraint to check the found code pattern for
- * user-defined constraints
- * @return iterator of matches where e.nextElement() returns an array of
- * instruction handles describing the matched area
- */
- public final Iterator search(String pattern, InstructionHandle from, CodeConstraint constraint) {
- String search = compilePattern(pattern);
- int start = -1;
-
- for (int i = 0; i < handles.length; i++) {
- if (handles[i] == from) {
- start = i; // Where to start search from (index)
- break;
- }
- }
-
- if (start == -1)
- throw new ClassGenException("Instruction handle " + from + " not found in instruction list.");
-
- Pattern regex = Pattern.compile(search);
- List matches = new ArrayList();
-
- Matcher matcher = regex.matcher(il_string);
-
- while (start < il_string.length() && matcher.find(start)) {
- int startExpr = matcher.start();
- int endExpr = matcher.end();
- int lenExpr = (endExpr - startExpr) + 1;
-
- InstructionHandle[] match = getMatch(startExpr, lenExpr);
-
- if ((constraint == null) || constraint.checkCode(match)) {
- matches.add(match);
- }
-
- start = endExpr;
- }
-
- return matches.iterator();
-
- }
-
- /**
- * Start search beginning from the start of the given instruction list.
- *
- * @param pattern
- * the instruction pattern to search for, where case is ignored
- * @return iterator of matches where e.nextElement() returns an array of
- * instruction handles describing the matched area
- */
- public final Iterator search(String pattern) {
- return search(pattern, il.getStart(), null);
- }
-
- /**
- * Start search beginning from `from'.
- *
- * @param pattern
- * the instruction pattern to search for, where case is ignored
- * @param from
- * where to start the search in the instruction list
- * @return iterator of matches where e.nextElement() returns an array of
- * instruction handles describing the matched area
- */
- public final Iterator search(String pattern, InstructionHandle from) {
- return search(pattern, from, null);
- }
-
- /**
- * Start search beginning from the start of the given instruction list. Check
- * found matches with the constraint object.
- *
- * @param pattern
- * the instruction pattern to search for, case is ignored
- * @param constraint
- * constraints to be checked on matching code
- * @return instruction handle or `null' if the match failed
- */
- public final Iterator search(String pattern, CodeConstraint constraint) {
- return search(pattern, il.getStart(), constraint);
- }
-
- /**
- * Convert opcode number to char.
- */
- private static final char makeChar(short opcode) {
- return (char) (opcode + OFFSET);
- }
-
- /**
- * @return the inquired instruction list
- */
- public final InstructionList getInstructionList() {
- return il;
- }
-
- /**
- * Code patterns found may be checked using an additional user-defined
- * constraint object whether they really match the needed criterion. I.e.,
- * check constraints that can not expressed with regular expressions.
- *
- */
- public static interface CodeConstraint {
- /**
- * @param match
- * array of instructions matching the requested pattern
- * @return true if the matched area is really useful
- */
- public boolean checkCode(InstructionHandle[] match);
- }
-
- // Initialize pattern map
-
- static {
- map
- .put(
- "arithmeticinstruction",
- "(irem|lrem|iand|ior|ineg|isub|lneg|fneg|fmul|ldiv|fadd|lxor|frem|idiv|land|ixor|ishr|fsub|lshl|fdiv|iadd|lor|dmul|lsub|ishl|imul|lmul|lushr|dneg|iushr|lshr|ddiv|drem|dadd|ladd|dsub)");
- map.put("invokeinstruction", "(invokevirtual|invokeinterface|invokestatic|invokespecial)");
- map
- .put(
- "arrayinstruction",
- "(baload|aastore|saload|caload|fastore|lastore|iaload|castore|iastore|aaload|bastore|sastore|faload|laload|daload|dastore)");
- map.put("gotoinstruction", "(goto|goto_w)");
- map.put("conversioninstruction",
- "(d2l|l2d|i2s|d2i|l2i|i2b|l2f|d2f|f2i|i2d|i2l|f2d|i2c|f2l|i2f)");
- map.put("localvariableinstruction",
- "(fstore|iinc|lload|dstore|dload|iload|aload|astore|istore|fload|lstore)");
- map.put("loadinstruction", "(fload|dload|lload|iload|aload)");
- map.put("fieldinstruction", "(getfield|putstatic|getstatic|putfield)");
- map
- .put(
- "cpinstruction",
- "(ldc2_w|invokeinterface|multianewarray|putstatic|instanceof|getstatic|checkcast|getfield|invokespecial|ldc_w|invokestatic|invokevirtual|putfield|ldc|new|anewarray)");
- map.put("stackinstruction", "(dup2|swap|dup2_x2|pop|pop2|dup|dup2_x1|dup_x2|dup_x1)");
- map
- .put(
- "branchinstruction",
- "(ifle|if_acmpne|if_icmpeq|if_acmpeq|ifnonnull|goto_w|iflt|ifnull|if_icmpne|tableswitch|if_icmple|ifeq|if_icmplt|jsr_w|if_icmpgt|ifgt|jsr|goto|ifne|ifge|lookupswitch|if_icmpge)");
- map.put("returninstruction", "(lreturn|ireturn|freturn|dreturn|areturn|return)");
- map.put("storeinstruction", "(istore|fstore|dstore|astore|lstore)");
- map.put("select", "(tableswitch|lookupswitch)");
- map
- .put(
- "ifinstruction",
- "(ifeq|ifgt|if_icmpne|if_icmpeq|ifge|ifnull|ifne|if_icmple|if_icmpge|if_acmpeq|if_icmplt|if_acmpne|ifnonnull|iflt|if_icmpgt|ifle)");
- map.put("jsrinstruction", "(jsr|jsr_w)");
- map.put("variablelengthinstruction", "(tableswitch|jsr|goto|lookupswitch)");
- map.put("unconditionalbranch", "(goto|jsr|jsr_w|athrow|goto_w)");
- map.put("constantpushinstruction", "(dconst|bipush|sipush|fconst|iconst|lconst)");
- map
- .put(
- "typedinstruction",
- "(imul|lsub|aload|fload|lor|new|aaload|fcmpg|iand|iaload|lrem|idiv|d2l|isub|dcmpg|dastore|ret|f2d|f2i|drem|iinc|i2c|checkcast|frem|lreturn|astore|lushr|daload|dneg|fastore|istore|lshl|ldiv|lstore|areturn|ishr|ldc_w|invokeinterface|aastore|lxor|ishl|l2d|i2f|return|faload|sipush|iushr|caload|instanceof|invokespecial|putfield|fmul|ireturn|laload|d2f|lneg|ixor|i2l|fdiv|lastore|multianewarray|i2b|getstatic|i2d|putstatic|fcmpl|saload|ladd|irem|dload|jsr_w|dconst|dcmpl|fsub|freturn|ldc|aconst_null|castore|lmul|ldc2_w|dadd|iconst|f2l|ddiv|dstore|land|jsr|anewarray|dmul|bipush|dsub|sastore|d2i|i2s|lshr|iadd|l2i|lload|bastore|fstore|fneg|iload|fadd|baload|fconst|ior|ineg|dreturn|l2f|lconst|getfield|invokevirtual|invokestatic|iastore)");
- map.put("popinstruction", "(fstore|dstore|pop|pop2|astore|putstatic|istore|lstore)");
- map.put("allocationinstruction", "(multianewarray|new|anewarray|newarray)");
- map
- .put(
- "indexedinstruction",
- "(lload|lstore|fload|ldc2_w|invokeinterface|multianewarray|astore|dload|putstatic|instanceof|getstatic|checkcast|getfield|invokespecial|dstore|istore|iinc|ldc_w|ret|fstore|invokestatic|iload|putfield|invokevirtual|ldc|new|aload|anewarray)");
- map
- .put(
- "pushinstruction",
- "(dup|lload|dup2|bipush|fload|ldc2_w|sipush|lconst|fconst|dload|getstatic|ldc_w|aconst_null|dconst|iload|ldc|iconst|aload)");
- map
- .put(
- "stackproducer",
- "(imul|lsub|aload|fload|lor|new|aaload|fcmpg|iand|iaload|lrem|idiv|d2l|isub|dcmpg|dup|f2d|f2i|drem|i2c|checkcast|frem|lushr|daload|dneg|lshl|ldiv|ishr|ldc_w|invokeinterface|lxor|ishl|l2d|i2f|faload|sipush|iushr|caload|instanceof|invokespecial|fmul|laload|d2f|lneg|ixor|i2l|fdiv|getstatic|i2b|swap|i2d|dup2|fcmpl|saload|ladd|irem|dload|jsr_w|dconst|dcmpl|fsub|ldc|arraylength|aconst_null|tableswitch|lmul|ldc2_w|iconst|dadd|f2l|ddiv|land|jsr|anewarray|dmul|bipush|dsub|d2i|newarray|i2s|lshr|iadd|lload|l2i|fneg|iload|fadd|baload|fconst|lookupswitch|ior|ineg|lconst|l2f|getfield|invokevirtual|invokestatic)");
- map
- .put(
- "stackconsumer",
- "(imul|lsub|lor|iflt|fcmpg|if_icmpgt|iand|ifeq|if_icmplt|lrem|ifnonnull|idiv|d2l|isub|dcmpg|dastore|if_icmpeq|f2d|f2i|drem|i2c|checkcast|frem|lreturn|astore|lushr|pop2|monitorexit|dneg|fastore|istore|lshl|ldiv|lstore|areturn|if_icmpge|ishr|monitorenter|invokeinterface|aastore|lxor|ishl|l2d|i2f|return|iushr|instanceof|invokespecial|fmul|ireturn|d2f|lneg|ixor|pop|i2l|ifnull|fdiv|lastore|i2b|if_acmpeq|ifge|swap|i2d|putstatic|fcmpl|ladd|irem|dcmpl|fsub|freturn|ifgt|castore|lmul|dadd|f2l|ddiv|dstore|land|if_icmpne|if_acmpne|dmul|dsub|sastore|ifle|d2i|i2s|lshr|iadd|l2i|bastore|fstore|fneg|fadd|ior|ineg|ifne|dreturn|l2f|if_icmple|getfield|invokevirtual|invokestatic|iastore)");
- map
- .put(
- "exceptionthrower",
- "(irem|lrem|laload|putstatic|baload|dastore|areturn|getstatic|ldiv|anewarray|iastore|castore|idiv|saload|lastore|fastore|putfield|lreturn|caload|getfield|return|aastore|freturn|newarray|instanceof|multianewarray|athrow|faload|iaload|aaload|dreturn|monitorenter|checkcast|bastore|arraylength|new|invokevirtual|sastore|ldc_w|ireturn|invokespecial|monitorexit|invokeinterface|ldc|invokestatic|daload)");
- map
- .put(
- "loadclass",
- "(multianewarray|invokeinterface|instanceof|invokespecial|putfield|checkcast|putstatic|invokevirtual|new|getstatic|invokestatic|getfield|anewarray)");
- map
- .put(
- "instructiontargeter",
- "(ifle|if_acmpne|if_icmpeq|if_acmpeq|ifnonnull|goto_w|iflt|ifnull|if_icmpne|tableswitch|if_icmple|ifeq|if_icmplt|jsr_w|if_icmpgt|ifgt|jsr|goto|ifne|ifge|lookupswitch|if_icmpge)");
-
- // Some aliases
- map.put("if_icmp", "(if_icmpne|if_icmpeq|if_icmple|if_icmpge|if_icmplt|if_icmpgt)");
- map.put("if_acmp", "(if_acmpeq|if_acmpne)");
- map.put("if", "(ifeq|ifne|iflt|ifge|ifgt|ifle)");
-
- // Precompile some aliases first
- map.put("iconst", precompile(Constants.ICONST_0, Constants.ICONST_5, Constants.ICONST_M1));
- map.put("lconst", new String(new char[] { '(', makeChar(Constants.LCONST_0), '|',
- makeChar(Constants.LCONST_1), ')' }));
- map.put("dconst", new String(new char[] { '(', makeChar(Constants.DCONST_0), '|',
- makeChar(Constants.DCONST_1), ')' }));
- map.put("fconst", new String(new char[] { '(', makeChar(Constants.FCONST_0), '|',
- makeChar(Constants.FCONST_1), ')' }));
-
- map.put("iload", precompile(Constants.ILOAD_0, Constants.ILOAD_3, Constants.ILOAD));
- map.put("dload", precompile(Constants.DLOAD_0, Constants.DLOAD_3, Constants.DLOAD));
- map.put("fload", precompile(Constants.FLOAD_0, Constants.FLOAD_3, Constants.FLOAD));
- map.put("aload", precompile(Constants.ALOAD_0, Constants.ALOAD_3, Constants.ALOAD));
-
- map.put("istore", precompile(Constants.ISTORE_0, Constants.ISTORE_3, Constants.ISTORE));
- map.put("dstore", precompile(Constants.DSTORE_0, Constants.DSTORE_3, Constants.DSTORE));
- map.put("fstore", precompile(Constants.FSTORE_0, Constants.FSTORE_3, Constants.FSTORE));
- map.put("astore", precompile(Constants.ASTORE_0, Constants.ASTORE_3, Constants.ASTORE));
-
- // Compile strings
-
- for (Iterator i = map.keySet().iterator(); i.hasNext();) {
- String key = (String) i.next();
- String value = (String) map.get(key);
-
- char ch = value.charAt(1); // Omit already precompiled patterns
- if (ch < OFFSET) {
- map.put(key, compilePattern(value)); // precompile all patterns
- }
- }
-
- // Add instruction alias to match anything
-
- StringBuffer buf = new StringBuffer("(");
-
- for (short i = 0; i < NO_OPCODES; i++) {
- if (Constants.NO_OF_OPERANDS[i] != Constants.UNDEFINED) { // Not an
- // invalid
- // opcode
- buf.append(makeChar(i));
-
- if (i < NO_OPCODES - 1)
- buf.append('|');
- }
- }
- buf.append(')');
-
- map.put("instruction", buf.toString());
- }
-
- private static String precompile(short from, short to, short extra) {
- StringBuffer buf = new StringBuffer("(");
-
- for (short i = from; i <= to; i++) {
- buf.append(makeChar(i));
- buf.append('|');
- }
-
- buf.append(makeChar(extra));
- buf.append(")");
- return buf.toString();
- }
-
- /*
- * Internal debugging routines.
- */
- private static final String pattern2string(String pattern) {
- return pattern2string(pattern, true);
- }
-
- private static final String pattern2string(String pattern, boolean make_string) {
- StringBuffer buf = new StringBuffer();
-
- for (int i = 0; i < pattern.length(); i++) {
- char ch = pattern.charAt(i);
-
- if (ch >= OFFSET) {
- if (make_string)
- buf.append(Constants.OPCODE_NAMES[ch - OFFSET]);
- else
- buf.append((int) (ch - OFFSET));
- } else
- buf.append(ch);
- }
- return buf.toString();
- }
+ private static final int OFFSET = 32767; // char + OFFSET is
+ // outside of
+ // LATIN-1
+ private static final int NO_OPCODES = 256; // Potential number,
+ // some are not used
+ private static final Map map = new HashMap(); // Map<String,Pattern>
+ private InstructionList il;
+ private String il_string; // instruction list
+ // as string
+ private InstructionHandle[] handles; // map instruction
+
+
+ // list to array
+ /**
+ * @param il
+ * instruction list to search for given patterns
+ */
+ public InstructionFinder(InstructionList il) {
+ this.il = il;
+ reread();
+ }
+
+
+ /**
+ * Reread the instruction list, e.g., after you've altered the list upon a
+ * match.
+ */
+ public final void reread() {
+ int size = il.getLength();
+ char[] buf = new char[size]; // Create a string with length equal to il
+ // length
+ handles = il.getInstructionHandles();
+ // Map opcodes to characters
+ for (int i = 0; i < size; i++) {
+ buf[i] = makeChar(handles[i].getInstruction().getOpcode());
+ }
+ il_string = new String(buf);
+ }
+
+
+ /**
+ * Map symbolic instruction names like "getfield" to a single character.
+ *
+ * @param pattern
+ * instruction pattern in lower case
+ * @return encoded string for a pattern such as "BranchInstruction".
+ */
+ private static final String mapName( String pattern ) {
+ String result = (String) map.get(pattern);
+ if (result != null) {
+ return result;
+ }
+ for (short i = 0; i < NO_OPCODES; i++) {
+ if (pattern.equals(Constants.OPCODE_NAMES[i])) {
+ return "" + makeChar(i);
+ }
+ }
+ throw new RuntimeException("Instruction unknown: " + pattern);
+ }
+
+
+ /**
+ * Replace symbolic names of instructions with the appropiate character and
+ * remove all white space from string. Meta characters such as +, * are
+ * ignored.
+ *
+ * @param pattern
+ * The pattern to compile
+ * @return translated regular expression string
+ */
+ private static final String compilePattern( String pattern ) {
+ //Bug: 38787 - Instructions are assumed to be english, to avoid odd Locale issues
+ String lower = pattern.toLowerCase(Locale.ENGLISH);
+ StringBuffer buf = new StringBuffer();
+ int size = pattern.length();
+ for (int i = 0; i < size; i++) {
+ char ch = lower.charAt(i);
+ if (Character.isLetterOrDigit(ch)) {
+ StringBuffer name = new StringBuffer();
+ while ((Character.isLetterOrDigit(ch) || ch == '_') && i < size) {
+ name.append(ch);
+ if (++i < size) {
+ ch = lower.charAt(i);
+ } else {
+ break;
+ }
+ }
+ i--;
+ buf.append(mapName(name.toString()));
+ } else if (!Character.isWhitespace(ch)) {
+ buf.append(ch);
+ }
+ }
+ return buf.toString();
+ }
+
+
+ /**
+ * @return the matched piece of code as an array of instruction (handles)
+ */
+ private InstructionHandle[] getMatch( int matched_from, int match_length ) {
+ InstructionHandle[] match = new InstructionHandle[match_length];
+ System.arraycopy(handles, matched_from, match, 0, match_length);
+ return match;
+ }
+
+
+ /**
+ * Search for the given pattern in the instruction list. You can search for
+ * any valid opcode via its symbolic name, e.g. "istore". You can also use a
+ * super class or an interface name to match a whole set of instructions, e.g.
+ * "BranchInstruction" or "LoadInstruction". "istore" is also an alias for all
+ * "istore_x" instructions. Additional aliases are "if" for "ifxx", "if_icmp"
+ * for "if_icmpxx", "if_acmp" for "if_acmpxx".
+ *
+ * Consecutive instruction names must be separated by white space which will
+ * be removed during the compilation of the pattern.
+ *
+ * For the rest the usual pattern matching rules for regular expressions
+ * apply.
+ * <P>
+ * Example pattern:
+ *
+ * <pre>
+ * search("BranchInstruction NOP ((IfInstruction|GOTO)+ ISTORE Instruction)*");
+ * </pre>
+ *
+ * <p>
+ * If you alter the instruction list upon a match such that other matching
+ * areas are affected, you should call reread() to update the finder and call
+ * search() again, because the matches are cached.
+ *
+ * @param pattern
+ * the instruction pattern to search for, where case is ignored
+ * @param from
+ * where to start the search in the instruction list
+ * @param constraint
+ * optional CodeConstraint to check the found code pattern for
+ * user-defined constraints
+ * @return iterator of matches where e.nextElement() returns an array of
+ * instruction handles describing the matched area
+ */
+ public final Iterator search( String pattern, InstructionHandle from, CodeConstraint constraint ) {
+ String search = compilePattern(pattern);
+ int start = -1;
+ for (int i = 0; i < handles.length; i++) {
+ if (handles[i] == from) {
+ start = i; // Where to start search from (index)
+ break;
+ }
+ }
+ if (start == -1) {
+ throw new ClassGenException("Instruction handle " + from
+ + " not found in instruction list.");
+ }
+ Pattern regex = Pattern.compile(search);
+ List matches = new ArrayList();
+ Matcher matcher = regex.matcher(il_string);
+ while (start < il_string.length() && matcher.find(start)) {
+ int startExpr = matcher.start();
+ int endExpr = matcher.end();
+ int lenExpr = (endExpr - startExpr) + 1;
+ InstructionHandle[] match = getMatch(startExpr, lenExpr);
+ if ((constraint == null) || constraint.checkCode(match)) {
+ matches.add(match);
+ }
+ start = endExpr;
+ }
+ return matches.iterator();
+ }
+
+
+ /**
+ * Start search beginning from the start of the given instruction list.
+ *
+ * @param pattern
+ * the instruction pattern to search for, where case is ignored
+ * @return iterator of matches where e.nextElement() returns an array of
+ * instruction handles describing the matched area
+ */
+ public final Iterator search( String pattern ) {
+ return search(pattern, il.getStart(), null);
+ }
+
+
+ /**
+ * Start search beginning from `from'.
+ *
+ * @param pattern
+ * the instruction pattern to search for, where case is ignored
+ * @param from
+ * where to start the search in the instruction list
+ * @return iterator of matches where e.nextElement() returns an array of
+ * instruction handles describing the matched area
+ */
+ public final Iterator search( String pattern, InstructionHandle from ) {
+ return search(pattern, from, null);
+ }
+
+
+ /**
+ * Start search beginning from the start of the given instruction list. Check
+ * found matches with the constraint object.
+ *
+ * @param pattern
+ * the instruction pattern to search for, case is ignored
+ * @param constraint
+ * constraints to be checked on matching code
+ * @return instruction handle or `null' if the match failed
+ */
+ public final Iterator search( String pattern, CodeConstraint constraint ) {
+ return search(pattern, il.getStart(), constraint);
+ }
+
+
+ /**
+ * Convert opcode number to char.
+ */
+ private static final char makeChar( short opcode ) {
+ return (char) (opcode + OFFSET);
+ }
+
+
+ /**
+ * @return the inquired instruction list
+ */
+ public final InstructionList getInstructionList() {
+ return il;
+ }
+
+ /**
+ * Code patterns found may be checked using an additional user-defined
+ * constraint object whether they really match the needed criterion. I.e.,
+ * check constraints that can not expressed with regular expressions.
+ *
+ */
+ public static interface CodeConstraint {
+
+ /**
+ * @param match
+ * array of instructions matching the requested pattern
+ * @return true if the matched area is really useful
+ */
+ public boolean checkCode( InstructionHandle[] match );
+ }
+
+ // Initialize pattern map
+ static {
+ map
+ .put(
+ "arithmeticinstruction",
+ "(irem|lrem|iand|ior|ineg|isub|lneg|fneg|fmul|ldiv|fadd|lxor|frem|idiv|land|ixor|ishr|fsub|lshl|fdiv|iadd|lor|dmul|lsub|ishl|imul|lmul|lushr|dneg|iushr|lshr|ddiv|drem|dadd|ladd|dsub)");
+ map.put("invokeinstruction", "(invokevirtual|invokeinterface|invokestatic|invokespecial)");
+ map
+ .put(
+ "arrayinstruction",
+ "(baload|aastore|saload|caload|fastore|lastore|iaload|castore|iastore|aaload|bastore|sastore|faload|laload|daload|dastore)");
+ map.put("gotoinstruction", "(goto|goto_w)");
+ map.put("conversioninstruction",
+ "(d2l|l2d|i2s|d2i|l2i|i2b|l2f|d2f|f2i|i2d|i2l|f2d|i2c|f2l|i2f)");
+ map.put("localvariableinstruction",
+ "(fstore|iinc|lload|dstore|dload|iload|aload|astore|istore|fload|lstore)");
+ map.put("loadinstruction", "(fload|dload|lload|iload|aload)");
+ map.put("fieldinstruction", "(getfield|putstatic|getstatic|putfield)");
+ map
+ .put(
+ "cpinstruction",
+ "(ldc2_w|invokeinterface|multianewarray|putstatic|instanceof|getstatic|checkcast|getfield|invokespecial|ldc_w|invokestatic|invokevirtual|putfield|ldc|new|anewarray)");
+ map.put("stackinstruction", "(dup2|swap|dup2_x2|pop|pop2|dup|dup2_x1|dup_x2|dup_x1)");
+ map
+ .put(
+ "branchinstruction",
+ "(ifle|if_acmpne|if_icmpeq|if_acmpeq|ifnonnull|goto_w|iflt|ifnull|if_icmpne|tableswitch|if_icmple|ifeq|if_icmplt|jsr_w|if_icmpgt|ifgt|jsr|goto|ifne|ifge|lookupswitch|if_icmpge)");
+ map.put("returninstruction", "(lreturn|ireturn|freturn|dreturn|areturn|return)");
+ map.put("storeinstruction", "(istore|fstore|dstore|astore|lstore)");
+ map.put("select", "(tableswitch|lookupswitch)");
+ map
+ .put(
+ "ifinstruction",
+ "(ifeq|ifgt|if_icmpne|if_icmpeq|ifge|ifnull|ifne|if_icmple|if_icmpge|if_acmpeq|if_icmplt|if_acmpne|ifnonnull|iflt|if_icmpgt|ifle)");
+ map.put("jsrinstruction", "(jsr|jsr_w)");
+ map.put("variablelengthinstruction", "(tableswitch|jsr|goto|lookupswitch)");
+ map.put("unconditionalbranch", "(goto|jsr|jsr_w|athrow|goto_w)");
+ map.put("constantpushinstruction", "(dconst|bipush|sipush|fconst|iconst|lconst)");
+ map
+ .put(
+ "typedinstruction",
+ "(imul|lsub|aload|fload|lor|new|aaload|fcmpg|iand|iaload|lrem|idiv|d2l|isub|dcmpg|dastore|ret|f2d|f2i|drem|iinc|i2c|checkcast|frem|lreturn|astore|lushr|daload|dneg|fastore|istore|lshl|ldiv|lstore|areturn|ishr|ldc_w|invokeinterface|aastore|lxor|ishl|l2d|i2f|return|faload|sipush|iushr|caload|instanceof|invokespecial|putfield|fmul|ireturn|laload|d2f|lneg|ixor|i2l|fdiv|lastore|multianewarray|i2b|getstatic|i2d|putstatic|fcmpl|saload|ladd|irem|dload|jsr_w|dconst|dcmpl|fsub|freturn|ldc|aconst_null|castore|lmul|ldc2_w|dadd|iconst|f2l|ddiv|dstore|land|jsr|anewarray|dmul|bipush|dsub|sastore|d2i|i2s|lshr|iadd|l2i|lload|bastore|fstore|fneg|iload|fadd|baload|fconst|ior|ineg|dreturn|l2f|lconst|getfield|invokevirtual|invokestatic|iastore)");
+ map.put("popinstruction", "(fstore|dstore|pop|pop2|astore|putstatic|istore|lstore)");
+ map.put("allocationinstruction", "(multianewarray|new|anewarray|newarray)");
+ map
+ .put(
+ "indexedinstruction",
+ "(lload|lstore|fload|ldc2_w|invokeinterface|multianewarray|astore|dload|putstatic|instanceof|getstatic|checkcast|getfield|invokespecial|dstore|istore|iinc|ldc_w|ret|fstore|invokestatic|iload|putfield|invokevirtual|ldc|new|aload|anewarray)");
+ map
+ .put(
+ "pushinstruction",
+ "(dup|lload|dup2|bipush|fload|ldc2_w|sipush|lconst|fconst|dload|getstatic|ldc_w|aconst_null|dconst|iload|ldc|iconst|aload)");
+ map
+ .put(
+ "stackproducer",
+ "(imul|lsub|aload|fload|lor|new|aaload|fcmpg|iand|iaload|lrem|idiv|d2l|isub|dcmpg|dup|f2d|f2i|drem|i2c|checkcast|frem|lushr|daload|dneg|lshl|ldiv|ishr|ldc_w|invokeinterface|lxor|ishl|l2d|i2f|faload|sipush|iushr|caload|instanceof|invokespecial|fmul|laload|d2f|lneg|ixor|i2l|fdiv|getstatic|i2b|swap|i2d|dup2|fcmpl|saload|ladd|irem|dload|jsr_w|dconst|dcmpl|fsub|ldc|arraylength|aconst_null|tableswitch|lmul|ldc2_w|iconst|dadd|f2l|ddiv|land|jsr|anewarray|dmul|bipush|dsub|d2i|newarray|i2s|lshr|iadd|lload|l2i|fneg|iload|fadd|baload|fconst|lookupswitch|ior|ineg|lconst|l2f|getfield|invokevirtual|invokestatic)");
+ map
+ .put(
+ "stackconsumer",
+ "(imul|lsub|lor|iflt|fcmpg|if_icmpgt|iand|ifeq|if_icmplt|lrem|ifnonnull|idiv|d2l|isub|dcmpg|dastore|if_icmpeq|f2d|f2i|drem|i2c|checkcast|frem|lreturn|astore|lushr|pop2|monitorexit|dneg|fastore|istore|lshl|ldiv|lstore|areturn|if_icmpge|ishr|monitorenter|invokeinterface|aastore|lxor|ishl|l2d|i2f|return|iushr|instanceof|invokespecial|fmul|ireturn|d2f|lneg|ixor|pop|i2l|ifnull|fdiv|lastore|i2b|if_acmpeq|ifge|swap|i2d|putstatic|fcmpl|ladd|irem|dcmpl|fsub|freturn|ifgt|castore|lmul|dadd|f2l|ddiv|dstore|land|if_icmpne|if_acmpne|dmul|dsub|sastore|ifle|d2i|i2s|lshr|iadd|l2i|bastore|fstore|fneg|fadd|ior|ineg|ifne|dreturn|l2f|if_icmple|getfield|invokevirtual|invokestatic|iastore)");
+ map
+ .put(
+ "exceptionthrower",
+ "(irem|lrem|laload|putstatic|baload|dastore|areturn|getstatic|ldiv|anewarray|iastore|castore|idiv|saload|lastore|fastore|putfield|lreturn|caload|getfield|return|aastore|freturn|newarray|instanceof|multianewarray|athrow|faload|iaload|aaload|dreturn|monitorenter|checkcast|bastore|arraylength|new|invokevirtual|sastore|ldc_w|ireturn|invokespecial|monitorexit|invokeinterface|ldc|invokestatic|daload)");
+ map
+ .put(
+ "loadclass",
+ "(multianewarray|invokeinterface|instanceof|invokespecial|putfield|checkcast|putstatic|invokevirtual|new|getstatic|invokestatic|getfield|anewarray)");
+ map
+ .put(
+ "instructiontargeter",
+ "(ifle|if_acmpne|if_icmpeq|if_acmpeq|ifnonnull|goto_w|iflt|ifnull|if_icmpne|tableswitch|if_icmple|ifeq|if_icmplt|jsr_w|if_icmpgt|ifgt|jsr|goto|ifne|ifge|lookupswitch|if_icmpge)");
+ // Some aliases
+ map.put("if_icmp", "(if_icmpne|if_icmpeq|if_icmple|if_icmpge|if_icmplt|if_icmpgt)");
+ map.put("if_acmp", "(if_acmpeq|if_acmpne)");
+ map.put("if", "(ifeq|ifne|iflt|ifge|ifgt|ifle)");
+ // Precompile some aliases first
+ map.put("iconst", precompile(Constants.ICONST_0, Constants.ICONST_5, Constants.ICONST_M1));
+ map.put("lconst", new String(new char[] {
+ '(', makeChar(Constants.LCONST_0), '|', makeChar(Constants.LCONST_1), ')'
+ }));
+ map.put("dconst", new String(new char[] {
+ '(', makeChar(Constants.DCONST_0), '|', makeChar(Constants.DCONST_1), ')'
+ }));
+ map.put("fconst", new String(new char[] {
+ '(', makeChar(Constants.FCONST_0), '|', makeChar(Constants.FCONST_1), ')'
+ }));
+ map.put("iload", precompile(Constants.ILOAD_0, Constants.ILOAD_3, Constants.ILOAD));
+ map.put("dload", precompile(Constants.DLOAD_0, Constants.DLOAD_3, Constants.DLOAD));
+ map.put("fload", precompile(Constants.FLOAD_0, Constants.FLOAD_3, Constants.FLOAD));
+ map.put("aload", precompile(Constants.ALOAD_0, Constants.ALOAD_3, Constants.ALOAD));
+ map.put("istore", precompile(Constants.ISTORE_0, Constants.ISTORE_3, Constants.ISTORE));
+ map.put("dstore", precompile(Constants.DSTORE_0, Constants.DSTORE_3, Constants.DSTORE));
+ map.put("fstore", precompile(Constants.FSTORE_0, Constants.FSTORE_3, Constants.FSTORE));
+ map.put("astore", precompile(Constants.ASTORE_0, Constants.ASTORE_3, Constants.ASTORE));
+ // Compile strings
+ for (Iterator i = map.keySet().iterator(); i.hasNext();) {
+ String key = (String) i.next();
+ String value = (String) map.get(key);
+ char ch = value.charAt(1); // Omit already precompiled patterns
+ if (ch < OFFSET) {
+ map.put(key, compilePattern(value)); // precompile all patterns
+ }
+ }
+ // Add instruction alias to match anything
+ StringBuffer buf = new StringBuffer("(");
+ for (short i = 0; i < NO_OPCODES; i++) {
+ if (Constants.NO_OF_OPERANDS[i] != Constants.UNDEFINED) { // Not an
+ // invalid
+ // opcode
+ buf.append(makeChar(i));
+ if (i < NO_OPCODES - 1) {
+ buf.append('|');
+ }
+ }
+ }
+ buf.append(')');
+ map.put("instruction", buf.toString());
+ }
+
+
+ private static String precompile( short from, short to, short extra ) {
+ StringBuffer buf = new StringBuffer("(");
+ for (short i = from; i <= to; i++) {
+ buf.append(makeChar(i));
+ buf.append('|');
+ }
+ buf.append(makeChar(extra));
+ buf.append(")");
+ return buf.toString();
+ }
+
+
+ /*
+ * Internal debugging routines.
+ */
+ private static final String pattern2string( String pattern ) {
+ return pattern2string(pattern, true);
+ }
+
+
+ private static final String pattern2string( String pattern, boolean make_string ) {
+ StringBuffer buf = new StringBuffer();
+ for (int i = 0; i < pattern.length(); i++) {
+ char ch = pattern.charAt(i);
+ if (ch >= OFFSET) {
+ if (make_string) {
+ buf.append(Constants.OPCODE_NAMES[ch - OFFSET]);
+ } else {
+ buf.append((ch - OFFSET));
+ }
+ } else {
+ buf.append(ch);
+ }
+ }
+ return buf.toString();
+ }
}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/util/JavaWrapper.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/util/JavaWrapper.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/util/JavaWrapper.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/util/JavaWrapper.java Wed Mar 15 03:31:56 2006
@@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- */
+ */
package org.apache.bcel.util;
-
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -37,80 +36,82 @@
* @see ClassLoader
*/
public class JavaWrapper {
- private java.lang.ClassLoader loader;
- private static java.lang.ClassLoader getClassLoader() {
- String s = System.getProperty("bcel.classloader");
+ private java.lang.ClassLoader loader;
- if((s == null) || "".equals(s))
- s = "org.apache.bcel.util.ClassLoader";
- try {
- return (java.lang.ClassLoader)Class.forName(s).newInstance();
- } catch(Exception e) {
- throw new RuntimeException(e.toString());
+ private static java.lang.ClassLoader getClassLoader() {
+ String s = System.getProperty("bcel.classloader");
+ if ((s == null) || "".equals(s)) {
+ s = "org.apache.bcel.util.ClassLoader";
+ }
+ try {
+ return (java.lang.ClassLoader) Class.forName(s).newInstance();
+ } catch (Exception e) {
+ throw new RuntimeException(e.toString());
+ }
}
- }
-
- public JavaWrapper(java.lang.ClassLoader loader) {
- this.loader = loader;
- }
-
- public JavaWrapper() {
- this(getClassLoader());
- }
-
- /** Runs the main method of the given class with the arguments passed in argv
- *
- * @param class_name the fully qualified class name
- * @param argv the arguments just as you would pass them directly
- */
- public void runMain(String class_name, String[] argv) throws ClassNotFoundException
- {
- Class cl = loader.loadClass(class_name);
- Method method = null;
-
- try {
- method = cl.getMethod("main", new Class[] { argv.getClass() });
-
- /* Method main is sane ?
- */
- int m = method.getModifiers();
- Class r = method.getReturnType();
-
- if(!(Modifier.isPublic(m) && Modifier.isStatic(m)) ||
- Modifier.isAbstract(m) || (r != Void.TYPE))
- throw new NoSuchMethodException();
- } catch(NoSuchMethodException no) {
- System.out.println("In class " + class_name +
- ": public static void main(String[] argv) is not defined");
- return;
+
+
+ public JavaWrapper(java.lang.ClassLoader loader) {
+ this.loader = loader;
}
-
- try {
- method.invoke(null, new Object[] { argv });
- } catch(Exception ex) {
- ex.printStackTrace();
+
+
+ public JavaWrapper() {
+ this(getClassLoader());
}
- }
- /** Default main method used as wrapper, expects the fully qualified class name
- * of the real class as the first argument.
- */
- public static void main(String[] argv) throws Exception {
- /* Expects class name as first argument, other arguments are by-passed.
+
+ /** Runs the main method of the given class with the arguments passed in argv
+ *
+ * @param class_name the fully qualified class name
+ * @param argv the arguments just as you would pass them directly
*/
- if(argv.length == 0) {
- System.out.println("Missing class name.");
- return;
+ public void runMain( String class_name, String[] argv ) throws ClassNotFoundException {
+ Class cl = loader.loadClass(class_name);
+ Method method = null;
+ try {
+ method = cl.getMethod("main", new Class[] {
+ argv.getClass()
+ });
+ /* Method main is sane ?
+ */
+ int m = method.getModifiers();
+ Class r = method.getReturnType();
+ if (!(Modifier.isPublic(m) && Modifier.isStatic(m)) || Modifier.isAbstract(m)
+ || (r != Void.TYPE)) {
+ throw new NoSuchMethodException();
+ }
+ } catch (NoSuchMethodException no) {
+ System.out.println("In class " + class_name
+ + ": public static void main(String[] argv) is not defined");
+ return;
+ }
+ try {
+ method.invoke(null, new Object[] {
+ argv
+ });
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
}
- String class_name = argv[0];
- String[] new_argv = new String[argv.length - 1];
- System.arraycopy(argv, 1, new_argv, 0, new_argv.length);
-
- JavaWrapper wrapper = new JavaWrapper();
- wrapper.runMain(class_name, new_argv);
- }
-}
+ /** Default main method used as wrapper, expects the fully qualified class name
+ * of the real class as the first argument.
+ */
+ public static void main( String[] argv ) throws Exception {
+ /* Expects class name as first argument, other arguments are by-passed.
+ */
+ if (argv.length == 0) {
+ System.out.println("Missing class name.");
+ return;
+ }
+ String class_name = argv[0];
+ String[] new_argv = new String[argv.length - 1];
+ System.arraycopy(argv, 1, new_argv, 0, new_argv.length);
+ JavaWrapper wrapper = new JavaWrapper();
+ wrapper.runMain(class_name, new_argv);
+ }
+}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/util/MethodHTML.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/util/MethodHTML.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/util/MethodHTML.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/util/MethodHTML.java Wed Mar 15 03:31:56 2006
@@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- */
+ */
package org.apache.bcel.util;
-
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
@@ -36,135 +35,122 @@
*
*/
final class MethodHTML implements org.apache.bcel.Constants {
- private String class_name; // name of current class
- private PrintWriter file; // file to write to
- private ConstantHTML constant_html;
- private AttributeHTML attribute_html;
-
- MethodHTML(String dir, String class_name,
- Method[] methods, Field[] fields,
- ConstantHTML constant_html, AttributeHTML attribute_html) throws IOException
- {
- this.class_name = class_name;
- this.attribute_html = attribute_html;
- this.constant_html = 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>" +
- "<TH ALIGN=LEFT>Field name</TH></TR>");
- for(int i=0; i < fields.length; i++)
- writeField(fields[i]);
- file.println("</TABLE>");
-
- file.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();
- }
-
- /**
- * Print field of class.
- *
- * @param field field to print
- * @exception java.io.IOException
- */
- private void writeField(Field field) throws IOException {
- String type = Utility.signatureToString(field.getSignature());
- String name = field.getName();
- String access = Utility.accessToString(field.getAccessFlags());
- Attribute[] attributes;
-
- access = Utility.replace(access, " ", " ");
-
- file.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();
-
- // Write them to the Attributes.html file with anchor "<name>[<i>]"
- for(int i=0; i < attributes.length; i++)
- attribute_html.writeAttribute(attributes[i], name + "@" + i);
-
- for(int i=0; i < attributes.length; i++) {
- if(attributes[i].getTag() == ATTR_CONSTANT_VALUE) { // Default value
- String str = ((ConstantValue)attributes[i]).toString();
-
- // Reference attribute in _attributes.html
- file.print("<TD>= <A HREF=\"" + class_name + "_attributes.html#" +
- name + "@" + i + "\" TARGET=\"Attributes\">" + str + "</TD>\n");
- break;
- }
- }
-
- file.println("</TR>");
- }
-
- private final void writeMethod(Method method, int method_number) throws IOException {
- // Get raw signature
- String signature = method.getSignature();
- // Get array of strings containing the argument types
- String[] args = Utility.methodSignatureArgumentTypes(signature, false);
- // Get return type string
- String type = Utility.methodSignatureReturnType(signature, false);
- // Get method name
- String name = method.getName(), html_name;
- // Get method's access flags
- String access = Utility.accessToString(method.getAccessFlags());
- // Get the method's attributes, the Code Attribute in particular
- Attribute[] attributes = method.getAttributes();
- /* HTML doesn't like names like <clinit> and spaces are places to break
- * lines. Both we don't want...
- */
- access = Utility.replace(access, " ", " ");
- html_name = Class2HTML.toHTML(name);
+ private String class_name; // name of current class
+ private PrintWriter file; // file to write to
+ private ConstantHTML constant_html;
+ private AttributeHTML attribute_html;
+
+
+ MethodHTML(String dir, String class_name, Method[] methods, Field[] fields,
+ ConstantHTML constant_html, AttributeHTML attribute_html) throws IOException {
+ this.class_name = class_name;
+ this.attribute_html = attribute_html;
+ this.constant_html = 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>"
+ + "<TH ALIGN=LEFT>Field name</TH></TR>");
+ for (int i = 0; i < fields.length; i++) {
+ writeField(fields[i]);
+ }
+ file.println("</TABLE>");
+ file.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();
+ }
- file.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=" + class_name + "_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]));
- if(i < args.length - 1)
- file.print(", ");
+ /**
+ * Print field of class.
+ *
+ * @param field field to print
+ * @exception java.io.IOException
+ */
+ private void writeField( Field field ) throws IOException {
+ String type = Utility.signatureToString(field.getSignature());
+ String name = field.getName();
+ String access = Utility.accessToString(field.getAccessFlags());
+ Attribute[] attributes;
+ access = Utility.replace(access, " ", " ");
+ file.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();
+ // Write them to the Attributes.html file with anchor "<name>[<i>]"
+ for (int i = 0; i < attributes.length; i++) {
+ attribute_html.writeAttribute(attributes[i], name + "@" + i);
+ }
+ for (int i = 0; i < attributes.length; i++) {
+ if (attributes[i].getTag() == ATTR_CONSTANT_VALUE) { // Default value
+ String str = ((ConstantValue) attributes[i]).toString();
+ // Reference attribute in _attributes.html
+ file.print("<TD>= <A HREF=\"" + class_name + "_attributes.html#" + name + "@" + i
+ + "\" TARGET=\"Attributes\">" + str + "</TD>\n");
+ break;
+ }
+ }
+ file.println("</TR>");
}
- file.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);
-
- byte tag = attributes[i].getTag();
- if(tag == ATTR_EXCEPTIONS) {
- file.print("<TR VALIGN=TOP><TD COLSPAN=2></TD><TH ALIGN=LEFT>throws</TH><TD>");
- int[] exceptions = ((ExceptionTable)attributes[i]).getExceptionIndexTable();
-
- for(int j=0; j < exceptions.length; j++) {
- file.print(constant_html.referenceConstant(exceptions[j]));
-
- if(j < exceptions.length - 1)
- file.print(", ");
- }
- file.println("</TD></TR>");
- } else if(tag == ATTR_CODE) {
- Attribute[] c_a = ((Code)attributes[i]).getAttributes();
-
- for(int j=0; j < c_a.length; j++)
- attribute_html.writeAttribute(c_a[j], "method" + method_number + "@" + i + "@" + j,
- method_number);
- }
+ private final void writeMethod( Method method, int method_number ) throws IOException {
+ // Get raw signature
+ String signature = method.getSignature();
+ // Get array of strings containing the argument types
+ String[] args = Utility.methodSignatureArgumentTypes(signature, false);
+ // Get return type string
+ String type = Utility.methodSignatureReturnType(signature, false);
+ // Get method name
+ String name = method.getName(), html_name;
+ // Get method's access flags
+ String access = Utility.accessToString(method.getAccessFlags());
+ // Get the method's attributes, the Code Attribute in particular
+ Attribute[] attributes = method.getAttributes();
+ /* HTML doesn't like names like <clinit> and spaces are places to break
+ * lines. Both we don't want...
+ */
+ access = Utility.replace(access, " ", " ");
+ html_name = Class2HTML.toHTML(name);
+ file.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=" + class_name
+ + "_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]));
+ if (i < args.length - 1) {
+ file.print(", ");
+ }
+ }
+ file.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);
+ byte tag = attributes[i].getTag();
+ if (tag == ATTR_EXCEPTIONS) {
+ file.print("<TR VALIGN=TOP><TD COLSPAN=2></TD><TH ALIGN=LEFT>throws</TH><TD>");
+ int[] exceptions = ((ExceptionTable) attributes[i]).getExceptionIndexTable();
+ for (int j = 0; j < exceptions.length; j++) {
+ file.print(constant_html.referenceConstant(exceptions[j]));
+ if (j < exceptions.length - 1) {
+ file.print(", ");
+ }
+ }
+ file.println("</TD></TR>");
+ } else if (tag == ATTR_CODE) {
+ Attribute[] c_a = ((Code) attributes[i]).getAttributes();
+ for (int j = 0; j < c_a.length; j++) {
+ attribute_html.writeAttribute(c_a[j], "method" + method_number + "@" + i + "@"
+ + j, method_number);
+ }
+ }
+ }
}
- }
}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/util/Repository.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/util/Repository.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/util/Repository.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/util/Repository.java Wed Mar 15 03:31:56 2006
@@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- */
+ */
package org.apache.bcel.util;
-
import org.apache.bcel.classfile.JavaClass;
/**
@@ -30,44 +29,45 @@
* @author David Dixon-Peugh
*/
public interface Repository extends java.io.Serializable {
- /**
- * Store the provided class under "clazz.getClassName()"
- */
- public void storeClass(JavaClass clazz);
-
- /**
- * Remove class from repository
- */
- public void removeClass(JavaClass clazz);
-
- /**
- * Find the class with the name provided, if the class
- * isn't there, return NULL.
- */
- public JavaClass findClass(String className);
-
- /**
- * Find the class with the name provided, if the class
- * isn't there, make an attempt to load it.
- */
- public JavaClass loadClass(String className)
- throws java.lang.ClassNotFoundException;
-
- /**
- * Find the JavaClass instance for the given run-time class object
- */
- public JavaClass loadClass(Class clazz)
- throws java.lang.ClassNotFoundException;
-
- /** Clear all entries from cache.
- */
- public void clear();
-
- /** Get the ClassPath associated with this Repository
- */
- public ClassPath getClassPath();
-}
+ /**
+ * Store the provided class under "clazz.getClassName()"
+ */
+ public void storeClass( JavaClass clazz );
+
+
+ /**
+ * Remove class from repository
+ */
+ public void removeClass( JavaClass clazz );
+
+
+ /**
+ * Find the class with the name provided, if the class
+ * isn't there, return NULL.
+ */
+ public JavaClass findClass( String className );
+ /**
+ * Find the class with the name provided, if the class
+ * isn't there, make an attempt to load it.
+ */
+ public JavaClass loadClass( String className ) throws java.lang.ClassNotFoundException;
+
+ /**
+ * Find the JavaClass instance for the given run-time class object
+ */
+ public JavaClass loadClass( Class clazz ) throws java.lang.ClassNotFoundException;
+
+
+ /** Clear all entries from cache.
+ */
+ public void clear();
+
+
+ /** Get the ClassPath associated with this Repository
+ */
+ public ClassPath getClassPath();
+}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/util/SyntheticRepository.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/util/SyntheticRepository.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/util/SyntheticRepository.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/util/SyntheticRepository.java Wed Mar 15 03:31:56 2006
@@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- */
+ */
package org.apache.bcel.util;
-
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
@@ -35,7 +34,7 @@
* It is designed to be used as a singleton, however it
* can also be used with custom classpaths.
*
-/**
+ /**
* Abstract definition of a class repository. Instances may be used
* to load classes from different sources and may be used in the
* Repository.setRepository method.
@@ -47,149 +46,145 @@
* @author David Dixon-Peugh
*/
public class SyntheticRepository implements Repository {
- private static final String DEFAULT_PATH = ClassPath.getClassPath();
- private static Map _instances = new HashMap(); // CLASSPATH X REPOSITORY
-
- private ClassPath _path = null;
- private Map _loadedClasses = new HashMap(); // CLASSNAME X JAVACLASS
-
- private SyntheticRepository(ClassPath path) {
- _path = path;
- }
-
- public static SyntheticRepository getInstance() {
- return getInstance(ClassPath.SYSTEM_CLASS_PATH);
- }
-
- public static SyntheticRepository getInstance(ClassPath classPath) {
- SyntheticRepository rep = (SyntheticRepository)_instances.get(classPath);
-
- if (rep == null) {
- rep = new SyntheticRepository(classPath);
- _instances.put(classPath, rep);
- }
-
- return rep;
- }
-
- /**
- * Store a new JavaClass instance into this Repository.
- */
- public void storeClass(JavaClass clazz) {
- _loadedClasses.put(clazz.getClassName(), new SoftReference(clazz));
- clazz.setRepository(this);
- }
-
- /**
- * Remove class from repository
- */
- public void removeClass(JavaClass clazz) {
- _loadedClasses.remove(clazz.getClassName());
- }
-
- /**
- * Find an already defined (cached) JavaClass object by name.
- */
- public JavaClass findClass(String className) {
- SoftReference ref = (SoftReference)_loadedClasses.get(className);
- if (ref == null)
- return null;
- return (JavaClass)ref.get();
- }
-
- /**
- * Find a JavaClass object by name.
- * If it is already in this Repository, the Repository version
- * is returned. Otherwise, the Repository's classpath is searched for
- * the class (and it is added to the Repository if found).
- *
- * @param className the name of the class
- * @return the JavaClass object
- * @throws ClassNotFoundException if the class is not in the
- * Repository, and could not be found on the classpath
- */
- public JavaClass loadClass(String className) throws ClassNotFoundException {
- if (className == null || className.equals("")) {
- throw new IllegalArgumentException("Invalid class name " + className);
- }
-
- className = className.replace('/', '.'); // Just in case, canonical form
-
- JavaClass clazz = findClass(className);
- if (clazz != null) {
- return clazz;
- }
-
- try {
- return loadClass(_path.getInputStream(className), className);
- } catch (IOException e) {
- throw new ClassNotFoundException(
- "Exception while looking for class " + className + ": " + e.toString());
- }
- }
-
- /**
- * Find the JavaClass object for a runtime Class object.
- * If a class with the same name is already in this Repository,
- * the Repository version is returned. Otherwise, getResourceAsStream()
- * is called on the Class object to find the class's representation.
- * If the representation is found, it is added to the Repository.
- *
- * @see Class
- * @param clazz the runtime Class object
- * @return JavaClass object for given runtime class
- * @throws ClassNotFoundException if the class is not in the
- * Repository, and its representation could not be found
- */
- public JavaClass loadClass(Class clazz) throws ClassNotFoundException {
- String className = clazz.getName();
-
- JavaClass repositoryClass = findClass(className);
- if (repositoryClass != null) {
- return repositoryClass;
- }
-
- String name = className;
- int i = name.lastIndexOf('.');
-
- if (i > 0) {
- name = name.substring(i + 1);
- }
-
- return loadClass(clazz.getResourceAsStream(name + ".class"), className);
- }
-
- private JavaClass loadClass(InputStream is, String className)
- throws ClassNotFoundException {
-
- try {
- if (is != null) {
- ClassParser parser = new ClassParser(is, className);
- JavaClass clazz = parser.parse();
-
- storeClass(clazz);
-
- return clazz;
- }
- } catch (IOException e) {
- throw new ClassNotFoundException(
- "Exception while looking for class " + className + ": " + e.toString());
- }
-
- throw new ClassNotFoundException(
- "SyntheticRepository could not load " + className);
- }
-
- /** ClassPath associated with the Repository.
- */
- public ClassPath getClassPath() {
- return _path;
- }
-
- /** Clear all entries from cache.
- */
- public void clear() {
- _loadedClasses.clear();
- }
+ private static final String DEFAULT_PATH = ClassPath.getClassPath();
+ private static Map _instances = new HashMap(); // CLASSPATH X REPOSITORY
+ private ClassPath _path = null;
+ private Map _loadedClasses = new HashMap(); // CLASSNAME X JAVACLASS
+
+
+ private SyntheticRepository(ClassPath path) {
+ _path = path;
+ }
+
+
+ public static SyntheticRepository getInstance() {
+ return getInstance(ClassPath.SYSTEM_CLASS_PATH);
+ }
+
+
+ public static SyntheticRepository getInstance( ClassPath classPath ) {
+ SyntheticRepository rep = (SyntheticRepository) _instances.get(classPath);
+ if (rep == null) {
+ rep = new SyntheticRepository(classPath);
+ _instances.put(classPath, rep);
+ }
+ return rep;
+ }
+
+
+ /**
+ * Store a new JavaClass instance into this Repository.
+ */
+ public void storeClass( JavaClass clazz ) {
+ _loadedClasses.put(clazz.getClassName(), new SoftReference(clazz));
+ clazz.setRepository(this);
+ }
+
+
+ /**
+ * Remove class from repository
+ */
+ public void removeClass( JavaClass clazz ) {
+ _loadedClasses.remove(clazz.getClassName());
+ }
+
+
+ /**
+ * Find an already defined (cached) JavaClass object by name.
+ */
+ public JavaClass findClass( String className ) {
+ SoftReference ref = (SoftReference) _loadedClasses.get(className);
+ if (ref == null) {
+ return null;
+ }
+ return (JavaClass) ref.get();
+ }
+
+
+ /**
+ * Find a JavaClass object by name.
+ * If it is already in this Repository, the Repository version
+ * is returned. Otherwise, the Repository's classpath is searched for
+ * the class (and it is added to the Repository if found).
+ *
+ * @param className the name of the class
+ * @return the JavaClass object
+ * @throws ClassNotFoundException if the class is not in the
+ * Repository, and could not be found on the classpath
+ */
+ public JavaClass loadClass( String className ) throws ClassNotFoundException {
+ if (className == null || className.equals("")) {
+ throw new IllegalArgumentException("Invalid class name " + className);
+ }
+ className = className.replace('/', '.'); // Just in case, canonical form
+ JavaClass clazz = findClass(className);
+ if (clazz != null) {
+ return clazz;
+ }
+ try {
+ return loadClass(_path.getInputStream(className), className);
+ } catch (IOException e) {
+ throw new ClassNotFoundException("Exception while looking for class " + className
+ + ": " + e.toString());
+ }
+ }
+
+
+ /**
+ * Find the JavaClass object for a runtime Class object.
+ * If a class with the same name is already in this Repository,
+ * the Repository version is returned. Otherwise, getResourceAsStream()
+ * is called on the Class object to find the class's representation.
+ * If the representation is found, it is added to the Repository.
+ *
+ * @see Class
+ * @param clazz the runtime Class object
+ * @return JavaClass object for given runtime class
+ * @throws ClassNotFoundException if the class is not in the
+ * Repository, and its representation could not be found
+ */
+ public JavaClass loadClass( Class clazz ) throws ClassNotFoundException {
+ String className = clazz.getName();
+ JavaClass repositoryClass = findClass(className);
+ if (repositoryClass != null) {
+ return repositoryClass;
+ }
+ String name = className;
+ int i = name.lastIndexOf('.');
+ if (i > 0) {
+ name = name.substring(i + 1);
+ }
+ return loadClass(clazz.getResourceAsStream(name + ".class"), className);
+ }
+
+
+ private JavaClass loadClass( InputStream is, String className ) throws ClassNotFoundException {
+ try {
+ if (is != null) {
+ ClassParser parser = new ClassParser(is, className);
+ JavaClass clazz = parser.parse();
+ storeClass(clazz);
+ return clazz;
+ }
+ } catch (IOException e) {
+ throw new ClassNotFoundException("Exception while looking for class " + className
+ + ": " + e.toString());
+ }
+ throw new ClassNotFoundException("SyntheticRepository could not load " + className);
+ }
+
+
+ /** ClassPath associated with the Repository.
+ */
+ public ClassPath getClassPath() {
+ return _path;
+ }
+
+
+ /** Clear all entries from cache.
+ */
+ public void clear() {
+ _loadedClasses.clear();
+ }
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.