@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; } }
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; }
/** * 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; }
/** * 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 isStatic * true if the method is static, false otherwise * @return the MethodAnnotation */ public static MethodAnnotation fromForeignMethod(String className, String methodName, String methodSig, boolean isStatic) { // FIXME: would be nice to do this without using BCEL className = ClassName.toDottedClassName(className); // Create MethodAnnotation. // It won't have source lines yet. MethodAnnotation methodAnnotation = new MethodAnnotation(className, methodName, methodSig, isStatic); if (AnalysisContext.currentAnalysisContext() != null) { SourceLineAnnotation sourceLines = SourceLineAnnotation .getSourceAnnotationForMethod(className, methodName, methodSig); methodAnnotation.setSourceLines(sourceLines); } return methodAnnotation; }
/** * Add a method annotation. If this is the first method annotation added, it * becomes the primary method annotation. If the method has source line * information, then a SourceLineAnnotation is added to the method. * * @param methodGen * the MethodGen object for the method * @param sourceFile * source file method is defined in * @return this object */ @Nonnull public BugInstance addMethod(MethodGen methodGen, String sourceFile) { String className = methodGen.getClassName(); MethodAnnotation methodAnnotation = new MethodAnnotation(className, methodGen.getName(), methodGen.getSignature(), methodGen.isStatic()); addMethod(methodAnnotation); addSourceLinesForMethod(methodAnnotation, SourceLineAnnotation.fromVisitedMethod(methodGen, sourceFile)); return this; }
/** * Add a method annotation. If this is the first method annotation added, it * becomes the primary method annotation. If the method has source line * information, then a SourceLineAnnotation is added to the method. * * @param javaClass * the class the method is defined in * @param method * the method * @return this object */ @Nonnull public BugInstance addMethod(JavaClass javaClass, Method method) { MethodAnnotation methodAnnotation = new MethodAnnotation(javaClass.getClassName(), method.getName(), method.getSignature(), method.isStatic()); SourceLineAnnotation methodSourceLines = SourceLineAnnotation.forEntireMethod(javaClass, method); methodAnnotation.setSourceLines(methodSourceLines); addMethod(methodAnnotation); return this; }
bugAnnotation = bugAnnotationWithSourceLines = new MethodAnnotation(classname, fieldOrMethodName, signature, Boolean.valueOf(isStatic));
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());
/** * Factory method to create a MethodAnnotation from the method the given * visitor is currently visiting. * * @param visitor * the BetterVisitor currently visiting the method */ public static MethodAnnotation fromVisitedMethod(PreorderVisitor visitor) { String className = visitor.getDottedClassName(); MethodAnnotation result = new MethodAnnotation(className, visitor.getMethodName(), visitor.getMethodSig(), visitor .getMethod().isStatic()); // Try to find the source lines for the method SourceLineAnnotation srcLines = SourceLineAnnotation.fromVisitedMethod(visitor); result.setSourceLines(srcLines); return result; }
/** * Rewrite a MethodAnnotation to update the class name, and any class names * mentioned in the method signature. * * @param classNameRewriter * a ClassNameRewriter * @param annotation * a MethodAnnotation * @return the possibly-rewritten MethodAnnotation */ public static MethodAnnotation convertMethodAnnotation(ClassNameRewriter classNameRewriter, MethodAnnotation annotation) { if (classNameRewriter != IdentityClassNameRewriter.instance()) { annotation = new MethodAnnotation(classNameRewriter.rewriteClassName(annotation.getClassName()), annotation.getMethodName(), rewriteMethodSignature(classNameRewriter, annotation.getMethodSignature()), annotation.isStatic()); } return annotation; }
&& "(I)Ljava/lang/Integer;".equals(previousMethodCall.getSignature()) ) { MethodAnnotation preferred = new MethodAnnotation("java.lang.Integer", "toString", "(I)Ljava/lang/String;", true); BugInstance bug = new BugInstance(this, "DM_BOXED_PRIMITIVE_TOSTRING", HIGH_PRIORITY).addClassAndMethod(this) .addCalledMethod(this).addMethod(preferred).describe(MethodAnnotation.SHOULD_CALL); )) { MethodAnnotation preferred = new MethodAnnotation("java.lang.Integer", "parseInt", "(Ljava/lang/String;)I", true); && "(Ljava/lang/String;)Ljava/lang/Long;".equals(previousMethodCall.getSignature())) ) { MethodAnnotation preferred = new MethodAnnotation("java.lang.Long", "parseLong", "(Ljava/lang/String;)J", true); && "()Ljava/lang/String;".equals(getSigConstantOperand())) { BugInstance bug = new BugInstance(this, "DM_BOXED_PRIMITIVE_TOSTRING", NORMAL_PRIORITY).addClassAndMethod(this).addCalledMethod(this); MethodAnnotation preferred = new MethodAnnotation(ClassName.toDottedClassName(primitiveObjCtorSeen), "toString", "("+ClassName.getPrimitiveType(primitiveObjCtorSeen)+")Ljava/lang/String;", true); bug.addMethod(preferred).describe(MethodAnnotation.SHOULD_CALL);
@Override public void sawOpcode(int seen) { switch (seen) { case INVOKEVIRTUAL: case INVOKESPECIAL: case INVOKESTATIC: if (getDottedClassConstantOperand().equals(className)) { String className = getDottedClassConstantOperand(); MethodAnnotation called = new MethodAnnotation(className, getNameConstantOperand(), getSigConstantOperand(), seen == INVOKESTATIC); calledMethods.add(called); calledMethodNames.add(getNameConstantOperand().toLowerCase()); } break; default: break; } }
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; }
/** * 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 & Constants.ACC_STATIC) != 0); SourceLineAnnotation sourceLines = SourceLineAnnotation.getSourceAnnotationForMethod(className, methodName, methodSig); methodAnnotation.setSourceLines(sourceLines); return methodAnnotation; }
/** * 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 isStatic * true if the method is static, false otherwise * @return the MethodAnnotation */ public static MethodAnnotation fromForeignMethod(String className, String methodName, String methodSig, boolean isStatic) { // FIXME: would be nice to do this without using BCEL className = ClassName.toDottedClassName(className); // Create MethodAnnotation. // It won't have source lines yet. MethodAnnotation methodAnnotation = new MethodAnnotation(className, methodName, methodSig, isStatic); if (AnalysisContext.currentAnalysisContext() != null) { SourceLineAnnotation sourceLines = SourceLineAnnotation .getSourceAnnotationForMethod(className, methodName, methodSig); methodAnnotation.setSourceLines(sourceLines); } return methodAnnotation; }
/** * Add a method annotation. If this is the first method annotation added, it * becomes the primary method annotation. If the method has source line * information, then a SourceLineAnnotation is added to the method. * * @param methodGen * the MethodGen object for the method * @param sourceFile * source file method is defined in * @return this object */ @Nonnull public BugInstance addMethod(MethodGen methodGen, String sourceFile) { String className = methodGen.getClassName(); MethodAnnotation methodAnnotation = new MethodAnnotation(className, methodGen.getName(), methodGen.getSignature(), methodGen.isStatic()); addMethod(methodAnnotation); addSourceLinesForMethod(methodAnnotation, SourceLineAnnotation.fromVisitedMethod(methodGen, sourceFile)); return this; }
/** * Add a method annotation. If this is the first method annotation added, it * becomes the primary method annotation. If the method has source line * information, then a SourceLineAnnotation is added to the method. * * @param javaClass * the class the method is defined in * @param method * the method * @return this object */ @Nonnull public BugInstance addMethod(JavaClass javaClass, Method method) { MethodAnnotation methodAnnotation = new MethodAnnotation(javaClass.getClassName(), method.getName(), method.getSignature(), method.isStatic()); SourceLineAnnotation methodSourceLines = SourceLineAnnotation.forEntireMethod(javaClass, method); methodAnnotation.setSourceLines(methodSourceLines); addMethod(methodAnnotation); return this; }
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());
/** * Factory method to create a MethodAnnotation from the method the given * visitor is currently visiting. * * @param visitor * the BetterVisitor currently visiting the method */ public static MethodAnnotation fromVisitedMethod(PreorderVisitor visitor) { String className = visitor.getDottedClassName(); MethodAnnotation result = new MethodAnnotation(className, visitor.getMethodName(), visitor.getMethodSig(), visitor .getMethod().isStatic()); // Try to find the source lines for the method SourceLineAnnotation srcLines = SourceLineAnnotation.fromVisitedMethod(visitor); result.setSourceLines(srcLines); return result; }
/** * Rewrite a MethodAnnotation to update the class name, and any class names * mentioned in the method signature. * * @param classNameRewriter * a ClassNameRewriter * @param annotation * a MethodAnnotation * @return the possibly-rewritten MethodAnnotation */ public static MethodAnnotation convertMethodAnnotation(ClassNameRewriter classNameRewriter, MethodAnnotation annotation) { if (classNameRewriter != IdentityClassNameRewriter.instance()) { annotation = new MethodAnnotation(classNameRewriter.rewriteClassName(annotation.getClassName()), annotation.getMethodName(), rewriteMethodSignature(classNameRewriter, annotation.getMethodSignature()), annotation.isStatic()); } return annotation; }