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

The following examples show how to use org.apache.bcel.Const#ACC_PUBLIC . 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: Naming.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void visit(Field obj) {
    if (getFieldName().length() == 1) {
        return;
    }

    if (isEclipseNLS) {
        int flags = obj.getAccessFlags();
        if ((flags & Const.ACC_STATIC) != 0 && ((flags & Const.ACC_PUBLIC) != 0) && "Ljava/lang/String;".equals(getFieldSig())) {
            // ignore "public statis String InstallIUCommandTooltip;"
            // messages from Eclipse NLS bundles
            return;
        }
    }
    if (badFieldName(obj)) {
        bugReporter.reportBug(new BugInstance(this, "NM_FIELD_NAMING_CONVENTION", classIsPublicOrProtected
                && (obj.isPublic() || obj.isProtected()) && !hasBadFieldNames ? NORMAL_PRIORITY : LOW_PRIORITY)
                        .addClass(this).addVisitedField(this));
    }
}
 
Example 2
Source File: FindReturnRef.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void visit(Method obj) {
    check = publicClass && (obj.getAccessFlags() & (Const.ACC_PUBLIC)) != 0;
    if (!check) {
        return;
    }
    staticMethod = (obj.getAccessFlags() & (Const.ACC_STATIC)) != 0;
    // variableNames = obj.getLocalVariableTable();
    parameterCount = getNumberMethodArguments();

    if (!staticMethod) {
        parameterCount++;
    }

    thisOnTOS = false;
    fieldOnTOS = false;
    super.visit(obj);
    thisOnTOS = false;
    fieldOnTOS = false;
}
 
Example 3
Source File: TestArrayAccess02Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
public TestArrayAccess02Creator() {
  _cg = new ClassGen(TEST_PACKAGE+".TestArrayAccess02", "java.lang.Object", "TestArrayAccess02.java",
          Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {  });

  _cp = _cg.getConstantPool();
  _factory = new InstructionFactory(_cg, _cp);
}
 
Example 4
Source File: TestArrayAccess04Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
public TestArrayAccess04Creator() {
  _cg = new ClassGen(TEST_PACKAGE+".TestArrayAccess04", "java.lang.Object", "TestArrayAccess04.java",
          Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {  });

  _cp = _cg.getConstantPool();
  _factory = new InstructionFactory(_cg, _cp);
}
 
Example 5
Source File: TestArrayAccess02Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
private void createMethod_1() {
  final InstructionList il = new InstructionList();
  final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS, new String[] {  },
          "test", TEST_PACKAGE+".TestArrayAccess02", il, _cp);

  final InstructionHandle ih_0 = il.append(new PUSH(_cp, 1));
  Assert.assertNotNull(ih_0); // TODO why is this not used
  il.append(_factory.createNewArray(new ObjectType(TEST_PACKAGE+".TestArrayAccess02"), (short) 1));
  il.append(InstructionFactory.createStore(Type.OBJECT, 0));
  final InstructionHandle ih_5 = il.append(new PUSH(_cp, 1));
  Assert.assertNotNull(ih_5); // TODO why is this not used
  il.append(_factory.createNewArray(Type.STRING, (short) 1));
  il.append(InstructionFactory.createStore(Type.OBJECT, 1));
  final InstructionHandle ih_10 = il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
  Assert.assertNotNull(ih_10); // TODO why is this not used
  il.append(new PUSH(_cp, 0));
  il.append(_factory.createNew(TEST_PACKAGE+".TestArrayAccess02"));
  il.append(InstructionConst.DUP);
  il.append(_factory.createInvoke(TEST_PACKAGE+".TestArrayAccess02", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
  il.append(InstructionConst.AASTORE);
  final InstructionHandle ih_20 = il.append(InstructionFactory.createReturn(Type.VOID));
  Assert.assertNotNull(ih_20); // TODO why is this not used
  method.setMaxStack();
  method.setMaxLocals();
  _cg.addMethod(method.getMethod());
  il.dispose();
}
 
Example 6
Source File: TestReturn01Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
private void createMethod_0() {
  final InstructionList il = new InstructionList();
  final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {  },
          "<init>", TEST_PACKAGE+".TestReturn01", il, _cp);

  final InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
  Assert.assertNotNull(ih_0); // TODO why is this not used
  il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
  final InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID));
  Assert.assertNotNull(ih_4); // TODO why is this not used
  method.setMaxStack();
  method.setMaxLocals();
  _cg.addMethod(method.getMethod());
  il.dispose();
}
 
Example 7
Source File: TestArrayAccess02Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
private void createMethod_0() {
  final InstructionList il = new InstructionList();
  final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {  }, "<init>",
          TEST_PACKAGE+".TestArrayAccess02", il, _cp);

  final InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
  Assert.assertNotNull(ih_0); // TODO why is this not used
  il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
  final InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID));
  Assert.assertNotNull(ih_4); // TODO why is this not used
  method.setMaxStack();
  method.setMaxLocals();
  _cg.addMethod(method.getMethod());
  il.dispose();
}
 
Example 8
Source File: TestArrayAccess03Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
public TestArrayAccess03Creator() {
  _cg = new ClassGen(TEST_PACKAGE+".TestArrayAccess03", "java.lang.Object", "TestArrayAccess03.java",
          Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {  });

  _cp = _cg.getConstantPool();
  _factory = new InstructionFactory(_cg, _cp);
}
 
Example 9
Source File: GeneratingAnnotatedClassesTestCase.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
private MethodGen createMethodGen(final String methodname, final InstructionList il,
        final ConstantPoolGen cp)
{
    return new MethodGen(Const.ACC_STATIC | Const.ACC_PUBLIC, // access
            // flags
            Type.VOID, // return type
            new Type[] { new ArrayType(Type.STRING, 1) }, // argument
            // types
            new String[] { "argv" }, // arg names
            methodname, "HelloWorld", // method, class
            il, cp);
}
 
Example 10
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 11
Source File: TestArrayAccess03Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
private void createMethod_0() {
  final InstructionList il = new InstructionList();
  final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {  }, "<init>",
          TEST_PACKAGE+".TestArrayAccess03", il, _cp);

  final InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
  Assert.assertNotNull(ih_0); // TODO why is this not used
  il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
  final InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID));
  Assert.assertNotNull(ih_4); // TODO why is this not used
  method.setMaxStack();
  method.setMaxLocals();
  _cg.addMethod(method.getMethod());
  il.dispose();
}
 
Example 12
Source File: TestArrayAccess04Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
private void createMethod_0() {
  final InstructionList il = new InstructionList();
  final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {  }, "<init>",
          TEST_PACKAGE+".TestArrayAccess04", il, _cp);

  final InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
  Assert.assertNotNull(ih_0); // TODO why is this not used
  il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
  final InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID));
  Assert.assertNotNull(ih_4); // TODO why is this not used
  method.setMaxStack();
  method.setMaxLocals();
  _cg.addMethod(method.getMethod());
  il.dispose();
}
 
Example 13
Source File: MutableStaticFields.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visit(JavaClass obj) {
    super.visit(obj);
    int flags = obj.getAccessFlags();
    publicClass = (flags & Const.ACC_PUBLIC) != 0 && !getDottedClassName().startsWith("sun.");

    packageName = extractPackage(getClassName());
    isEclipseNLS = "org.eclipse.osgi.util.NLS".equals(obj.getSuperclassName());
}
 
Example 14
Source File: FindFinalizeInvocations.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visit(Method obj) {
    if (DEBUG) {
        System.out.println("FFI: visiting " + getFullyQualifiedMethodName());
    }
    if ("finalize".equals(getMethodName()) && "()V".equals(getMethodSig()) && (obj.getAccessFlags() & (Const.ACC_PUBLIC)) != 0) {
        bugReporter
                .reportBug(new BugInstance(this, "FI_PUBLIC_SHOULD_BE_PROTECTED", NORMAL_PRIORITY).addClassAndMethod(this));
    }
}
 
Example 15
Source File: AccessFlags.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
public final boolean isPublic() {
    return (access_flags & Const.ACC_PUBLIC) != 0;
}
 
Example 16
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.");
        }
    }
}
 
Example 17
Source File: AbstractClassMember.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public boolean isPublic() {
    return (accessFlags & Const.ACC_PUBLIC) != 0;
}
 
Example 18
Source File: GeneratingAnnotatedClassesTestCase.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
private ClassGen createClassGen(final String classname)
{
    return new ClassGen(classname, "java.lang.Object", "<generated>",
            Const.ACC_PUBLIC | Const.ACC_SUPER, null);
}
 
Example 19
Source File: DontUseEnum.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
private boolean isVisible(FieldOrMethod obj) {
    return (obj.getAccessFlags() & Const.ACC_PUBLIC) != 0 || (obj.getAccessFlags() & Const.ACC_PROTECTED) != 0;
}
 
Example 20
Source File: SerializableIdiom.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visit(Method obj) {

    int accessFlags = obj.getAccessFlags();
    boolean isSynchronized = (accessFlags & Const.ACC_SYNCHRONIZED) != 0;
    if (Const.CONSTRUCTOR_NAME.equals(getMethodName()) && "()V".equals(getMethodSig()) && (accessFlags & Const.ACC_PUBLIC) != 0) {
        hasPublicVoidConstructor = true;
    }
    if (!Const.CONSTRUCTOR_NAME.equals(getMethodName()) && isSynthetic(obj)) {
        foundSynthetic = true;
        // System.out.println(methodName + isSynchronized);
    }

    if ("readExternal".equals(getMethodName()) && "(Ljava/io/ObjectInput;)V".equals(getMethodSig())) {
        sawReadExternal = true;
        if (DEBUG && !obj.isPrivate()) {
            System.out.println("Non-private readExternal method in: " + getDottedClassName());
        }
    } else if ("writeExternal".equals(getMethodName()) && "(Ljava/io/Objectoutput;)V".equals(getMethodSig())) {
        sawWriteExternal = true;
        if (DEBUG && !obj.isPrivate()) {
            System.out.println("Non-private writeExternal method in: " + getDottedClassName());
        }
    } else if ("readResolve".equals(getMethodName()) && getMethodSig().startsWith("()") && isSerializable) {
        sawReadResolve = true;
        if (!"()Ljava/lang/Object;".equals(getMethodSig())) {
            bugReporter.reportBug(new BugInstance(this, "SE_READ_RESOLVE_MUST_RETURN_OBJECT", HIGH_PRIORITY)
                    .addClassAndMethod(this));
        } else if (obj.isStatic()) {
            bugReporter.reportBug(new BugInstance(this, "SE_READ_RESOLVE_IS_STATIC", HIGH_PRIORITY).addClassAndMethod(this));
        } else if (obj.isPrivate()) {
            try {
                Set<ClassDescriptor> subtypes = AnalysisContext.currentAnalysisContext().getSubtypes2()
                        .getSubtypes(getClassDescriptor());
                if (subtypes.size() > 1) {
                    BugInstance bug = new BugInstance(this, "SE_PRIVATE_READ_RESOLVE_NOT_INHERITED", NORMAL_PRIORITY)
                            .addClassAndMethod(this);
                    boolean nasty = false;
                    for (ClassDescriptor subclass : subtypes) {
                        if (!subclass.equals(getClassDescriptor())) {

                            XClass xSub = AnalysisContext.currentXFactory().getXClass(subclass);
                            if (xSub != null && xSub.findMethod("readResolve", "()Ljava/lang/Object;", false) == null
                                    && xSub.findMethod("writeReplace", "()Ljava/lang/Object;", false) == null) {
                                bug.addClass(subclass).describe(ClassAnnotation.SUBCLASS_ROLE);
                                nasty = true;
                            }
                        }
                    }
                    if (nasty) {
                        bug.setPriority(HIGH_PRIORITY);
                    } else if (!getThisClass().isPublic()) {
                        bug.setPriority(LOW_PRIORITY);
                    }
                    bugReporter.reportBug(bug);
                }

            } catch (ClassNotFoundException e) {
                bugReporter.reportMissingClass(e);
            }
        }

    } else if ("readObject".equals(getMethodName()) && "(Ljava/io/ObjectInputStream;)V".equals(getMethodSig())
            && isSerializable) {
        sawReadObject = true;
        if (!obj.isPrivate()) {
            bugReporter.reportBug(new BugInstance(this, "SE_METHOD_MUST_BE_PRIVATE", isExternalizable ? NORMAL_PRIORITY : HIGH_PRIORITY)
                    .addClassAndMethod(this));
        }

    } else if ("readObjectNoData".equals(getMethodName()) && "()V".equals(getMethodSig()) && isSerializable) {

        if (!obj.isPrivate()) {
            bugReporter.reportBug(new BugInstance(this, "SE_METHOD_MUST_BE_PRIVATE", isExternalizable ? NORMAL_PRIORITY : HIGH_PRIORITY)
                    .addClassAndMethod(this));
        }

    } else if ("writeObject".equals(getMethodName()) && "(Ljava/io/ObjectOutputStream;)V".equals(getMethodSig())
            && isSerializable) {
        sawWriteObject = true;
        if (!obj.isPrivate()) {
            bugReporter.reportBug(new BugInstance(this, "SE_METHOD_MUST_BE_PRIVATE", isExternalizable ? NORMAL_PRIORITY : HIGH_PRIORITY)
                    .addClassAndMethod(this));
        }
    }

    if (isSynchronized) {
        if ("readObject".equals(getMethodName()) && "(Ljava/io/ObjectInputStream;)V".equals(getMethodSig()) && isSerializable) {
            bugReporter.reportBug(new BugInstance(this, "RS_READOBJECT_SYNC", isExternalizable ? LOW_PRIORITY : NORMAL_PRIORITY)
                    .addClassAndMethod(this));
        } else if ("writeObject".equals(getMethodName()) && "(Ljava/io/ObjectOutputStream;)V".equals(getMethodSig())
                && isSerializable) {
            writeObjectIsSynchronized = true;
        } else {
            foundSynchronizedMethods = true;
        }
    }
    super.visit(obj);

}