Skip to content

Commit

Permalink
#33 "source code style rule definition"
Browse files Browse the repository at this point in the history
- usage of static and wildcard imports
  • Loading branch information
janScheible committed Jul 23, 2022
1 parent 3c21cb6 commit 63a2d72
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 36 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,5 @@ Unresolvable methods (no coverage information available):
1. don't overuse inner types, as soon as the type is also useful in some other context it should be a top-level type
1. don't use checked (and unchecked) exceptions for program flow, always prefer returning proper result types (exceptions are reserved for real, unexpected errors)
1. wrapping an `IOException` with `UncheckedIOException` (or more general any exception in a `IllegalStateException`) is okay but takes away the possibility from the caller to react to errors (for example by skipping a single file that cause an `IOException` while reading it)
1. no static imports in production code (only allowed in tests)
1. no wildcard imports in general
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.scheible.testgapanalysis.analysis.testgap;

import static java.util.Collections.emptySet;

import java.io.File;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
Expand Down Expand Up @@ -59,7 +58,7 @@ public TestGapReport run(final File workDir, final Set<File> jaCoCoReportFiles,
}

private RepositoryResult identifyFileChanges(final Optional<String> referenceCommitHash, final File workDir) {
Set<NewOrChangedFile> newOrChangedFiles = emptySet();
Set<NewOrChangedFile> newOrChangedFiles = Collections.emptySet();

final RepositoryStatus status = referenceCommitHash
.map(h -> gitDiffer.ofCommitComparedToHead(workDir, h, NON_TEST_JAVA_FILE))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.scheible.testgapanalysis.common;

import static java.util.Objects.requireNonNull;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
Expand All @@ -10,6 +8,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -31,7 +30,7 @@ public static String readUtf8(final Class<?> anchor, final String name) {
}

public static String readUtf8(final InputStream inputStream) {
try (InputStream tryCatchInputStream = requireNonNull(inputStream)) {
try (InputStream tryCatchInputStream = Objects.requireNonNull(inputStream)) {

final ByteArrayOutputStream result = new ByteArrayOutputStream();
final byte[] buffer = new byte[1024];
Expand All @@ -48,7 +47,7 @@ public static String readUtf8(final InputStream inputStream) {

public static String readUtf8(final File file) {
try {
return new String(Files.readAllBytes(requireNonNull(file).toPath()), UTF_8_CHARSET);
return new String(Files.readAllBytes(Objects.requireNonNull(file).toPath()), UTF_8_CHARSET);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.scheible.testgapanalysis.jacoco;

import static javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -18,6 +16,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -53,7 +52,7 @@ public Set<MethodWithCoverageInfo> getMethodCoverage(final File reportXmlFile) {
private Set<MethodWithCoverageInfo> getMethodCoverage(final InputSource inputSource) {
try {
final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setFeature(FEATURE_SECURE_PROCESSING, true);
builderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
final DocumentBuilder builder = builderFactory.newDocumentBuilder();
builder.setEntityResolver((String publicId,
String systemId) -> systemId.contains("report.dtd") ? new InputSource(new StringReader("")) : null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package com.scheible.testgapanalysis.jacoco.resolver;

import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;

import static com.scheible.testgapanalysis.common.JavaMethodUtils.normalizeMethodArguments;
import static com.scheible.testgapanalysis.common.JavaMethodUtils.parseDescriptorArguments;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -19,6 +14,7 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import com.scheible.testgapanalysis.common.JavaMethodUtils;
import com.scheible.testgapanalysis.jacoco.MethodWithCoverageInfo;
import com.scheible.testgapanalysis.parser.ParsedMethod;

Expand Down Expand Up @@ -60,7 +56,7 @@ public CoverageResult resolve(final Set<ParsedMethod> methods) {
CoverageResult resolveType(final TopLevelType type, final Set<ParsedMethod> methods) {
final CoverageResult result = new CoverageResult();

final Set<MethodWithCoverageInfo> coverage = coverageReport.getOrDefault(type, emptySet());
final Set<MethodWithCoverageInfo> coverage = coverageReport.getOrDefault(type, Collections.emptySet());

result.add(resolveInitializers(filter(methods, ParsedMethod::isInitializer), coverage));
result.add(resolveStaticInitializers(filter(methods, ParsedMethod::isStaticInitializer), coverage));
Expand All @@ -84,7 +80,7 @@ private CoverageResult resolveInitializers(final Set<ParsedMethod> initializers,
if (initializers.size() == 1 && coverageConstructors.size() == 1) {
final Map<ParsedMethod, MethodWithCoverageInfo> resolvedInitializers = new HashMap<>();
resolvedInitializers.put(initializers.iterator().next(), coverageConstructors.get(0));
return new CoverageResult(resolvedInitializers, emptySet());
return new CoverageResult(resolvedInitializers, Collections.emptySet());
} else {
// try to find a constructor with covered code, if there is none use any non-covered constructor
final Optional<MethodWithCoverageInfo> firstConstructorCoverage = coverageConstructors.stream()
Expand All @@ -94,7 +90,7 @@ private CoverageResult resolveInitializers(final Set<ParsedMethod> initializers,
if (firstConstructorCoverage.isPresent()) {
final Map<ParsedMethod, MethodWithCoverageInfo> resolvedInitializers = new HashMap<>();
initializers.forEach(im -> resolvedInitializers.put(im, firstConstructorCoverage.get()));
return new CoverageResult(resolvedInitializers, emptySet());
return new CoverageResult(resolvedInitializers, Collections.emptySet());
}
}

Expand All @@ -113,7 +109,7 @@ private CoverageResult resolveStaticInitializers(final Set<ParsedMethod> staticI
if (!coverageStaticInitializers.isEmpty()) { // make sure that a coverage report is there
final Map<ParsedMethod, MethodWithCoverageInfo> resolvedStaticInitializers = new HashMap<>();
staticInitializers.forEach(im -> resolvedStaticInitializers.put(im, coverageStaticInitializers.get(0)));
return new CoverageResult(resolvedStaticInitializers, emptySet());
return new CoverageResult(resolvedStaticInitializers, Collections.emptySet());
}

return new CoverageResult();
Expand All @@ -132,8 +128,8 @@ private CoverageResult resolveConstructor(final Set<ParsedMethod> constructors,
final Set<ParsedMethod> unresolved = new HashSet<>();

for (final ParsedMethod constructor : constructors) {
final List<String> normalizedConstructorArguments = new ArrayList<>(
normalizeMethodArguments(constructor.getArgumentTypes(), constructor.getTypeParameters()));
final List<String> normalizedConstructorArguments = new ArrayList<>(JavaMethodUtils
.normalizeMethodArguments(constructor.getArgumentTypes(), constructor.getTypeParameters()));

// The constructors of enums have two additional parameter of type String and int. Most likely the name and
// index of the enum const is passed via this parameter.
Expand All @@ -150,8 +146,8 @@ private CoverageResult resolveConstructor(final Set<ParsedMethod> constructors,
final List<MethodWithCoverageInfo> coverageConstructors = coverage.stream()
.filter(MethodWithCoverageInfo::isConstructor)
.filter(mwci -> mwci.getEnclosingSimpleName().equals(constructor.getEnclosingSimpleName()))
.filter(mwci -> normalizedConstructorArguments.equals(
normalizeMethodArguments(parseDescriptorArguments(mwci.getDescription()), emptyMap())))
.filter(mwci -> normalizedConstructorArguments.equals(JavaMethodUtils.normalizeMethodArguments(
JavaMethodUtils.parseDescriptorArguments(mwci.getDescription()), Collections.emptyMap())))
.collect(Collectors.toList());
if (coverageConstructors.size() == 1) {
resolved.put(constructor, coverageConstructors.get(0));
Expand All @@ -177,8 +173,8 @@ private CoverageResult resolveNonLambdaMethods(final Set<ParsedMethod> methods,
for (final MethodWithCoverageInfo coverageMethod : coverage.stream()
.filter(MethodWithCoverageInfo::isNonLambdaMethod).collect(Collectors.toSet())) {
if (method.containsLine(coverageMethod.getLine()) && method.getName().equals(coverageMethod.getName())
&& method.getArgumentCount() == parseDescriptorArguments(coverageMethod.getDescription())
.size()) {
&& method.getArgumentCount() == JavaMethodUtils
.parseDescriptorArguments(coverageMethod.getDescription()).size()) {
candidates.add(coverageMethod);
}
}
Expand Down Expand Up @@ -249,9 +245,8 @@ private CoverageResult resolveLambdaMethodsCluster(final Entry<ParsedMethod, Set
boolean withoutErrors = true;

for (int i = 0; i < lambdaCoverage.size(); i++) {
if (lambdas.get(i)
.getArgumentCount() <= parseDescriptorArguments(lambdaCoverage.get(i).getDescription())
.size()) {
if (lambdas.get(i).getArgumentCount() <= JavaMethodUtils
.parseDescriptorArguments(lambdaCoverage.get(i).getDescription()).size()) {
currentResolved.put(lambdas.get(i), lambdaCoverage.get(i));
} else {
withoutErrors = false;
Expand All @@ -260,11 +255,11 @@ private CoverageResult resolveLambdaMethodsCluster(final Entry<ParsedMethod, Set
}

if (withoutErrors) {
return new CoverageResult(currentResolved, emptySet());
return new CoverageResult(currentResolved, Collections.emptySet());
}
}

return new CoverageResult(emptyMap(), new HashSet<>(lambdas));
return new CoverageResult(Collections.emptyMap(), new HashSet<>(lambdas));
}

private static Map<TopLevelType, Set<ParsedMethod>> groupMethodsByType(final Set<ParsedMethod> methods) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.scheible.testgapanalysis.parser;

import static java.util.Collections.emptyMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -59,8 +57,8 @@ public enum MethodType {
: IntStream.range(0, builder.argumentCount).boxed().map(i -> "Object").collect(Collectors.toList()));

argumentCount = argumentTypes.size();
this.typeParameters = Collections
.unmodifiableMap(builder.typeParameters != null ? new HashMap<>(builder.typeParameters) : emptyMap());
this.typeParameters = Collections.unmodifiableMap(
builder.typeParameters != null ? new HashMap<>(builder.typeParameters) : Collections.emptyMap());
this.outerDeclaringType = builder.outerDeclaringType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.scheible.pocketsaw.api.SubModule;
import com.scheible.testgapanalysis.ExternalFunctionalities.Slf4j;
import com.scheible.testgapanalysis.analysis.*;
import com.scheible.testgapanalysis.common.CommonSubModule;
import com.scheible.testgapanalysis.git.GitSubModule;
import com.scheible.testgapanalysis.jacoco.JaCoCoSubModule;
Expand Down

0 comments on commit 63a2d72

Please sign in to comment.