Skip to content

Commit

Permalink
ISSUES-36 factory method returns factory interface (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 authored Dec 11, 2023
1 parent d7e904b commit b469a5d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,7 @@ TypeSpec generate() {
.addModifiers(FINAL)
.addSuperinterface(component.element().asType());
spec.addFields(getFields());
for (DependencyRequest r : component.requests()) {
MethodSpec.Builder method = MethodSpec.methodBuilder(r.requestingElement().getSimpleName().toString());
method.addStatement("return $L", sorted.get(r.key()).name());
method.returns(r.key().typeName());
method.addAnnotation(Override.class);
method.addModifiers(PUBLIC);
spec.addMethod(method.build());
}
spec.addMethods(generateGetters());
component.factoryElement().ifPresent(factory -> {
spec.addMethod(generateFactoryMethod(factory));
spec.addType(factoryImpl.generate(factory));
Expand Down Expand Up @@ -105,6 +98,19 @@ TypeSpec generate() {
return spec.build();
}

private List<MethodSpec> generateGetters() {
List<MethodSpec> result = new ArrayList<>(component.requests().size());
for (DependencyRequest r : component.requests()) {
MethodSpec.Builder method = MethodSpec.methodBuilder(r.requestingElement().getSimpleName().toString());
method.addStatement("return $L", sorted.get(r.key()).name());
method.returns(r.key().typeName());
method.addAnnotation(Override.class);
method.addModifiers(PUBLIC);
result.add(method.build());
}
return result;
}

private String getComments() {
String version = Objects.toString(getClass().getPackage().getImplementationVersion(), "");
return "https://github.com/jbock-java/simple-component" + (version.isEmpty() ? "" : " " + version);
Expand All @@ -114,7 +120,7 @@ private MethodSpec generateFactoryMethod(FactoryElement factory) {
MethodSpec.Builder spec = MethodSpec.methodBuilder(FACTORY_METHOD)
.addModifiers(STATIC)
.addModifiers(modifiers)
.returns(factory.generatedClass());
.returns(TypeName.get(factory.element().asType()));
spec.addStatement("return new $T()", factory.generatedClass());
return spec.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void noParameters() {
" return a;",
" }",
"",
" static Factory_Impl factory() {",
" static TestClass.AComponent.Factory factory() {",
" return new Factory_Impl();",
" }",
"",
Expand Down Expand Up @@ -105,7 +105,7 @@ void factoryParameterIdentity() {
" return s;",
" }",
"",
" public static Factory_Impl factory() {",
" public static TestClass.AComponent.Factory factory() {",
" return new Factory_Impl();",
" }",
"",
Expand Down Expand Up @@ -160,7 +160,7 @@ void factoryParameter() {
" return a;",
" }",
"",
" static Factory_Impl factory() {",
" static TestClass.AComponent.Factory factory() {",
" return new Factory_Impl();",
" }",
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void qualifiedIdentity() {
" return a;",
" }",
"",
" static Factory_Impl factory() {",
" static TestClass.AComponent.Factory factory() {",
" return new Factory_Impl();",
" }",
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void qualifiedIdentity() {
" return a;",
" }",
"",
" static Factory_Impl factory() {",
" static TestClass.AComponent.Factory factory() {",
" return new Factory_Impl();",
" }",
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void providesPrimitive() {
" return a;",
" }",
"",
" public static Factory_Impl factory() {",
" public static TestClass.AComponent.Factory factory() {",
" return new Factory_Impl();",
" }",
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void qualifiedIdentity() {
" return a;",
" }",
"",
" static Factory_Impl factory() {",
" static TestClass.AComponent.Factory factory() {",
" return new Factory_Impl();",
" }",
"",
Expand Down

0 comments on commit b469a5d

Please sign in to comment.