@Override public MethodVisitor visitMethod(int access, String methodName, String description, String signature, String[] exceptions) { AsmMethod method = asmClass.getMethodOrCreateIt(methodName + description); if (isInheritedMethodSignature(method.getParent(), method.getKey())) { method.setInherited(true); } method.setSignature(signature); method.setBodyLoaded(true); method.setAccessFlags(access); method.clearOutogingEdges(); String[] internalNames = AsmSignature.extractInternalNames(description, signature); AsmClass[] asmClasses = internalNamesToAsmClasses(internalNames, DETAIL_LEVEL.NOTHING); method.addUsesOfClasses(asmClasses); AsmClass[] asmExceptionClasses = internalNamesToAsmClasses(exceptions, DETAIL_LEVEL.STRUCTURE); method.addUsesOfClasses(asmExceptionClasses); method.addThrowsOfClasses(asmExceptionClasses); if (level == DETAIL_LEVEL.STRUCTURE_AND_CALLS) { return new AsmMethodVisitor(method, asmClassProvider); } return null; }
private void computeAccessedField() { if (!isConstructor()) { for (AsmEdge edge : getOutgoingEdges()) { if (isCallToNonStaticInternalField(edge)) { if (isFieldAccesingDifferentField((AsmField) edge.getTo())) { accessedField = null; break; } accessedField = (AsmField) edge.getTo(); } else if (isCallToNonStaticInternalMethod(edge)) { AsmMethod method = (AsmMethod) edge.getTo(); if (isMethodNotAccessorOrAccessingDifferentField(method)) { accessedField = null; break; } accessedField = method.getAccessedField(); } } } }
public String formatMessage(AsmMethod asmMethod) { if (asmMethod.isConstructor()) { return "Constructor '" + getShortClassName(asmMethod.getParent()) + "(...)' is deprecated."; } else { return "Method '" + getShortClassName(asmMethod.getParent()) + "." + asmMethod.getName() + "(...)' is deprecated."; } }
protected final int getMethodLineNumber(AsmMethod asmMethod) { MethodSignature methodSignature = MethodSignatureScanner.scan(asmMethod.getGenericKey()); AsmClass asmClass = asmMethod.getParent(); Integer result = javaResourceLocator.getMethodStartLine(asmClass.getInternalName() + "#" + MethodSignaturePrinter.print(methodSignature)); if (result != null) { return result; } return -1; }
@Override public void visitMethod(AsmMethod asmMethod) { if (!asmMethod.isUsed() && asmMethod.isProtected() && !asmClass.isAbstract() && !SerializableContract.methodMatch(asmMethod) && !asmMethod.isInherited()) { CheckMessage message = new CheckMessage(this, "Protected method '" + asmMethod.getName() + "(...)' is never used."); int line = getMethodLineNumber(asmMethod); if (line > 0) { message.setLine(line); } SourceFile file = getSourceFile(asmClass); file.log(message); } }
void addThrowsOfClasses(AsmClass[] asmClasses) { for (AsmClass asmClass : asmClasses) { addEdge(new AsmEdge(this, asmClass, SourceCodeEdgeUsage.THROWS)); } }
@Override public void visitEdge(AsmEdge edge) { if (edge.getTo() instanceof AsmMethod) { AsmMethod targetMethod = (AsmMethod) edge.getTo(); AsmClass targetClass = targetMethod.getParent(); if ("java/io/File".equals(targetClass.getInternalName()) && "deleteOnExit()V".equals(targetMethod.getKey())) { SourceFile sourceFile = getSourceFile(asmClass); CheckMessage message = new CheckMessage(this, "Do not use method 'File#deleteOnExit()'."); message.setLine(edge.getSourceLineNumber()); sourceFile.log(message); } } }
public String getGenericKey() { if (signature != null) { return name + signature; } return getKey(); }
AsmMethod getMethodOrCreateIt(String key) { AsmMethod method = getMethod(key); if (method != null) { return method; } method = new AsmMethod(this, key); method.setBodyLoaded(false); addMethod(method); return method; }
@Override public void visitMethod(AsmMethod asmMethod) { if (isPrivateUnused(asmMethod) && !isExcludedFromCheck(asmMethod)) { String messageStr = "Private method '" + asmMethod.getName() + "' is never used."; if ("<init>".equals(asmMethod.getName())) { messageStr = "Private constructor '" + asmClass.getDisplayName() + "("; List<String> params = Lists.newArrayList(); for (Parameter param : MethodSignatureScanner.scan(asmMethod.getGenericKey()).getArgumentTypes()) { String paramName = param.getClassName(); if (StringUtils.isEmpty(paramName)) { paramName = MethodSignatureScanner.getReadableType(param.getJvmJavaType()); } params.add(paramName + (param.isArray() ? "[]" : "")); } messageStr += Joiner.on(",").join(params) + ")' is never used."; } CheckMessage message = new CheckMessage(this, messageStr); int line = getMethodLineNumber(asmMethod); if (line > 0) { message.setLine(line); } SourceFile file = getSourceFile(asmClass); file.log(message); } }
private boolean isCallToNonStaticInternalField(AsmEdge edge) { return edge.getTargetAsmClass() == getParent() && edge.getUsage() == SourceCodeEdgeUsage.CALLS_FIELD && !((AsmField) edge.getTo()).isStatic(); }
protected final int getMethodLineNumber(AsmMethod asmMethod) { MethodSignature methodSignature = MethodSignatureScanner.scan(asmMethod.getGenericKey()); AsmClass asmClass = asmMethod.getParent(); Integer result = context.getJavaResourceLocator().getMethodStartLine(asmClass.getInternalName() + "#" + MethodSignaturePrinter.print(methodSignature)); if (result != null) { return result; } return -1; }
void addThrowsOfClasses(AsmClass[] asmClasses) { for (AsmClass asmClass : asmClasses) { addEdge(new AsmEdge(this, asmClass, SourceCodeEdgeUsage.THROWS)); } }
public String getGenericKey() { if (signature != null) { return name + signature; } return getKey(); }
AsmMethod getMethodOrCreateIt(String key) { AsmMethod method = getMethod(key); if (method != null) { return method; } method = new AsmMethod(this, key); method.setBodyLoaded(false); addMethod(method); return method; }
private boolean isCallToNonStaticInternalField(AsmEdge edge) { return edge.getTargetAsmClass() == getParent() && edge.getUsage() == SourceCodeEdgeUsage.CALLS_FIELD && !((AsmField) edge.getTo()).isStatic(); }