Search in sources :

Example 6 with MemberID

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

the class AppSetup method getMainSignature.

private MemberID getMainSignature(String signature) throws BadConfigurationException {
    MemberID sMain;
    ClassPath path = new ClassPath(config.getOption(Config.CLASSPATH));
    MemberID sMainMethod = MemberID.parse(config.getOption(Config.MAIN_METHOD_NAME), path);
    if (signature == null || "".equals(signature)) {
        sMain = sMainMethod;
    } else {
        // try to parse the signature
        sMain = MemberID.parse(signature, path);
        // use --mm if only main class has been given
        if (!sMain.hasMemberName()) {
            if (!sMainMethod.hasMemberName()) {
                throw new BadConfigurationException("Option '" + Config.MAIN_METHOD_NAME.getKey() + "' needs to specify a method name.");
            }
            sMain = new MemberID(sMain.getClassName(), sMainMethod.getMemberName(), sMainMethod.getDescriptor());
        }
    }
    return sMain;
}
Also used : MemberID(com.jopdesign.common.type.MemberID) ClassPath(org.apache.bcel.util.ClassPath) BadConfigurationException(com.jopdesign.common.config.Config.BadConfigurationException)

Example 7 with MemberID

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

the class AppSetup method initProcessorModel.

private void initProcessorModel(Model model) {
    ProcessorModel pm;
    switch(model) {
        case JOP:
            pm = new JOPModel(config);
            break;
        case jamuth:
            pm = new JamuthModel(config);
            break;
        case allocation:
            pm = new AllocationModel(config);
            break;
        case JVM:
            pm = new JVMModel();
            break;
        default:
            throw new BadConfigurationError("Unknown processor model " + model);
    }
    appInfo.setProcessorModel(pm);
    // load referenced classes as roots
    for (String jvmClass : pm.getJVMClasses()) {
        ClassInfo rootInfo = appInfo.loadClass(jvmClass.replaceAll("/", "."));
        if (rootInfo == null) {
            System.err.println("Error loading JVM class '" + jvmClass + "'.");
            System.exit(4);
        }
    }
    if (appInfo.doLoadNatives()) {
        for (String nativeClass : pm.getNativeClasses()) {
            ClassInfo rootInfo = appInfo.loadClass(nativeClass.replaceAll("/", "."));
            if (rootInfo == null) {
                System.err.println("Error loading Native class '" + nativeClass + "'.");
                System.exit(4);
            }
        }
    }
    // we do not set the JVM and native classes as root anymore, instead we let the PM decide which roots we need
    for (String root : pm.getJVMRoots()) {
        MemberID mID = MemberID.parse(root);
        // make sure the class exists..
        ClassInfo cls = appInfo.loadClass(mID.getClassName());
        // Get the member and add it as root
        if (mID.hasMemberName()) {
            MethodInfo methodInfo = cls.getMethodInfo(mID);
            if (methodInfo == null) {
                System.err.println("Could not find JVM root " + root);
                System.exit(5);
            }
            appInfo.addRoot(methodInfo);
        } else {
            appInfo.addRoot(cls);
        }
    }
}
Also used : MemberID(com.jopdesign.common.type.MemberID) BadConfigurationError(com.jopdesign.common.config.Config.BadConfigurationError) JOPModel(com.jopdesign.common.processormodel.JOPModel) AllocationModel(com.jopdesign.common.processormodel.AllocationModel) ProcessorModel(com.jopdesign.common.processormodel.ProcessorModel) JVMModel(com.jopdesign.common.processormodel.JVMModel) JamuthModel(com.jopdesign.common.processormodel.JamuthModel)

Example 8 with MemberID

use of com.jopdesign.common.type.MemberID 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 9 with MemberID

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

the class AppSetup method getMainMethod.

private MethodInfo getMainMethod(String signature) throws Config.BadConfigurationException {
    ClassInfo clsInfo;
    String clsName;
    MemberID sMain = getMainSignature(signature);
    clsName = sMain.getClassName();
    if (clsName == null) {
        throw new BadConfigurationException("You need to specify a classname for the main method.");
    }
    try {
        clsInfo = appInfo.loadClass(clsName, true, false);
    } catch (ClassInfoNotFoundException e) {
        throw new BadConfigurationException("Class for '" + signature + "' could not be loaded: " + e.getMessage(), e);
    }
    // check if we have a full signature
    if (sMain.hasMethodSignature()) {
        MethodInfo method = clsInfo.getMethodInfo(sMain.getMethodSignature());
        if (method == null) {
            throw new BadConfigurationException("Method '" + sMain.getMethodSignature() + "' not found in '" + clsName + "'.");
        }
        return method;
    }
    // try to find main method
    String mainName = sMain.getMemberName();
    if (mainName == null) {
        mainName = config.getOption(Config.MAIN_METHOD_NAME);
        if (mainName != null) {
            mainName = MemberID.parse(mainName).getMethodSignature();
        }
    }
    Collection<MethodInfo> methods = clsInfo.getMethodByName(mainName);
    if (methods.isEmpty()) {
        throw new BadConfigurationException("'No method '" + mainName + "' found in '" + clsName + "'.");
    }
    if (methods.size() > 1) {
        StringBuilder s = new StringBuilder(String.format("Multiple candidates for '%s' in '%s', please specify with a signature: ", mainName, clsName));
        for (MethodInfo m : methods) {
            s.append("\n");
            s.append(m.getMemberID());
        }
        throw new BadConfigurationException(s.toString());
    }
    return methods.iterator().next();
}
Also used : MemberID(com.jopdesign.common.type.MemberID) ClassInfoNotFoundException(com.jopdesign.common.misc.ClassInfoNotFoundException) BadConfigurationException(com.jopdesign.common.config.Config.BadConfigurationException)

Example 10 with MemberID

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

the class Config method parseMethodList.

/**
     * @param methodNames comma separated list of method names, either fully qualified with or without descriptor, or
     *        method names of methods in the main class.
     * @return the set of methods represented by these names
     * @throws BadConfigurationException if a name is not resolvable
     */
public static List<MethodInfo> parseMethodList(String methodNames) throws BadConfigurationException {
    List<String> names = splitStringList(methodNames);
    List<MethodInfo> methods = new ArrayList<MethodInfo>(names.size());
    for (String name : names) {
        MemberID id = MemberID.parse(name);
        if (!id.hasClassName()) {
            ClassInfo main = AppInfo.getSingleton().getMainMethod().getClassInfo();
            Set<MethodInfo> m = main.getMethodInfos(id);
            if (m.isEmpty()) {
                throw new BadConfigurationException("Cannot find method '" + name + "' in main class " + main);
            }
            methods.addAll(m);
        } else {
            try {
                Collection<MethodInfo> infos = AppInfo.getSingleton().getMethodInfos(id);
                if (infos.isEmpty()) {
                    throw new BadConfigurationException("Cannot find methods for " + name);
                }
                methods.addAll(infos);
            } catch (MethodNotFoundException e) {
                throw new BadConfigurationException("Cannot find class for " + name, e);
            }
        }
    }
    return methods;
}
Also used : MemberID(com.jopdesign.common.type.MemberID) ArrayList(java.util.ArrayList) MethodInfo(com.jopdesign.common.MethodInfo) MethodNotFoundException(com.jopdesign.common.misc.MethodNotFoundException) ClassInfo(com.jopdesign.common.ClassInfo)

Aggregations

MemberID (com.jopdesign.common.type.MemberID)15 MethodInfo (com.jopdesign.common.MethodInfo)7 ClassInfo (com.jopdesign.common.ClassInfo)4 ArrayList (java.util.ArrayList)3 BadConfigurationException (com.jopdesign.common.config.Config.BadConfigurationException)2 MethodNotFoundException (com.jopdesign.common.misc.MethodNotFoundException)2 MethodRef (com.jopdesign.common.type.MethodRef)2 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)2 ClassMemberInfo (com.jopdesign.common.ClassMemberInfo)1 FieldInfo (com.jopdesign.common.FieldInfo)1 CallString (com.jopdesign.common.code.CallString)1 BadConfigurationError (com.jopdesign.common.config.Config.BadConfigurationError)1 ClassHierarchyTraverser (com.jopdesign.common.graphutils.ClassHierarchyTraverser)1 ClassVisitor (com.jopdesign.common.graphutils.ClassVisitor)1 EmptyClassVisitor (com.jopdesign.common.graphutils.EmptyClassVisitor)1