Search in sources :

Example 1 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project qpid-broker-j by apache.

the class BrokerTestHelper method createSubClassByteCode.

private static byte[] createSubClassByteCode(final String className, final Class<?>[] interfaces) {
    String[] ifnames = new String[interfaces.length];
    for (int i = 0; i < interfaces.length; i++) {
        ifnames[i] = interfaces[i].getName();
    }
    ClassGen classGen = new ClassGen(className, "java.lang.Object", "<generated>", ACC_PUBLIC | ACC_SUPER | ACC_INTERFACE, ifnames);
    JavaClass javaClass = classGen.getJavaClass();
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        javaClass.dump(out);
        return out.toByteArray();
    } catch (IOException ioex) {
        throw new IllegalStateException("Could not write to a ByteArrayOutputStream - should not happen", ioex);
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) ClassGen(org.apache.bcel.generic.ClassGen) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 2 with JavaClass

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

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

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

the class HiddenStaticMethodCheck 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 Method[] methods = javaClass.getMethods();
    // Check all methods
    for (int i = 0; i < methods.length; i++) {
        final Method method = methods[i];
        // Check that the method is a possible match
        if (!method.isPrivate() && method.isStatic()) {
            // Go through all their superclasses
            for (int j = 0; j < superClasses.length; j++) {
                final JavaClass superClass = superClasses[j];
                final String superClassName = superClass.getClassName();
                final Method[] superClassMethods = superClass.getMethods();
                // Go through the methods of the superclasses
                for (int k = 0; k < superClassMethods.length; k++) {
                    final Method superClassMethod = superClassMethods[k];
                    if (superClassMethod.getName().equals(method.getName()) && !ignore(className, method)) {
                        Type[] methodTypes = method.getArgumentTypes();
                        Type[] superTypes = superClassMethod.getArgumentTypes();
                        if (methodTypes.length == superTypes.length) {
                            boolean match = true;
                            for (int arg = 0; arg < methodTypes.length; arg++) {
                                if (!methodTypes[arg].equals(superTypes[arg])) {
                                    match = false;
                                }
                            }
                            // Same method parameters
                            if (match) {
                                log(javaClass, 0, "hidden.static.method", new Object[] { method, superClassName });
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Type(org.apache.bcel.generic.Type) JavaClass(org.apache.bcel.classfile.JavaClass) Method(org.apache.bcel.classfile.Method)

Example 5 with JavaClass

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

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