Search in sources :

Example 16 with JavaClass

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

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

the class PossibleMemoryBloat method parseFields.

private void parseFields(ClassContext classContext) {
    JavaClass cls = classContext.getJavaClass();
    Field[] fields = cls.getFields();
    for (Field f : fields) {
        String sig = f.getSignature();
        if (f.isStatic()) {
            if (bloatableSigs.contains(sig)) {
                bloatableCandidates.put(XFactory.createXField(cls.getClassName(), f.getName(), f.getSignature(), f.isStatic()), FieldAnnotation.fromBCELField(cls, f));
            }
        } else if ("Ljava/lang/ThreadLocal;".equals(sig)) {
            threadLocalNonStaticFields.add(FieldAnnotation.fromBCELField(cls, f));
        }
    }
}
Also used : Field(org.apache.bcel.classfile.Field) XField(edu.umd.cs.findbugs.ba.XField) JavaClass(org.apache.bcel.classfile.JavaClass)

Example 18 with JavaClass

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

the class PresizeCollections method isSizedSource.

@Nullable
private PSCUserValue isSizedSource(OpcodeStack.Item itm) {
    try {
        String sig = itm.getSignature();
        JavaClass cls = Repository.lookupClass(sig.substring(1, sig.length() - 1));
        if (cls.instanceOf(collectionClass)) {
            return new PSCUserValue(true);
        }
    } catch (ClassNotFoundException e) {
        bugReporter.reportMissingClass(e);
    }
    return null;
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) ToString(com.mebigfatguy.fbcontrib.utils.ToString) Nullable(javax.annotation.Nullable)

Example 19 with JavaClass

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

the class ReflectionOnObjectMethods method visitClassContext.

/**
 * implements the visitor to create the stack and local and field maps for Class arrays to be used for getting the reflection method
 *
 * @param classContext
 *            the context object of the currently parse class
 */
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        stack = new OpcodeStack();
        localClassTypes = new HashMap<>();
        fieldClassTypes = new HashMap<>();
        JavaClass cls = classContext.getJavaClass();
        Method staticInit = findStaticInitializer(cls);
        if (staticInit != null) {
            setupVisitorForClass(cls);
            doVisitMethod(staticInit);
        }
        super.visitClassContext(classContext);
    } finally {
        stack = null;
        localClassTypes = null;
        fieldClassTypes = null;
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) JavaClass(org.apache.bcel.classfile.JavaClass) Method(org.apache.bcel.classfile.Method)

Example 20 with JavaClass

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

the class RuntimeExceptionDeclared method visitMethod.

/**
 * overrides the visitor to find declared runtime exceptions
 *
 * @param obj
 *            the method object of the currently parsed method
 */
@Override
public void visitMethod(final Method obj) {
    if (obj.isSynthetic()) {
        return;
    }
    ExceptionTable et = obj.getExceptionTable();
    if (et != null) {
        String[] exNames = et.getExceptionNames();
        Set<String> methodRTExceptions = new HashSet<>(6);
        int priority = LOW_PRIORITY;
        boolean foundRuntime = false;
        for (String ex : exNames) {
            boolean isRuntime = false;
            if (runtimeExceptions.contains(ex)) {
                isRuntime = true;
            } else {
                try {
                    JavaClass exClass = Repository.lookupClass(ex);
                    if (exClass.instanceOf(runtimeExceptionClass)) {
                        runtimeExceptions.add(ex);
                        if (ex.startsWith("java.lang.")) {
                            priority = NORMAL_PRIORITY;
                        }
                        isRuntime = true;
                    }
                } catch (ClassNotFoundException cnfe) {
                    bugReporter.reportMissingClass(cnfe);
                }
            }
            if (isRuntime) {
                foundRuntime = true;
                methodRTExceptions.add(ex);
            }
        }
        if (foundRuntime) {
            BugInstance bug = new BugInstance(this, BugType.DRE_DECLARED_RUNTIME_EXCEPTION.name(), priority).addClass(this).addMethod(this);
            for (String ex : methodRTExceptions) {
                bug.add(new StringAnnotation(ex));
            }
            bugReporter.reportBug(bug);
        }
    }
}
Also used : StringAnnotation(edu.umd.cs.findbugs.StringAnnotation) JavaClass(org.apache.bcel.classfile.JavaClass) BugInstance(edu.umd.cs.findbugs.BugInstance) ExceptionTable(org.apache.bcel.classfile.ExceptionTable) HashSet(java.util.HashSet)

Aggregations

JavaClass (org.apache.bcel.classfile.JavaClass)144 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)45 BugInstance (edu.umd.cs.findbugs.BugInstance)43 Method (org.apache.bcel.classfile.Method)28 ToString (com.mebigfatguy.fbcontrib.utils.ToString)27 Field (org.apache.bcel.classfile.Field)17 HashSet (java.util.HashSet)14 HashMap (java.util.HashMap)11 ClassParser (org.apache.bcel.classfile.ClassParser)10 ArrayList (java.util.ArrayList)9 IOException (java.io.IOException)8 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)8 XField (edu.umd.cs.findbugs.ba.XField)7 Nullable (javax.annotation.Nullable)7 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)7 Type (org.apache.bcel.generic.Type)7 Iterator (java.util.Iterator)6 List (java.util.List)6 Map (java.util.Map)6 Set (java.util.Set)6