Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

Adding error for fake complete=false module #371

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

import static dagger.Provides.Type.SET;
import static dagger.Provides.Type.SET_VALUES;
import static dagger.internal.Linker.ErrorHandler.NULL;
import static dagger.internal.codegen.Util.getAnnotation;
import static dagger.internal.codegen.Util.getPackage;
import static dagger.internal.codegen.Util.isInterface;
Expand Down Expand Up @@ -115,7 +116,9 @@ public final class GraphAnalysisProcessor extends AbstractProcessor {
if (annotation.get("complete").equals(Boolean.TRUE)) {
Map<String, Binding<?>> bindings;
try {
bindings = processCompleteModule(moduleType, false);
Linker.ErrorHandler errorHandler = new GraphAnalysisErrorHandler(processingEnv,
moduleType.getQualifiedName().toString());
bindings = processCompleteModule(moduleType, errorHandler);
new ProblemDetector().detectCircularDependencies(bindings.values());
} catch (ModuleValidationException e) {
error("Graph validation failed: " + e.getMessage(), e.source);
Expand All @@ -140,10 +143,24 @@ public final class GraphAnalysisProcessor extends AbstractProcessor {
.printMessage(Diagnostic.Kind.WARNING,
"Graph visualization failed. Please report this as a bug.\n\n" + sw, moduleType);
}
} else {
NonCompleteModuleErrorHandler errorHandler = new NonCompleteModuleErrorHandler();
try {
processCompleteModule(moduleType, errorHandler);
} catch (RuntimeException e) {
// Any error here is fine, we're not supposed to process real incomplete modules.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty loose. I suspect there's a better way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. That's mostly me not knowing what can jump out of there. I guess we don't like checked exceptions for some reason :) .

continue;
}
// A module with complete = false is expected to have graph validation errors.
// If there is none, this module should be declared complete.
if (errorHandler.shouldBeComplete()) {
error("A module with no missing binding not have complete = false", element);
}
}

if (annotation.get("library").equals(Boolean.FALSE)) {
Map<String, Binding<?>> bindings = processCompleteModule(moduleType, true);
Linker.ErrorHandler errorHandler = NULL;
Map<String, Binding<?>> bindings = processCompleteModule(moduleType, errorHandler);
try {
new ProblemDetector().detectUnusedBinding(bindings.values());
} catch (IllegalStateException e) {
Expand All @@ -159,14 +176,12 @@ private void error(String message, Element element) {
}

private Map<String, Binding<?>> processCompleteModule(TypeElement rootModule,
boolean ignoreCompletenessErrors) {
Linker.ErrorHandler errorHandler) {
Map<String, TypeElement> allModules = new LinkedHashMap<String, TypeElement>();
collectIncludesRecursively(rootModule, allModules, new LinkedList<String>());
ArrayList<GraphAnalysisStaticInjection> staticInjections =
new ArrayList<GraphAnalysisStaticInjection>();

Linker.ErrorHandler errorHandler = ignoreCompletenessErrors ? Linker.ErrorHandler.NULL
: new GraphAnalysisErrorHandler(processingEnv, rootModule.getQualifiedName().toString());
Linker linker = new Linker(null, new GraphAnalysisLoader(processingEnv), errorHandler);
// Linker requires synchronization for calls to requestBinding and linkAll.
// We know statically that we're single threaded, but we synchronize anyway
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dagger.internal.codegen;

import dagger.internal.Linker;
import java.util.List;

public class NonCompleteModuleErrorHandler implements Linker.ErrorHandler {

private boolean shouldBeComplete = true;

@Override public void handleErrors(List<String> errors) {
if (errors.size() > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be shortened to:

shouldBeComplete &= errors.isEmpty();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's so much easier to understand this way :) ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the very least you should be using isEmpty() in the conditional here.

shouldBeComplete = false;
}
}

public boolean shouldBeComplete() {
return shouldBeComplete;
}
}