public static XMethod createXMethod(MethodAnnotation ma) { return createXMethod(ma.getClassName(), ma.getMethodName(), ma.getMethodSignature(), ma.isStatic()); }
static MethodAnnotation obfuscate(MethodAnnotation m) { String className = m.getClassName(); if (className.startsWith("java")) { return m; } String methodName = m.getMethodName(); String methodSignature = m.getMethodSignature(); if ("hashCode".equals(methodName) && "()I".equals(methodSignature) || "equals".equals(methodName) && "(Ljava/lang/Object;)Z".equals(methodSignature) || "compareTo".equals(methodName) && "(Ljava/lang/Object;)I".equals(methodSignature) || "<init>".equals(methodName) || "<clinit>".equals(methodName)) { // don't need to obfuscate method name } else { methodName = hashData(methodName); } MethodAnnotation result = new MethodAnnotation(hashClass(className), methodName, hashMethodSignature(methodSignature), m.isStatic()); result.setDescription(m.getDescription()); return result; }
public int compareMethods(BugCollection lhsCollection, BugCollection rhsCollection, MethodAnnotation lhsMethod, MethodAnnotation rhsMethod) { if (lhsMethod == null || rhsMethod == null) { return compareNullElements(lhsMethod, rhsMethod); } // Compare for exact match int cmp = lhsMethod.compareTo(rhsMethod); return cmp; }
/** * Create a MethodAnnotation from a method that is not directly accessible. * We will use the repository to try to find its class in order to populate * the information as fully as possible. * * @param className * class containing called method * @param methodName * name of called method * @param methodSig * signature of called method * @param isStatic * true if called method is static * @return the MethodAnnotation for the called method */ public static MethodAnnotation fromCalledMethod(String className, String methodName, String methodSig, boolean isStatic) { MethodAnnotation methodAnnotation = fromForeignMethod(className, methodName, methodSig, isStatic); methodAnnotation.setDescription("METHOD_CALLED"); return methodAnnotation; }
@Override public void writeXML(XMLOutput xmlOutput, boolean addMessages, boolean isPrimary) throws IOException { XMLAttributeList attributeList = new XMLAttributeList().addAttribute("classname", getClassName()) .addAttribute("name", getMethodName()).addAttribute("signature", getMethodSignature()) .addAttribute("isStatic", String.valueOf(isStatic())); if (isPrimary) { attributeList.addAttribute("primary", "true"); } String role = getDescription(); if (!DEFAULT_ROLE.equals(role)) { attributeList.addAttribute("role", role); } if (sourceLines == null && !addMessages) { xmlOutput.openCloseTag(ELEMENT_NAME, attributeList); } else { xmlOutput.openTag(ELEMENT_NAME, attributeList); if (sourceLines != null) { sourceLines.writeXML(xmlOutput); } if (addMessages) { xmlOutput.openTag(MESSAGE_TAG); xmlOutput.writeText(this.toString()); xmlOutput.closeTag(MESSAGE_TAG); } xmlOutput.closeTag(ELEMENT_NAME); } }
String name = ((ConstantUtf8)cp.getConstant(nameAndType.getNameIndex())).getBytes(); String signature = ((ConstantUtf8)cp.getConstant(nameAndType.getSignatureIndex())).getBytes(); MethodAnnotation called = new MethodAnnotation(ClassName.toDottedClassName(className), name, signature, kind==6 /* invokestatic */); calledMethods.add(called); calledMethodNames.add(name.toLowerCase()); String methodName = m.getMethodName(); if (methodName.equals(simpleClassName) && "()V".equals(m.getMethodSignature())) { continue;
if (!omitMethodName) { if (useJVMMethodName) { result.append(getMethodName()); } else { result.append(getJavaSourceMethodName()); result.append(removePackageName(converter.parseNext())); } else { result.append(converter.parseNext());
if(methodAnn.getMethodName().startsWith("apply") && fullClassName != null) { Matcher m = ANON_FUNCTION_SCALA_PATTERN.matcher(fullClassName); if(m.find()) { //Scala function enclose in criteriaMatches &= methodAnn.getMethodName().equals(methodName) || methodName.equals(m.group(1)); criteriaMatches &= methodAnn.getMethodName().equals(methodName);
if (downCast && operandTypeIsExact) { if ("[Ljava/lang/Object;".equals(refSig) && source instanceof MethodAnnotation && "toArray".equals(((MethodAnnotation) source).getMethodName()) && "()[Ljava/lang/Object;".equals(((MethodAnnotation) source).getMethodSignature())) { bugPattern = "BC_IMPOSSIBLE_DOWNCAST_OF_TOARRAY"; } else { XMethod xm = m.toXMethod(); if (xm != null && (xm.isPrivate() || xm.isStatic()) && priority == Priorities.LOW_PRIORITY) { continue; && "toArray".equals(((MethodAnnotation) source).getMethodName()) && "()[Ljava/lang/Object;".equals(((MethodAnnotation) source).getMethodSignature())) { bugReporter.reportBug(new BugInstance(this, "BC_IMPOSSIBLE_DOWNCAST_OF_TOARRAY", HIGH_PRIORITY) .addClassAndMethod(methodGen, sourceFile)
/** * Factory method to create the MethodAnnotation from the classname, method * name, signature, etc. The method tries to look up source line information * for the method. * * @param className * name of the class containing the method * @param methodName * name of the method * @param methodSig * signature of the method * @param accessFlags * the access flags of the method * @return the MethodAnnotation */ public static MethodAnnotation fromForeignMethod(@SlashedClassName String className, String methodName, String methodSig, int accessFlags) { className = ClassName.toDottedClassName(className); // Create MethodAnnotation. // It won't have source lines yet. MethodAnnotation methodAnnotation = new MethodAnnotation(className, methodName, methodSig, (accessFlags & Const.ACC_STATIC) != 0); SourceLineAnnotation sourceLines = SourceLineAnnotation.getSourceAnnotationForMethod(className, methodName, methodSig); methodAnnotation.setSourceLines(sourceLines); return methodAnnotation; }
@Override public boolean match(BugInstance bugInstance) { MethodAnnotation methodAnnotation = null; if (role == null || "".equals(role)) { methodAnnotation = bugInstance.getPrimaryMethod(); } else { for (BugAnnotation a : bugInstance.getAnnotations()) { if (a instanceof MethodAnnotation && role.equals(a.getDescription())) { methodAnnotation = (MethodAnnotation) a; break; } } } if (methodAnnotation == null) { return false; } if (!name.match(methodAnnotation.getMethodName())) { return false; } if (signature != null && !signature.match(methodAnnotation.getMethodSignature())) { return false; } return true; }
return false; if (!callsPattern.matcher(m.getClassName()).find() && !callsPattern.matcher(m.getMethodName()).find()) { return false;
@Override public void visit(Method obj) { int flags = obj.getAccessFlags(); if ((flags & doNotConsider) != 0) { return; } String name = obj.getName(); boolean isSynchronized = (flags & Const.ACC_SYNCHRONIZED) != 0; /* * String sig = obj.getSignature(); char firstArg = sig.charAt(1); char * returnValue = sig.charAt(1 + sig.indexOf(')')); boolean firstArgIsRef * = (firstArg == 'L') || (firstArg == '['); boolean returnValueIsRef = * (returnValue == 'L') || (returnValue == '['); * * System.out.println(className + "." + name + " " + firstArgIsRef + " " * + returnValueIsRef + " " + isSynchronized + " " + isNative ); */ if (name.startsWith("get") && !isSynchronized // && returnValueIsRef ) { getMethods.put(name.substring(3), MethodAnnotation.fromVisitedMethod(this)); } else if (name.startsWith("set") && isSynchronized // && firstArgIsRef ) { setMethods.put(name.substring(3), MethodAnnotation.fromVisitedMethod(this)); } } }
public static @CheckForNull BugAnnotation getFieldOrMethodValueSource(@CheckForNull OpcodeStack.Item item) { if (item == null) { return null; } XField xField = item.getXField(); if (xField != null) { FieldAnnotation a = FieldAnnotation.fromXField(xField); a.setDescription(FieldAnnotation.LOADED_FROM_ROLE); return a; } XMethod xMethod = item.getReturnValueOf(); if (xMethod != null) { MethodAnnotation a = MethodAnnotation.fromXMethod(xMethod); a.setDescription(MethodAnnotation.METHOD_RETURN_VALUE_OF); return a; } return null; }
/** * Add a method annotation. If this is the first method annotation added, it * becomes the primary method annotation. * * @param className * name of the class containing the method * @param methodName * name of the method * @param methodSig * type signature of the method * @param isStatic * true if the method is static, false otherwise * @return this object */ @Nonnull public BugInstance addMethod(String className, String methodName, String methodSig, boolean isStatic) { addMethod(MethodAnnotation.fromForeignMethod(className, methodName, methodSig, isStatic)); return this; }
public MethodAnnotation getMethodAnnotation() { return MethodAnnotation.fromXMethod(method); }
/** * Add a method annotation for the method which has been called by the * method currently being visited by given visitor. Assumes that the visitor * has just looked at an invoke instruction of some kind. * * @param visitor * the DismantleBytecode object * @return this object */ @Nonnull public BugInstance addCalledMethod(DismantleBytecode visitor) { return addMethod(MethodAnnotation.fromCalledMethod(visitor)).describe(MethodAnnotation.METHOD_CALLED); }
/** * Add a MethodAnnotation from an MethodDescriptor. * * @param method * the method * @return this object */ @Nonnull public BugInstance addMethod(MethodDescriptor method) { addMethod(MethodAnnotation.fromMethodDescriptor(method)); return this; }
@Override public void sawOpcode(int seen) { switch (seen) { case Const.INVOKEVIRTUAL: case Const.INVOKESPECIAL: case Const.INVOKESTATIC: if (getDottedClassConstantOperand().equals(className)) { String className = getDottedClassConstantOperand(); MethodAnnotation called = new MethodAnnotation(className, getNameConstantOperand(), getSigConstantOperand(), seen == Const.INVOKESTATIC); calledMethods.add(called); calledMethodNames.add(getNameConstantOperand().toLowerCase()); } break; default: break; } }
@Override public boolean match(BugInstance bugInstance) { if (!super.match(bugInstance)) { return false; } MethodAnnotation bugMethod = bugInstance.getPrimaryMethod(); if (bugMethod != null && !method.equals(bugMethod)) { return false; } if (DEBUG) { System.out.println("Suppressing " + bugInstance); } return true; } }