@Override public void leaveFile(@Nullable AstNode astNode) { getContext().peekSourceCode().add(metric, lines.size()); } }
public static int getRecursiveMeasureInt(SourceCode sourceCode, MetricDef metric) { int childrenValue = 0; if (sourceCode.getChildren() != null) { for (SourceCode child : sourceCode.getChildren()) { childrenValue += getRecursiveMeasureInt(child, metric); } } return sourceCode.getInt(metric) + childrenValue; }
private void decorateWith(SourceCode sourceCode, MetricDef... metrics) { if (sourceCode.hasChildren()) { for (SourceCode child : sourceCode.getChildren()) { decorateWith(child, metrics); } } for (MetricDef metric : metrics) { if (!metric.aggregateIfThereIsAlreadyAValue() && Double.doubleToRawLongBits(sourceCode.getDouble(metric)) != 0) { continue; } if (sourceCode.hasChildren()) { for (SourceCode child : sourceCode.getChildren()) { if (!metric.isCalculatedMetric() && metric.isThereAggregationFormula()) { sourceCode.add(metric, child); } } } } } }
public <S extends SourceCode> S getAncestor(Class<S> withClass) { S ancestor = getParent(withClass); if (ancestor != null) { S parentAncestor = ancestor.getAncestor(withClass); if (parentAncestor != null) { ancestor = parentAncestor; } } return ancestor; }
public void add(MetricDef metric, SourceCode child) { add(metric, child.getMeasure(metric)); }
@Override public void index(SourceCode sourceCode) { sourceCode.setSourceCodeIndexer(this); index.put(sourceCode.getKey(), sourceCode); } }
@Override public void leaveFile(AstNode astNode) { int fileLength = getContext().peekSourceCode().getInt(CSharpMetric.LINES); for (int line = 1; line <= fileLength; line++) { fileLinesContext.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, linesOfCode.contains(line) ? 1 : 0); fileLinesContext.setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, line, linesOfComments.contains(line) ? 1 : 0); } fileLinesContext.save(); linesOfCode.clear(); linesOfComments.clear(); }
@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 SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { AstNode identifier = astNode.getFirstChild(EcmaScriptTokenType.IDENTIFIER, EcmaScriptGrammar.PROPERTY_NAME, Kind.IDENTIFIER); final String functionName = identifier == null ? "anonymous" : identifier.getTokenValue(); final String fileKey = parentSourceCode.isType(SourceFile.class) ? parentSourceCode.getKey() : parentSourceCode.getParent(SourceFile.class).getKey(); SourceFunction function = new SourceFunction(fileKey + ":" + functionName + ":" + astNode.getToken().getLine() + ":" + astNode.getToken().getColumn()); function.setStartAtLine(astNode.getTokenLine()); return function; } }, FUNCTION_NODES));
private void saveClassComplexity(org.sonar.api.resources.File sonarFile, SourceFile squidFile) { double complexityInClasses = 0; Set<SourceCode> children = squidFile.getChildren(); if (children != null) { for (SourceCode sourceCode : squidFile.getChildren()) { if (sourceCode.isType(SourceClass.class)) { complexityInClasses += sourceCode.getDouble(EcmaScriptMetric.COMPLEXITY); } } } context.saveMeasure(sonarFile, CoreMetrics.COMPLEXITY_IN_CLASSES, complexityInClasses); }
@Override public boolean match(SourceCode unit) { switch (operator) { case EQUALS: return NumberUtils.compare(unit.getDouble(metric), value) == 0; case GREATER_THAN: return unit.getDouble(metric) > value; case GREATER_THAN_EQUALS: return unit.getDouble(metric) >= value; case LESS_THAN_EQUALS: return unit.getDouble(metric) <= value; case LESS_THAN: return unit.getDouble(metric) < value; default: throw new IllegalStateException("The operator value '" + operator + "' is unknown."); } }
@Override public String toString() { return getKey(); }
@Override public boolean match(SourceCode unit) { return unit.isType(resourceType); }
public <S extends SourceCode> S getParent(Class<S> sourceCode) { if (parent == null) { return null; } if (parent.getClass().equals(sourceCode)) { return (S) parent; } return parent.getParent(sourceCode); }
public void log(CheckMessage message) { message.setSourceCode(this); getCheckMessages().add(message); }
/** {@inheritDoc} */ @Override public void addSourceCode(SourceCode child) { peekSourceCode().addChild(child); sourceCodeStack.add(child); }
@Override public void leaveFile(AstNode astNode) { File sonarFile = File.fromIOFile(getContext().getFile(), project); FileLinesContext fileLinesContext = fileLinesContextFactory.createFor(sonarFile); int fileLength = getContext().peekSourceCode().getInt(CssMetric.LINES); for (int line = 1; line <= fileLength; line++) { fileLinesContext.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, linesOfCode.contains(line) ? 1 : 0); fileLinesContext.setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, line, linesOfComments.contains(line) ? 1 : 0); } fileLinesContext.save(); linesOfCode.clear(); linesOfComments.clear(); }
/** * {@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()) + "'"); } }
public Map<Integer, Integer> distributeAccordingTo(MetricDef metric, int... thresholds) { Map<Integer, Integer> result = new TreeMap<Integer, Integer>(); for (int threshold : thresholds) { result.put(threshold, 0); } for (SourceCode unit : units) { for (int index = thresholds.length - 1; index >= 0; index--) { if (unit.getDouble(metric) >= thresholds[index]) { result.put(thresholds[index], result.get(thresholds[index]) + 1); break; } } } return result; } }