Search in sources :

Example 36 with ClassInfo

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

the class UsedCodeFinder method visitReferences.

private void visitReferences(Set<String> refs) {
    for (String id : refs) {
        // The member IDs returned by the reference finder use a syntax which is
        // always unique, so if in doubt, we need to interpret it as a classname, not a fieldname
        MemberID sig = MemberID.parse(id, false);
        // find/load the corresponding classInfo
        ClassInfo cls = getClassInfo(sig);
        // class has been excluded from loading, skip this class
        if (cls == null) {
            continue;
        }
        // referenced class is used, visit it if it has not yet been visited, but skip its class members
        // Note that this class might be different than the class containing the class member
        markUsedMembers(cls, false);
        // check if this id specifies a class member (or just a class, in this case we are done)
        if (sig.hasMethodSignature()) {
            // It's a method! mark the method as used (implementations are marked later)
            MethodRef ref = appInfo.getMethodRef(sig);
            MethodInfo method = ref.getMethodInfo();
            // we keep the declarations. We mark it without following it and make it abstract later.
            if (method != null) {
                setMark(method, false);
            }
        } else if (sig.hasMemberName()) {
            // It's a field! No need to look in subclasses, fields are not virtual
            FieldRef ref = appInfo.getFieldRef(sig);
            FieldInfo field = ref.getFieldInfo();
            if (field != null) {
                markUsedMembers(field);
            }
        }
    }
}
Also used : MemberID(com.jopdesign.common.type.MemberID) MethodRef(com.jopdesign.common.type.MethodRef) FieldRef(com.jopdesign.common.type.FieldRef) MethodInfo(com.jopdesign.common.MethodInfo) FieldInfo(com.jopdesign.common.FieldInfo) ClassInfo(com.jopdesign.common.ClassInfo)

Example 37 with ClassInfo

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

the class ClassWriter method write.

public void write(String writeDir) throws IOException {
    AppInfo appInfo = AppInfo.getSingleton();
    if (logger.isInfoEnabled()) {
        logger.info("Start writing classes to '" + writeDir + "' ..");
    }
    File classDir = new File(writeDir);
    if (classDir.exists()) {
        if (classDir.isFile()) {
            throw new IOException("Output directory '" + classDir + "' is a file.");
        }
    } else if (!classDir.mkdirs()) {
        throw new IOException("Could not create output directory " + classDir);
    }
    for (ClassInfo cls : appInfo.getClassInfos()) {
        if (logger.isDebugEnabled()) {
            logger.debug("Writing class: " + cls.getClassName());
        }
        JavaClass jc = cls.compile();
        String filename = classDir + File.separator + cls.getClassName().replace(".", File.separator) + ".class";
        File file = new File(filename);
        String parent = file.getParent();
        if (parent != null) {
            File pDir = new File(parent);
            //noinspection ResultOfMethodCallIgnored
            pDir.mkdirs();
        }
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        DataOutputStream stream = new DataOutputStream(out);
        jc.dump(stream);
        stream.close();
    }
    if (logger.isInfoEnabled()) {
        logger.info(appInfo.getClassInfos().size() + " classes written.");
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) AppInfo(com.jopdesign.common.AppInfo) ClassInfo(com.jopdesign.common.ClassInfo)

Example 38 with ClassInfo

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

the class ClinitOrder method findOrder.

/**
     * Find a 'correct' oder for the static <clinit>.
     * Throws an error on cyclic dependencies.
     *
     * @return the ordered list of classes
     * @throws JavaClassFormatError if a cyclic dependency has been found.
     */
public List<ClassInfo> findOrder() {
    printDependency(false);
    Set<ClassInfo> cliSet = clinit.keySet();
    List<ClassInfo> order = new LinkedList<ClassInfo>();
    int maxIter = cliSet.size();
    // maximum loop bound detects cyclic dependency
    for (int i = 0; i < maxIter && cliSet.size() != 0; ++i) {
        Iterator itCliSet = cliSet.iterator();
        while (itCliSet.hasNext()) {
            ClassInfo clinf = (ClassInfo) itCliSet.next();
            Set<ClassInfo> depends = clinit.get(clinf);
            if (depends.size() == 0) {
                order.add(clinf);
                // element (a leave in the dependent tree
                for (ClassInfo clinfInner : clinit.keySet()) {
                    Set<ClassInfo> dep = clinit.get(clinfInner);
                    dep.remove(clinf);
                }
                itCliSet.remove();
            }
        }
    }
    if (cliSet.size() != 0) {
        printDependency(true);
        throw new JavaClassFormatError("Cyclic dependency in <clinit>");
    }
    return order;
}
Also used : JavaClassFormatError(com.jopdesign.common.misc.JavaClassFormatError) Iterator(java.util.Iterator) LinkedList(java.util.LinkedList) ClassInfo(com.jopdesign.common.ClassInfo)

Example 39 with ClassInfo

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

the class ExampleTool method doSomething.

public void doSomething(Config config) {
    AppInfo appInfo = AppInfo.getSingleton();
    // access and modify some classes
    System.out.println("field of main class: " + manager.getMyField(appInfo.getMainMethod().getClassInfo()));
    for (ClassInfo root : appInfo.getRootClasses()) {
        System.out.println("field of root: " + manager.getMyField(root));
    }
    // create a new class and new methods
    try {
        ClassInfo newCls = appInfo.createClass("MyTest", appInfo.getClassRef("java.lang.Object"), false);
    } catch (NamingConflictException e) {
        e.printStackTrace();
    }
}
Also used : AppInfo(com.jopdesign.common.AppInfo) ClassInfo(com.jopdesign.common.ClassInfo) NamingConflictException(com.jopdesign.common.misc.NamingConflictException)

Example 40 with ClassInfo

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

the class TypeGraph method assignRelativeNumbering.

private int assignRelativeNumbering(ClassInfo node, Map<ClassInfo, Pair<Integer, Integer>> rn, int low) {
    int next = low;
    for (DefaultEdge subEdge : outgoingEdgesOf(node)) {
        ClassInfo sub = getEdgeTarget(subEdge);
        if (sub.isInterface())
            continue;
        next = assignRelativeNumbering(sub, rn, next + 1);
    }
    rn.put(node, new Pair<Integer, Integer>(low, next));
    return next;
}
Also used : DefaultEdge(org.jgrapht.graph.DefaultEdge) ClassInfo(com.jopdesign.common.ClassInfo)

Aggregations

ClassInfo (com.jopdesign.common.ClassInfo)49 MethodInfo (com.jopdesign.common.MethodInfo)19 DefaultEdge (org.jgrapht.graph.DefaultEdge)8 AppInfo (com.jopdesign.common.AppInfo)7 HashMap (java.util.HashMap)7 InstructionHandle (org.apache.bcel.generic.InstructionHandle)7 HashSet (java.util.HashSet)6 Set (java.util.Set)6 IOException (java.io.IOException)5 LinkedList (java.util.LinkedList)5 FieldInfo (com.jopdesign.common.FieldInfo)4 JavaClassFormatError (com.jopdesign.common.misc.JavaClassFormatError)4 MemberID (com.jopdesign.common.type.MemberID)4 BitSet (java.util.BitSet)3 AppSetup (com.jopdesign.common.AppSetup)2 MethodCode (com.jopdesign.common.MethodCode)2 BasicBlock (com.jopdesign.common.code.BasicBlock)2 CallString (com.jopdesign.common.code.CallString)2 CFGNode (com.jopdesign.common.code.ControlFlowGraph.CFGNode)2 ExecutionContext (com.jopdesign.common.code.ExecutionContext)2