Search in sources :

Example 11 with Field

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

the class ConstantPoolReferenceFinder method findReferencedMembers.

/**
 * Find all referenced members in a class.
 * @param classInfo the class to search.
 * @param checkMembers if false, do not check fields and methods. Else check everything.
 * @return a set of class names and class member signatures found in the class.
 */
public static Set<String> findReferencedMembers(ClassInfo classInfo, boolean checkMembers) {
    // Else we need to go into details..
    Set<String> members = new HashSet<String>();
    ClassMemberVisitor visitor = new ClassMemberVisitor(members);
    JavaClass javaClass = classInfo.compile();
    Set<Integer> ids = findPoolReferences(classInfo, javaClass);
    List<InvokeSite> invokes = new ArrayList<InvokeSite>();
    if (checkMembers) {
        for (Field field : javaClass.getFields()) {
            FieldInfo fieldInfo = classInfo.getFieldInfo(field.getName());
            // add members found in the field
            visitor.visitField(fieldInfo);
            // there are no invokesites in a field, only add found ids
            ids.addAll(findPoolReferences(fieldInfo, field));
        }
        for (Method method : javaClass.getMethods()) {
            MethodInfo methodInfo = classInfo.getMethodInfo(method.getName() + method.getSignature());
            // add members found in the method
            visitor.visitMethod(methodInfo);
            // add all ids for checking, add all invoke sites
            ids.addAll(findPoolReferences(methodInfo, method));
        }
    }
    // fill the members list with all found constantpool references
    visitor.visitClass(classInfo);
    visitPoolReferences(classInfo, visitor, ids);
    return members;
}
Also used : ArrayList(java.util.ArrayList) ConstantString(org.apache.bcel.classfile.ConstantString) Method(org.apache.bcel.classfile.Method) EnclosingMethod(com.jopdesign.common.bcel.EnclosingMethod) 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) InvokeSite(com.jopdesign.common.code.InvokeSite) ConstantFieldInfo(com.jopdesign.common.type.ConstantFieldInfo) FieldInfo(com.jopdesign.common.FieldInfo) HashSet(java.util.HashSet)

Example 12 with Field

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

the class ClassIntermediateVisitor method visitJavaClass.

@Override
public void visitJavaClass(JavaClass obj) {
    this.classBlock.setClassName(javaClass.getClassName());
    this.classBlock.setPackageName(obj.getPackageName());
    this.classBlock.setSuperClassName(obj.getSuperclassName());
    // process the pool.
    Constant[] pool = obj.getConstantPool().getConstantPool();
    for (Constant c : pool) {
        if (c == null)
            continue;
        c.accept(this);
    }
    Field[] fields = obj.getFields();
    for (int i = 0, j = fields.length; i < j; i++) {
        fields[i].accept(this);
    }
    // run through all of the methods
    Method[] methods = obj.getMethods();
    for (int i = 0, j = methods.length; i < j; i++) {
        methods[i].accept(this);
    }
}
Also used : Field(org.apache.bcel.classfile.Field) Constant(org.apache.bcel.classfile.Constant) Method(org.apache.bcel.classfile.Method)

Example 13 with Field

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

the class MethodIntermediateVisitor method visitPUTSTATIC.

public void visitPUTSTATIC(PUTSTATIC instruction) {
    ConstantPoolGen cpg = context.getMethodGen().getConstantPool();
    String fieldName = instruction.getFieldName(cpg);
    Type fieldType = instruction.getFieldType(cpg);
    Expression right = context.getExpressions().pop();
    Variable variable = new Variable(context.getCurrentInstruction(), fieldType, fieldName);
    Assignment assignment = new Assignment(context.getCurrentInstruction(), variable, right);
    if (LOG.isDebugEnabled()) {
        for (Field field : context.getJavaClass().getFields()) {
            LOG.debug(field);
        }
    }
    StatementIntermediate complete = new StatementIntermediate(context.getCurrentInstruction(), assignment);
    context.pushIntermediateToInstruction(complete);
}
Also used : Assignment(org.candle.decompiler.intermediate.expression.Assignment) Field(org.apache.bcel.classfile.Field) OperationType(org.candle.decompiler.intermediate.expression.OperationType) ArithmeticType(org.candle.decompiler.intermediate.expression.ArithmeticType) GeneratedVariable(org.candle.decompiler.intermediate.expression.GeneratedVariable) Variable(org.candle.decompiler.intermediate.expression.Variable) IntermediateVariable(org.candle.decompiler.intermediate.IntermediateVariable) TypedExpression(org.candle.decompiler.intermediate.expression.TypedExpression) Expression(org.candle.decompiler.intermediate.expression.Expression) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate)

Example 14 with Field

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

the class NonFunctionalField method visitClassContext.

/**
 * checks to see if the class is Serializable, then looks for fields that are both final and transient
 *
 * @param classContext
 *            the context object of the currently parsed class
 */
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        if ((serializableClass != null) && (cls.implementationOf(serializableClass))) {
            Field[] fields = cls.getFields();
            setupVisitorForClass(cls);
            for (Field f : fields) {
                if (!f.isStatic() && f.isFinal() && f.isTransient()) {
                    bugReporter.reportBug(new BugInstance(this, BugType.NFF_NON_FUNCTIONAL_FIELD.name(), Priorities.NORMAL_PRIORITY).addClass(this).addField(cls.getClassName(), f.getName(), f.getSignature(), f.getAccessFlags()));
                }
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }
}
Also used : Field(org.apache.bcel.classfile.Field) JavaClass(org.apache.bcel.classfile.JavaClass) BugInstance(edu.umd.cs.findbugs.BugInstance)

Example 15 with Field

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

the class WiringIssues method visitClassContext.

@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "SF_SWITCH_NO_DEFAULT", justification = "Only a few cases need special handling")
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        Field[] fields = cls.getFields();
        if (fields.length > 0) {
            Map<WiringType, FieldAnnotation> wiredFields = new HashMap<>();
            boolean loadedParents = false;
            for (Field field : fields) {
                boolean hasAutowired = false;
                String qualifier = "";
                for (AnnotationEntry entry : field.getAnnotationEntries()) {
                    switch(entry.getAnnotationType()) {
                        case SPRING_AUTOWIRED:
                            if (!loadedParents) {
                                loadParentAutowireds(cls.getSuperClass(), wiredFields);
                                loadedParents = true;
                            }
                            hasAutowired = true;
                            break;
                        case SPRING_QUALIFIER:
                            qualifier = entry.getElementValuePairs()[0].getValue().stringifyValue();
                            break;
                    }
                }
                if (hasAutowired) {
                    WiringType wt = new WiringType(field.getSignature(), field.getGenericSignature(), qualifier);
                    FieldAnnotation existingAnnotation = wiredFields.get(wt);
                    if (existingAnnotation == null) {
                        wiredFields.put(wt, FieldAnnotation.fromBCELField(cls.getClassName(), field));
                    } else {
                        bugReporter.reportBug(new BugInstance(this, BugType.WI_DUPLICATE_WIRED_TYPES.name(), NORMAL_PRIORITY).addClass(cls).addField(FieldAnnotation.fromBCELField(cls, field)).addField(existingAnnotation));
                        wiredFields.remove(wt);
                    }
                }
            }
        }
        stack = new OpcodeStack();
        super.visitClassContext(classContext);
    } catch (ClassNotFoundException e) {
        bugReporter.reportMissingClass(e);
    } finally {
        stack = null;
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) HashMap(java.util.HashMap) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) Field(org.apache.bcel.classfile.Field) AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) JavaClass(org.apache.bcel.classfile.JavaClass) FieldAnnotation(edu.umd.cs.findbugs.FieldAnnotation)

Aggregations

Field (org.apache.bcel.classfile.Field)23 JavaClass (org.apache.bcel.classfile.JavaClass)17 XField (edu.umd.cs.findbugs.ba.XField)6 Method (org.apache.bcel.classfile.Method)6 ToString (com.mebigfatguy.fbcontrib.utils.ToString)5 BugInstance (edu.umd.cs.findbugs.BugInstance)5 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)4 FieldAnnotation (edu.umd.cs.findbugs.FieldAnnotation)3 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)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 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 ConstantInteger (org.apache.bcel.classfile.ConstantInteger)2 ConstantValue (org.apache.bcel.classfile.ConstantValue)2 InvokeSite (com.jopdesign.common.code.InvokeSite)1