Skip to content

Commit 1c7cd5d

Browse files
committed
generate public factory and builder method when component is public
1 parent 4a494e7 commit 1c7cd5d

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

compiler/src/main/java/io/jbock/simple/processor/writing/ComponentImpl.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ private ComponentImpl(
4848
}
4949

5050
TypeSpec generate() {
51-
TypeSpec.Builder spec = TypeSpec.classBuilder(component.generatedClass()).addSuperinterface(component.element().asType());
52-
spec.addModifiers(component.element().getModifiers().stream()
53-
.filter(m -> m == PUBLIC || m == PROTECTED).toArray(Modifier[]::new));
51+
Modifier[] modifiers = component.element().getModifiers().stream()
52+
.filter(m -> m == PUBLIC).toArray(Modifier[]::new);
53+
TypeSpec.Builder spec = TypeSpec.classBuilder(component.generatedClass())
54+
.addModifiers(modifiers)
55+
.addSuperinterface(component.element().asType());
5456
MethodSpec.Builder constructor = MethodSpec.constructorBuilder().addModifiers(PRIVATE);
5557
for (NamedBinding namedBinding : sorted.values()) {
5658
Binding b = namedBinding.binding();
@@ -73,16 +75,13 @@ TypeSpec generate() {
7375
method.addStatement("return $L", sorted.get(r.key()).name());
7476
method.returns(r.key().typeName());
7577
method.addAnnotation(Override.class);
76-
method.addModifiers(r.requestingElement().getModifiers().stream()
77-
.filter(m -> m == PUBLIC || m == PROTECTED)
78-
.collect(Collectors.toList()));
78+
method.addModifiers(PUBLIC);
7979
spec.addMethod(method.build());
8080
}
8181
component.factoryElement().ifPresent(factory -> {
8282
spec.addMethod(MethodSpec.methodBuilder("factory")
8383
.addModifiers(STATIC)
84-
.addModifiers(component.element().getModifiers().stream()
85-
.filter(m -> m == PUBLIC).toArray(Modifier[]::new))
84+
.addModifiers(modifiers)
8685
.returns(TypeName.get(factory.element().asType()))
8786
.addStatement("return new $T()", factory.generatedClass())
8887
.build());
@@ -91,8 +90,7 @@ TypeSpec generate() {
9190
component.builderElement().ifPresent(builder -> {
9291
spec.addMethod(MethodSpec.methodBuilder("builder")
9392
.addModifiers(STATIC)
94-
.addModifiers(component.element().getModifiers().stream()
95-
.filter(m -> m == PUBLIC).toArray(Modifier[]::new))
93+
.addModifiers(modifiers)
9694
.returns(TypeName.get(builder.element().asType()))
9795
.addStatement("return new $T()", builder.generatedClass())
9896
.build());
@@ -101,6 +99,7 @@ TypeSpec generate() {
10199
if (component.factoryElement().isEmpty() && component.builderElement().isEmpty()) {
102100
spec.addMethod(MethodSpec.methodBuilder("create")
103101
.addModifiers(STATIC)
102+
.addModifiers(modifiers)
104103
.returns(TypeName.get(component.element().asType()))
105104
.addStatement("return new $T()", component.generatedClass())
106105
.build());

compiler/src/test/java/io/jbock/simple/processor/BuilderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void factoryParameterIdentity() {
7777
"final class TestClass {",
7878
"",
7979
" @Component",
80-
" interface AComponent {",
80+
" public interface AComponent {",
8181
" String getS();",
8282
"",
8383
" @Component.Builder",
@@ -93,7 +93,7 @@ void factoryParameterIdentity() {
9393
.containsLines(
9494
"package test;",
9595
"",
96-
"final class TestClass_AComponent_Impl implements TestClass.AComponent {",
96+
"public final class TestClass_AComponent_Impl implements TestClass.AComponent {",
9797
" private final String s;",
9898
"",
9999
" private TestClass_AComponent_Impl(String s) {",
@@ -105,7 +105,7 @@ void factoryParameterIdentity() {
105105
" return s;",
106106
" }",
107107
"",
108-
" static TestClass.AComponent.Builder builder() {",
108+
" public static TestClass.AComponent.Builder builder() {",
109109
" return new Builder_Impl();",
110110
" }",
111111
"",

compiler/src/test/java/io/jbock/simple/processor/FactoryTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void factoryParameterIdentity() {
7777
"final class TestClass {",
7878
"",
7979
" @Component",
80-
" interface AComponent {",
80+
" public interface AComponent {",
8181
" String getS();",
8282
"",
8383
" @Component.Factory",
@@ -92,7 +92,7 @@ void factoryParameterIdentity() {
9292
.containsLines(
9393
"package test;",
9494
"",
95-
"final class TestClass_AComponent_Impl implements TestClass.AComponent {",
95+
"public final class TestClass_AComponent_Impl implements TestClass.AComponent {",
9696
" private final String s;",
9797
"",
9898
" private TestClass_AComponent_Impl(String s) {",
@@ -104,7 +104,7 @@ void factoryParameterIdentity() {
104104
" return s;",
105105
" }",
106106
"",
107-
" static TestClass.AComponent.Factory factory() {",
107+
" public static TestClass.AComponent.Factory factory() {",
108108
" return new Factory_Impl();",
109109
" }",
110110
"",

compiler/src/test/java/io/jbock/simple/processor/ProcessorComponentTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void staticMethodBindings() {
3737
" }",
3838
"",
3939
" @Component",
40-
" interface AComponent {",
40+
" public interface AComponent {",
4141
" A getA();",
4242
" }",
4343
"}");
@@ -48,7 +48,7 @@ void staticMethodBindings() {
4848
.containsLines(
4949
"package test;",
5050
"",
51-
"final class TestClass_AComponent_Impl implements TestClass.AComponent {",
51+
"public final class TestClass_AComponent_Impl implements TestClass.AComponent {",
5252
" private final TestClass.A a;",
5353
"",
5454
" private TestClass_AComponent_Impl() {",
@@ -61,6 +61,10 @@ void staticMethodBindings() {
6161
" public TestClass.A getA() {",
6262
" return a;",
6363
" }",
64+
"",
65+
" public static TestClass.AComponent create() {",
66+
" return new TestClass_AComponent_Impl();",
67+
" }",
6468
"}");
6569
}
6670

0 commit comments

Comments
 (0)