Search in sources :

Example 21 with AppInfo

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

the class ConstantInfo method createFromConstant.

/**
 * Create a new constantInfo from a BCEL constant.
 * If the constantpool contains invalid data, a {@link JavaClassFormatError} is thrown.
 *
 * @param cp the constantpool used to resolve the index references.
 * @param constant the BCEL constant to convert
 * @return a new ConstantInfo containing the constant value.
 */
public static ConstantInfo createFromConstant(ConstantPool cp, Constant constant) {
    MemberID sig;
    MethodRef methodRef;
    ConstantNameAndType nRef;
    AppInfo appInfo = AppInfo.getSingleton();
    byte tag = constant.getTag();
    switch(tag) {
        case Constants.CONSTANT_Class:
            ClassRef classRef = appInfo.getClassRef(((ConstantClass) constant).getBytes(cp).replace('/', '.'));
            return new ConstantClassInfo(classRef);
        case Constants.CONSTANT_Fieldref:
            ConstantFieldref fRef = (ConstantFieldref) constant;
            nRef = (ConstantNameAndType) cp.getConstant(fRef.getNameAndTypeIndex());
            sig = new MemberID(fRef.getClass(cp), nRef.getName(cp), nRef.getSignature(cp));
            FieldRef fieldRef = appInfo.getFieldRef(sig);
            return new ConstantFieldInfo(fieldRef);
        case Constants.CONSTANT_Methodref:
            ConstantMethodref mRef = (ConstantMethodref) constant;
            nRef = (ConstantNameAndType) cp.getConstant(mRef.getNameAndTypeIndex());
            sig = new MemberID(mRef.getClass(cp), nRef.getName(cp), nRef.getSignature(cp));
            methodRef = appInfo.getMethodRef(sig, false);
            return new ConstantMethodInfo(methodRef);
        case Constants.CONSTANT_InterfaceMethodref:
            ConstantInterfaceMethodref imRef = (ConstantInterfaceMethodref) constant;
            nRef = (ConstantNameAndType) cp.getConstant(imRef.getNameAndTypeIndex());
            sig = new MemberID(imRef.getClass(cp), nRef.getName(cp), nRef.getSignature(cp));
            methodRef = appInfo.getMethodRef(sig, true);
            return new ConstantMethodInfo(methodRef);
        case Constants.CONSTANT_String:
            return new ConstantStringInfo(((ConstantString) constant).getBytes(cp), false);
        case Constants.CONSTANT_Integer:
            return new ConstantIntegerInfo(((ConstantInteger) constant).getBytes());
        case Constants.CONSTANT_Float:
            return new ConstantFloatInfo(((ConstantFloat) constant).getBytes());
        case Constants.CONSTANT_Long:
            return new ConstantLongInfo(((ConstantLong) constant).getBytes());
        case Constants.CONSTANT_Double:
            return new ConstantDoubleInfo(((ConstantDouble) constant).getBytes());
        case Constants.CONSTANT_NameAndType:
            String name = ((ConstantNameAndType) constant).getName(cp);
            String signature = ((ConstantNameAndType) constant).getSignature(cp);
            return new ConstantNameAndTypeInfo(new MemberID(name, signature));
        case Constants.CONSTANT_Utf8:
            return new ConstantStringInfo(((ConstantUtf8) constant).getBytes(), true);
        default:
            throw new JavaClassFormatError("Invalid byte tag in constant pool: " + tag);
    }
}
Also used : ConstantString(org.apache.bcel.classfile.ConstantString) ConstantNameAndType(org.apache.bcel.classfile.ConstantNameAndType) AppInfo(com.jopdesign.common.AppInfo) ConstantMethodref(org.apache.bcel.classfile.ConstantMethodref) ConstantFieldref(org.apache.bcel.classfile.ConstantFieldref) JavaClassFormatError(com.jopdesign.common.misc.JavaClassFormatError) ConstantInterfaceMethodref(org.apache.bcel.classfile.ConstantInterfaceMethodref) ConstantClass(org.apache.bcel.classfile.ConstantClass)

Aggregations

AppInfo (com.jopdesign.common.AppInfo)21 AppSetup (com.jopdesign.common.AppSetup)7 ClassInfo (com.jopdesign.common.ClassInfo)7 MethodInfo (com.jopdesign.common.MethodInfo)7 File (java.io.File)4 IOException (java.io.IOException)4 TestFramework (com.jopdesign.common.TestFramework)3 JavaClassFormatError (com.jopdesign.common.misc.JavaClassFormatError)2 FileWriter (java.io.FileWriter)2 LinkedHashSet (java.util.LinkedHashSet)2 FieldInfo (com.jopdesign.common.FieldInfo)1 MethodCode (com.jopdesign.common.MethodCode)1 CallGraph (com.jopdesign.common.code.CallGraph)1 CFGNode (com.jopdesign.common.code.ControlFlowGraph.CFGNode)1 InvokeNode (com.jopdesign.common.code.ControlFlowGraph.InvokeNode)1 ExecutionContext (com.jopdesign.common.code.ExecutionContext)1 InvokeSite (com.jopdesign.common.code.InvokeSite)1 SuperGraph (com.jopdesign.common.code.SuperGraph)1 ContextCFG (com.jopdesign.common.code.SuperGraph.ContextCFG)1 SuperEdge (com.jopdesign.common.code.SuperGraph.SuperEdge)1