Skip to content

Commit

Permalink
#33 "source code style rule definition"
Browse files Browse the repository at this point in the history
- now there are also class members of type Optional<?> (instead of wrapping them on-the-fly in the getter)
- had to add OptionalTypeAdapter because Gson does not!!! support Optional<?> in 2022 (see google/gson#1102)
  • Loading branch information
janScheible committed Jul 10, 2022
1 parent a9ab48d commit 07e0d89
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 16 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,7 @@ Unresolvable methods (no coverage information available):
1. usage of immutable data structures only
1. for collections defensive copies and `Collections.unmodifiableXzy(...)` in constructor
1. other fields or elements in collections have to be immutable
1. usage of `Optional<?>` (was actually designed for method return types only, but is the only JDK built-in way to indicate a nullable value (all `@Nullable` annotations are from third-party libraries))
1. never pass or return `null`, use `Optional<?>` instead (this eliminates the need for `null` checks everywhere)
1. but prefer method overloading or usage of a builder over `Optional<?>` method parameters
1. even use `Optional<?>` for class fields (avoids unnecessary `Optional.ofNullable(...)` calls in getter of immutable objects)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.scheible.testgapanalysis.maven;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import java.util.Optional;

/**
* Unfortunately Gson has no built-in support for Optional.
*
* @author sj
*/
class OptionalTypeAdapter implements JsonDeserializer<Optional<?>>, JsonSerializer<Optional<?>> {

OptionalTypeAdapter() {
}

@Override
public Optional<?> deserialize(final JsonElement element, final Type type, final JsonDeserializationContext context) throws JsonParseException {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public JsonElement serialize(final Optional<?> value, final Type type, final JsonSerializationContext context) {
return context.serialize(value.orElse(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ private void logReport(final TestGapReport report) {

private void writeJsonReport(final TestGapReport report) throws MojoExecutionException {
final File reportFile = new File(buildDir, "test-gap-report.json");
final Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
final Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()
.registerTypeAdapter(Optional.class, new OptionalTypeAdapter()).create();

try {
Files.write(reportFile.toPath(), gson.toJson(report).getBytes());
Expand All @@ -114,3 +115,4 @@ private void writeJsonReport(final TestGapReport report) throws MojoExecutionExc
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public static class TestGapMethod {
private final int sourceLine;
private final int sourceColumn;

private final String coveredMethodName;
private final Integer coveredMethodLine;
private final Optional<String> coveredMethodName;
private final Optional<Integer> coveredMethodLine;

private TestGapMethod(final String topLevelTypeFqn, final String description, final int sourceLine,
final int sourceColumn, final String coveredMethodName, final Integer coveredMethodLine) {
Expand All @@ -68,8 +68,8 @@ private TestGapMethod(final String topLevelTypeFqn, final String description, fi
this.sourceLine = sourceLine;
this.sourceColumn = sourceColumn;

this.coveredMethodName = coveredMethodName;
this.coveredMethodLine = coveredMethodLine;
this.coveredMethodName = Optional.ofNullable(coveredMethodName);
this.coveredMethodLine = Optional.ofNullable(coveredMethodLine);
}

public TestGapMethod(final String topLevelTypeFqn, final String description, final int sourceLine,
Expand Down Expand Up @@ -100,17 +100,17 @@ public int getSourceColumn() {
}

public Optional<String> getCoveredMethodName() {
return Optional.ofNullable(coveredMethodName);
return coveredMethodName;
}

public Optional<Integer> getCoveredMethodLine() {
return Optional.ofNullable(coveredMethodLine);
return coveredMethodLine;
}

@Override
public String toString() {
final String coverageInfo = coveredMethodName != null && coveredMethodLine != null
? String.format(" resolved to '%s' with line %d", coveredMethodName, coveredMethodLine)
final String coverageInfo = coveredMethodName.isPresent() && coveredMethodLine.isPresent()
? String.format(" resolved to '%s' with line %d", coveredMethodName.get(), coveredMethodLine.get())
: "";
return String.format("%s%s at %d:%d%s", topLevelTypeFqn, description, sourceLine, sourceColumn,
coverageInfo);
Expand Down Expand Up @@ -144,7 +144,7 @@ public String toString() {
private final String workDir;

private final String oldCommitHash;
private final String newCommitHash;
private final Optional<String> newCommitHash;
private final Boolean compareWithWorkingCopyChanges;

private final Set<String> jaCoCoReportFiles;
Expand Down Expand Up @@ -176,7 +176,7 @@ public static WorkDirStep builder() {
this.workDir = builder.workDir;

this.oldCommitHash = builder.oldCommitHash;
this.newCommitHash = builder.newCommitHash.orElse(null);
this.newCommitHash = builder.newCommitHash;
compareWithWorkingCopyChanges = builder.newCommitHash.isPresent() ? null : Boolean.TRUE;

this.jaCoCoReportFiles = Collections.unmodifiableSet(new HashSet<>(builder.jaCoCoReportFiles));
Expand Down Expand Up @@ -210,7 +210,7 @@ public String getOldCommitHash() {
}

public Optional<String> getNewCommitHash() {
return Optional.ofNullable(newCommitHash);
return newCommitHash;
}

public Boolean getCompareWithWorkingCopyChanges() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public enum MethodType {
private final int argumentCount;
private final List<String> argumentTypes;
private final Map<String, String> typeParameters;
private final String outerDeclaringType;
private final Optional<String> outerDeclaringType;

public static MethodTypeStep builder() {
return new BuilderImpl();
Expand All @@ -65,7 +65,7 @@ public static MethodTypeStep builder() {
argumentCount = argumentTypes.size();
this.typeParameters = Collections
.unmodifiableMap(builder.typeParameters != null ? new HashMap<>(builder.typeParameters) : emptyMap());
this.outerDeclaringType = builder.outerDeclaringType.orElse(null);
this.outerDeclaringType = builder.outerDeclaringType;
}

public MethodType getMethodType() {
Expand Down Expand Up @@ -175,7 +175,7 @@ public Map<String, String> getTypeParameters() {
}

public Optional<String> getOuterDeclaringType() {
return Optional.of(outerDeclaringType);
return outerDeclaringType;
}

public String getDescription() {
Expand Down Expand Up @@ -238,6 +238,6 @@ public String toString() {
+ ", empty=" + empty + (!argumentTypes.isEmpty() ? ", parameterTypes=" + argumentTypes : "")
+ ", argumentCount=" + argumentCount
+ (!typeParameters.isEmpty() ? ", typeParameters=" + typeParameters : "")
+ (outerDeclaringType != null ? ", outerDeclaringType=" + outerDeclaringType : "") + "]";
+ (outerDeclaringType.isPresent() ? ", outerDeclaringType=" + outerDeclaringType.get() : "") + "]";
}
}

0 comments on commit 07e0d89

Please sign in to comment.