Search in sources :

Example 21 with ConstantPoolGen

use of org.apache.bcel.generic.ConstantPoolGen in project jop by jop-devel.

the class MethodCode method getFieldRef.

public FieldRef getFieldRef(FieldInstruction instr) {
    ConstantPoolGen cpg = getConstantPoolGen();
    String classname = getReferencedClassName(instr);
    return getAppInfo().getFieldRef(classname, instr.getFieldName(cpg));
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) HashedString(com.jopdesign.common.misc.HashedString) CallString(com.jopdesign.common.code.CallString)

Example 22 with ConstantPoolGen

use of org.apache.bcel.generic.ConstantPoolGen in project jop by jop-devel.

the class InvokeSite method getReferencedMethod.

////////////////////////////////////////////////////////////////////////////////
// Private methods
////////////////////////////////////////////////////////////////////////////////
/**
     * Get a MethodRef for the referenced method in a given invoke instruction in the invoker method.
     * This does not resolve any super methods or devirtualize the call.
     * <p>
     * If you call {@link MethodRef#getMethodInfo()}, the method found in the referenced class
     * or in its superclasses will be returned. For InvokeSpecial, this might not be correct,
     * so use {@link #getInvokeeRef()} or {@link AppInfo#findImplementations(InvokeSite)} instead.</p>
     * <p>
     * Note: Since this could easily be used incorrectly, this method has been moved here from MethodInfo and
     * made private in favor of getInvokeeRef()
     * </p>
     *
     * @param invoker the method containing the instruction, used to resolve constantpool references.
     * @param instr the instruction to resolve
     * @return a method reference representing the invoke reference.
     */
private static MethodRef getReferencedMethod(MethodInfo invoker, InvokeInstruction instr) {
    ConstantPoolGen cpg = invoker.getConstantPoolGen();
    String methodname = instr.getMethodName(cpg);
    String classname = invoker.getCode().getReferencedClassName(instr);
    MemberID memberID = new MemberID(classname, methodname, instr.getSignature(cpg));
    if ("<clinit>".equals(methodname)) {
        // can have <clinit> methods which are invoked by invokestatic
        return AppInfo.getSingleton().getMethodRef(memberID);
    }
    boolean isInterface = (instr instanceof INVOKEINTERFACE);
    return AppInfo.getSingleton().getMethodRef(memberID, isInterface);
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) MemberID(com.jopdesign.common.type.MemberID) INVOKEINTERFACE(org.apache.bcel.generic.INVOKEINTERFACE)

Example 23 with ConstantPoolGen

use of org.apache.bcel.generic.ConstantPoolGen in project jop by jop-devel.

the class MethodCode method getReferencedClassName.

/**
     * Get the classname or array-type name referenced by an invoke- or field instruction in this code.
     *
     * @param instr the instruction to check, using this methods constantpool.
     * @return the referenced classname or array-typename, to be used for a ClassRef or AppInfo getter. 
     */
public String getReferencedClassName(FieldOrMethod instr) {
    ConstantPoolGen cpg = getConstantPoolGen();
    ReferenceType refType = instr.getReferenceType(cpg);
    String classname;
    if (refType instanceof ObjectType) {
        classname = ((ObjectType) refType).getClassName();
    } else if (refType instanceof ArrayType) {
        // need to call array.<method>, which class should we use? Let's decide later..
        String msg = "Calling a method of an array: " + refType.getSignature() + "#" + instr.getName(cpg) + " in " + methodInfo;
        logger.debug(msg);
        classname = refType.getSignature();
    } else {
        // Hu??
        throw new JavaClassFormatError("Unknown reference type " + refType);
    }
    return classname;
}
Also used : ArrayType(org.apache.bcel.generic.ArrayType) ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) ObjectType(org.apache.bcel.generic.ObjectType) JavaClassFormatError(com.jopdesign.common.misc.JavaClassFormatError) HashedString(com.jopdesign.common.misc.HashedString) CallString(com.jopdesign.common.code.CallString) ReferenceType(org.apache.bcel.generic.ReferenceType)

Example 24 with ConstantPoolGen

use of org.apache.bcel.generic.ConstantPoolGen in project jop by jop-devel.

the class InnerClassesInfo method buildInnerClasses.

/**
     * Build a map of InnerClasses containing all entries for all inner classes in the 'classes'
     * parameter and all their enclosing inner classes.
     *
     * @param classes a collection of classes the result should contain.
     * @return the map of className->InnerClass containing all inner classes in the 'classes' parameter.
     */
private Collection<InnerClass> buildInnerClasses(Collection<ClassRef> classes) {
    List<ClassRef> queue = new LinkedList<ClassRef>(classes);
    Set<String> visited = new HashSet<String>();
    List<InnerClass> inner = new LinkedList<InnerClass>();
    ConstantPoolGen cpg = classGen.getConstantPool();
    InnerClasses ic = getInnerClassesAttribute();
    while (!queue.isEmpty()) {
        ClassRef ref = queue.remove(0);
        ClassInfo cls = ref.getClassInfo();
        int innerClassIdx;
        int outerClassIdx = 0;
        int innerNameIdx = 0;
        int flags = 0;
        String enclosingClass = null;
        if (cls != null) {
            InnerClassesInfo info = cls.getInnerClassesInfo();
            // only nested classes have an entry
            if (!info.isNestedClass()) {
                continue;
            }
            enclosingClass = info.getEnclosingClassName();
            innerClassIdx = cpg.addClass(ref.getClassName());
            if (!info.isLocalInnerClass()) {
                // class is a member, add outer class reference
                outerClassIdx = cpg.addClass(enclosingClass);
            }
            if (!info.isAnonymousInnerClass()) {
                // class has a simple name
                innerNameIdx = cpg.addUtf8(info.getInnerName());
            }
            flags = cls.getAccessFlags();
        } else {
            // unknown class, need to check the existing InnerClass array, if it exists
            InnerClass i = findInnerClass(ic, ref.getClassName());
            if (i == null) {
                // class is not an innerclass according to our old InnerClasses table
                continue;
            }
            innerClassIdx = i.getInnerClassIndex();
            outerClassIdx = i.getOuterClassIndex();
            innerNameIdx = i.getInnerNameIndex();
            flags = i.getInnerAccessFlags();
        }
        // will add a reference to the enclosing class anyway
        if (enclosingClass != null && !visited.contains(enclosingClass)) {
            queue.add(classInfo.getAppInfo().getClassRef(enclosingClass));
        }
        visited.add(ref.getClassName());
        inner.add(new InnerClass(innerClassIdx, outerClassIdx, innerNameIdx, flags));
    }
    return inner;
}
Also used : ClassRef(com.jopdesign.common.type.ClassRef) InnerClasses(org.apache.bcel.classfile.InnerClasses) LinkedList(java.util.LinkedList) ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) InnerClass(org.apache.bcel.classfile.InnerClass) HashSet(java.util.HashSet) ConstantClassInfo(com.jopdesign.common.type.ConstantClassInfo)

Example 25 with ConstantPoolGen

use of org.apache.bcel.generic.ConstantPoolGen in project jop by jop-devel.

the class MemberInfo method setDeprecated.

public void setDeprecated(boolean flag) {
    if (flag) {
        if (findDeprecated() == null) {
            ConstantPoolGen cpg = getClassInfo().getConstantPoolGen();
            int index = cpg.addUtf8("Deprecated");
            addAttribute(new org.apache.bcel.classfile.Deprecated(index, 0, new byte[0], cpg.getConstantPool()));
        }
    } else {
        org.apache.bcel.classfile.Deprecated d = findDeprecated();
        if (d != null) {
            removeAttribute(d);
        }
    }
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen)

Aggregations

ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)28 Instruction (org.apache.bcel.generic.Instruction)6 InstructionHandle (org.apache.bcel.generic.InstructionHandle)6 FieldInstruction (org.apache.bcel.generic.FieldInstruction)5 InstructionList (org.apache.bcel.generic.InstructionList)5 CallString (com.jopdesign.common.code.CallString)4 Constant (org.apache.bcel.classfile.Constant)4 Method (org.apache.bcel.classfile.Method)4 ObjectType (org.apache.bcel.generic.ObjectType)4 Type (org.apache.bcel.generic.Type)4 MethodInfo (com.jopdesign.common.MethodInfo)3 AnnotationAttribute (com.jopdesign.common.bcel.AnnotationAttribute)3 ClassRef (com.jopdesign.common.type.ClassRef)3 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3 ArrayInstruction (org.apache.bcel.generic.ArrayInstruction)3 BranchInstruction (org.apache.bcel.generic.BranchInstruction)3 MethodGen (org.apache.bcel.generic.MethodGen)3 ClassInfo (com.jopdesign.common.ClassInfo)2 HashedString (com.jopdesign.common.misc.HashedString)2