Search in sources :

Example 1 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.signatureToString(fld.getSignature());
        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 2 with ConstantPool

use of org.apache.bcel.classfile.ConstantPool in project contribution by checkstyle.

the class ReferenceVisitor method visitObject.

/**
 * @see com.puppycrawl.tools.checkstyle.bcel.IDeepVisitor
 */
public void visitObject(Object aObject) {
    final JavaClass javaClass = (JavaClass) aObject;
    final ConstantPool pool = javaClass.getConstantPool();
    mCurrentPoolGen = new ConstantPoolGen(pool);
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) JavaClass(org.apache.bcel.classfile.JavaClass) ConstantPool(org.apache.bcel.classfile.ConstantPool)

Example 3 with ConstantPool

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

the class ExceptionSoftening method buildConstrainingInfo.

/**
 * returns exception names describing what exceptions are allowed to be thrown
 *
 * @param cls
 *            the cls to find the exceptions in
 * @param m
 *            the method to add exceptions from
 * @return a map with one entry of a class name to a set of exceptions that constrain what can be thrown.
 *
 * @throws ClassNotFoundException
 *             if an exception class can't be loaded from the repository
 */
private Map<String, Set<String>> buildConstrainingInfo(JavaClass cls, Method m) throws ClassNotFoundException {
    Map<String, Set<String>> constraintInfo = new HashMap<>();
    Set<String> exs = new HashSet<>();
    ExceptionTable et = m.getExceptionTable();
    if (et != null) {
        int[] indexTable = et.getExceptionIndexTable();
        ConstantPool pool = cls.getConstantPool();
        for (int index : indexTable) {
            if (index != 0) {
                ConstantClass ccls = (ConstantClass) pool.getConstant(index);
                String exName = ccls.getBytes(pool);
                JavaClass exClass = Repository.lookupClass(exName);
                if (!exClass.instanceOf(runtimeClass)) {
                    exs.add(ccls.getBytes(pool));
                }
            }
        }
    }
    constraintInfo.put(cls.getClassName(), exs);
    return constraintInfo;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) BitSet(java.util.BitSet) JavaClass(org.apache.bcel.classfile.JavaClass) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConstantPool(org.apache.bcel.classfile.ConstantPool) ExceptionTable(org.apache.bcel.classfile.ExceptionTable) ToString(com.mebigfatguy.fbcontrib.utils.ToString) ConstantClass(org.apache.bcel.classfile.ConstantClass) HashSet(java.util.HashSet)

Example 4 with ConstantPool

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

the class FieldCouldBeLocal method visitClassContext.

/**
 * overrides the visitor to collect localizable fields, and then report those that survive all method checks.
 *
 * @param classContext
 *            the context object that holds the JavaClass parsed
 */
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        localizableFields = new HashMap<>();
        visitedBlocks = new BitSet();
        clsContext = classContext;
        clsName = clsContext.getJavaClass().getClassName();
        clsSig = SignatureUtils.classToSignature(clsName);
        JavaClass cls = classContext.getJavaClass();
        Field[] fields = cls.getFields();
        ConstantPool cp = classContext.getConstantPoolGen().getConstantPool();
        for (Field f : fields) {
            if (!f.isStatic() && !f.isVolatile() && (f.getName().indexOf(Values.SYNTHETIC_MEMBER_CHAR) < 0) && f.isPrivate()) {
                FieldAnnotation fa = new FieldAnnotation(cls.getClassName(), f.getName(), f.getSignature(), false);
                boolean hasExternalAnnotation = false;
                for (AnnotationEntry entry : f.getAnnotationEntries()) {
                    ConstantUtf8 cutf = (ConstantUtf8) cp.getConstant(entry.getTypeIndex());
                    if (!cutf.getBytes().startsWith(Values.JAVA)) {
                        hasExternalAnnotation = true;
                        break;
                    }
                }
                localizableFields.put(f.getName(), new FieldInfo(fa, hasExternalAnnotation));
            }
        }
        if (!localizableFields.isEmpty()) {
            buildMethodFieldModifiers(classContext);
            super.visitClassContext(classContext);
            for (FieldInfo fi : localizableFields.values()) {
                FieldAnnotation fa = fi.getFieldAnnotation();
                SourceLineAnnotation sla = fi.getSrcLineAnnotation();
                BugInstance bug = new BugInstance(this, BugType.FCBL_FIELD_COULD_BE_LOCAL.name(), NORMAL_PRIORITY).addClass(this).addField(fa);
                if (sla != null) {
                    bug.addSourceLine(sla);
                }
                bugReporter.reportBug(bug);
            }
        }
    } finally {
        localizableFields = null;
        visitedBlocks = null;
        clsContext = null;
        methodFieldModifiers = null;
    }
}
Also used : BitSet(java.util.BitSet) BugInstance(edu.umd.cs.findbugs.BugInstance) ConstantUtf8(org.apache.bcel.classfile.ConstantUtf8) Field(org.apache.bcel.classfile.Field) AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) JavaClass(org.apache.bcel.classfile.JavaClass) SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) ConstantPool(org.apache.bcel.classfile.ConstantPool) FieldAnnotation(edu.umd.cs.findbugs.FieldAnnotation)

Example 5 with ConstantPool

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

the class SillynessPotPourri method checkEqualsStringBufferLength.

private void checkEqualsStringBufferLength() {
    if (stack.getStackDepth() > 0) {
        OpcodeStack.Item itm = stack.getStackItem(0);
        lastIfEqWasBoolean = Values.SIG_PRIMITIVE_BOOLEAN.equals(itm.getSignature());
    }
    byte[] bytes = getCode().getCode();
    if ((lastPCs[1] != -1) && (CodeByteUtils.getbyte(bytes, lastPCs[3]) == Const.INVOKEVIRTUAL)) {
        int loadIns = CodeByteUtils.getbyte(bytes, lastPCs[2]);
        if (((loadIns == Const.LDC) || (loadIns == Const.LDC_W)) && (CodeByteUtils.getbyte(bytes, lastPCs[1]) == Const.INVOKEVIRTUAL)) {
            ConstantPool pool = getConstantPool();
            int toStringIndex = CodeByteUtils.getshort(bytes, lastPCs[1] + 1);
            Constant cmr = pool.getConstant(toStringIndex);
            if (cmr instanceof ConstantMethodref) {
                ConstantMethodref toStringMR = (ConstantMethodref) cmr;
                String toStringCls = toStringMR.getClass(pool);
                if (toStringCls.startsWith("java.lang.StringBu")) {
                    int consIndex = CodeByteUtils.getbyte(bytes, lastPCs[2] + 1);
                    Constant c = pool.getConstant(consIndex);
                    if ((c instanceof ConstantString) && ((ConstantString) c).getBytes(pool).isEmpty()) {
                        int nandtIndex = toStringMR.getNameAndTypeIndex();
                        ConstantNameAndType cnt = (ConstantNameAndType) pool.getConstant(nandtIndex);
                        if (Values.TOSTRING.equals(cnt.getName(pool))) {
                            int lengthIndex = CodeByteUtils.getshort(bytes, lastPCs[3] + 1);
                            ConstantMethodref lengthMR = (ConstantMethodref) pool.getConstant(lengthIndex);
                            nandtIndex = lengthMR.getNameAndTypeIndex();
                            cnt = (ConstantNameAndType) pool.getConstant(nandtIndex);
                            if ("equals".equals(cnt.getName(pool))) {
                                bugReporter.reportBug(new BugInstance(this, BugType.SPP_USE_STRINGBUILDER_LENGTH.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) ConstantMethodref(org.apache.bcel.classfile.ConstantMethodref) ConstantString(org.apache.bcel.classfile.ConstantString) ConstantPool(org.apache.bcel.classfile.ConstantPool) Constant(org.apache.bcel.classfile.Constant) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) ConstantString(org.apache.bcel.classfile.ConstantString) 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