private void logMessage(String fromClass, String toClass, int sourceLineNumber) { CheckMessage message = new CheckMessage(this, fromClass + " must not use " + toClass); if(sourceLineNumber != 0) { message.setLine(sourceLineNumber); } internalNames.put(toClass, message); }
/** * {@inheritDoc} */ @Override public void createLineViolation(CodeCheck check, String message, int line, Object... messageParameters) { CheckMessage checkMessage = new CheckMessage((Object) check, message, messageParameters); if (line > 0) { checkMessage.setLine(line); } log(checkMessage); }
/** {@inheritDoc} */ @Override public void createLineViolation(CodeCheck check, String message, int line, Object... messageParameters) { CheckMessage checkMessage = new CheckMessage(check, message, messageParameters); if (line > 0) { checkMessage.setLine(line); } log(checkMessage); }
@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); } }
private static Collection<CheckMessage> getCheckMessages(List<Issue> issues) { List<CheckMessage> checkMessages = new ArrayList<>(); for (Issue issue : issues) { CheckMessage checkMessage; if (issue instanceof FileIssue) { FileIssue fileIssue = (FileIssue) issue; checkMessage = new CheckMessage(fileIssue.check(), fileIssue.message()); } else if (issue instanceof LineIssue) { LineIssue lineIssue = (LineIssue) issue; checkMessage = new CheckMessage(lineIssue.check(), lineIssue.message()); checkMessage.setLine(lineIssue.line()); } else { PreciseIssue preciseIssue = (PreciseIssue) issue; checkMessage = new CheckMessage(preciseIssue.check(), preciseIssue.primaryLocation().message()); checkMessage.setLine(preciseIssue.primaryLocation().startLine()); } if (issue.cost() != null) { checkMessage.setCost(issue.cost()); } checkMessages.add(checkMessage); } return checkMessages; }
private static Collection<CheckMessage> getCheckMessages(List<Issue> issues) { List<CheckMessage> checkMessages = new ArrayList<>(); for (Issue issue : issues) { CheckMessage checkMessage; if (issue instanceof FileIssue) { FileIssue fileIssue = (FileIssue) issue; checkMessage = new CheckMessage(fileIssue.check(), fileIssue.message()); } else if (issue instanceof LineIssue) { LineIssue lineIssue = (LineIssue) issue; checkMessage = new CheckMessage(lineIssue.check(), lineIssue.message()); checkMessage.setLine(lineIssue.line()); } else { PreciseIssue preciseIssue = (PreciseIssue) issue; checkMessage = new CheckMessage(preciseIssue.check(), preciseIssue.primaryLocation().message()); checkMessage.setLine(preciseIssue.primaryLocation().startLine()); } if (issue.cost() != null) { checkMessage.setCost(issue.cost()); } checkMessages.add(checkMessage); } return checkMessages; }
@Override public void visitToken(Token token) { for (Trivia trivia : token.getTrivia()) { if (trivia.isComment()) { String[] commentLines = getContext().getCommentAnalyser().getContents(trivia.getToken().getOriginalValue()).split("(\r)?\n|\r", -1); int line = trivia.getToken().getLine(); for (String commentLine : commentLines) { if (commentLine.contains("NOSONAR")) { CheckMessage violation = new CheckMessage((Object) this, "Is NOSONAR usage acceptable or does it hide a real quality flaw?"); violation.setLine(line); violation.setBypassExclusion(true); getContext().log(violation); } line++; } } } }
@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); } }