Search in sources :

Example 1 with ConstantMethodInfo

use of com.jopdesign.common.type.ConstantMethodInfo in project jop by jop-devel.

the class ClinitOrder method findDependencies.

private Set<ClassInfo> findDependencies(MethodInfo method, boolean inRec) {
    //		System.out.println("find dep. in "+cli.clazz.getClassName()+":"+mi.getMethod().getName());
    Set<ClassInfo> depends = new HashSet<ClassInfo>();
    if (method.isNative() || method.isAbstract()) {
        // subclasses???? :-(
        return depends;
    }
    ClassInfo classInfo = method.getClassInfo();
    ConstantPoolGen cpoolgen = method.getConstantPoolGen();
    InstructionList il = method.getCode().getInstructionList();
    InstructionFinder f = new InstructionFinder(il);
    // TODO can we encounter an empty instruction list?
    //if(il.getStart() == null) {
    //    return depends;
    //}
    // 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 = cpoolgen.getConstant(idx);
        ClassRef classRef = null;
        Set addDepends = null;
        ClassInfo clinfo;
        MethodInfo minfo;
        switch(co.getTag()) {
            case Constants.CONSTANT_Class:
                classRef = classInfo.getConstantInfo(co).getClassRef();
                clinfo = classRef.getClassInfo();
                if (clinfo != null) {
                    minfo = clinfo.getMethodInfo("<init>()V");
                    if (minfo != null) {
                        addDepends = findDependencies(minfo, true);
                    }
                }
                break;
            case Constants.CONSTANT_Fieldref:
            case Constants.CONSTANT_InterfaceMethodref:
                classRef = classInfo.getConstantInfo(co).getClassRef();
                break;
            case Constants.CONSTANT_Methodref:
                ConstantMethodInfo mref = (ConstantMethodInfo) classInfo.getConstantInfo(co);
                classRef = mref.getClassRef();
                minfo = mref.getMethodRef().getMethodInfo();
                if (minfo != null) {
                    addDepends = findDependencies(minfo, true);
                }
                break;
        }
        if (classRef != null) {
            ClassInfo clinf = classRef.getClassInfo();
            if (clinf != null) {
                if (clinf.getMethodInfo(clinitSig) != null) {
                    // don't add myself as dependency
                    if (!clinf.equals(method.getClassInfo())) {
                        depends.add(clinf);
                    }
                }
            }
        }
        if (addDepends != null) {
            for (Object addDepend : addDepends) {
                ClassInfo addCli = (ClassInfo) addDepend;
                if (addCli.equals(method.getClassInfo())) {
                    throw new JavaClassFormatError("cyclic indirect <clinit> dependency");
                }
                depends.add(addCli);
            }
        }
    }
    return depends;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ClassRef(com.jopdesign.common.type.ClassRef) InstructionList(org.apache.bcel.generic.InstructionList) Constant(org.apache.bcel.classfile.Constant) InstructionFinder(org.apache.bcel.util.InstructionFinder) InstructionHandle(org.apache.bcel.generic.InstructionHandle) ConstantMethodInfo(com.jopdesign.common.type.ConstantMethodInfo) ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) CPInstruction(org.apache.bcel.generic.CPInstruction) JavaClassFormatError(com.jopdesign.common.misc.JavaClassFormatError) Iterator(java.util.Iterator) MethodInfo(com.jopdesign.common.MethodInfo) ConstantMethodInfo(com.jopdesign.common.type.ConstantMethodInfo) HashSet(java.util.HashSet) ClassInfo(com.jopdesign.common.ClassInfo)

Example 2 with ConstantMethodInfo

use of com.jopdesign.common.type.ConstantMethodInfo in project jop by jop-devel.

the class RelinkInvokesuper method relinkInvokeSuper.

private void relinkInvokeSuper(InvokeSite is) {
    // this already resolves to the correct method..
    MethodRef invokee = is.getInvokeeRef();
    MethodInfo target = invokee.getMethodInfo();
    if (target == null) {
        // .. or not (class or method excluded or unknown)
        logger.warn("Cannot try to relink invokespecial to unknown super method " + invokee);
        return;
    }
    // now simply relink instruction (no need to check if it changes)
    int index = is.getInvoker().getClassInfo().addConstantInfo(new ConstantMethodInfo(invokee));
    if (!(is.getInvokeInstruction() instanceof INVOKESPECIAL)) {
        throw new JavaClassFormatError("Invokesuper is not an invokespecial instruction!");
    }
    is.getInvokeInstruction().setIndex(index);
}
Also used : ConstantMethodInfo(com.jopdesign.common.type.ConstantMethodInfo) MethodRef(com.jopdesign.common.type.MethodRef) JavaClassFormatError(com.jopdesign.common.misc.JavaClassFormatError) MethodInfo(com.jopdesign.common.MethodInfo) ConstantMethodInfo(com.jopdesign.common.type.ConstantMethodInfo) INVOKESPECIAL(org.apache.bcel.generic.INVOKESPECIAL)

Aggregations

MethodInfo (com.jopdesign.common.MethodInfo)2 JavaClassFormatError (com.jopdesign.common.misc.JavaClassFormatError)2 ConstantMethodInfo (com.jopdesign.common.type.ConstantMethodInfo)2 ClassInfo (com.jopdesign.common.ClassInfo)1 ClassRef (com.jopdesign.common.type.ClassRef)1 MethodRef (com.jopdesign.common.type.MethodRef)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 Constant (org.apache.bcel.classfile.Constant)1 CPInstruction (org.apache.bcel.generic.CPInstruction)1 ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)1 INVOKESPECIAL (org.apache.bcel.generic.INVOKESPECIAL)1 InstructionHandle (org.apache.bcel.generic.InstructionHandle)1 InstructionList (org.apache.bcel.generic.InstructionList)1 InstructionFinder (org.apache.bcel.util.InstructionFinder)1