Skip to content

Commit 8d78654

Browse files
committed
Allows asClass in conditional components
Modify the parameter given to the template, in order to manage forced types.
1 parent b55740d commit 8d78654

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

restx-factory-testing/src/test/java/restx/factory/conditional/ConditionalTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
import org.junit.BeforeClass;
1414
import org.junit.Test;
1515

16+
import java.util.Comparator;
1617
import java.util.Set;
1718
import restx.factory.Factory;
1819
import restx.factory.Name;
20+
import restx.factory.NamedComponent;
21+
import restx.factory.TestGreeting;
1922
import restx.factory.conditional.components.TestConditionalComponent;
2023
import restx.factory.conditional.components.TestInterfaces;
2124
import restx.factory.conditional.components.TestModuleWithConditional;
@@ -124,4 +127,14 @@ public void should_find_conditional_component_when_condition_is_satisfied() {
124127
conditional = factory.queryByName(Name.of(TestConditionalComponent.class, "conditional")).findOneAsComponent();
125128
assertThat(conditional.isPresent()).isTrue();
126129
}
130+
131+
@Test
132+
public void should_use_asClass_parameter_for_conditional_components() {
133+
overrideComponents().set("allow.comparator", "true");
134+
135+
Factory factory = Factory.newInstance();
136+
Optional<NamedComponent<Comparator>> one = factory.queryByClass(Comparator.class).findOne();
137+
assertThat(one.isPresent());
138+
assertThat(one.get().getName().getClazz()).isEqualTo(Comparator.class);
139+
}
127140
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package restx.factory.conditional.components;
2+
3+
/**
4+
* @author apeyrard
5+
*/
6+
7+
import java.util.Comparator;
8+
import javax.inject.Named;
9+
import restx.factory.Component;
10+
import restx.factory.When;
11+
12+
@Component(asClass = Comparator.class)
13+
@Named("test.comparator")
14+
@When(name = "allow.comparator", value = "true")
15+
public class TestConditionalAsClass implements Comparator {
16+
@Override
17+
public int compare(Object o1, Object o2) {
18+
return 0;
19+
}
20+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ private void generateMachineFile(ComponentClass componentClass, When when) throw
464464
.put("machine", componentClass.name + "FactoryMachine")
465465
.put("imports", ImmutableList.of(componentClass.fqcn))
466466
.put("componentType", componentClass.name)
467-
.put("componentInjectionType", componentClass.name)
467+
.put("componentInjectionType", componentClass.producedName)
468468
.put("priority", String.valueOf(componentClass.priority))
469469
.put("whenName", when.name())
470470
.put("whenValue", when.value())

0 commit comments

Comments
 (0)