Java Code Examples for org.apache.bcel.generic.Type#getReturnType()

The following examples show how to use org.apache.bcel.generic.Type#getReturnType() . 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: FindUselessObjects.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
boolean propagateToReturnValue(Set<ValueInfo> vals, ValueNumber vn, GenLocation location, MethodDescriptor m)
        throws DataflowAnalysisException {
    for (ValueInfo vi : vals) {
        if (vi.type.getSignature().startsWith("[") && vi.hasObjectOnlyCall && vi.var == null && vn.getNumber() == vi.origValue) {
            // Ignore initialized arrays passed to methods
            vi.escaped = true;
            count--;
        }
    }
    if (Type.getReturnType(m.getSignature()) == Type.VOID || location instanceof ExceptionLocation) {
        return false;
    }
    InstructionHandle nextHandle = location.getHandle().getNext();
    if (nextHandle == null || (nextHandle.getInstruction() instanceof POP || nextHandle.getInstruction() instanceof POP2)) {
        return false;
    }
    return propagateValues(vals, null, location.frameAfter().getTopValue());
}
 
Example 2
Source File: TransitiveHull.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
private void visitRef(final ConstantCP ccp, final boolean method) {
    final String class_name = ccp.getClass(cp);
    add(class_name);

    final ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(ccp.getNameAndTypeIndex(),
            Constants.CONSTANT_NameAndType);

    final String signature = cnat.getSignature(cp);

    if (method) {
        final Type type = Type.getReturnType(signature);

        checkType(type);

        for (final Type type1 : Type.getArgumentTypes(signature)) {
            checkType(type1);
        }
    } else {
        checkType(Type.getType(signature));
    }
}
 
Example 3
Source File: FindNoSideEffectMethods.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param methodDescriptor
 */
private void sawNoSideEffectCall(MethodDescriptor methodDescriptor) {
    if (uselessVoidCandidate && Type.getReturnType(methodDescriptor.getSignature()) == Type.VOID
            && !methodDescriptor.getName().equals(Const.CONSTRUCTOR_NAME)) {
        /* To reduce false-positives we do not mark method as useless void if it calls
         * another useless void method. If that another method also in the scope of our project
         * then we will report it instead. If there's a cycle of no-side-effect calls, then
         * it's probably some delegation pattern and methods can be extended in future/derived
         * projects to do something useful.
         */
        uselessVoidCandidate = false;
    }
}
 
Example 4
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstantMethodref(final ConstantMethodref obj) {
    if (obj.getTag() != Const.CONSTANT_Methodref) {
        throw new ClassConstraintException("ConstantMethodref '"+tostring(obj)+"' has wrong tag!");
    }
    final int name_and_type_index = obj.getNameAndTypeIndex();
    final ConstantNameAndType cnat = (ConstantNameAndType) (cp.getConstant(name_and_type_index));
    final String name = ((ConstantUtf8) (cp.getConstant(cnat.getNameIndex()))).getBytes(); // Field or Method name
    if (!validClassMethodName(name)) {
        throw new ClassConstraintException(
            "Invalid (non-interface) method name '"+name+"' referenced by '"+tostring(obj)+"'.");
    }

    final int class_index = obj.getClassIndex();
    final ConstantClass cc = (ConstantClass) (cp.getConstant(class_index));
    final String className = ((ConstantUtf8) (cp.getConstant(cc.getNameIndex()))).getBytes(); // Class Name in internal form
    if (! validClassName(className)) {
        throw new ClassConstraintException("Illegal class name '"+className+"' used by '"+tostring(obj)+"'.");
    }

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

    try{
        final Type   t  = Type.getReturnType(sig);
        if ( name.equals(Const.CONSTRUCTOR_NAME) && (t != Type.VOID) ) {
            throw new ClassConstraintException("Instance initialization method must have VOID return type.");
        }
    }
    catch (final ClassFormatException cfe) {
        throw new ClassConstraintException("Illegal descriptor (==signature) '"+sig+"' used by '"+tostring(obj)+"'.", cfe);
    }
}
 
Example 5
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstantInterfaceMethodref(final ConstantInterfaceMethodref obj) {
    if (obj.getTag() != Const.CONSTANT_InterfaceMethodref) {
        throw new ClassConstraintException("ConstantInterfaceMethodref '"+tostring(obj)+"' has wrong tag!");
    }
    final int name_and_type_index = obj.getNameAndTypeIndex();
    final ConstantNameAndType cnat = (ConstantNameAndType) (cp.getConstant(name_and_type_index));
    final String name = ((ConstantUtf8) (cp.getConstant(cnat.getNameIndex()))).getBytes(); // Field or Method name
    if (!validInterfaceMethodName(name)) {
        throw new ClassConstraintException("Invalid (interface) method name '"+name+"' referenced by '"+tostring(obj)+"'.");
    }

    final int class_index = obj.getClassIndex();
    final ConstantClass cc = (ConstantClass) (cp.getConstant(class_index));
    final String className = ((ConstantUtf8) (cp.getConstant(cc.getNameIndex()))).getBytes(); // Class Name in internal form
    if (! validClassName(className)) {
        throw new ClassConstraintException("Illegal class name '"+className+"' used by '"+tostring(obj)+"'.");
    }

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

    try{
        final Type   t  = Type.getReturnType(sig);
        if ( name.equals(Const.STATIC_INITIALIZER_NAME) && (t != Type.VOID) ) {
            addMessage("Class or interface initialization method '"+Const.STATIC_INITIALIZER_NAME+
                "' usually has VOID return type instead of '"+t+
                "'. Note this is really not a requirement of The Java Virtual Machine Specification, Second Edition.");
        }
    }
    catch (final ClassFormatException cfe) {
        throw new ClassConstraintException("Illegal descriptor (==signature) '"+sig+"' used by '"+tostring(obj)+"'.", cfe);
    }

}
 
Example 6
Source File: Method.java    From ApkToolPlus with Apache License 2.0 4 votes vote down vote up
/**
 * @return return type of method
 */
public Type getReturnType() {
  return Type.getReturnType(getSignature());
}
 
Example 7
Source File: Field.java    From ApkToolPlus with Apache License 2.0 4 votes vote down vote up
/**
 * @return type of field
 */
public Type getType() {
  return Type.getReturnType(getSignature());
}
 
Example 8
Source File: BuildObligationPolicyDatabase.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Handle a method with a WillCloseWhenClosed parameter annotation.
 */
private void handleWillCloseWhenClosed(XMethod xmethod, Obligation deletedObligation) {
    if (deletedObligation == null) {
        if (DEBUG_ANNOTATIONS) {
            System.out.println("Method " + xmethod.toString() + " is marked @WillCloseWhenClosed, "
                    + "but its parameter is not an obligation");
        }
        return;
    }

    // See what type of obligation is being created.
    Obligation createdObligation = null;
    if ("<init>".equals(xmethod.getName())) {
        // Constructor - obligation type is the type of object being created
        // (or some supertype)
        createdObligation = database.getFactory().getObligationByType(xmethod.getClassDescriptor());
    } else {
        // Factory method - obligation type is the return type
        Type returnType = Type.getReturnType(xmethod.getSignature());
        if (returnType instanceof ObjectType) {
            try {
                createdObligation = database.getFactory().getObligationByType((ObjectType) returnType);
            } catch (ClassNotFoundException e) {
                reporter.reportMissingClass(e);
                return;
            }
        }

    }
    if (createdObligation == null) {
        if (DEBUG_ANNOTATIONS) {
            System.out.println("Method " + xmethod.toString() + " is marked @WillCloseWhenClosed, "
                    + "but its return type is not an obligation");
        }
        return;
    }

    // Add database entries:
    // - parameter obligation is deleted
    // - return value obligation is added
    database.addEntry(new MatchMethodEntry(xmethod, ObligationPolicyDatabaseActionType.DEL,
            ObligationPolicyDatabaseEntryType.STRONG, deletedObligation));
    database.addEntry(new MatchMethodEntry(xmethod, ObligationPolicyDatabaseActionType.ADD,
            ObligationPolicyDatabaseEntryType.STRONG, createdObligation));
}
 
Example 9
Source File: FindNoSideEffectMethods.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visit(Code obj) {
    uselessVoidCandidate = !classInit && !constructor && !getXMethod().isSynthetic() && Type.getReturnType(getMethodSig()) == Type.VOID;
    byte[] code = obj.getCode();
    if (code.length == 4 && (code[0] & 0xFF) == Const.GETSTATIC && (code[3] & 0xFF) == Const.ARETURN) {
        getStaticMethods.add(getMethodDescriptor());
        handleStatus();
        return;
    }

    if (code.length <= 2 && !getXMethod().isStatic() && (getXMethod().isPublic() || getXMethod().isProtected())
            && !getXMethod().isFinal() && (getXClass().isPublic() || getXClass().isProtected())) {
        for (byte[] stubMethod : STUB_METHODS) {
            if (Arrays.equals(stubMethod, code)
                    && (getClassName().endsWith("Visitor") || getClassName().endsWith("Listener") || !hasOtherImplementations(getXMethod()))) {
                // stub method which can be extended: assume it can be extended with possible side-effect
                status = SideEffectStatus.SIDE_EFFECT;
                handleStatus();
                return;
            }
        }
    }
    if (statusMap.containsKey(getMethodDescriptor())) {
        return;
    }
    finallyTargets = new HashSet<>();
    for (CodeException ex : getCode().getExceptionTable()) {
        if (ex.getCatchType() == 0) {
            finallyTargets.add(ex.getHandlerPC());
        }
    }
    finallyExceptionRegisters = new HashSet<>();
    try {
        super.visit(obj);
    } catch (EarlyExitException e) {
        // Ignore
    }
    if (uselessVoidCandidate && code.length > 1
            && (status == SideEffectStatus.UNSURE || status == SideEffectStatus.NO_SIDE_EFFECT)) {
        uselessVoidCandidates.add(getMethodDescriptor());
    }
    handleStatus();
}
 
Example 10
Source File: Method.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * @return return type of method
 */
public Type getReturnType() {
    return Type.getReturnType(getSignature());
}
 
Example 11
Source File: Field.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * @return type of field
 */
public Type getType() {
    return Type.getReturnType(getSignature());
}