Skip to content

Commit ee1545f

Browse files
committed
refactor: use Conditional keyword instead of Alternative in processor
This will prepare the next step, brigging conditional provides methods, methods which are using @provides combined to @when (or module using the @when annotation at the class level).
1 parent 4ffb109 commit ee1545f

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

restx-factory/src/main/java/restx/factory/processor/FactoryAnnotationProcessor.java

+22-23
Original file line numberDiff line numberDiff line change
@@ -122,22 +122,24 @@ private void processModules(RoundEnvironment roundEnv) throws IOException {
122122
}
123123
}
124124

125-
AlternativeMethod m = new AlternativeMethod(
126-
exec.getReturnType().toString(),
125+
// the conditional component name, is the one specified in @Alternative annotation or the simple name of the produced class
126+
String componentName = !alternative.named().isEmpty() ? alternative.named() : alternativeTo.getSimpleName().toString();
127+
128+
ConditionalProviderMethod m = new ConditionalProviderMethod(
127129
alternativeTo.getQualifiedName().toString(),
128-
alternativeTo.getSimpleName().toString(),
130+
componentName,
129131
exec.getSimpleName().toString(),
130132
alternative.priority(),
131-
!alternative.named().isEmpty() ? Optional.of(alternative.named()) : Optional.<String>absent(),
132133
when.name(),
133134
when.value(),
135+
"Alternative",
134136
exec);
135137

136138
buildInjectableParams(exec, m.parameters);
137139

138140
buildCheckedExceptions(exec, m.exceptions);
139141

140-
module.alternativeMethods.add(m);
142+
module.conditionalProviderMethods.add(m);
141143
}
142144
}
143145
}
@@ -315,7 +317,7 @@ private Optional<String> getInjectionName(Named named) {
315317

316318
private void generateMachineFile(ModuleClass moduleClass) throws IOException {
317319
List<ImmutableMap<String, Object>> engines = Lists.newArrayList();
318-
List<ImmutableMap<String, Object>> alternativesEngines = Lists.newArrayList();
320+
List<ImmutableMap<String, Object>> conditionalsEngines = Lists.newArrayList();
319321

320322
for (ProviderMethod method : moduleClass.providerMethods) {
321323
engines.add(ImmutableMap.<String, Object>builder()
@@ -331,12 +333,11 @@ private void generateMachineFile(ModuleClass moduleClass) throws IOException {
331333
.build());
332334
}
333335

334-
for (AlternativeMethod method : moduleClass.alternativeMethods) {
335-
alternativesEngines.add(ImmutableMap.<String, Object>builder()
336+
for (ConditionalProviderMethod method : moduleClass.conditionalProviderMethods) {
337+
conditionalsEngines.add(ImmutableMap.<String, Object>builder()
336338
.put("componentType", method.componentType)
337-
.put("alternativeToComponentType", method.alternativeType)
338-
.put("alternativeToComponentName", method.injectionName.or(method.alternativeName))
339-
.put("alternativeToComponentSimpleName", method.alternativeName)
339+
.put("componentName", method.componentName)
340+
.put("conditionalFactoryMachineName", method.methodName + method.componentName + method.factoryMachineNameSuffix)
340341
.put("whenName", method.whenName)
341342
.put("whenValue", method.whenValue)
342343
.put("priority", method.priority)
@@ -355,7 +356,7 @@ private void generateMachineFile(ModuleClass moduleClass) throws IOException {
355356
.put("moduleType", moduleClass.name)
356357
.put("priority", moduleClass.priority)
357358
.put("engines", engines)
358-
.put("alternativesEngines", alternativesEngines)
359+
.put("conditionalsEngines", conditionalsEngines)
359360
.build();
360361

361362
generateJavaClass(moduleClass.fqcn + "FactoryMachine", moduleMachineTpl, ctx,
@@ -580,7 +581,7 @@ private static class ModuleClass {
580581
final String fqcn;
581582

582583
final List<ProviderMethod> providerMethods = Lists.newArrayList();
583-
final List<AlternativeMethod> alternativeMethods = Lists.newArrayList();
584+
final List<ConditionalProviderMethod> conditionalProviderMethods = Lists.newArrayList();
584585
final Element originatingElement;
585586
final String pack;
586587
final String name;
@@ -613,31 +614,29 @@ private static class ProviderMethod {
613614
}
614615
}
615616

616-
private static class AlternativeMethod {
617+
private static class ConditionalProviderMethod {
617618
final Element originatingElement;
618619
final String componentType;
619-
final String alternativeType;
620-
final String alternativeName;
620+
final String componentName;
621621
final String methodName;
622622
final int priority;
623-
final Optional<String> injectionName;
624623
final String whenName;
625624
final String whenValue;
625+
final String factoryMachineNameSuffix;
626626
final List<InjectableParameter> parameters = Lists.newArrayList();
627627
final List<String> exceptions = Lists.newArrayList();
628628

629-
AlternativeMethod(String componentType, String alternativeType,
630-
String alternativeName, String methodName, int priority, Optional<String> injectionName,
631-
String whenName, String whenValue, Element originatingElement) {
629+
ConditionalProviderMethod(String componentType,
630+
String componentName, String methodName, int priority,
631+
String whenName, String whenValue, String factoryMachineNameSuffix, Element originatingElement) {
632632
this.componentType = componentType;
633-
this.alternativeType = alternativeType;
634-
this.alternativeName = alternativeName;
633+
this.componentName = componentName;
635634
this.methodName = methodName;
636635
this.priority = priority;
637-
this.injectionName = injectionName;
638636
this.whenName = whenName;
639637
this.whenValue = whenValue;
640638
this.originatingElement = originatingElement;
639+
this.factoryMachineNameSuffix = factoryMachineNameSuffix;
641640
}
642641
}
643642

restx-factory/src/main/resources/restx/factory/processor/ModuleMachine.mustache

+8-8
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,18 @@ public class {{machine}} extends DefaultFactoryMachine {
3838
},
3939
{{/engines}}
4040

41-
{{#alternativesEngines}}
41+
{{#conditionalsEngines}}
4242
new StdMachineEngine<FactoryMachine>(
43-
Name.of(FactoryMachine.class, "{{methodName}}{{alternativeToComponentSimpleName}}Alternative"), 0, BoundlessComponentBox.FACTORY) {
43+
Name.of(FactoryMachine.class, "{{conditionalFactoryMachineName}}"), 0, BoundlessComponentBox.FACTORY) {
4444
private Factory.Query<String> query = Factory.Query.byName(Name.of(String.class, "{{whenName}}")).optional();
4545
4646
@Override
4747
protected FactoryMachine doNewComponent(SatisfiedBOM satisfiedBOM) {
4848
if (satisfiedBOM.getOne(query).isPresent()
4949
&& satisfiedBOM.getOne(query).get().getComponent().equals("{{whenValue}}")) {
50-
return new SingleNameFactoryMachine<{{alternativeToComponentType}}>({{priority}},
51-
new StdMachineEngine<{{alternativeToComponentType}}>(
52-
Name.of({{alternativeToComponentType}}.class, "{{alternativeToComponentName}}"),
50+
return new SingleNameFactoryMachine<{{componentType}}>({{priority}},
51+
new StdMachineEngine<{{componentType}}>(
52+
Name.of({{componentType}}.class, "{{componentName}}"),
5353
{{priority}}, BoundlessComponentBox.FACTORY) {
5454
{{queriesDeclarations}}
5555

@@ -61,7 +61,7 @@ public class {{machine}} extends DefaultFactoryMachine {
6161
}
6262

6363
@Override
64-
protected {{alternativeToComponentType}} doNewComponent(SatisfiedBOM satisfiedBOM) {
64+
protected {{componentType}} doNewComponent(SatisfiedBOM satisfiedBOM) {
6565
{{#exceptions}}
6666
try {
6767
{{/exceptions}}
@@ -70,7 +70,7 @@ public class {{machine}} extends DefaultFactoryMachine {
7070
);
7171
{{#exceptions}}
7272
} catch ({{exceptions}} e) {
73-
throw new ProvisionException("Could not create component {{alternativeToComponentName}}", e);
73+
throw new ProvisionException("Could not create component {{componentName}}", e);
7474
}
7575
{{/exceptions}}
7676
}
@@ -85,7 +85,7 @@ public class {{machine}} extends DefaultFactoryMachine {
8585
return BillOfMaterials.of(query);
8686
}
8787
},
88-
{{/alternativesEngines}}
88+
{{/conditionalsEngines}}
8989
});
9090
}
9191
}

0 commit comments

Comments
 (0)