org.apache.bcel.generic.INVOKESPECIAL Java Examples

The following examples show how to use org.apache.bcel.generic.INVOKESPECIAL. 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: Stream.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
public boolean isStreamOpen(BasicBlock basicBlock, InstructionHandle handle, ConstantPoolGen cpg, ResourceValueFrame frame) {
    if (isOpenOnCreation) {
        return false;
    }

    Instruction ins = handle.getInstruction();
    if (!(ins instanceof INVOKESPECIAL)) {
        return false;
    }

    // Does this instruction open the stream?
    INVOKESPECIAL inv = (INVOKESPECIAL) ins;

    return frame.isValid() && getInstanceValue(frame, inv, cpg).isInstance()
            && matchMethod(inv, cpg, this.getResourceClass(), Const.CONSTRUCTOR_NAME);
}
 
Example #2
Source File: FindRefComparison.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visitINVOKESPECIAL(INVOKESPECIAL obj) {
    if (returnsString(obj)) {
        handleInstanceMethod(obj);
    } else {
        super.visitINVOKESPECIAL(obj);
    }
}
 
Example #3
Source File: TaintFrameModelingVisitor.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void visitINVOKESPECIAL(INVOKESPECIAL obj) {
    visitInvoke(obj);
}
 
Example #4
Source File: ResourceValueFrameModelingVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visitINVOKESPECIAL(INVOKESPECIAL inv) {
    handleInvoke(inv);
}
 
Example #5
Source File: Hierarchy2.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static @CheckForNull XMethod findInvocationLeastUpperBound(InvokeInstruction inv, ConstantPoolGen cpg,
        JavaClassAndMethodChooser methodChooser) {

    if (DEBUG_METHOD_LOOKUP) {
        System.out.println("Find prototype method for " + SignatureConverter.convertMethodSignature(inv, cpg));
    }

    short opcode = inv.getOpcode();

    if (opcode == Const.INVOKESTATIC) {
        if (methodChooser == INSTANCE_METHOD) {
            return null;
        }
    } else {
        if (methodChooser == STATIC_METHOD) {
            return null;
        }
    }

    // Find the method
    if (opcode == Const.INVOKESPECIAL) {
        // Non-virtual dispatch
        return findExactMethod(inv, cpg, methodChooser);
    } else {
        String className = inv.getClassName(cpg);
        String methodName = inv.getName(cpg);
        String methodSig = inv.getSignature(cpg);
        if (DEBUG_METHOD_LOOKUP) {
            System.out.println("[Class name is " + className + "]");
            System.out.println("[Method name is " + methodName + "]");
            System.out.println("[Method signature is " + methodSig + "]");
        }

        if (className.startsWith(Values.SIG_ARRAY_PREFIX)) {
            // Java 1.5 allows array classes to appear as the class name
            className = Values.DOTTED_JAVA_LANG_OBJECT;
        }

        try {
            return thisOrNothing(
                    findInvocationLeastUpperBound(getXClassFromDottedClassName(className), methodName, methodSig,
                            opcode == Const.INVOKESTATIC, opcode == Const.INVOKEINTERFACE), methodChooser);
        } catch (CheckedAnalysisException e) {
            return null;
        }

    }
}
 
Example #6
Source File: Hierarchy2.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Resolve possible method call targets. This works for both static and
 * instance method calls.
 *
 * @param invokeInstruction
 *            the InvokeInstruction
 * @param typeFrame
 *            the TypeFrame containing the types of stack values
 * @param cpg
 *            the ConstantPoolGen
 * @return Set of methods which might be called
 * @throws DataflowAnalysisException
 * @throws ClassNotFoundException
 */
public static @Nonnull Set<XMethod> resolveMethodCallTargets(InvokeInstruction invokeInstruction, TypeFrame typeFrame, ConstantPoolGen cpg)
        throws DataflowAnalysisException, ClassNotFoundException {

    short opcode = invokeInstruction.getOpcode();

    if (opcode == Const.INVOKESTATIC) {
        return Util.emptyOrNonnullSingleton(findInvocationLeastUpperBound(invokeInstruction, cpg, STATIC_METHOD));
    }

    if (!typeFrame.isValid()) {
        return Collections.<XMethod>emptySet();
    }

    // XXX handle INVOKEDYNAMIC
    if (opcode == Const.INVOKEDYNAMIC) {
        return Collections.<XMethod>emptySet();
    }

    Type receiverType;
    boolean receiverTypeIsExact;

    if (opcode == Const.INVOKESPECIAL) {
        // invokespecial instructions are dispatched to EXACTLY
        // the class specified by the instruction
        receiverType = ObjectTypeFactory.getInstance(invokeInstruction.getClassName(cpg));
        receiverTypeIsExact = false; // Doesn't actually matter
    } else {
        // For invokevirtual and invokeinterface instructions, we have
        // virtual dispatch. By taking the receiver type (which may be a
        // subtype of the class specified by the instruction),
        // we may get a more precise set of call targets.
        int instanceStackLocation = typeFrame.getInstanceStackLocation(invokeInstruction, cpg);
        receiverType = typeFrame.getStackValue(instanceStackLocation);
        if (!(receiverType instanceof ReferenceType)) {
            return Collections.<XMethod>emptySet();
        }
        receiverTypeIsExact = typeFrame.isExact(instanceStackLocation);
    }
    if (DEBUG_METHOD_LOOKUP) {
        System.out.println("[receiver type is " + receiverType + ", " + (receiverTypeIsExact ? "exact]" : " not exact]"));
    }

    return resolveMethodCallTargets((ReferenceType) receiverType, invokeInstruction, cpg, receiverTypeIsExact);
}
 
Example #7
Source File: IsNullValueFrameModelingVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visitINVOKESPECIAL(INVOKESPECIAL obj) {
    handleInvoke(obj);
}
 
Example #8
Source File: ReferenceVisitor.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/** @see org.apache.bcel.generic.Visitor */
public void visitINVOKESPECIAL(INVOKESPECIAL aINVOKESPECIAL)
{
    addInvokeReference(
        new InvokeReference(aINVOKESPECIAL, mCurrentPoolGen));
}
 
Example #9
Source File: ReferenceVisitor.java    From contribution with GNU Lesser General Public License v2.1 4 votes vote down vote up
/** @see org.apache.bcel.generic.Visitor */
public void visitINVOKESPECIAL(INVOKESPECIAL aINVOKESPECIAL)
{
    addInvokeReference(
        new InvokeReference(aINVOKESPECIAL, mCurrentPoolGen));
}
 
Example #10
Source File: helloify.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Patch a method.
 */
private static Method helloifyMethod(Method m) {
    final Code code = m.getCode();
    final int flags = m.getAccessFlags();
    final String name = m.getName();

    // Sanity check
    if (m.isNative() || m.isAbstract() || (code == null)) {
        return m;
    }

    // Create instruction list to be inserted at method start.
    final String mesg = "Hello from " + Utility.methodSignatureToString(m.getSignature(),
            name,
            Utility.accessToString(flags));
    final InstructionList patch = new InstructionList();
    patch.append(new GETSTATIC(out));
    patch.append(new PUSH(cp, mesg));
    patch.append(new INVOKEVIRTUAL(println));

    final MethodGen mg = new MethodGen(m, class_name, cp);
    final InstructionList il = mg.getInstructionList();
    final InstructionHandle[] ihs = il.getInstructionHandles();

    if (name.equals("<init>")) { // First let the super or other constructor be called
        for (int j = 1; j < ihs.length; j++) {
            if (ihs[j].getInstruction() instanceof INVOKESPECIAL) {
                il.append(ihs[j], patch); // Should check: method name == "<init>"
                break;
            }
        }
    } else {
        il.insert(ihs[0], patch);
    }

    // Stack size must be at least 2, since the println method takes 2 argument.
    if (code.getMaxStack() < 2) {
        mg.setMaxStack(2);
    }

    m = mg.getMethod();

    il.dispose(); // Reuse instruction handles

    return m;
}
 
Example #11
Source File: Pass3aVerifier.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/** Checks if the constraints of operands of the said instruction(s) are satisfied. */
@Override
public void visitINVOKESPECIAL(final INVOKESPECIAL o) {
    try {
    // INVOKESPECIAL is a LoadClass; the Class where the referenced method is declared in,
    // is therefore resolved/verified.
    // INVOKESPECIAL is an InvokeInstruction, the argument and return types are resolved/verified,
    // too. So are the allowed method names.
    final String classname = o.getClassName(constantPoolGen);
    final JavaClass jc = Repository.lookupClass(classname);
    final Method m = getMethodRecursive(jc, o);
    if (m == null) {
        constraintViolated(o, "Referenced method '"+o.getMethodName(constantPoolGen)+"' with expected signature '"+o.getSignature(constantPoolGen)
            +"' not found in class '"+jc.getClassName()+"'.");
    }

    JavaClass current = Repository.lookupClass(myOwner.getClassName());
    if (current.isSuper()) {

        if ((Repository.instanceOf( current, jc )) && (!current.equals(jc))) {

            if (! (o.getMethodName(constantPoolGen).equals(Const.CONSTRUCTOR_NAME) )) {
                // Special lookup procedure for ACC_SUPER classes.

                int supidx = -1;

                Method meth = null;
                while (supidx != 0) {
                    supidx = current.getSuperclassNameIndex();
                    current = Repository.lookupClass(current.getSuperclassName());

                    final Method[] meths = current.getMethods();
                    for (final Method meth2 : meths) {
                        if    ( (meth2.getName().equals(o.getMethodName(constantPoolGen))) &&
                             (Type.getReturnType(meth2.getSignature()).equals(o.getReturnType(constantPoolGen))) &&
                             (objarrayequals(Type.getArgumentTypes(meth2.getSignature()), o.getArgumentTypes(constantPoolGen))) ) {
                            meth = meth2;
                            break;
                        }
                    }
                    if (meth != null) {
                        break;
                    }
                }
                if (meth == null) {
                    constraintViolated(o, "ACC_SUPER special lookup procedure not successful: method '"+
                        o.getMethodName(constantPoolGen)+"' with proper signature not declared in superclass hierarchy.");
                }
            }
        }
    }

    } catch (final ClassNotFoundException e) {
    // FIXME: maybe not the best way to handle this
    throw new AssertionViolatedException("Missing class: " + e, e);
    }

}