Skip to content

Commit

Permalink
Fix ambiguity in generated code if the module is the root package.
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-schlichtherle committed Aug 18, 2023
1 parent ce871fc commit f3098b2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class NamingModuleSpec extends AnyWordSpec {

"yield the expected simple names" in {
module.simple shouldBe "NamingModule$$"
module.name1.simple shouldBe "WhatsMyName"
module.name2.simple shouldBe "WhatsMyName"
module.name1.simple shouldBe "WhatsMyName$"
module.name2.simple shouldBe "WhatsMyName$"
}

"yield the expected qualified names" in {
module.qualified should endWith(".NamingModule$$")
module.name1.qualified should endWith(".NamingModule$$1WhatsMyName")
module.name2.qualified should endWith(".NamingModule$$2WhatsMyName")
module.name1.qualified should endWith(".NamingModule$$1WhatsMyName$")
module.name2.qualified should endWith(".NamingModule$$2WhatsMyName$")
}
}
}
11 changes: 7 additions & 4 deletions java/src/main/java/bali/java/AnnotationProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,12 @@ ExecutableElement getMethodElement() {

abstract class ModuleMethod extends Method {

@Getter(lazy = true)
private final Name localMakeElementName =
getMakeElementPackage().equals(getPackageElement())
? getMakeElement().getSimpleName()
: ((QualifiedNameable) getMakeElement()).getQualifiedName();

@Getter(lazy = true)
private final String localMakeType =
getMakeElementPackage().equals(getPackageElement())
Expand All @@ -406,9 +412,6 @@ abstract class ModuleMethod extends Method {
@Getter(lazy = true)
private final PackageElement makeElementPackage = packageOf(getMakeElement());

@Getter(lazy = true)
private final Name makeElementQualifiedName = ((QualifiedNameable) getMakeElement()).getQualifiedName();

@Getter(lazy = true)
private final Name makeElementSimpleName = getMakeElement().getSimpleName();

Expand Down Expand Up @@ -560,7 +563,7 @@ private String resolveAccessedElementRef() {
return isParameterRef()
? getModuleParamName().toString()
: isSuperRef()
? (isMakeTypeInterface() ? getMakeElementQualifiedName() + "." : "") + "super." + getMethodName() + "(" + getMethodParametersWithoutTypesList() + ")"
? (isMakeTypeInterface() ? getLocalMakeElementName() + "." : "") + "super." + getMethodName() + "(" + getMethodParametersWithoutTypesList() + ")"
: getAccessedElement().map(Tuple2::getT1).orElseGet(ModuleInterface.this::getElement).getSimpleName()
+ (isStaticRef() ? "$" : "$.this")
+ (isModuleRef() ? "" : "." + (isFieldRef() ? getModuleFieldName() + "" : getModuleMethodName() + "(" + getMethodParametersWithoutTypesList() + ")"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private Consumer<Output> visitMethodBegin1(ModuleMethod m) {
.ad("default ").ad(m.getMethodSignatureWithoutModifiers()).ad("{").nl()
.in();
if (m.isMakeTypeAbstract()) {
out.ad("final class ").ad(m.getMakeElementSimpleName()).ad(m.isMakeTypeInterface() ? " implements " : " extends ").ad(m.getMakeType().toString()).ad(" {").nl().in();
out.ad("final class ").ad(m.getMakeElementSimpleName()).ad("$").ad(m.isMakeTypeInterface() ? " implements " : " extends ").ad(m.getMakeType().toString()).ad(" {").nl().in();
m.forAllComponentMethods().accept(out);
out.out().ad("}").nl();
}
Expand Down
2 changes: 1 addition & 1 deletion java/src/main/java/bali/java/MethodVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface MethodVisitor {

default Consumer<Output> visitModuleMethod4CompanionInterface(ModuleMethod m) {
return visitMethodBegin(m)
.andThen(out -> out.ad("new ").ad(m.isMakeTypeAbstract() ? m.getMakeElementSimpleName() : m.getLocalMakeType()).ad("()"))
.andThen(out -> out.ad("new ").ad(m.isMakeTypeAbstract() ? m.getMakeElementSimpleName() + "$" : m.getLocalMakeType()).ad("()"))
.andThen(visitMethodEnd(m));
}

Expand Down

0 comments on commit f3098b2

Please sign in to comment.