Search in sources :

Example 1 with Field

use of org.apache.bcel.classfile.Field in project ant by apache.

the class JavaClassHelper method getConstants.

/**
 * Get the constants declared in a file as name=value
 *
 * @param bytes the class as a array of bytes
 * @return a StringBuffer contains the name=value pairs
 * @exception IOException if an error occurs
 */
public static StringBuffer getConstants(final byte[] bytes) throws IOException {
    final StringBuffer sb = new StringBuffer();
    final ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    final ClassParser parser = new ClassParser(bis, "");
    final JavaClass javaClass = parser.parse();
    final Field[] fields = javaClass.getFields();
    for (final Field field : fields) {
        if (field != null) {
            final ConstantValue cv = field.getConstantValue();
            if (cv != null) {
                String cvs = cv.toString();
                // Remove start and end quotes if field is a String
                if (cvs.startsWith("\"") && cvs.endsWith("\"")) {
                    cvs = cvs.substring(1, cvs.length() - 1);
                }
                sb.append(field.getName());
                sb.append('=');
                sb.append(cvs);
                sb.append(LS);
            }
        }
    }
    return sb;
}
Also used : Field(org.apache.bcel.classfile.Field) JavaClass(org.apache.bcel.classfile.JavaClass) ByteArrayInputStream(java.io.ByteArrayInputStream) ConstantValue(org.apache.bcel.classfile.ConstantValue) ClassParser(org.apache.bcel.classfile.ClassParser)

Example 2 with Field

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

the class HiddenInheritedFieldCheck method visitObject.

/**
 * @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor
 */
public void visitObject(Object aJavaClass) {
    final JavaClass javaClass = (JavaClass) aJavaClass;
    final String className = javaClass.getClassName();
    final JavaClass[] superClasses = javaClass.getSuperClasses();
    final Field[] fields = javaClass.getFields();
    // Check all fields
    for (int i = 0; i < fields.length; i++) {
        final Field field = fields[i];
        // Go through all superclasses
        for (int j = 0; j < superClasses.length; j++) {
            final JavaClass superClass = superClasses[j];
            final String superClassName = superClass.getClassName();
            final Field[] superClassFields = superClass.getFields();
            // Go through the filds of the superclasses
            for (int k = 0; k < superClassFields.length; k++) {
                final Field superClassField = superClassFields[k];
                if (!superClassField.isPrivate() && superClassField.getName().equals(field.getName()) && !ignore(className, field)) {
                    log(javaClass, 0, "hidden.inherited.field", new Object[] { fields[i], superClassName });
                }
            }
        }
    }
}
Also used : Field(org.apache.bcel.classfile.Field) JavaClass(org.apache.bcel.classfile.JavaClass)

Example 3 with Field

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

the class UnreadFieldCheck method leaveSet.

/**
 * @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor
 */
public void leaveSet(Set aJavaClasses) {
    final Iterator it = aJavaClasses.iterator();
    while (it.hasNext()) {
        final JavaClass javaClass = (JavaClass) it.next();
        final String className = javaClass.getClassName();
        final JavaClassDefinition classDef = findJavaClassDef(javaClass);
        final FieldDefinition[] fieldDefs = classDef.getFieldDefs();
        for (int i = 0; i < fieldDefs.length; i++) {
            if (fieldDefs[i].getReadReferenceCount() == 0) {
                final Field field = fieldDefs[i].getField();
                if (!field.isFinal() && (!ignore(className, field))) {
                    log(javaClass, 0, "unread.field", new Object[] { fieldDefs[i] });
                }
            }
        }
    }
}
Also used : Field(org.apache.bcel.classfile.Field) JavaClass(org.apache.bcel.classfile.JavaClass) FieldDefinition(com.puppycrawl.tools.checkstyle.bcel.classfile.FieldDefinition) Iterator(java.util.Iterator) JavaClassDefinition(com.puppycrawl.tools.checkstyle.bcel.classfile.JavaClassDefinition)

Example 4 with Field

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

the class DubiousListCollection method getFieldAnnotation.

/**
 * builds a field annotation by finding the field in the classes' field list
 *
 * @param fieldName
 *            the field for which to built the field annotation
 *
 * @return the field annotation of the specified field
 */
@Nullable
private FieldAnnotation getFieldAnnotation(final String fieldName) {
    JavaClass cls = getClassContext().getJavaClass();
    Field[] fields = cls.getFields();
    for (Field f : fields) {
        if (f.getName().equals(fieldName)) {
            return new FieldAnnotation(cls.getClassName(), fieldName, f.getSignature(), f.isStatic());
        }
    }
    // shouldn't happen
    return null;
}
Also used : Field(org.apache.bcel.classfile.Field) XField(edu.umd.cs.findbugs.ba.XField) JavaClass(org.apache.bcel.classfile.JavaClass) FieldAnnotation(edu.umd.cs.findbugs.FieldAnnotation) Nullable(javax.annotation.Nullable)

Example 5 with Field

use of org.apache.bcel.classfile.Field 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)

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