Skip to content

Commit

Permalink
#33 "source code style rule definition"
Browse files Browse the repository at this point in the history
- usage of forEach with Stream API
  • Loading branch information
janScheible committed Jul 29, 2022
1 parent 951a721 commit cca9b6a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,6 @@ Unresolvable methods (no coverage information available):
1. `hashCode()` must use `Objects.hash(...)`
1. always use `this.` for class fields but never for instance methods (same approach as https://github.com/spring-projects/spring-framework/wiki/Code-Style#field-and-method-references)
1. `static` fields and methods of own class must never be prefixed with the own class name
1. usage of Java Stream API
1. don't overuse lambdas with streams, prefer method references or simple one line lambdas
1. usage of `forEach` with `stream()` is suspicious, should only be used in rare cases with a method reference (good old for-each loops might be easier to read and debug)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.scheible.testgapanalysis.analysis.testgap;

import java.io.File;
import java.util.AbstractMap;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -103,7 +103,7 @@ private static TestGapMethod toTestGapMethod(ParsedMethod method, MethodWithCove
private static Map<CoverageReportMethod, Set<TestGapMethod>> toAmbigouslyResolvedTestGapMethod(
Map<MethodWithCoverageInfo, Set<ParsedMethod>> ambiguouslyResolvedCoverage) {
return ambiguouslyResolvedCoverage.entrySet().stream()
.map(e -> new AbstractMap.SimpleImmutableEntry<>(
.map(e -> new SimpleImmutableEntry<>(
new CoverageReportMethod(e.getKey().getName(), e.getKey().getLine()),
toTestGapMethods(e.getValue())))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ private CoverageResult resolveInitializers(Set<ParsedMethod> initializers, Set<M
.findFirst();

if (firstConstructorCoverage.isPresent()) {
Map<ParsedMethod, MethodWithCoverageInfo> resolvedInitializers = new HashMap<>();
initializers.forEach(im -> resolvedInitializers.put(im, firstConstructorCoverage.get()));
Map<ParsedMethod, MethodWithCoverageInfo> resolvedInitializers = initializers.stream()
.collect(Collectors.toMap(Function.identity(), initilaizer -> firstConstructorCoverage.get()));
return new CoverageResult(resolvedInitializers, Collections.emptySet());
}
}
Expand All @@ -106,8 +106,8 @@ private CoverageResult resolveStaticInitializers(Set<ParsedMethod> staticInitial
.filter(MethodWithCoverageInfo::isStaticInitializer).collect(Collectors.toList());

if (!coverageStaticInitializers.isEmpty()) { // make sure that a coverage report is there
Map<ParsedMethod, MethodWithCoverageInfo> resolvedStaticInitializers = new HashMap<>();
staticInitializers.forEach(im -> resolvedStaticInitializers.put(im, coverageStaticInitializers.get(0)));
Map<ParsedMethod, MethodWithCoverageInfo> resolvedStaticInitializers = staticInitializers.stream().collect(
Collectors.toMap(Function.identity(), staticInitilaizer -> coverageStaticInitializers.get(0)));
return new CoverageResult(resolvedStaticInitializers, Collections.emptySet());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.scheible.testgapanalysis.parser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -10,6 +9,7 @@

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.ConstructorDeclaration;
import com.github.javaparser.ast.body.EnumDeclaration;
Expand All @@ -19,6 +19,7 @@
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
import com.github.javaparser.ast.type.TypeParameter;

/**
*
Expand Down Expand Up @@ -82,29 +83,27 @@ private static List<Node> getParents(Node node) {

static Map<String, String> getTypeParameters(ConstructorDeclaration node) {
List<Node> parents = ParserUtils.getParents(node);
Map<String, String> typeParameters = new HashMap<>();
NodeList<TypeParameter> typeParameters = new NodeList<>();

for (int i = parents.size() - 1; i >= 0; i--) {
if (parents.get(i) instanceof ClassOrInterfaceDeclaration) {
ClassOrInterfaceDeclaration classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration) parents.get(i);
classOrInterfaceDeclaration.getTypeParameters().stream().forEach(tp -> typeParameters.put(
tp.getNameAsString(),
tp.getTypeBound().isNonEmpty() ? tp.getTypeBound().get(0).getNameAsString() : "Object"));
typeParameters.addAll(classOrInterfaceDeclaration.getTypeParameters());

if (classOrInterfaceDeclaration.isStatic()) {
break;
}
} else if (parents.get(i) instanceof RecordDeclaration) {
RecordDeclaration recordDeclaration = (RecordDeclaration) parents.get(i);
recordDeclaration.getTypeParameters().stream().forEach(tp -> typeParameters.put(tp.getNameAsString(),
tp.getTypeBound().isNonEmpty() ? tp.getTypeBound().get(0).getNameAsString() : "Object"));
typeParameters.addAll(recordDeclaration.getTypeParameters());

break;
}
}
node.getTypeParameters().stream().forEach(tp -> typeParameters.put(tp.getNameAsString(),
tp.getTypeBound().isNonEmpty() ? tp.getTypeBound().get(0).getNameAsString() : "Object"));
return typeParameters;
typeParameters.addAll(node.getTypeParameters());

return typeParameters.stream().collect(Collectors.toMap(TypeParameter::getNameAsString,
tp -> tp.getTypeBound().isNonEmpty() ? tp.getTypeBound().get(0).getNameAsString() : "Object"));
}

/**
Expand Down

0 comments on commit cca9b6a

Please sign in to comment.