Java Code Examples for org.apache.bcel.classfile.ExceptionTable

The following examples show how to use org.apache.bcel.classfile.ExceptionTable. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: commons-bcel   Source File: MethodGen.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Return string representation close to declaration format,
 * `public static void main(String[]) throws IOException', e.g.
 *
 * @return String representation of the method.
 */
@Override
public final String toString() {
    final String access = Utility.accessToString(super.getAccessFlags());
    String signature = Type.getMethodSignature(super.getType(), argTypes);
    signature = Utility.methodSignatureToString(signature, super.getName(), access, true,
            getLocalVariableTable(super.getConstantPool()));
    final StringBuilder buf = new StringBuilder(signature);
    for (final Attribute a : getAttributes()) {
        if (!((a instanceof Code) || (a instanceof ExceptionTable))) {
            buf.append(" [").append(a).append("]");
        }
    }

    if (throwsList.size() > 0) {
        for (final String throwsDescriptor : throwsList) {
            buf.append("\n\t\tthrows ").append(throwsDescriptor);
        }
    }
    return buf.toString();
}
 
Example 2
HashSet<String> thrownExceptions(Method m) {
    HashSet<String> result = new HashSet<>();
    ExceptionTable exceptionTable = m.getExceptionTable();
    if (exceptionTable != null) {
        Collections.addAll(result, exceptionTable.getExceptionNames());
    }
    return result;
}
 
Example 3
@Override
public void visit(ExceptionTable obj) {
    if (DEBUG) {
        String names[] = obj.getExceptionNames();
        for (String name : names) {
            if ("java.lang.Exception".equals(name) || "java.lang.Throwable".equals(name)) {
                System.out.println(name + " thrown by " + getFullyQualifiedMethodName());
            }
        }
    }
}
 
Example 4
Source Project: commons-bcel   Source File: JasminVisitor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void visitExceptionTable(final ExceptionTable e) {
    for (final String name : e.getExceptionNames()) {
        out.println(".throws " + name.replace('.', '/'));
    }

    printEndMethod(e);
}
 
Example 5
Source Project: commons-bcel   Source File: MethodGen.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return `Exceptions' attribute of all the exceptions thrown by this method.
 */
private ExceptionTable getExceptionTable( final ConstantPoolGen cp ) {
    final int size = throwsList.size();
    final int[] ex = new int[size];
    for (int i = 0; i < size; i++) {
        ex[i] = cp.addClass(throwsList.get(i));
    }
    return new ExceptionTable(cp.addUtf8("Exceptions"), 2 + 2 * size, ex, cp.getConstantPool());
}
 
Example 6
public void visit(ExceptionTable obj) {
    visit((Attribute) obj);
}
 
Example 7
@Override
public void visitExceptionTable(ExceptionTable obj) {
    visit(obj);
}
 
Example 8
Source Project: commons-bcel   Source File: MethodGen.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Instantiate from existing method.
 *
 * @param method method
 * @param className class name containing this method
 * @param cp constant pool
 */
public MethodGen(final Method method, final String className, final ConstantPoolGen cp) {
    this(method.getAccessFlags(), Type.getReturnType(method.getSignature()),
        Type.getArgumentTypes(method.getSignature()), null /* may be overridden anyway */
        , method.getName(), className,
        ((method.getAccessFlags() & (Const.ACC_ABSTRACT | Const.ACC_NATIVE)) == 0)
            ? new InstructionList(getByteCodes(method))
            : null,
        cp);
    final Attribute[] attributes = method.getAttributes();
    for (final Attribute attribute : attributes) {
        Attribute a = attribute;
        if (a instanceof Code) {
            final Code c = (Code) a;
            setMaxStack(c.getMaxStack());
            setMaxLocals(c.getMaxLocals());
            final CodeException[] ces = c.getExceptionTable();
            if (ces != null) {
                for (final CodeException ce : ces) {
                    final int type = ce.getCatchType();
                    ObjectType c_type = null;
                    if (type > 0) {
                        final String cen = method.getConstantPool().getConstantString(type, Const.CONSTANT_Class);
                        c_type = ObjectType.getInstance(cen);
                    }
                    final int end_pc = ce.getEndPC();
                    final int length = getByteCodes(method).length;
                    InstructionHandle end;
                    if (length == end_pc) { // May happen, because end_pc is exclusive
                        end = il.getEnd();
                    } else {
                        end = il.findHandle(end_pc);
                        end = end.getPrev(); // Make it inclusive
                    }
                    addExceptionHandler(il.findHandle(ce.getStartPC()), end, il.findHandle(ce.getHandlerPC()),
                        c_type);
                }
            }
            final Attribute[] c_attributes = c.getAttributes();
            for (final Attribute c_attribute : c_attributes) {
                a = c_attribute;
                if (a instanceof LineNumberTable) {
                    final LineNumber[] ln = ((LineNumberTable) a).getLineNumberTable();
                    for (final LineNumber l : ln) {
                        final InstructionHandle ih = il.findHandle(l.getStartPC());
                        if (ih != null) {
                            addLineNumber(ih, l.getLineNumber());
                        }
                    }
                } else if (a instanceof LocalVariableTable) {
                    updateLocalVariableTable((LocalVariableTable) a);
                } else if (a instanceof LocalVariableTypeTable) {
                    this.localVariableTypeTable = (LocalVariableTypeTable) a.copy(cp.getConstantPool());
                } else {
                    addCodeAttribute(a);
                }
            }
        } else if (a instanceof ExceptionTable) {
            final String[] names = ((ExceptionTable) a).getExceptionNames();
            for (final String name2 : names) {
                addException(name2);
            }
        } else if (a instanceof Annotations) {
            final Annotations runtimeAnnotations = (Annotations) a;
            final AnnotationEntry[] aes = runtimeAnnotations.getAnnotationEntries();
            for (final AnnotationEntry element : aes) {
                addAnnotationEntry(new AnnotationEntryGen(element, cp, false));
            }
        } else {
            addAttribute(a);
        }
    }
}
 
Example 9
Source Project: commons-bcel   Source File: StringRepresentation.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void visitExceptionTable(final ExceptionTable obj) {
    tostring = toString(obj);
}
 
Example 10
Source Project: commons-bcel   Source File: MethodHTML.java    License: Apache License 2.0 4 votes vote down vote up
private void writeMethod( final Method method, final int method_number ) {
    // Get raw signature
    final String signature = method.getSignature();
    // Get array of strings containing the argument types
    final String[] args = Utility.methodSignatureArgumentTypes(signature, false);
    // Get return type string
    final String type = Utility.methodSignatureReturnType(signature, false);
    // Get method name
    final String name = method.getName();
    String html_name;
    // Get method's access flags
    String access = Utility.accessToString(method.getAccessFlags());
    // Get the method's attributes, the Code Attribute in particular
    final 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, " ", "&nbsp;");
    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=" + 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]));
        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);
        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>");
            final int[] exceptions = ((ExceptionTable) attributes[i]).getExceptionIndexTable();
            for (int j = 0; j < exceptions.length; j++) {
                file.print(constantHtml.referenceConstant(exceptions[j]));
                if (j < exceptions.length - 1) {
                    file.print(", ");
                }
            }
            file.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++) {
                attribute_html.writeAttribute(c_a[j], "method" + method_number + "@" + i + "@"
                        + j, method_number);
            }
        }
    }
}
 
Example 11
Source Project: commons-bcel   Source File: CounterVisitor.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void visitExceptionTable(final ExceptionTable obj)
{
    exceptionTableCount++;
}