Search in sources :

Example 46 with Method

use of org.apache.bcel.classfile.Method in project jop by jop-devel.

the class JopMethodInfo method setInfo.

/**
 * Extract linking information for this method
 * @param addr the bytecode start address
 */
public void setInfo(int addr) {
    codeAddress = addr;
    Method m = getMethod();
    margs = 0;
    Type[] at = m.getArgumentTypes();
    for (int i = 0; i < at.length; ++i) {
        margs += at[i].getSize();
    }
    // for now only handle inits
    if (!m.isStatic()) {
        margs++;
    }
    if (m.isAbstract()) {
        mstack = mreallocals = len = exclen = 0;
        exctab = null;
    } else {
        mstack = m.getCode().getMaxStack();
        // the 'real' locals - means without arguments
        mreallocals = m.getCode().getMaxLocals() - margs;
        // System.err.println(" ++++++++++++ "+methodId+" --> mlocals
        // ="+mlocals+" margs ="+margs);
        len = (m.getCode().getCode().length + 3) / 4;
        exctab = m.getCode().getExceptionTable();
        exclen = exctab != null ? exctab.length : 0;
        // TODO: couldn't len=JOP...MAX_SIZE/4 be ok?
        if (len >= JOPizer.METHOD_MAX_SIZE / 4 || mreallocals > 31 || margs > 31) {
            // we interprete clinit on JOP - no size restriction
            if (!m.getName().equals("<clinit>")) {
                System.err.println("len(max:" + (JOPizer.METHOD_MAX_SIZE / 4) + ")=" + len + "mreallocals(max:31)=" + mreallocals + " margs(max:31)=" + margs);
                System.err.println("wrong size: " + getCli().clazz.getClassName() + "." + methodId);
                throw new Error();
            }
        }
    // System.out.println((mstack+m.getCode().getMaxLocals())+" "+
    // m.getName()+" maxStack="+mstack+"
    // locals="+m.getCode().getMaxLocals());
    }
}
Also used : Type(org.apache.bcel.generic.Type) Method(org.apache.bcel.classfile.Method)

Example 47 with Method

use of org.apache.bcel.classfile.Method in project jop by jop-devel.

the class ConstantPoolReferenceFinder method findPoolReferences.

// //////////////////////////////////////////////////////////////
// Find/replace references, member names, Pool entries
// //////////////////////////////////////////////////////////////
public static Set<Integer> findPoolReferences(ClassInfo classInfo, boolean checkMembers) {
    JavaClass javaClass = classInfo.compile();
    Set<Integer> ids = findPoolReferences(classInfo, javaClass);
    if (checkMembers) {
        for (Field field : javaClass.getFields()) {
            FieldInfo fieldInfo = classInfo.getFieldInfo(field.getName());
            ids.addAll(findPoolReferences(fieldInfo, field));
        }
        for (Method method : javaClass.getMethods()) {
            MethodInfo methodInfo = classInfo.getMethodInfo(method.getName() + method.getSignature());
            ids.addAll(findPoolReferences(methodInfo, method));
        }
    }
    return ids;
}
Also used : ConstantInteger(org.apache.bcel.classfile.ConstantInteger) Field(org.apache.bcel.classfile.Field) JavaClass(org.apache.bcel.classfile.JavaClass) MethodInfo(com.jopdesign.common.MethodInfo) ConstantMethodInfo(com.jopdesign.common.type.ConstantMethodInfo) Method(org.apache.bcel.classfile.Method) EnclosingMethod(com.jopdesign.common.bcel.EnclosingMethod) ConstantFieldInfo(com.jopdesign.common.type.ConstantFieldInfo) FieldInfo(com.jopdesign.common.FieldInfo)

Example 48 with Method

use of org.apache.bcel.classfile.Method in project contribution by checkstyle.

the class UnusedMethodCheck method leaveSet.

/**
 * @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor
 */
public void leaveSet(Set aJavaClasses) {
    final Iterator it = aJavaClasses.iterator();
    while (it.hasNext()) {
        final JavaClass javaClass = (JavaClass) it.next();
        final String className = javaClass.getClassName();
        final JavaClassDefinition classDef = findJavaClassDef(javaClass);
        final MethodDefinition[] methodDefs = classDef.getMethodDefs();
        for (int i = 0; i < methodDefs.length; i++) {
            if (!classDef.hasReference(methodDefs[i], getReferenceDAO())) {
                final Method method = methodDefs[i].getMethod();
                if (!ignore(className, method)) {
                    log(javaClass, 0, "unused.method", new Object[] { methodDefs[i] });
                }
            }
        }
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) MethodDefinition(com.puppycrawl.tools.checkstyle.bcel.classfile.MethodDefinition) Iterator(java.util.Iterator) JavaClassDefinition(com.puppycrawl.tools.checkstyle.bcel.classfile.JavaClassDefinition) Method(org.apache.bcel.classfile.Method)

Example 49 with Method

use of org.apache.bcel.classfile.Method in project fb-contrib by mebigfatguy.

the class NonRecycleableTaglibs method getAttributes.

/**
 * collect all possible attributes given the name of methods available.
 *
 * @param cls
 *            the class to look for setter methods to infer properties
 * @return the map of possible attributes/types
 */
private static Map<QMethod, String> getAttributes(JavaClass cls) {
    Map<QMethod, String> atts = new HashMap<>();
    Method[] methods = cls.getMethods();
    for (Method m : methods) {
        String name = m.getName();
        if (name.startsWith("set") && m.isPublic() && !m.isStatic()) {
            String sig = m.getSignature();
            List<String> args = SignatureUtils.getParameterSignatures(sig);
            if ((args.size() == 1) && Values.SIG_VOID.equals(SignatureUtils.getReturnSignature(sig))) {
                String parmSig = args.get(0);
                if (validAttrTypes.contains(parmSig)) {
                    Code code = m.getCode();
                    if ((code != null) && (code.getCode().length < MAX_ATTRIBUTE_CODE_LENGTH)) {
                        atts.put(new QMethod(name, sig), parmSig);
                    }
                }
            }
        }
    }
    return atts;
}
Also used : QMethod(com.mebigfatguy.fbcontrib.utils.QMethod) HashMap(java.util.HashMap) Method(org.apache.bcel.classfile.Method) QMethod(com.mebigfatguy.fbcontrib.utils.QMethod) Code(org.apache.bcel.classfile.Code)

Example 50 with Method

use of org.apache.bcel.classfile.Method in project fb-contrib by mebigfatguy.

the class NonSymmetricEquals method visitCode.

/**
 * implements the visitor to see if this method is equals(Object o)
 *
 * @param obj
 *            the context object of the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    Method m = getMethod();
    String name = m.getName();
    String signature = m.getSignature();
    if ("equals".equals(name) && SignatureBuilder.SIG_OBJECT_TO_BOOLEAN.equals(signature) && prescreen(m)) {
        stack.resetForMethodEntry(this);
        super.visitCode(obj);
    }
}
Also used : Method(org.apache.bcel.classfile.Method)

Aggregations

Method (org.apache.bcel.classfile.Method)79 JavaClass (org.apache.bcel.classfile.JavaClass)28 BugInstance (edu.umd.cs.findbugs.BugInstance)20 ToString (com.mebigfatguy.fbcontrib.utils.ToString)12 StopOpcodeParsingException (com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException)11 FQMethod (com.mebigfatguy.fbcontrib.utils.FQMethod)7 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 Field (org.apache.bcel.classfile.Field)6 Type (org.apache.bcel.generic.Type)6 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)5 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)5 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)4 SourceLineAnnotation (edu.umd.cs.findbugs.SourceLineAnnotation)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)4 BugType (com.mebigfatguy.fbcontrib.utils.BugType)3 QMethod (com.mebigfatguy.fbcontrib.utils.QMethod)3 XMethod (edu.umd.cs.findbugs.ba.XMethod)3