Skip to content

Commit

Permalink
#164 - Detect when duplicate modules occurs and throw a nice error in…
Browse files Browse the repository at this point in the history
… this case
  • Loading branch information
rbygrave committed Dec 2, 2021
1 parent d58121f commit 625c75f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ProcessingContext {
private final Filer filer;
private final Elements elementUtils;
private final Types typeUtils;
private final Set<String> uniqueModuleNames = new HashSet<>();

ProcessingContext(ProcessingEnvironment processingEnv) {
this.processingEnv = processingEnv;
Expand Down Expand Up @@ -134,4 +135,14 @@ Element asElement(TypeMirror returnType) {
PackageElement getPackageOf(Element element) {
return elementUtils.getPackageOf(element);
}

void addModule(String moduleFullName) {
if (moduleFullName != null) {
uniqueModuleNames.add(moduleFullName);
}
}

boolean isDuplicateModule(String moduleFullName) {
return uniqueModuleNames.contains(moduleFullName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import io.avaje.inject.spi.DependencyMeta;

import javax.annotation.processing.FilerException;
import javax.lang.model.element.*;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.util.*;
Expand Down Expand Up @@ -183,8 +186,17 @@ private void initialiseModule() {
if (!moduleInitialised) {
try {
initialiseName(MetaTopPackage.of(metaData.values()));
context.addModule(moduleFullName);
moduleInitialised = true;
} catch (IOException e) {
if (context.isDuplicateModule(moduleFullName)) {
String msg = "Attempting to create 2 modules both called " + moduleFullName
+ ". This can occur when a custom scope (named from it's annotation) has a name clash with" +
" the default module which can be named from the package. Look to resolve this by either " +
"changing the name of the custom scope annotation, or explicitly naming the default scope " +
"using @InjectModule(name), or changing the top level package used by the default scope";
throw new IllegalStateException(msg);
}
context.logError("Failed to create module filer " + e.getMessage());
}
}
Expand Down

0 comments on commit 625c75f

Please sign in to comment.