Java Code Examples for org.apache.bcel.generic.InvokeInstruction#getOpcode()
The following examples show how to use
org.apache.bcel.generic.InvokeInstruction#getOpcode() .
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: StreamFrameModelingVisitor.java From spotbugs with GNU Lesser General Public License v2.1 | 5 votes |
@Override protected boolean instanceEscapes(InvokeInstruction inv, int instanceArgNum) { ConstantPoolGen cpg = getCPG(); String className = inv.getClassName(cpg); // System.out.print("[Passed as arg="+instanceArgNum+" at " + inv + // "]"); boolean escapes = (inv.getOpcode() == Const.INVOKESTATIC || instanceArgNum != 0); String methodName = inv.getMethodName(cpg); String methodSig = inv.getSignature(cpg); if (inv.getOpcode() == Const.INVOKEVIRTUAL && ("load".equals(methodName) || "loadFromXml".equals(methodName) || "store".equals(methodName) || "save".equals(methodName)) && "java.util.Properties".equals(className)) { escapes = false; } if (inv.getOpcode() == Const.INVOKEVIRTUAL && ("load".equals(methodName) || "store".equals(methodName)) && "java.security.KeyStore".equals(className)) { escapes = false; } if (inv.getOpcode() == Const.INVOKEVIRTUAL && "getChannel".equals(methodName) && "()Ljava/nio/channels/FileChannel;".equals(methodSig)) { escapes = true; } if (FindOpenStream.DEBUG && escapes) { System.out.println("ESCAPE at " + location + " at call to " + className + "." + methodName + ":" + methodSig); } // Record the fact that this might be a stream escape if (stream.getOpenLocation() != null) { resourceTracker.addStreamEscape(stream, location); } return escapes; }
Example 2
Source File: BCELFactory.java From commons-bcel with Apache License 2.0 | 5 votes |
@Override public void visitInvokeInstruction( final InvokeInstruction i ) { final short opcode = i.getOpcode(); final String class_name = i.getClassName(_cp); final String method_name = i.getMethodName(_cp); final Type type = i.getReturnType(_cp); final Type[] arg_types = i.getArgumentTypes(_cp); _out.println("il.append(_factory.createInvoke(\"" + class_name + "\", \"" + method_name + "\", " + BCELifier.printType(type) + ", " + BCELifier.printArgumentTypes(arg_types) + ", " + CONSTANT_PREFIX + Const.getOpcodeName(opcode).toUpperCase(Locale.ENGLISH) + "));"); }
Example 3
Source File: Hierarchy.java From spotbugs with GNU Lesser General Public License v2.1 | 4 votes |
public static @CheckForNull JavaClassAndMethod findInvocationLeastUpperBound(InvokeInstruction inv, ConstantPoolGen cpg, JavaClassAndMethodChooser methodChooser) throws ClassNotFoundException { 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); } if (opcode == Const.INVOKEDYNAMIC) { return null; } 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; } JavaClass jClass = Repository.lookupClass(className); return findInvocationLeastUpperBound(jClass, methodName, methodSig, methodChooser, opcode == Const.INVOKEINTERFACE); }
Example 4
Source File: Hierarchy.java From spotbugs with GNU Lesser General Public License v2.1 | 4 votes |
/** * 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 Set<JavaClassAndMethod> resolveMethodCallTargets(InvokeInstruction invokeInstruction, TypeFrame typeFrame, ConstantPoolGen cpg) throws DataflowAnalysisException, ClassNotFoundException { short opcode = invokeInstruction.getOpcode(); if (opcode == Const.INVOKESTATIC) { HashSet<JavaClassAndMethod> result = new HashSet<>(); JavaClassAndMethod targetMethod = findInvocationLeastUpperBound(invokeInstruction, cpg, CONCRETE_METHOD); if (targetMethod != null) { result.add(targetMethod); } return result; } if (!typeFrame.isValid()) { return new HashSet<>(); } 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 if (opcode == Const.INVOKEDYNAMIC) { // XXX handle INVOKEDYNAMIC return new HashSet<>(); } 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 new HashSet<>(); } 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 5
Source File: Hierarchy.java From spotbugs with GNU Lesser General Public License v2.1 | 4 votes |
/** * Resolve possible instance method call targets. * * @param receiverType * type of the receiver object * @param invokeInstruction * the InvokeInstruction * @param cpg * the ConstantPoolGen * @param receiverTypeIsExact * if true, the receiver type is known exactly, which should * allow a precise result * @return Set of methods which might be called * @throws ClassNotFoundException */ public static Set<JavaClassAndMethod> resolveMethodCallTargets(ReferenceType receiverType, InvokeInstruction invokeInstruction, ConstantPoolGen cpg, boolean receiverTypeIsExact) throws ClassNotFoundException { HashSet<JavaClassAndMethod> result = new HashSet<>(); if (invokeInstruction.getOpcode() == Const.INVOKESTATIC) { throw new IllegalArgumentException(); } String methodName = invokeInstruction.getName(cpg); String methodSig = invokeInstruction.getSignature(cpg); // Array method calls aren't virtual. // They should just resolve to Object methods. if (receiverType instanceof ArrayType) { JavaClass javaLangObject = AnalysisContext.currentAnalysisContext().lookupClass(Values.DOTTED_JAVA_LANG_OBJECT); JavaClassAndMethod classAndMethod = findMethod(javaLangObject, methodName, methodSig, INSTANCE_METHOD); if (classAndMethod != null) { result.add(classAndMethod); } return result; } if (receiverType instanceof NullType) { return Collections.<JavaClassAndMethod>emptySet(); } AnalysisContext analysisContext = AnalysisContext.currentAnalysisContext(); // Get the receiver class. String receiverClassName = ((ObjectType) receiverType).getClassName(); JavaClass receiverClass = analysisContext.lookupClass(receiverClassName); ClassDescriptor receiverDesc = DescriptorFactory.createClassDescriptorFromDottedClassName(receiverClassName); // Figure out the upper bound for the method. // This is what will be called if this is not a virtual call site. JavaClassAndMethod upperBound = findMethod(receiverClass, methodName, methodSig, CONCRETE_METHOD); if (upperBound == null) { upperBound = findInvocationLeastUpperBound(receiverClass, methodName, methodSig, CONCRETE_METHOD, false); } if (upperBound != null) { if (DEBUG_METHOD_LOOKUP) { System.out.println("Adding upper bound: " + SignatureConverter.convertMethodSignature(upperBound.getJavaClass(), upperBound.getMethod())); } result.add(upperBound); } // Is this a virtual call site? boolean virtualCall = (invokeInstruction.getOpcode() == Const.INVOKEVIRTUAL || invokeInstruction.getOpcode() == Const.INVOKEINTERFACE) && (upperBound == null || !upperBound.getJavaClass().isFinal() && !upperBound.getMethod().isFinal()) && !receiverTypeIsExact; if (virtualCall) { if (!Values.DOTTED_JAVA_LANG_OBJECT.equals(receiverClassName)) { // This is a true virtual call: assume that any concrete // subtype method may be called. Set<ClassDescriptor> subTypeSet = analysisContext.getSubtypes2().getSubtypes(receiverDesc); for (ClassDescriptor subtype : subTypeSet) { XMethod concreteSubtypeMethod = findMethod(subtype, methodName, methodSig, false); if (concreteSubtypeMethod != null && (concreteSubtypeMethod.getAccessFlags() & Const.ACC_ABSTRACT) == 0) { result.add(new JavaClassAndMethod(concreteSubtypeMethod)); } } if (false && subTypeSet.size() > 500) { new RuntimeException(receiverClassName + " has " + subTypeSet.size() + " subclasses, " + result.size() + " of which implement " + methodName + methodSig + " " + invokeInstruction) .printStackTrace(System.out); } } } return result; }
Example 6
Source File: Hierarchy2.java From spotbugs with GNU Lesser General Public License v2.1 | 4 votes |
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 7
Source File: Hierarchy2.java From spotbugs with GNU Lesser General Public License v2.1 | 4 votes |
/** * 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 8
Source File: Invoke.java From spotbugs with GNU Lesser General Public License v2.1 | 4 votes |
@Override public MatchResult match(InstructionHandle handle, ConstantPoolGen cpg, ValueNumberFrame before, ValueNumberFrame after, BindingSet bindingSet) throws DataflowAnalysisException { // See if the instruction is an InvokeInstruction Instruction ins = handle.getInstruction(); if (!(ins instanceof InvokeInstruction)) { return null; } InvokeInstruction inv = (InvokeInstruction) ins; String methodName = inv.getMethodName(cpg); boolean isStatic = inv.getOpcode() == Const.INVOKESTATIC; boolean isCtor = Const.CONSTRUCTOR_NAME.equals(methodName); int actualMode = 0; if (isStatic) { actualMode |= STATIC; } if (isCtor) { actualMode |= CONSTRUCTOR; } if (!isStatic && !isCtor) { actualMode |= INSTANCE; } // Intersection of actual and desired modes must be nonempty. if ((actualMode & mode) == 0) { return null; } // Check class name, method name, and method signature. if (!methodNameMatcher.match(methodName) || !methodSigMatcher.match(inv.getSignature(cpg)) || !classNameMatcher.match(inv.getClassName(cpg))) { return null; } // It's a match! return new MatchResult(this, bindingSet); }