Search in sources :

Example 36 with Method

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

the class ReplaceNativeAndCPIdx method visitJavaClass.

public void visitJavaClass(JavaClass clazz) {
    super.visitJavaClass(clazz);
    Method[] methods = clazz.getMethods();
    cp = clazz.getConstantPool();
    cpoolgen = new ConstantPoolGen(cp);
    for (int i = 0; i < methods.length; i++) {
        if (!(methods[i].isAbstract() || methods[i].isNative())) {
            Method m = replace(methods[i]);
            OldMethodInfo mi = getCli().getMethodInfo(m.getName() + m.getSignature());
            // set new method also in MethodInfo
            mi.setMethod(m);
            if (m != null) {
                methods[i] = m;
            }
        }
    }
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) Method(org.apache.bcel.classfile.Method)

Example 37 with Method

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

the class FindUsedConstants method visitJavaClass.

public void visitJavaClass(JavaClass clazz) {
    super.visitJavaClass(clazz);
    cpool = new ConstantPoolGen(clazz.getConstantPool());
    Method[] methods = clazz.getMethods();
    for (int i = 0; i < methods.length; i++) {
        if (!(methods[i].isAbstract() || methods[i].isNative())) {
            // methods[i] = find(methods[i]);
            find(methods[i]);
        }
    }
// clazz.setConstantPool(cpoolNew.getConstantPool());
// System.out.println(clazz.getConstantPool());
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) Method(org.apache.bcel.classfile.Method)

Example 38 with Method

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

the class OldClinitOrder method findDependencies.

private Set findDependencies(OldClassInfo cli, OldMethodInfo mi, boolean inRec) {
    // System.out.println("find dep. in "+cli.clazz.getClassName()+":"+mi.getMethod().getName());
    Method method = mi.getMethod();
    Set depends = new HashSet();
    if (method.isNative() || method.isAbstract()) {
        // subclasses???? :-(
        return depends;
    }
    ConstantPool cpool = cli.clazz.getConstantPool();
    ConstantPoolGen cpoolgen = new ConstantPoolGen(cpool);
    MethodGen mg = new MethodGen(method, cli.clazz.getClassName(), cpoolgen);
    InstructionList il = mg.getInstructionList();
    InstructionFinder f = new InstructionFinder(il);
    // find instructions that access the constant pool
    // collect all indices to constants in ClassInfo
    String cpInstr = "CPInstruction";
    for (Iterator it = f.search(cpInstr); it.hasNext(); ) {
        InstructionHandle[] match = (InstructionHandle[]) it.next();
        InstructionHandle first = match[0];
        CPInstruction ii = (CPInstruction) first.getInstruction();
        int idx = ii.getIndex();
        Constant co = cpool.getConstant(idx);
        ConstantClass cocl = null;
        Set addDepends = null;
        String clname;
        OldClassInfo clinfo;
        OldMethodInfo minfo;
        switch(co.getTag()) {
            case Constants.CONSTANT_Class:
                cocl = (ConstantClass) co;
                clname = cocl.getBytes(cpool).replace('/', '.');
                clinfo = (OldClassInfo) ai.cliMap.get(clname);
                if (clinfo != null) {
                    minfo = clinfo.getMethodInfo("<init>()V");
                    if (minfo != null) {
                        addDepends = findDependencies(clinfo, minfo, true);
                    }
                }
                break;
            case Constants.CONSTANT_InterfaceMethodref:
                cocl = (ConstantClass) cpool.getConstant(((ConstantInterfaceMethodref) co).getClassIndex());
                break;
            case Constants.CONSTANT_Methodref:
                cocl = (ConstantClass) cpool.getConstant(((ConstantMethodref) co).getClassIndex());
                clname = cocl.getBytes(cpool).replace('/', '.');
                clinfo = (OldClassInfo) ai.cliMap.get(clname);
                int sigidx = ((ConstantMethodref) co).getNameAndTypeIndex();
                ConstantNameAndType signt = (ConstantNameAndType) cpool.getConstant(sigidx);
                String sigstr = signt.getName(cpool) + signt.getSignature(cpool);
                if (clinfo != null) {
                    minfo = clinfo.getMethodInfo(sigstr);
                    if (minfo != null) {
                        addDepends = findDependencies(clinfo, minfo, true);
                    }
                }
                break;
            case Constants.CONSTANT_Fieldref:
                cocl = (ConstantClass) cpool.getConstant(((ConstantFieldref) co).getClassIndex());
                break;
        }
        if (cocl != null) {
            clname = cocl.getBytes(cpool).replace('/', '.');
            OldClassInfo clinf = (OldClassInfo) ai.cliMap.get(clname);
            if (clinf != null) {
                if (clinf.getMethodInfo(OldAppInfo.clinitSig) != null) {
                    // don't add myself as dependency
                    if (clinf != cli) {
                        depends.add(clinf);
                    }
                }
            }
        }
        if (addDepends != null) {
            Iterator itAddDep = addDepends.iterator();
            while (itAddDep.hasNext()) {
                OldClassInfo addCli = (OldClassInfo) itAddDep.next();
                if (addCli == cli) {
                    throw new Error("cyclic indirect <clinit> dependency");
                }
                depends.add(addCli);
            }
        }
    }
    il.dispose();
    return depends;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) InstructionList(org.apache.bcel.generic.InstructionList) Constant(org.apache.bcel.classfile.Constant) Method(org.apache.bcel.classfile.Method) InstructionFinder(org.apache.bcel.util.InstructionFinder) MethodGen(org.apache.bcel.generic.MethodGen) InstructionHandle(org.apache.bcel.generic.InstructionHandle) ConstantNameAndType(org.apache.bcel.classfile.ConstantNameAndType) ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) CPInstruction(org.apache.bcel.generic.CPInstruction) ConstantMethodref(org.apache.bcel.classfile.ConstantMethodref) ConstantPool(org.apache.bcel.classfile.ConstantPool) Iterator(java.util.Iterator) ConstantClass(org.apache.bcel.classfile.ConstantClass) HashSet(java.util.HashSet)

Example 39 with Method

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

the class ConstantPoolReferenceFinder method findReferencedMembers.

/**
 * Find all referenced members in a class.
 * @param classInfo the class to search.
 * @param checkMembers if false, do not check fields and methods. Else check everything.
 * @return a set of class names and class member signatures found in the class.
 */
public static Set<String> findReferencedMembers(ClassInfo classInfo, boolean checkMembers) {
    // Else we need to go into details..
    Set<String> members = new HashSet<String>();
    ClassMemberVisitor visitor = new ClassMemberVisitor(members);
    JavaClass javaClass = classInfo.compile();
    Set<Integer> ids = findPoolReferences(classInfo, javaClass);
    List<InvokeSite> invokes = new ArrayList<InvokeSite>();
    if (checkMembers) {
        for (Field field : javaClass.getFields()) {
            FieldInfo fieldInfo = classInfo.getFieldInfo(field.getName());
            // add members found in the field
            visitor.visitField(fieldInfo);
            // there are no invokesites in a field, only add found ids
            ids.addAll(findPoolReferences(fieldInfo, field));
        }
        for (Method method : javaClass.getMethods()) {
            MethodInfo methodInfo = classInfo.getMethodInfo(method.getName() + method.getSignature());
            // add members found in the method
            visitor.visitMethod(methodInfo);
            // add all ids for checking, add all invoke sites
            ids.addAll(findPoolReferences(methodInfo, method));
        }
    }
    // fill the members list with all found constantpool references
    visitor.visitClass(classInfo);
    visitPoolReferences(classInfo, visitor, ids);
    return members;
}
Also used : ArrayList(java.util.ArrayList) ConstantString(org.apache.bcel.classfile.ConstantString) Method(org.apache.bcel.classfile.Method) EnclosingMethod(com.jopdesign.common.bcel.EnclosingMethod) 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) InvokeSite(com.jopdesign.common.code.InvokeSite) ConstantFieldInfo(com.jopdesign.common.type.ConstantFieldInfo) FieldInfo(com.jopdesign.common.FieldInfo) HashSet(java.util.HashSet)

Example 40 with Method

use of org.apache.bcel.classfile.Method in project candle-decompiler by bradsdavis.

the class ClassIntermediateVisitor method visitJavaClass.

@Override
public void visitJavaClass(JavaClass obj) {
    this.classBlock.setClassName(javaClass.getClassName());
    this.classBlock.setPackageName(obj.getPackageName());
    this.classBlock.setSuperClassName(obj.getSuperclassName());
    // process the pool.
    Constant[] pool = obj.getConstantPool().getConstantPool();
    for (Constant c : pool) {
        if (c == null)
            continue;
        c.accept(this);
    }
    Field[] fields = obj.getFields();
    for (int i = 0, j = fields.length; i < j; i++) {
        fields[i].accept(this);
    }
    // run through all of the methods
    Method[] methods = obj.getMethods();
    for (int i = 0, j = methods.length; i < j; i++) {
        methods[i].accept(this);
    }
}
Also used : Field(org.apache.bcel.classfile.Field) Constant(org.apache.bcel.classfile.Constant) 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