Search in sources :

Example 11 with ConstantPool

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

the class OldClinitOrder method findDependencies.

private Set findDependencies(OldClassInfo cli, OldMethodInfo mi, boolean inRec) {
    // System.out.println("find dep. in "+cli.clazz.getClassName()+":"+mi.getMethod().getName());
    Method method = mi.getMethod();
    Set depends = new HashSet();
    if (method.isNative() || method.isAbstract()) {
        // subclasses???? :-(
        return depends;
    }
    ConstantPool cpool = cli.clazz.getConstantPool();
    ConstantPoolGen cpoolgen = new ConstantPoolGen(cpool);
    MethodGen mg = new MethodGen(method, cli.clazz.getClassName(), cpoolgen);
    InstructionList il = mg.getInstructionList();
    InstructionFinder f = new InstructionFinder(il);
    // find instructions that access the constant pool
    // collect all indices to constants in ClassInfo
    String cpInstr = "CPInstruction";
    for (Iterator it = f.search(cpInstr); it.hasNext(); ) {
        InstructionHandle[] match = (InstructionHandle[]) it.next();
        InstructionHandle first = match[0];
        CPInstruction ii = (CPInstruction) first.getInstruction();
        int idx = ii.getIndex();
        Constant co = cpool.getConstant(idx);
        ConstantClass cocl = null;
        Set addDepends = null;
        String clname;
        OldClassInfo clinfo;
        OldMethodInfo minfo;
        switch(co.getTag()) {
            case Constants.CONSTANT_Class:
                cocl = (ConstantClass) co;
                clname = cocl.getBytes(cpool).replace('/', '.');
                clinfo = (OldClassInfo) ai.cliMap.get(clname);
                if (clinfo != null) {
                    minfo = clinfo.getMethodInfo("<init>()V");
                    if (minfo != null) {
                        addDepends = findDependencies(clinfo, minfo, true);
                    }
                }
                break;
            case Constants.CONSTANT_InterfaceMethodref:
                cocl = (ConstantClass) cpool.getConstant(((ConstantInterfaceMethodref) co).getClassIndex());
                break;
            case Constants.CONSTANT_Methodref:
                cocl = (ConstantClass) cpool.getConstant(((ConstantMethodref) co).getClassIndex());
                clname = cocl.getBytes(cpool).replace('/', '.');
                clinfo = (OldClassInfo) ai.cliMap.get(clname);
                int sigidx = ((ConstantMethodref) co).getNameAndTypeIndex();
                ConstantNameAndType signt = (ConstantNameAndType) cpool.getConstant(sigidx);
                String sigstr = signt.getName(cpool) + signt.getSignature(cpool);
                if (clinfo != null) {
                    minfo = clinfo.getMethodInfo(sigstr);
                    if (minfo != null) {
                        addDepends = findDependencies(clinfo, minfo, true);
                    }
                }
                break;
            case Constants.CONSTANT_Fieldref:
                cocl = (ConstantClass) cpool.getConstant(((ConstantFieldref) co).getClassIndex());
                break;
        }
        if (cocl != null) {
            clname = cocl.getBytes(cpool).replace('/', '.');
            OldClassInfo clinf = (OldClassInfo) ai.cliMap.get(clname);
            if (clinf != null) {
                if (clinf.getMethodInfo(OldAppInfo.clinitSig) != null) {
                    // don't add myself as dependency
                    if (clinf != cli) {
                        depends.add(clinf);
                    }
                }
            }
        }
        if (addDepends != null) {
            Iterator itAddDep = addDepends.iterator();
            while (itAddDep.hasNext()) {
                OldClassInfo addCli = (OldClassInfo) itAddDep.next();
                if (addCli == cli) {
                    throw new Error("cyclic indirect <clinit> dependency");
                }
                depends.add(addCli);
            }
        }
    }
    il.dispose();
    return depends;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) InstructionList(org.apache.bcel.generic.InstructionList) Constant(org.apache.bcel.classfile.Constant) Method(org.apache.bcel.classfile.Method) InstructionFinder(org.apache.bcel.util.InstructionFinder) MethodGen(org.apache.bcel.generic.MethodGen) InstructionHandle(org.apache.bcel.generic.InstructionHandle) ConstantNameAndType(org.apache.bcel.classfile.ConstantNameAndType) ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) CPInstruction(org.apache.bcel.generic.CPInstruction) ConstantMethodref(org.apache.bcel.classfile.ConstantMethodref) ConstantPool(org.apache.bcel.classfile.ConstantPool) Iterator(java.util.Iterator) ConstantClass(org.apache.bcel.classfile.ConstantClass) HashSet(java.util.HashSet)

Example 12 with ConstantPool

use of org.apache.bcel.classfile.ConstantPool in project candle-decompiler by bradsdavis.

the class ClassIntermediateVisitor method visitConstantClass.

@Override
public void visitConstantClass(ConstantClass obj) {
    ConstantPool pool = javaClass.getConstantPool();
    String classVal = obj.getConstantValue(pool).toString();
    classVal = StringUtils.replace(classVal, "/", ".");
    if (StringUtils.equals(classVal, this.classBlock.getClassName())) {
        // skip adding class name.
        return;
    }
    this.classBlock.getImports().add(classVal);
}
Also used : ConstantPool(org.apache.bcel.classfile.ConstantPool)

Example 13 with ConstantPool

use of org.apache.bcel.classfile.ConstantPool in project OpenGrok by OpenGrok.

the class JavaClassAnalyzer method getContent.

// TODO this class needs to be thread safe to avoid bug 13364, which was fixed by just updating bcel to 5.2
private void getContent(Writer out, Writer fout, JavaClass c, List<String> defs, List<String> refs, List<String> full) throws IOException {
    String t;
    ConstantPool cp = c.getConstantPool();
    int[] v = new int[cp.getLength() + 1];
    out.write(linkPath(t = c.getSourceFileName()));
    defs.add(t);
    refs.add(t);
    fout.write(t);
    out.write(EOL);
    fout.write(EOL);
    out.write(PACKAGE);
    fout.write(PACKAGE);
    out.write(linkDef(t = c.getPackageName()));
    defs.add(t);
    refs.add(t);
    fout.write(t);
    out.write(EOL);
    fout.write(EOL);
    String aflg;
    out.write(aflg = Utility.accessToString(c.getAccessFlags(), true));
    if (aflg != null) {
        out.write(SPACE);
        fout.write(aflg);
        fout.write(SPACE);
    }
    v[c.getClassNameIndex()] = 1;
    out.write(tagDef(t = c.getClassName()));
    defs.add(t);
    refs.add(t);
    fout.write(t);
    out.write(EXTENDS);
    fout.write(EXTENDS);
    v[c.getSuperclassNameIndex()] = 1;
    out.write(linkDef(t = c.getSuperclassName()));
    refs.add(t);
    fout.write(t);
    for (int i : c.getInterfaceIndices()) {
        v[i] = 1;
    }
    String[] ins = c.getInterfaceNames();
    if (ins != null && ins.length > 0) {
        out.write(IMPLEMENTS);
        fout.write(IMPLEMENTS);
        for (String in : ins) {
            out.write(linkDef(t = in));
            refs.add(t);
            fout.write(t);
            out.write(SPACE);
            fout.write(SPACE);
        }
    }
    out.write(LCBREOL);
    fout.write(LCBREOL);
    for (Attribute a : c.getAttributes()) {
        if (a.getTag() == org.apache.bcel.Const.ATTR_CODE) {
            for (Attribute ca : ((Code) a).getAttributes()) {
                if (ca.getTag() == org.apache.bcel.Const.ATTR_LOCAL_VARIABLE_TABLE) {
                    for (LocalVariable l : ((LocalVariableTable) ca).getLocalVariableTable()) {
                        printLocal(out, fout, l, v, defs, refs);
                    }
                }
            }
        } else if (a.getTag() == org.apache.bcel.Const.ATTR_BOOTSTRAP_METHODS) {
        // TODO fill in bootstrap methods, fix the else if
        } else if (a.getTag() == org.apache.bcel.Const.ATTR_SOURCE_FILE) {
            v[a.getNameIndex()] = 1;
            break;
        }
    }
    String aflgs;
    String fldsig;
    String tdef;
    for (org.apache.bcel.classfile.Field fld : c.getFields()) {
        out.write(TAB);
        fout.write(TAB);
        aflgs = Utility.accessToString(fld.getAccessFlags());
        if (aflgs != null && aflgs.length() > 0) {
            out.write(aflgs);
            fout.write(aflgs);
            fout.write(SPACE);
            out.write(SPACE);
        }
        fldsig = Utility.typeSignatureToString(fld.getSignature(), true);
        out.write(fldsig);
        fout.write(fldsig);
        out.write(SPACE);
        fout.write(SPACE);
        tdef = tagDef(t = fld.getName());
        out.write(tdef);
        fout.write(tdef);
        defs.add(t);
        refs.add(t);
        out.write(EOL);
        fout.write(EOL);
    // TODO show Attributes
    }
    String sig;
    String msig;
    String ltdef;
    for (org.apache.bcel.classfile.Method m : c.getMethods()) {
        out.write(TAB);
        fout.write(TAB);
        aflgs = Utility.accessToString(m.getAccessFlags());
        if (aflgs != null && aflgs.length() > 0) {
            out.write(aflgs);
            fout.write(aflgs);
            out.write(SPACE);
            fout.write(SPACE);
        }
        sig = m.getSignature();
        msig = Utility.methodSignatureReturnType(sig, false);
        out.write(msig);
        fout.write(msig);
        out.write(SPACE);
        fout.write(SPACE);
        ltdef = tagDef(t = m.getName());
        out.write(ltdef);
        fout.write(ltdef);
        defs.add(t);
        refs.add(t);
        out.write(LBRA);
        fout.write(LBRA);
        String[] args = Utility.methodSignatureArgumentTypes(sig, false);
        for (int i = 0; i < args.length; i++) {
            t = args[i];
            out.write(t);
            fout.write(t);
            int spi = t.indexOf(SPACE);
            if (spi > 0) {
                refs.add(t.substring(0, spi));
                defs.add(t.substring(spi + 1));
            }
            if (i < args.length - 1) {
                out.write(COMMA);
                fout.write(COMMA);
            }
        }
        out.write(RBRA);
        fout.write(RBRA);
        ArrayList<LocalVariable[]> locals = new ArrayList<>();
        for (Attribute a : m.getAttributes()) {
            if (a.getTag() == org.apache.bcel.Const.ATTR_EXCEPTIONS) {
                for (int i : ((ExceptionTable) a).getExceptionIndexTable()) {
                    v[i] = 1;
                }
                String[] exs = ((ExceptionTable) a).getExceptionNames();
                if (exs != null && exs.length > 0) {
                    out.write(THROWS);
                    fout.write(THROWS);
                    for (String ex : exs) {
                        out.write(linkDef(ex));
                        fout.write(ex);
                        refs.add(ex);
                        out.write(SPACE);
                        fout.write(SPACE);
                    }
                }
            } else if (a.getTag() == org.apache.bcel.Const.ATTR_CODE) {
                for (Attribute ca : ((Code) a).getAttributes()) {
                    if (ca.getTag() == org.apache.bcel.Const.ATTR_LOCAL_VARIABLE_TABLE) {
                        locals.add(((LocalVariableTable) ca).getLocalVariableTable());
                    }
                }
            }
        }
        out.write(EOL);
        fout.write(EOL);
        if (!locals.isEmpty()) {
            for (LocalVariable[] ls : locals) {
                for (LocalVariable l : ls) {
                    printLocal(out, fout, l, v, defs, refs);
                }
            }
        }
    }
    out.write(RCBREOL);
    fout.write(RCBREOL);
    for (int i = 0; i < v.length - 1; i++) {
        if (v[i] != 1) {
            Constant constant = cp.getConstant(i);
            if (constant != null) {
                full.add(constantToString(constant, cp, v));
            }
        }
    }
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) Attribute(org.apache.bcel.classfile.Attribute) Constant(org.apache.bcel.classfile.Constant) LocalVariable(org.apache.bcel.classfile.LocalVariable) ArrayList(java.util.ArrayList) ExceptionTable(org.apache.bcel.classfile.ExceptionTable) ConstantString(org.apache.bcel.classfile.ConstantString) Code(org.apache.bcel.classfile.Code) ConstantPool(org.apache.bcel.classfile.ConstantPool)

Example 14 with ConstantPool

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

the class AppInfo method updateCheckSum.

public boolean updateCheckSum(MethodInfo prologue) {
    // Also compute SHA-1 checksum for this DFA problem
    // (for caching purposes)
    MessageDigest md, md2;
    try {
        md = MessageDigest.getInstance("SHA1");
        md2 = MessageDigest.getInstance("SHA1");
    } catch (NoSuchAlgorithmException e) {
        logger.info("No digest algorithm found", e);
        digest = null;
        return false;
    }
    PrintWriter writer = null;
    File tempFile = null;
    if (dumpCacheKeyFile != null) {
        try {
            tempFile = new File(dumpCacheKeyFile + "-temp.txt");
            writer = new PrintWriter(tempFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    if (prologue != null) {
        // TODO the prologue method is a hack, we should not use it in the checksum
        // instead we should only hash the required infos (entry-method, clinit-order?,..)
        updateChecksum(prologue, md);
    }
    List<String> classNames = new ArrayList<String>(getClassNames());
    // We iterate in lexical order to make MD5 checksum a bit more deterministic ..
    Collections.sort(classNames);
    for (String name : classNames) {
        ClassInfo ci = getClassInfo(name);
        List<String> methodNames = new ArrayList<String>(ci.getMethodSignatures());
        Collections.sort(methodNames);
        for (String method : methodNames) {
            MethodInfo mi = ci.getMethodInfo(method);
            if (mi.hasCode()) {
                updateChecksum(mi, md);
                if (writer != null) {
                    writer.print("M " + mi + ": ");
                    updateChecksum(mi, md2);
                    this.digest = md2.digest();
                    writer.println(getDigestString());
                }
            }
        }
        ConstantPool cp = ci.getConstantPoolGen().getFinalConstantPool();
        updateCheckSum(cp, md);
        if (writer != null) {
            writer.print("CP " + ci + ": ");
            updateCheckSum(cp, md2);
            this.digest = md2.digest();
            writer.print(cp.getLength() + " ");
            writer.println(getDigestString());
        }
    }
    this.digest = md.digest();
    logger.info("AppInfo has checksum: " + getDigestString());
    if (tempFile != null && writer != null) {
        writer.close();
        File dest = new File(dumpCacheKeyFile + "-" + getDigestString() + ".txt");
        // noinspection ResultOfMethodCallIgnored
        dest.delete();
        tempFile.renameTo(dest);
    }
    return true;
}
Also used : ConstantPool(org.apache.bcel.classfile.ConstantPool) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CallString(com.jopdesign.common.code.CallString) MessageDigest(java.security.MessageDigest) ClassFile(org.apache.bcel.util.ClassPath.ClassFile) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 15 with ConstantPool

use of org.apache.bcel.classfile.ConstantPool in project fb-contrib by mebigfatguy.

the class OverlyPermissiveMethod method sawOpcode.

@Override
public void sawOpcode(int seen) {
    try {
        stack.precomputation(this);
        switch(seen) {
            case Const.INVOKEVIRTUAL:
            case Const.INVOKEINTERFACE:
            case Const.INVOKESTATIC:
            case Const.INVOKESPECIAL:
                {
                    String calledClass = getClassConstantOperand();
                    String sig = getSigConstantOperand();
                    MethodInfo mi = Statistics.getStatistics().getMethodStatistics(calledClass, getNameConstantOperand(), sig);
                    if (mi != null) {
                        if (seen == Const.INVOKEINTERFACE) {
                            mi.addCallingAccess(Const.ACC_PUBLIC);
                        } else {
                            String calledPackage;
                            int slashPos = calledClass.lastIndexOf('/');
                            if (slashPos >= 0) {
                                calledPackage = calledClass.substring(0, slashPos);
                            } else {
                                calledPackage = "";
                            }
                            boolean sameClass = calledClass.equals(callingClass);
                            boolean samePackage = calledPackage.equals(callingPackage);
                            if (sameClass) {
                                mi.addCallingAccess(Const.ACC_PRIVATE);
                            } else if (samePackage) {
                                mi.addCallingAccess(0);
                            } else {
                                if (seen == Const.INVOKESTATIC) {
                                    mi.addCallingAccess(Const.ACC_PUBLIC);
                                } else if (isCallingOnThis(sig)) {
                                    mi.addCallingAccess(Const.ACC_PROTECTED);
                                } else {
                                    mi.addCallingAccess(Const.ACC_PUBLIC);
                                }
                            }
                        }
                    }
                }
                break;
            case Const.INVOKEDYNAMIC:
                ConstantInvokeDynamic id = (ConstantInvokeDynamic) getConstantRefOperand();
                BootstrapMethod bm = getBootstrapMethod(id.getBootstrapMethodAttrIndex());
                if (bm != null) {
                    ConstantPool pool = getConstantPool();
                    ConstantMethodHandle mh = getFirstMethodHandle(pool, bm);
                    if (mh != null) {
                        ConstantCP ref = (ConstantCP) pool.getConstant(mh.getReferenceIndex());
                        ConstantClass cc = (ConstantClass) pool.getConstant(ref.getClassIndex());
                        String clz = ((ConstantUtf8) pool.getConstant(cc.getNameIndex())).getBytes();
                        ConstantNameAndType nameAndType = (ConstantNameAndType) pool.getConstant(ref.getNameAndTypeIndex());
                        String sig = ((ConstantUtf8) pool.getConstant(nameAndType.getSignatureIndex())).getBytes();
                        String name = ((ConstantUtf8) pool.getConstant(nameAndType.getNameIndex())).getBytes();
                        MethodInfo mi = Statistics.getStatistics().getMethodStatistics(clz, name, sig);
                        mi.addCallingAccess(Const.ACC_PUBLIC);
                    }
                }
                break;
            default:
                break;
        }
    } finally {
        stack.sawOpcode(this, seen);
    }
}
Also used : BootstrapMethod(org.apache.bcel.classfile.BootstrapMethod) ConstantPool(org.apache.bcel.classfile.ConstantPool) ConstantInvokeDynamic(org.apache.bcel.classfile.ConstantInvokeDynamic) MethodInfo(com.mebigfatguy.fbcontrib.collect.MethodInfo) ConstantMethodHandle(org.apache.bcel.classfile.ConstantMethodHandle) ConstantUtf8(org.apache.bcel.classfile.ConstantUtf8) ConstantCP(org.apache.bcel.classfile.ConstantCP) ConstantClass(org.apache.bcel.classfile.ConstantClass) ConstantNameAndType(org.apache.bcel.classfile.ConstantNameAndType)

Aggregations

ConstantPool (org.apache.bcel.classfile.ConstantPool)17 ToString (com.mebigfatguy.fbcontrib.utils.ToString)7 BugInstance (edu.umd.cs.findbugs.BugInstance)7 Constant (org.apache.bcel.classfile.Constant)7 ConstantClass (org.apache.bcel.classfile.ConstantClass)6 ConstantNameAndType (org.apache.bcel.classfile.ConstantNameAndType)6 JavaClass (org.apache.bcel.classfile.JavaClass)5 ConstantMethodref (org.apache.bcel.classfile.ConstantMethodref)4 ConstantString (org.apache.bcel.classfile.ConstantString)4 ConstantUtf8 (org.apache.bcel.classfile.ConstantUtf8)4 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)3 ArrayList (java.util.ArrayList)3 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)3 Method (org.apache.bcel.classfile.Method)3 BitSet (java.util.BitSet)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Attribute (org.apache.bcel.classfile.Attribute)2 Code (org.apache.bcel.classfile.Code)2 ConstantInvokeDynamic (org.apache.bcel.classfile.ConstantInvokeDynamic)2