Skip to content

Commit

Permalink
Migrate from JSR 305 to JSpecify.
Browse files Browse the repository at this point in the history
  • Loading branch information
baron1405 committed Oct 27, 2024
1 parent 671c6e0 commit ead68c9
Show file tree
Hide file tree
Showing 31 changed files with 108 additions and 92 deletions.
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ plugins {
alias(libs.plugins.versions)
}

version = ProjectVersion("0.1.0", BuildType.snapshot)
version = ProjectVersion("0.2.0", BuildType.snapshot)
group = "org.cthing"
description = "A port of the Molinillo generic dependency resolution algorithm to Java."

Expand All @@ -39,9 +39,9 @@ java {
}

dependencies {
api(libs.jsr305)
api(libs.jspecify)

implementation(libs.cthingAnnots)
compileOnly(libs.cthingAnnots)

testImplementation(libs.assertJ)
testImplementation(libs.equalsVerifier)
Expand Down
10 changes: 5 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ junit = "5.11.2"
spotbugs = "4.8.6"

[plugins]
cthingVersioning = { id = "org.cthing.cthing-versioning", version = "1.0.1" }
dependencyAnalysis = { id = "com.autonomousapps.dependency-analysis", version = "2.2.0" }
cthingVersioning = { id = "org.cthing.cthing-versioning", version = "2.0.0" }
dependencyAnalysis = { id = "com.autonomousapps.dependency-analysis", version = "2.3.0" }
spotbugs = { id = "com.github.spotbugs", version = "6.0.25" }
versions = { id = "com.github.ben-manes.versions", version = "0.51.0" }

[libraries]
assertJ = "org.assertj:assertj-core:3.26.3"
cthingAnnots = "org.cthing:cthing-annotations:1.0.0"
cthingAnnots = "org.cthing:cthing-annotations:2.0.0"
equalsVerifier = "nl.jqno.equalsverifier:equalsverifier:3.17.1"
jacksonAnnots = { module = "com.fasterxml.jackson.core:jackson-annotations", version.ref = "jackson" }
jacksonCore = { module = "com.fasterxml.jackson.core:jackson-core", version.ref = "jackson" }
jacksonDatabind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" }
jsr305 = "com.google.code.findbugs:jsr305:3.0.2"
jspecify = "org.jspecify:jspecify:1.0.0"
junitApi = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "junit" }
junitEngine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit" }
junitLauncher = "org.junit.platform:junit-platform-launcher:1.11.2"
junitParams = { module = "org.junit.jupiter:junit-jupiter-params", version.ref = "junit" }
mockito = "org.mockito:mockito-core:5.14.2"
spotbugsContrib = "com.mebigfatguy.sb-contrib:sb-contrib:7.6.5"
versionParser = "org.cthing:versionparser:4.3.0"
versionParser = "org.cthing:versionparser:5.0.0"
2 changes: 1 addition & 1 deletion src/main/java/org/cthing/molinillo/Conflict.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Optional;
import java.util.Set;

import javax.annotation.Nullable;
import org.jspecify.annotations.Nullable;


/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/cthing/molinillo/DebugUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

import javax.annotation.Nullable;
import org.jspecify.annotations.Nullable;


/**
Expand Down Expand Up @@ -46,7 +46,7 @@ public void afterResolution() {
}

@Override
public void printf(final int depth, final String format, final Object... args) {
public void printf(final int depth, final String format, final @Nullable Object... args) {
if (isDebugMode()) {
WRITER.println(String.format(":%4d: ", depth) + String.format(format, args));
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/cthing/molinillo/DefaultUI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cthing.molinillo;

import javax.annotation.Nullable;

import org.jspecify.annotations.Nullable;


/**
Expand All @@ -26,7 +27,7 @@ public void afterResolution() {
}

@Override
public void printf(final int depth, final String format, final Object... args) {
public void printf(final int depth, final String format, final @Nullable Object... args) {
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/cthing/molinillo/DependencyGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
import java.util.function.Consumer;
import java.util.function.Function;

import javax.annotation.Nullable;

import org.cthing.annotations.AccessForTesting;
import org.cthing.molinillo.errors.CircularDependencyError;
import org.cthing.molinillo.graph.Edge;
import org.cthing.molinillo.graph.Log;
import org.cthing.molinillo.graph.Vertex;
import org.jspecify.annotations.Nullable;


/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cthing/molinillo/DependencyState.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;
import org.jspecify.annotations.Nullable;


/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cthing/molinillo/Payload.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Objects;

import javax.annotation.Nullable;
import org.jspecify.annotations.Nullable;


/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cthing/molinillo/PossibilitySet.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public List<S> getPossibilities() {
public Optional<S> getLatestVersion() {
return this.possibilities.isEmpty()
? Optional.empty()
: Optional.ofNullable(this.possibilities.get(this.possibilities.size() - 1));
: Optional.of(this.possibilities.get(this.possibilities.size() - 1));
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cthing/molinillo/PossibilityState.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;
import org.jspecify.annotations.Nullable;


/**
Expand Down
71 changes: 36 additions & 35 deletions src/main/java/org/cthing/molinillo/Resolution.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import javax.annotation.Nullable;

import org.cthing.molinillo.errors.CircularDependencyError;
import org.cthing.molinillo.errors.NoSuchDependencyError;
import org.cthing.molinillo.errors.ResolverError;
import org.cthing.molinillo.errors.VersionConflictError;
import org.cthing.molinillo.graph.Edge;
import org.cthing.molinillo.graph.Vertex;
import org.jspecify.annotations.Nullable;


/**
Expand All @@ -47,7 +46,7 @@ public class Resolution<R, S> {
private int iterationRate;
private int iterationCount;
private long startedAt;
private final List<ResolutionState<R, S>> states;
private final List<@Nullable ResolutionState<R, S>> states;

private final Map<R, List<Integer>> parentsOf;

Expand All @@ -64,7 +63,7 @@ public Resolution(final SpecificationProvider<R, S> specificationProvider, final
this.specificationProvider = specificationProvider;
this.resolverUi = resolverUi;
this.base = base;
this.originalRequested = originalRequested;
this.originalRequested = new HashSet<>(originalRequested);

this.states = new ArrayList<>();
this.parentsOf = new HashMap<>();
Expand Down Expand Up @@ -308,12 +307,12 @@ private boolean allowMissing(final R dependency) {
private NoSuchDependencyError processNoSuchDependencyError(final NoSuchDependencyError error) {
if (getState().isPresent()) {
getActivated().vertexNamed(nameForDependency(error.getDependency())).ifPresent(vertex -> {
error.getRequiredBy().addAll(vertex.getIncomingEdges()
.stream()
.map(e -> e.getOrigin().getName())
.toList());
error.addRequiredBy(vertex.getIncomingEdges()
.stream()
.map(e -> e.getOrigin().getName())
.toList());
if (!vertex.getExplicitRequirements().isEmpty()) {
error.getRequiredBy().add(nameForExplicitDependencySource());
error.addRequiredBy(nameForExplicitDependencySource());
}
});
}
Expand Down Expand Up @@ -391,7 +390,7 @@ private DependencyGraph<S, R> resolveActivatedSpecs() {
*/
private void processTopmostState() {
try {
final List<PossibilitySet<R, S>> possibilities = getPossibilities();
final List<@Nullable PossibilitySet<R, S>> possibilities = getPossibilities();
if (!possibilities.isEmpty() && possibilities.get(possibilities.size() - 1) != null) {
attemptToActivate();
} else {
Expand Down Expand Up @@ -449,18 +448,15 @@ private void unwindForConflict() {
detailsForUnwind.getStateIndex() / 2);

final Map<String, Conflict<R, S>> conflicts = getConflicts();
final List<ResolutionState<R, S>> statesToSlice = this.states.subList(detailsForUnwind.getStateIndex() + 1,
this.states.size());
final List<ResolutionState<R, S>> slicedStates = new ArrayList<>(statesToSlice);
final List<@Nullable ResolutionState<R, S>> statesToSlice =
this.states.subList(detailsForUnwind.getStateIndex() + 1, this.states.size());
final List<@Nullable ResolutionState<R, S>> slicedStates = new ArrayList<>(statesToSlice);
statesToSlice.clear();
raiseErrorUnlessState(conflicts);

if (!slicedStates.isEmpty()) {
if (slicedStates.get(0) == null) {
getActivated().rewindTo(INITIAL_STATE);
} else {
getActivated().rewindTo(slicedStates.get(0));
}
final ResolutionState<R, S> state = slicedStates.get(0);
getActivated().rewindTo(Objects.requireNonNullElse(state, INITIAL_STATE));
}

final ResolutionState<R, S> state = getState().orElseThrow();
Expand Down Expand Up @@ -540,13 +536,17 @@ private UnwindDetails<R, S> buildDetailsForUnwind() {

// Update the "requirementUnwoundToInstead" on any relevant unused unwinds.
for (final UnwindDetails<R, S> d : relevantUnusedUnwinds) {
@Nullable final R req = lastDetailForCurrentUnwind.getStateRequirement();
d.getRequirementsUnwoundToInstead().add(req);
final R req = lastDetailForCurrentUnwind.getStateRequirement();
if (req != null) {
d.getRequirementsUnwoundToInstead().add(req);
}
}

for (final UnwindDetails<R, S> d : unwindDetails) {
@Nullable final R req = lastDetailForCurrentUnwind.getStateRequirement();
d.getRequirementsUnwoundToInstead().add(req);
final R req = lastDetailForCurrentUnwind.getStateRequirement();
if (req != null) {
d.getRequirementsUnwoundToInstead().add(req);
}
}

return lastDetailForCurrentUnwind;
Expand Down Expand Up @@ -583,7 +583,7 @@ private List<UnwindDetails<R, S>> unwindOptionsForRequirements(final List<R> bin

// Next, look at the parent of this requirement, and check if the requirement could have been avoided
// if an alternative PossibilitySet had been chosen
@Nullable R parentR = parentOf(r);
R parentR = parentOf(r);
if (parentR != null) {
partialTree.add(0, parentR);
requirementState = findStateFor(parentR).orElseThrow();
Expand All @@ -595,7 +595,7 @@ private List<UnwindDetails<R, S>> unwindOptionsForRequirements(final List<R> bin

// Finally, look at the grandparent and up of this requirement, looking for any possibilities that
// wouldn't create their parent requirement
@Nullable R grandparentR = parentOf(parentR);
R grandparentR = parentOf(parentR);
while (grandparentR != null) {
partialTree.add(0, grandparentR);
requirementState = findStateFor(grandparentR).orElseThrow();
Expand Down Expand Up @@ -723,7 +723,7 @@ private void filterPossibilitiesForParentUnwind(final UnwindDetails<R, S> unwind
)
.toList();

final List<R> requirementsToAvoid = parentUnwinds.stream()
final List<@Nullable R> requirementsToAvoid = parentUnwinds.stream()
.flatMap(uw -> uw.subDependenciesToAvoid().stream())
.toList();

Expand Down Expand Up @@ -877,7 +877,7 @@ private Optional<R> requirementForExistingName(final String name) {
}

return this.states.stream()
.filter(state -> state.getName().equals(name))
.filter(state -> state != null && state.getName().equals(name))
.findFirst()
.flatMap(ResolutionState::getRequirement);

Expand All @@ -896,7 +896,7 @@ private Optional<ResolutionState<R, S>> findStateFor(@Nullable final R requireme
}

return this.states.stream()
.filter(state -> requirement.equals(state.getRequirement().orElse(null)))
.filter(state -> state != null && requirement.equals(state.getRequirement().orElse(null)))
.findFirst();
}

Expand Down Expand Up @@ -938,18 +938,18 @@ private Conflict<R, S> createConflict(@Nullable final RuntimeException underlyin

final Map<String, S> activatedByName = new HashMap<>();
for (final Vertex<Payload<R, S>, R> v : getActivated().getVertices().values()) {
if (v.getPayload().isPresent()) {
if (v.getPayload().isPresent() && v.getPayload().get().getPossibilitySet().getLatestVersion().isPresent()) {
activatedByName.put(v.getName(),
v.getPayload().get().getPossibilitySet().getLatestVersion().orElse(null));
v.getPayload().get().getPossibilitySet().getLatestVersion().get());
}
}

final R requirement = getRequirement().orElseThrow();
final Optional<S> existingSpecification =
vertex.getPayload().flatMap(payload -> payload.getPossibilitySet().getLatestVersion());
@Nullable final PossibilitySet<R, S> possibilitySet = getPossibilities().isEmpty()
? null
: getPossibilities().get(getPossibilities().size() - 1);
final PossibilitySet<R, S> possibilitySet = getPossibilities().isEmpty()
? null
: getPossibilities().get(getPossibilities().size() - 1);
final Conflict<R, S> conflict = new Conflict<>(requirement,
requirements,
existingSpecification.orElse(null),
Expand Down Expand Up @@ -984,7 +984,7 @@ private List<List<R>> requirementTrees() {
private List<R> requirementTreeFor(final R requirement) {
final List<R> tree = new ArrayList<>();

for (@Nullable R req = requirement; req != null; req = parentOf(req)) {
for (R req = requirement; req != null; req = parentOf(req)) {
tree.add(0, req);
}

Expand Down Expand Up @@ -1019,7 +1019,7 @@ private void indicateProgress() {
* @param format Output string passed to {@link String#format(String, Object...)}
* @param args Arguments for the output string passed to {@link String#format(String, Object...)}
*/
private void printf(final int depth, final String format, final Object... args) {
private void printf(final int depth, final String format, final @Nullable Object... args) {
this.resolverUi.printf(depth, format, args);
}

Expand Down Expand Up @@ -1159,10 +1159,11 @@ private void pushStateForRequirements(final Set<R> newRequirements, final boolea

final Function<R, Boolean> isRequirementUnique =
requirement -> this.states.stream()
.filter(Objects::nonNull)
.noneMatch(state -> Objects.equals(state.getRequirement().orElse(null),
requirement));

@Nullable R newRequirement;
R newRequirement;
do {
newRequirement = sortedRequirements.isEmpty() ? null : sortedRequirements.remove(0);
} while (newRequirement != null && !isRequirementUnique.apply(newRequirement));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cthing/molinillo/ResolutionState.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Map;
import java.util.Optional;

import javax.annotation.Nullable;
import org.jspecify.annotations.Nullable;


/**
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/cthing/molinillo/UI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cthing.molinillo;

import javax.annotation.Nullable;

import org.jspecify.annotations.Nullable;


/**
Expand Down Expand Up @@ -38,7 +39,7 @@ public interface UI {
* @param format Output string passed to {@link String#format(String, Object...)}
* @param args Arguments for the output string passed to {@link String#format(String, Object...)}
*/
void printf(int depth, String format, Object... args);
void printf(int depth, String format, @Nullable Object... args);

/**
* Indicates whether debug messages should be printed. If the {@code MOLINILLO_DEBUG} environment variable
Expand Down
Loading

0 comments on commit ead68c9

Please sign in to comment.