Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Dec 18, 2024
1 parent f3052ab commit 8d4d134
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ default Stream<P> problems() {
@Nonnull
Stream<P> problems(BuilderProblem.Severity severity);

/**
* Creates a new child collector. Parent (this) instance will accumulate values for itself and all of its children.
*/
@Nonnull
ProblemCollector<P> createChild();

/**
* Attaches a child collector. Parent (this) instance will accumulate values for itself and all of its children.
*/
void addChild(ProblemCollector<P> child);

/**
* Returns true if collector is present in passed in collector or its children.
*/
boolean presentCollector(ProblemCollector<P> collector);

/**
* Creates "empty" problem collector.
*/
Expand All @@ -155,6 +171,21 @@ public boolean reportProblem(P problem) {
public Stream<P> problems(BuilderProblem.Severity severity) {
return Stream.empty();
}

@Override
public ProblemCollector<P> createChild() {
throw new IllegalStateException("empty problem collector");
}

@Override
public void addChild(ProblemCollector<P> child) {
throw new IllegalStateException("empty problem collector");
}

@Override
public boolean presentCollector(ProblemCollector<P> collector) {
return false;
}
};
}

Expand Down Expand Up @@ -186,6 +217,7 @@ class Impl<P extends BuilderProblem> implements ProblemCollector<P> {
private final AtomicInteger totalCount;
private final ConcurrentMap<BuilderProblem.Severity, LongAdder> counters;
private final ConcurrentMap<BuilderProblem.Severity, List<P>> problems;
private final CopyOnWriteArrayList<ProblemCollector<P>> childCollectors;

private static final List<BuilderProblem.Severity> REVERSED_ORDER = Arrays.stream(
BuilderProblem.Severity.values())
Expand All @@ -200,20 +232,34 @@ private Impl(int maxCountLimit) {
this.totalCount = new AtomicInteger();
this.counters = new ConcurrentHashMap<>();
this.problems = new ConcurrentHashMap<>();
this.childCollectors = new CopyOnWriteArrayList<>();
}

@Override
public int problemsReportedFor(BuilderProblem.Severity... severity) {
int result = 0;
for (BuilderProblem.Severity s : severity) {
result += getCounter(s).intValue();
for (ProblemCollector<P> childCollector : childCollectors) {
if (childCollector.hasProblemsFor(s)) {
result += childCollector.problemsReportedFor(s);
}
}
}
return result;
}

@Override
public boolean problemsOverflow() {
return totalCount.get() > maxCountLimit;
if (totalCount.get() > maxCountLimit) {
return true;
}
for (ProblemCollector<P> childCollector : childCollectors) {
if (childCollector.problemsOverflow()) {
return true;
}
}
return false;
}

@Override
Expand All @@ -231,7 +277,45 @@ public boolean reportProblem(P problem) {
@Override
public Stream<P> problems(BuilderProblem.Severity severity) {
requireNonNull(severity, "severity");
return getProblems(severity).stream();
Stream<P> result = getProblems(severity).stream();
for (ProblemCollector<P> childCollector : childCollectors) {
if (childCollector.hasProblemsFor(severity)) {
result = Stream.concat(result, childCollector.problems(severity));
}
}
return result;
}

@Override
public ProblemCollector<P> createChild() {
ProblemCollector<P> result = new Impl<>(maxCountLimit);
childCollectors.add(result);
return result;
}

@Override
public void addChild(ProblemCollector<P> child) {
requireNonNull(child, "child");
if (child == this) {
throw new IllegalArgumentException("child cannot be attached to itself");
}
if (presentCollector(child)) {
throw new IllegalArgumentException("child already present");
}
childCollectors.add(child);
}

@Override
public boolean presentCollector(ProblemCollector<P> collector) {
if (collector == this) {
return true;
}
for (ProblemCollector<P> childCollector : childCollectors) {
if (childCollector.presentCollector(collector)) {
return true;
}
}
return false;
}

private LongAdder getCounter(BuilderProblem.Severity severity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,9 @@ private ModelBuilderSessionState(
}

ModelBuilderSessionState derive(ModelSource source) {
return derive(source, new DefaultModelBuilderResult(result.getProblemCollector()));
return derive(
source,
new DefaultModelBuilderResult(result.getProblemCollector().createChild()));
}

ModelBuilderSessionState derive(ModelSource source, DefaultModelBuilderResult result) {
Expand All @@ -314,7 +316,9 @@ ModelBuilderSessionState derive(ModelSource source, DefaultModelBuilderResult re
* Creates a new session, sharing cached datas and propagating errors.
*/
ModelBuilderSessionState derive(ModelBuilderRequest request) {
return derive(request, new DefaultModelBuilderResult(result.getProblemCollector()));
return derive(
request,
new DefaultModelBuilderResult(result.getProblemCollector().createChild()));
}

ModelBuilderSessionState derive(ModelBuilderRequest request, DefaultModelBuilderResult result) {
Expand Down Expand Up @@ -780,7 +784,8 @@ private void loadFilePom(

DefaultModelBuilderResult cr = Objects.equals(top, subprojectFile)
? result
: new DefaultModelBuilderResult(ProblemCollector.create(session));
: new DefaultModelBuilderResult(
r.getProblemCollector().createChild());
if (request.isRecursive()) {
r.getChildren().add(cr);
}
Expand All @@ -791,6 +796,9 @@ private void loadFilePom(
// gathered with problem collector
add(Severity.ERROR, Version.V40, "Failed to load project " + pom, e);
}
if (!result.getProblemCollector().presentCollector(r.getProblemCollector())) {
result.getProblemCollector().addChild(r.getProblemCollector());
}
}

static <T> Set<T> concat(Set<T> a, T b) {
Expand Down Expand Up @@ -1713,6 +1721,9 @@ private Model doLoadDependencyManagement(
modelBuilderSession.buildEffectiveModel(importIds);
importResult = modelBuilderSession.result;
} catch (ModelBuilderException e) {
if (!result.getProblemCollector().presentCollector(e.getProblemCollector())) {
result.getProblemCollector().addChild(e.getProblemCollector());
}
return null;
}

Expand Down

0 comments on commit 8d4d134

Please sign in to comment.