org.apache.bcel.classfile.Deprecated Java Examples

The following examples show how to use org.apache.bcel.classfile.Deprecated. 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
private boolean markedAsNotUsable(Method obj) {
    for (Attribute a : obj.getAttributes()) {
        if (a instanceof Deprecated) {
            return true;
        }
    }
    Code code = obj.getCode();
    if (code == null) {
        return false;
    }
    byte[] codeBytes = code.getCode();
    if (codeBytes.length > 1 && codeBytes.length < 10) {
        int lastOpcode = codeBytes[codeBytes.length - 1] & 0xff;
        if (lastOpcode != Const.ATHROW) {
            return false;
        }
        for (int b : codeBytes) {
            if ((b & 0xff) == Const.RETURN) {
                return false;
            }
        }
        return true;
    }
    return false;
}
 
Example #2
Source File: Pass2Verifier.java    From ApkToolPlus with Apache License 2.0 5 votes vote down vote up
public void visitDeprecated(Deprecated obj){//vmspec2 4.7.10
	checkIndex(obj, obj.getNameIndex(), CONST_Utf8);

	String name = ((ConstantUtf8) cp.getConstant(obj.getNameIndex())).getBytes();
	if (! name.equals("Deprecated")){
		throw new ClassConstraintException("The Deprecated attribute '"+tostring(obj)+"' is not correctly named 'Deprecated' but '"+name+"'.");
	}
}
 
Example #3
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitDeprecated(final Deprecated obj) {//vmspec2 4.7.10
    checkIndex(obj, obj.getNameIndex(), CONST_Utf8);

    final String name = ((ConstantUtf8) cp.getConstant(obj.getNameIndex())).getBytes();
    if (! name.equals("Deprecated")) {
        throw new ClassConstraintException("The Deprecated attribute '"+tostring(obj)+
            "' is not correctly named 'Deprecated' but '"+name+"'.");
    }
}
 
Example #4
Source File: Pass2Verifier.java    From ApkToolPlus with Apache License 2.0 4 votes vote down vote up
public void visitJavaClass(JavaClass obj){
	Attribute[] atts = obj.getAttributes();
	boolean foundSourceFile = false;
	boolean foundInnerClasses = false;

	// Is there an InnerClass referenced?
	// This is a costly check; existing verifiers don't do it!
	boolean hasInnerClass = new InnerClassDetector(jc).innerClassReferenced();

	for (int i=0; i<atts.length; i++){
		if ((! (atts[i] instanceof SourceFile)) &&
		    (! (atts[i] instanceof Deprecated))     &&
		    (! (atts[i] instanceof InnerClasses)) &&
		    (! (atts[i] instanceof Synthetic))){
			addMessage("Attribute '"+tostring(atts[i])+"' as an attribute of the ClassFile structure '"+tostring(obj)+"' is unknown and will therefore be ignored.");
		}

		if (atts[i] instanceof SourceFile){
			if (foundSourceFile == false) foundSourceFile = true;
			else throw new ClassConstraintException("A ClassFile structure (like '"+tostring(obj)+"') may have no more than one SourceFile attribute."); //vmspec2 4.7.7
		}

		if (atts[i] instanceof InnerClasses){
			if (foundInnerClasses == false) foundInnerClasses = true;
			else{
				if (hasInnerClass){
					throw new ClassConstraintException("A Classfile structure (like '"+tostring(obj)+"') must have exactly one InnerClasses attribute if at least one Inner Class is referenced (which is the case). More than one InnerClasses attribute was found.");
				}
			}
			if (!hasInnerClass){
				addMessage("No referenced Inner Class found, but InnerClasses attribute '"+tostring(atts[i])+"' found. Strongly suggest removal of that attribute.");
			}
		}

	}
	if (hasInnerClass && !foundInnerClasses){
		//throw new ClassConstraintException("A Classfile structure (like '"+tostring(obj)+"') must have exactly one InnerClasses attribute if at least one Inner Class is referenced (which is the case). No InnerClasses attribute was found.");
		//vmspec2, page 125 says it would be a constraint: but existing verifiers
		//don't check it and javac doesn't satisfy it when it comes to anonymous
		//inner classes
		addMessage("A Classfile structure (like '"+tostring(obj)+"') must have exactly one InnerClasses attribute if at least one Inner Class is referenced (which is the case). No InnerClasses attribute was found.");
	}
}
 
Example #5
Source File: Pass2Verifier.java    From ApkToolPlus with Apache License 2.0 4 votes vote down vote up
public void visitField(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() & ~(ACC_PUBLIC|ACC_PRIVATE|ACC_PROTECTED|ACC_STATIC|ACC_FINAL|ACC_VOLATILE|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);

			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);

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

			try{
				Type.getType(sig);  /* Don't need the return value */
			}
			catch (ClassFormatError cfe){ // sometimes BCEL is a little harsh describing exceptional situations.
				throw new ClassConstraintException("Illegal descriptor (==signature) '"+sig+"' used by '"+tostring(obj)+"'.");
			}

			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);

			Attribute[] atts = obj.getAttributes();
			for (int i=0; i<atts.length; i++){
				if ((! (atts[i] instanceof ConstantValue)) &&
				    (! (atts[i] instanceof Synthetic))     &&
				    (! (atts[i] instanceof Deprecated))){
					addMessage("Attribute '"+tostring(atts[i])+"' as an attribute of Field '"+tostring(obj)+"' is unknown and will therefore be ignored.");
				}
				if  (! (atts[i] instanceof ConstantValue)){
					addMessage("Attribute '"+tostring(atts[i])+"' as an attribute of Field '"+tostring(obj)+"' is not a ConstantValue and is therefore only of use for debuggers and such.");
				}
			}
		}
 
Example #6
Source File: StringRepresentation.java    From ApkToolPlus with Apache License 2.0 4 votes vote down vote up
public void visitDeprecated(Deprecated obj){
	tostring = toString(obj);
}
 
Example #7
Source File: JasminVisitor.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitDeprecated(final Deprecated attribute) {
    printEndMethod(attribute);
}
 
Example #8
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitJavaClass(final JavaClass obj) {
    final Attribute[] atts = obj.getAttributes();
    boolean foundSourceFile = false;
    boolean foundInnerClasses = false;

    // Is there an InnerClass referenced?
    // This is a costly check; existing verifiers don't do it!
    final boolean hasInnerClass = new InnerClassDetector(jc).innerClassReferenced();

    for (final Attribute att : atts) {
        if ((!(att instanceof SourceFile)) &&
                (!(att instanceof Deprecated)) &&
                (!(att instanceof InnerClasses)) &&
                (!(att instanceof Synthetic))) {
            addMessage("Attribute '" + tostring(att) + "' as an attribute of the ClassFile structure '" +
                tostring(obj) + "' is unknown and will therefore be ignored.");
        }

        if (att instanceof SourceFile) {
            if (!foundSourceFile) {
                foundSourceFile = true;
            } else {
                throw new ClassConstraintException("A ClassFile structure (like '" +
                    tostring(obj) + "') may have no more than one SourceFile attribute."); //vmspec2 4.7.7
            }
        }

        if (att instanceof InnerClasses) {
            if (!foundInnerClasses) {
                foundInnerClasses = true;
            } else {
                if (hasInnerClass) {
                    throw new ClassConstraintException("A Classfile structure (like '" + tostring(obj) +
                        "') must have exactly one InnerClasses attribute"+
                        " if at least one Inner Class is referenced (which is the case)."+
                        " More than one InnerClasses attribute was found.");
                }
            }
            if (!hasInnerClass) {
                addMessage("No referenced Inner Class found, but InnerClasses attribute '" + tostring(att) +
                    "' found. Strongly suggest removal of that attribute.");
            }
        }

    }
    if (hasInnerClass && !foundInnerClasses) {
        //throw new ClassConstraintException("A Classfile structure (like '"+tostring(obj)+
        // "') must have exactly one InnerClasses attribute if at least one Inner Class is referenced (which is the case)."+
        // " No InnerClasses attribute was found.");
        //vmspec2, page 125 says it would be a constraint: but existing verifiers
        //don't check it and javac doesn't satisfy it when it comes to anonymous
        //inner classes
        addMessage("A Classfile structure (like '"+tostring(obj)+
            "') must have exactly one InnerClasses attribute if at least one Inner Class is referenced (which is the case)."+
                " No InnerClasses attribute was found.");
    }
}
 
Example #9
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 #10
Source File: StringRepresentation.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitDeprecated(final Deprecated obj) {
    tostring = toString(obj);
}
 
Example #11
Source File: CounterVisitor.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitDeprecated(final Deprecated obj)
{
    deprecatedCount++;
}