Java Code Examples for org.apache.bcel.Const#ACC_FINAL

The following examples show how to use org.apache.bcel.Const#ACC_FINAL . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UselessSubclassMethod.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@SuppressWarnings("PMD.SimplifyBooleanReturns")
private boolean differentAttributes(Method m1, Method m2) {
    if (m1.getAnnotationEntries().length > 0 || m2.getAnnotationEntries().length > 0) {
        return true;
    }
    int access1 = m1.getAccessFlags()
            & (Const.ACC_PRIVATE | Const.ACC_PROTECTED | Const.ACC_PUBLIC | Const.ACC_FINAL);
    int access2 = m2.getAccessFlags()
            & (Const.ACC_PRIVATE | Const.ACC_PROTECTED | Const.ACC_PUBLIC | Const.ACC_FINAL);


    m1.getAnnotationEntries();
    if (access1 != access2) {
        return true;
    }
    if (!thrownExceptions(m1).equals(thrownExceptions(m2))) {
        return false;
    }
    return false;
}
 
Example 2
Source File: ClassParser.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/**
 * Reads information about the class and its super class.
 * @throws  IOException
 * @throws  ClassFormatException
 */
private void readClassInfo() throws IOException, ClassFormatException {
    accessFlags = dataInputStream.readUnsignedShort();
    /* Interfaces are implicitely abstract, the flag should be set
     * according to the JVM specification.
     */
    if ((accessFlags & Const.ACC_INTERFACE) != 0) {
        accessFlags |= Const.ACC_ABSTRACT;
    }
    if (((accessFlags & Const.ACC_ABSTRACT) != 0)
            && ((accessFlags & Const.ACC_FINAL) != 0)) {
        throw new ClassFormatException("Class " + fileName + " can't be both final and abstract");
    }
    classNameIndex = dataInputStream.readUnsignedShort();
    superclassNameIndex = dataInputStream.readUnsignedShort();
}
 
Example 3
Source File: MutableStaticFields.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visit(Field obj) {
    super.visit(obj);
    int flags = obj.getAccessFlags();
    boolean isStatic = (flags & Const.ACC_STATIC) != 0;
    if (!isStatic) {
        return;
    }
    boolean isVolatile = (flags & Const.ACC_VOLATILE) != 0;
    if (isVolatile) {
        return;
    }
    boolean isFinal = (flags & Const.ACC_FINAL) != 0;
    boolean isPublic = publicClass && (flags & Const.ACC_PUBLIC) != 0;
    boolean isProtected = publicClass && (flags & Const.ACC_PROTECTED) != 0;
    if (!isPublic && !isProtected) {
        return;
    }

    boolean isArray = getFieldSig().charAt(0) == '[';

    if (isFinal && !(isArray || isCollection(getFieldSig()))) {
        return;
    }
    if (isEclipseNLS && getFieldSig().equals("Ljava/lang/String;")) {
        return;
    }

    seen.add(getXField());
}
 
Example 4
Source File: FieldInfo.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private FieldInfo(@SlashedClassName String className, String fieldName, String fieldSignature,
        @CheckForNull String fieldSourceSignature, int accessFlags, Map<ClassDescriptor, AnnotationValue> fieldAnnotations,
        boolean isResolved) {
    super(className, fieldName, fieldSignature, (accessFlags & Const.ACC_STATIC) != 0);
    this.accessFlags = accessFlags | (fieldName.startsWith("this$") ? Const.ACC_FINAL : 0);
    this.fieldSourceSignature = fieldSourceSignature;
    this.fieldAnnotations = Util.immutableMap(fieldAnnotations);
    this.isResolved = isResolved;
}
 
Example 5
Source File: ClassDumper.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Processes information about the class and its super class.
 * @throws  IOException
 * @throws  ClassFormatException
 */
private final void processClassInfo () throws IOException, ClassFormatException {
    access_flags = file.readUnsignedShort();
    /* Interfaces are implicitely abstract, the flag should be set
     * according to the JVM specification.
     */
    if ((access_flags & Const.ACC_INTERFACE) != 0) {
        access_flags |= Const.ACC_ABSTRACT;
    }
    if (((access_flags & Const.ACC_ABSTRACT) != 0)
            && ((access_flags & Const.ACC_FINAL) != 0)) {
        throw new ClassFormatException("Class " + file_name +
                " can't be both final and abstract");
    }

    System.out.printf("%nClass info:%n");
    System.out.println("  flags: " + BCELifier.printFlags(access_flags,
            BCELifier.FLAGS.CLASS));
    class_name_index = file.readUnsignedShort();
    System.out.printf("  this_class: %d (", class_name_index);
    System.out.println(constantToString(class_name_index) + ")");

    superclass_name_index = file.readUnsignedShort();
    System.out.printf("  super_class: %d (", superclass_name_index);
    if (superclass_name_index > 0) {
        System.out.printf("%s", constantToString(superclass_name_index));
    }
    System.out.println(")");
}
 
Example 6
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitInnerClasses(final InnerClasses obj) {//vmspec2 4.7.5

    // exactly one InnerClasses attr per ClassFile if some inner class is refernced: see visitJavaClass()

    checkIndex(obj, obj.getNameIndex(), CONST_Utf8);

    final String name = ((ConstantUtf8) cp.getConstant(obj.getNameIndex())).getBytes();
    if (! name.equals("InnerClasses")) {
        throw new ClassConstraintException(
            "The InnerClasses attribute '"+tostring(obj)+"' is not correctly named 'InnerClasses' but '"+name+"'.");
    }

    final InnerClass[] ics = obj.getInnerClasses();

    for (final InnerClass ic : ics) {
        checkIndex(obj, ic.getInnerClassIndex(), CONST_Class);
        final int outer_idx = ic.getOuterClassIndex();
        if (outer_idx != 0) {
            checkIndex(obj, outer_idx, CONST_Class);
        }
        final int innername_idx = ic.getInnerNameIndex();
        if (innername_idx != 0) {
            checkIndex(obj, innername_idx, CONST_Utf8);
        }
        int acc = ic.getInnerAccessFlags();
        acc = acc & (~ (Const.ACC_PUBLIC | Const.ACC_PRIVATE | Const.ACC_PROTECTED |
                        Const.ACC_STATIC | Const.ACC_FINAL | Const.ACC_INTERFACE | Const.ACC_ABSTRACT));
        if (acc != 0) {
            addMessage(
                "Unknown access flag for inner class '"+tostring(ic)+"' set (InnerClasses attribute '"+tostring(obj)+"').");
        }
    }
    // Semantical consistency is not yet checked by Sun, see vmspec2 4.7.5.
    // [marked TODO in JustIce]
}
 
Example 7
Source File: StartInConstructor.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public boolean shouldVisit(JavaClass obj) {
    boolean isFinal = (obj.getAccessFlags() & Const.ACC_FINAL) != 0 || (obj.getAccessFlags() & Const.ACC_PUBLIC) == 0;
    return !isFinal;
}
 
Example 8
Source File: AbstractClassMember.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public boolean isFinal() {
    return (accessFlags & Const.ACC_FINAL) != 0;
}
 
Example 9
Source File: AccessFlags.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
public final boolean isFinal() {
    return (access_flags & Const.ACC_FINAL) != 0;
}
 
Example 10
Source File: MethodParameter.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
public boolean isFinal() {
    return (accessFlags & Const.ACC_FINAL) != 0;
}
 
Example 11
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitField(final Field obj) {

    if (jc.isClass()) {
        int maxone=0;
        if (obj.isPrivate()) {
            maxone++;
        }
        if (obj.isProtected()) {
            maxone++;
        }
        if (obj.isPublic()) {
            maxone++;
        }
        if (maxone > 1) {
            throw new ClassConstraintException("Field '"+tostring(obj)+
                "' must only have at most one of its ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC modifiers set.");
        }

        if (obj.isFinal() && obj.isVolatile()) {
            throw new ClassConstraintException("Field '"+tostring(obj)+
                "' must only have at most one of its ACC_FINAL, ACC_VOLATILE modifiers set.");
        }
    }
    else{ // isInterface!
        if (!obj.isPublic()) {
            throw new ClassConstraintException("Interface field '"+tostring(obj)+
                "' must have the ACC_PUBLIC modifier set but hasn't!");
        }
        if (!obj.isStatic()) {
            throw new ClassConstraintException("Interface field '"+tostring(obj)+
                "' must have the ACC_STATIC modifier set but hasn't!");
        }
        if (!obj.isFinal()) {
            throw new ClassConstraintException("Interface field '"+tostring(obj)+
                "' must have the ACC_FINAL modifier set but hasn't!");
        }
    }

    if ((obj.getAccessFlags() & ~(Const.ACC_PUBLIC|Const.ACC_PRIVATE|Const.ACC_PROTECTED|Const.ACC_STATIC|
                                  Const.ACC_FINAL|Const.ACC_VOLATILE|Const.ACC_TRANSIENT)) > 0) {
        addMessage("Field '"+tostring(obj)+
            "' has access flag(s) other than ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED,"+
                " ACC_STATIC, ACC_FINAL, ACC_VOLATILE, ACC_TRANSIENT set (ignored).");
    }

    checkIndex(obj, obj.getNameIndex(), CONST_Utf8);

    final String name = obj.getName();
    if (! validFieldName(name)) {
        throw new ClassConstraintException("Field '"+tostring(obj)+"' has illegal name '"+obj.getName()+"'.");
    }

    // A descriptor is often named signature in BCEL
    checkIndex(obj, obj.getSignatureIndex(), CONST_Utf8);

    final String sig  = ((ConstantUtf8) (cp.getConstant(obj.getSignatureIndex()))).getBytes(); // Field or Method sig.(=descriptor)

    try{
        Type.getType(sig);  /* Don't need the return value */
    }
    catch (final ClassFormatException cfe) {
        throw new ClassConstraintException("Illegal descriptor (==signature) '"+sig+"' used by '"+tostring(obj)+"'.", cfe);
    }

    final String nameanddesc = name+sig;
    if (field_names_and_desc.contains(nameanddesc)) {
        throw new ClassConstraintException("No two fields (like '"+tostring(obj)+
            "') are allowed have same names and descriptors!");
    }
    if (field_names.contains(name)) {
        addMessage("More than one field of name '"+name+
            "' detected (but with different type descriptors). This is very unusual.");
    }
    field_names_and_desc.add(nameanddesc);
    field_names.add(name);

    final Attribute[] atts = obj.getAttributes();
    for (final Attribute att : atts) {
        if ((!(att instanceof ConstantValue)) &&
                (!(att instanceof Synthetic)) &&
                (!(att instanceof Deprecated))) {
            addMessage("Attribute '" + tostring(att) + "' as an attribute of Field '" +
                tostring(obj) + "' is unknown and will therefore be ignored.");
        }
        if (!(att instanceof ConstantValue)) {
            addMessage("Attribute '" + tostring(att) + "' as an attribute of Field '" + tostring(obj) +
                "' is not a ConstantValue and is therefore only of use for debuggers and such.");
        }
    }
}