Skip to content

Commit

Permalink
ISSUES-36 improve setter names for the mock builder
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Dec 12, 2023
1 parent 1aad9b3 commit a686c23
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import io.jbock.simple.Inject;
import io.jbock.simple.Provides;
import io.jbock.simple.processor.util.ValidationFailure;
import io.jbock.simple.processor.util.Visitors;
import io.jbock.simple.processor.writing.NamedBinding;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand Down Expand Up @@ -39,13 +42,23 @@ public final class InjectBinding extends Binding {
if (element().getAnnotation(Provides.class) != null) {
return lowerFirst(removeMethodNamePrefix(element().getSimpleName().toString()));
}
TypeName typeName = key().typeName();
if (typeName instanceof ParameterizedTypeName) {
return lowerFirst(simpleTypeName((ParameterizedTypeName) typeName));
Element enclosing = element().getEnclosingElement();
if (enclosing != null && keyFactory().tool().isSameType(key().type(), enclosing.asType())) {
TypeElement enclosingOuter = Visitors.TYPE_ELEMENT_VISITOR.visit(enclosing.getEnclosingElement());
if (enclosingOuter != null && keyFactory().tool().isSameType(key().type(), enclosing.asType())) {
return lowerFirst(enclosingOuter.getSimpleName().toString() + simpleName(key().typeName()));
}
}
return lowerFirst(verySimpleTypeName(typeName.toString()));
return lowerFirst(simpleName(key().typeName()));
});

private String simpleName(TypeName typeName) {
if (typeName instanceof ParameterizedTypeName) {
return simpleTypeName((ParameterizedTypeName) typeName);
}
return verySimpleTypeName(typeName.toString());
}

private static String removeMethodNamePrefix(String s) {
for (String p : PROVIDES_METHOD_COMMON_PREFIXES) {
if (s.startsWith(p) && s.length() > p.length()) {
Expand Down
33 changes: 16 additions & 17 deletions compiler/src/test/java/io/jbock/simple/processor/BuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,15 @@ void builderParameter() {
"package test;",
"",
"public final class TestClass_AComponent_Impl implements TestClass.AComponent {",
" private final TestClass.B b;",
" private final TestClass.B testClassB;",
"",
" private TestClass_AComponent_Impl(TestClass.B b) {",
" this.b = b;",
" private TestClass_AComponent_Impl(TestClass.B testClassB) {",
" this.testClassB = testClassB;",
" }",
"",
" @Override",
" public TestClass.B getB() {",
" return b;",
" return testClassB;",
" }",
"",
" public static Builder_Impl builder() {",
Expand All @@ -193,34 +193,33 @@ void builderParameter() {
"",
" @Override",
" public TestClass.AComponent build() {",
" TestClass.A a = new TestClass.A(this.s);",
" TestClass.B b = new TestClass.B(a);",
" return new TestClass_AComponent_Impl(b);",
" TestClass.A testClassA = new TestClass.A(this.s);",
" TestClass.B testClassB = new TestClass.B(testClassA);",
" return new TestClass_AComponent_Impl(testClassB);",
" }",
" }",
"",
" public static final class MockBuilder {",
" private final String s;",
" private TestClass.A a;",
" private TestClass.B b;",
" private TestClass.A testClassA;",
" private TestClass.B testClassB;",
"",
" private MockBuilder(String s) {",
" this.s = s;",
" }",
"",
" public TestClass.AComponent build() {",
" TestClass.A a = this.a != null ? this.a : new TestClass.A(this.s);",
" TestClass.B b = this.b != null ? this.b : new TestClass.B(a);",
" return new TestClass_AComponent_Impl(b);",
" }",
" TestClass.A testClassA = this.testClassA != null ? this.testClassA : new TestClass.A(this.s);",
" TestClass.B testClassB = this.testClassB != null ? this.testClassB : new TestClass.B(testClassA);",
" return new TestClass_AComponent_Impl(testClassB);", " }",
"",
" public MockBuilder a(TestClass.A a) {",
" this.a = a;",
" public MockBuilder testClassA(TestClass.A testClassA) {",
" this.testClassA = testClassA;",
" return this;",
" }",
"",
" public MockBuilder b(TestClass.B b) {",
" this.b = b;",
" public MockBuilder testClassB(TestClass.B testClassB) {",
" this.testClassB = testClassB;",
" return this;",
" }",
" }",
Expand Down

0 comments on commit a686c23

Please sign in to comment.