Skip to content

Commit

Permalink
Simplify generated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-schlichtherle committed Aug 18, 2023
1 parent 25b81fc commit c3382e5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
12 changes: 12 additions & 0 deletions java-sample/src/main/java/bali/java/sample/naming/Nameable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package bali.java.sample.naming;

public interface Nameable {

default String qualified() {
return getClass().getName();
}

default String simple() {
return getClass().getSimpleName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,7 @@
import bali.Module;

@Module
public interface NamingModule {

@SuppressWarnings("unused")
interface WhatsMyName<Complication> {

default String qualified() {
return getClass().getName();
}

default String simple() {
return getClass().getSimpleName();
}
}
public interface NamingModule extends Nameable {

WhatsMyName<String> name1();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package bali.java.sample.naming;

@SuppressWarnings("unused")
public interface WhatsMyName<T> extends Nameable {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class NamingModuleSpec extends AnyWordSpec {
val module = NamingModule$.new$

"yield the expected simple names" in {
module.simple shouldBe "NamingModule$$"
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")
}
Expand Down
5 changes: 2 additions & 3 deletions java/src/main/java/bali/java/TypeVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public Consumer<Output> visitModuleInterface4CompanionInterface(ModuleInterface
out
.nl()
.ad("static ").ad(m.getTypeParametersWithBoundsList()).ad(m.getLocalDeclaredType()).ad(" new$() {").nl()
.ad(" return new ").ad(m.getSimpleName()).ad("$$").ad(m.getTypeParametersWithoutBoundsList()).ad("() {").nl()
.ad(" };").nl()
.ad(" return new ").ad(m.getSimpleName()).ad("$$();").nl()
.ad("}").nl();
}
m.forAllModuleMethods4CompanionInterface().accept(out);
Expand All @@ -48,7 +47,7 @@ public Consumer<Output> visitModuleInterface4CompanionClass(ModuleInterface m) {
.ad("package ").ad(m.getPackageName()).ad(";").nl()
.nl()
.ad(m.generated()).nl()
.ad(m.getModifiers().toString()).ad("abstract class ").ad(m.getSimpleName()).ad(m.getTypeParametersWithBoundsList().isEmpty() ? "$$ " : "$$").ad(m.getTypeParametersWithBoundsList()).ad("implements ").ad(m.getSimpleName()).ad(m.getTypeParametersWithoutBoundsList().isEmpty() ? "$ " : "$").ad(m.getTypeParametersWithoutBoundsList()).ad("{").nl()
.ad(m.getModifiers().toString()).ad(m.hasAbstractMethods() ? "abstract " : "").ad("class ").ad(m.getSimpleName()).ad(m.getTypeParametersWithBoundsList().isEmpty() ? "$$ " : "$$").ad(m.getTypeParametersWithBoundsList()).ad("implements ").ad(m.getSimpleName()).ad(m.getTypeParametersWithoutBoundsList().isEmpty() ? "$ " : "$").ad(m.getTypeParametersWithoutBoundsList()).ad("{").nl()
.in();
m.forAllModuleMethods4CompanionClass().accept(out);
out.out().ad("}").nl();
Expand Down

0 comments on commit c3382e5

Please sign in to comment.