Search in sources :

Example 11 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project jop by jop-devel.

the class OldAppInfo method iterate.

/**
	 * Iterate over all classes and run the visitor.
	 * 
	 * @param v
	 */
public void iterate(Visitor v) {
    Iterator<? extends OldClassInfo> it = cliMap.values().iterator();
    while (it.hasNext()) {
        JavaClass clz = it.next().clazz;
        new DescendingVisitor(clz, v).visit();
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) DescendingVisitor(org.apache.bcel.classfile.DescendingVisitor)

Example 12 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project jop by jop-devel.

the class OldClassInfo method genClassInfoMap.

/**
	 * Create ClassInfos and the map from class names to ClassInfo
	 * @param jc
	 * @return
	 */
Map<String, ? extends OldClassInfo> genClassInfoMap(JavaClass[] jc, OldAppInfo ai) {
    Map<String, OldClassInfo> map = new HashMap<String, OldClassInfo>();
    for (int i = 0; i < jc.length; ++i) {
        OldClassInfo cli = newClassInfo(jc[i], ai);
        map.put(cli.clazz.getClassName(), cli);
    }
    // second iteration over all class infos for additional information setting
    CliVisitor v = newCliVisitor(map);
    Iterator<? extends OldClassInfo> it = map.values().iterator();
    while (it.hasNext()) {
        JavaClass clz = it.next().clazz;
        new DescendingVisitor(clz, v).visit();
    }
    return map;
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) HashMap(java.util.HashMap) DescendingVisitor(org.apache.bcel.classfile.DescendingVisitor)

Example 13 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project jop by jop-devel.

the class ConstantPoolReferenceFinder method findPoolReferences.

////////////////////////////////////////////////////////////////
// Find/replace references, member names, Pool entries
////////////////////////////////////////////////////////////////
public static Set<Integer> findPoolReferences(ClassInfo classInfo, boolean checkMembers) {
    JavaClass javaClass = classInfo.compile();
    Set<Integer> ids = findPoolReferences(classInfo, javaClass);
    if (checkMembers) {
        for (Field field : javaClass.getFields()) {
            FieldInfo fieldInfo = classInfo.getFieldInfo(field.getName());
            ids.addAll(findPoolReferences(fieldInfo, field));
        }
        for (Method method : javaClass.getMethods()) {
            MethodInfo methodInfo = classInfo.getMethodInfo(method.getName() + method.getSignature());
            ids.addAll(findPoolReferences(methodInfo, method));
        }
    }
    return ids;
}
Also used : ConstantInteger(org.apache.bcel.classfile.ConstantInteger) Field(org.apache.bcel.classfile.Field) JavaClass(org.apache.bcel.classfile.JavaClass) MethodInfo(com.jopdesign.common.MethodInfo) ConstantMethodInfo(com.jopdesign.common.type.ConstantMethodInfo) Method(org.apache.bcel.classfile.Method) EnclosingMethod(com.jopdesign.common.bcel.EnclosingMethod) ConstantFieldInfo(com.jopdesign.common.type.ConstantFieldInfo) FieldInfo(com.jopdesign.common.FieldInfo)

Example 14 with JavaClass

use of org.apache.bcel.classfile.JavaClass 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 15 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project android-classyshark by google.

the class RootBuilder method fillFromJar.

private ClassNode fillFromJar(File file) {
    if (JayceReader.isJackAndJillArchive(file)) {
        return fillFromJayce(file);
    }
    ClassNode rootNode = new ClassNode(file.getName());
    try {
        JarFile theJar = new JarFile(file);
        Enumeration<? extends JarEntry> en = theJar.entries();
        while (en.hasMoreElements()) {
            JarEntry entry = en.nextElement();
            if (entry.getName().endsWith(".class")) {
                ClassParser cp = new ClassParser(theJar.getInputStream(entry), entry.getName());
                JavaClass jc = cp.parse();
                ClassInfo classInfo = new ClassInfo(jc.getClassName(), jc.getMethods().length);
                rootNode.add(classInfo);
            }
        }
    } catch (IOException e) {
        System.err.println("Error reading file: " + file + ". " + e.getMessage());
        e.printStackTrace(System.err);
    }
    return rootNode;
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) ClassParser(org.apache.bcel.classfile.ClassParser)

Aggregations

JavaClass (org.apache.bcel.classfile.JavaClass)15 IOException (java.io.IOException)3 Field (org.apache.bcel.classfile.Field)3 Method (org.apache.bcel.classfile.Method)3 ClassGen (org.apache.bcel.generic.ClassGen)3 InstructionHandle (org.apache.bcel.generic.InstructionHandle)3 InstructionList (org.apache.bcel.generic.InstructionList)3 MethodGen (org.apache.bcel.generic.MethodGen)3 FieldInfo (com.jopdesign.common.FieldInfo)2 MethodInfo (com.jopdesign.common.MethodInfo)2 EnclosingMethod (com.jopdesign.common.bcel.EnclosingMethod)2 ConstantFieldInfo (com.jopdesign.common.type.ConstantFieldInfo)2 ConstantMethodInfo (com.jopdesign.common.type.ConstantMethodInfo)2 FileOutputStream (java.io.FileOutputStream)2 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 ClassParser (org.apache.bcel.classfile.ClassParser)2 ConstantInteger (org.apache.bcel.classfile.ConstantInteger)2 DescendingVisitor (org.apache.bcel.classfile.DescendingVisitor)2 BranchInstruction (org.apache.bcel.generic.BranchInstruction)2