@Override public void leaveClass(AsmClass asmClass) { if (this.asmClass != null) { for (CheckMessage message : internalNames.values()) { SourceFile sourceFile = getSourceFile(asmClass); sourceFile.log(message); } } }
@Override public void addIssue(Tree tree, CheckMessage checkMessage) { checkMessage.setLine(((JavaTree) tree).getLine()); sourceFile.log(checkMessage); }
/** * Cost is set if <code>cost<code/> is more than zero. * */ private void commonAddIssue(JavaScriptCheck check, int line, String message, double cost){ Preconditions.checkNotNull(check); Preconditions.checkNotNull(message); CheckMessage checkMessage = new CheckMessage(check, message); if (cost > 0) { checkMessage.setCost(cost); } if (line > 0) { checkMessage.setLine(line); } sourceFile.log(checkMessage); }
@Override public void addIssue(int line, JavaCheck javaCheck, String message, @Nullable Double cost) { Preconditions.checkNotNull(javaCheck); Preconditions.checkNotNull(message); CheckMessage checkMessage = new CheckMessage(javaCheck, message); if (line > 0) { checkMessage.setLine(line); } if (cost == null) { Annotation linear = AnnotationUtils.getAnnotation(javaCheck, SqaleLinearRemediation.class); Annotation linearWithOffset = AnnotationUtils.getAnnotation(javaCheck, SqaleLinearWithOffsetRemediation.class); if(linear != null || linearWithOffset != null) { throw new IllegalStateException("A check annotated with a linear sqale function should provide an effort to fix"); } } else { checkMessage.setCost(cost); } sourceFile.log(checkMessage); }
@Override public void visitMethod(AsmMethod asmMethod) { int line = getMethodLineNumber(asmMethod); if (line > 0) { Set<String> reportedExceptions = Sets.newHashSet(); List<AsmClass> thrownClasses = asmMethod.getThrows(); for (AsmClass thrownClass : thrownClasses) { String thrownClassName = thrownClass.getDisplayName(); if (!reportedExceptions.contains(thrownClassName)) { String issueMessage = getIssueMessage(thrownClasses, thrownClass); if (issueMessage != null) { reportedExceptions.add(thrownClassName); CheckMessage message = new CheckMessage(this, issueMessage); message.setLine(line); SourceFile file = getSourceFile(asmClass); file.log(message); } } } } }
@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); } } }
@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); } }
@Override public void log(CheckMessage message) { if (peekSourceCode() instanceof SourceFile) { peekSourceCode().log(message); } else if (peekSourceCode().getParent(SourceFile.class) != null) { peekSourceCode().getParent(SourceFile.class).log(message); } else { throw new IllegalStateException("Unable to log a check message on source code '" + (peekSourceCode() == null ? "[NULL]" : peekSourceCode().getKey()) + "'"); } }
/** * {@inheritDoc} */ @Override public void log(CheckMessage message) { if (peekSourceCode() instanceof SourceFile) { peekSourceCode().log(message); } else if (peekSourceCode().getParent(SourceFile.class) != null) { peekSourceCode().getParent(SourceFile.class).log(message); } else { throw new IllegalStateException("Unable to log a check message on source code '" + (peekSourceCode() == null ? "[NULL]" : peekSourceCode().getKey()) + "'"); } }
@Override public void visitEdge(AsmEdge edge) { if (edge.getTo().isDeprecated() && edge.getTo() instanceof AsmMethod) { AsmMethod targetMethod = (AsmMethod) edge.getTo(); SourceFile sourceFile = getSourceFile(asmClass); CheckMessage message = new CheckMessage(this, formatMessage(targetMethod)); message.setLine(edge.getSourceLineNumber()); sourceFile.log(message); } }
@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); } }