Skip to content

Commit ceb7a5b

Browse files
committed
breaking: FactoryMachine.nameBuildableComponents use Type
This commit is preparing the field to be able to request the factory using types. As names are from now on a couple (String, Type), factory machines might be requested using types instead of classes. Migration has to be done in all sub-classes of FactoryMachine which are overriding the nameBuildableComponents method. There is several cases: - Either in the method you are using the `componentClass` parameter to do some equality assertions with other classes like that: ```java if (componentClass == SomeClass.class) { ... } ``` In that case the only thing to do is to change the type of the parameter from `Class<T>` to Type, it should work directly as Type is a super type of Class (you might also refactor the parameter name from componentClass to componentType, but this is up to you). - Or in the method you are using the `componentClass` parameter to compare it to other classes using `Class.isassignableFrom` method (in either way, `componentClass.isAssignableFrom(SomeClass.class)` or `SomeClass.class.isAssignableFrom(componentClass)`). In that case you have to replace the use of `Class.isAssignableFrom` to the static method `Types.isAssignableFrom`. - Or last but not least, you are doing some comparisons using `isAssignableFrom` like in the second point, but not on direct classes, but on classes of names, using `Name.getClazz()`. So you have to rely on names types instead of names classes, and do a migration like this: ```java componentClass.isAssignableFrom(aName.getClazz()) ``` becomes ```java Types.isAssignableFrom(componentType, aName.getType()) ```
1 parent 460715b commit ceb7a5b

12 files changed

+36
-21
lines changed

restx-factory/src/main/java/restx/config/ConsolidatedConfigFactoryMachine.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.google.common.base.Optional;
77
import com.google.common.collect.Iterables;
88

9+
import java.lang.reflect.Type;
910
import java.util.ArrayList;
1011
import java.util.Collections;
1112
import java.util.List;
@@ -98,8 +99,8 @@ public BillOfMaterials getBillOfMaterial() {
9899

99100
@Override
100101
@SuppressWarnings("unchecked")
101-
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
102-
if (componentClass == RestxConfig.class) {
102+
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
103+
if (componentType == RestxConfig.class) {
103104
return Collections.singleton(Name.of((Class<T>) RestxConfig.class));
104105
} else {
105106
return Collections.emptySet();

restx-factory/src/main/java/restx/config/ElementsFromConfigFactoryMachine.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.google.common.collect.Iterables;
77
import com.google.common.collect.Sets;
88

9+
import java.lang.reflect.Type;
910
import java.util.Collections;
1011
import java.util.Set;
1112
import restx.common.ConfigElement;
@@ -69,16 +70,16 @@ protected T doNewComponent(SatisfiedBOM satisfiedBOM) {
6970

7071
@Override
7172
@SuppressWarnings("unchecked")
72-
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
73-
if (String.class == componentClass) {
73+
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
74+
if (String.class == componentType) {
7475
return (Set) Sets.newHashSet(Iterables.transform(config.elements(),
7576
new Function<ConfigElement, Name<String>>() {
7677
@Override
7778
public Name<String> apply(ConfigElement input) {
7879
return Name.of(String.class, input.getKey());
7980
}
8081
}));
81-
} else if (ConfigElement.class == componentClass) {
82+
} else if (ConfigElement.class == componentType) {
8283
return (Set) Sets.newHashSet(Iterables.transform(config.elements(),
8384
new Function<ConfigElement, Name<ConfigElement>>() {
8485
@Override

restx-factory/src/main/java/restx/factory/DeactivationFactoryMachine.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.google.common.collect.Lists;
77
import com.google.common.collect.Sets;
88

9+
import java.lang.reflect.Type;
910
import java.util.Set;
1011

1112
public class DeactivationFactoryMachine implements FactoryMachine {
@@ -45,8 +46,8 @@ protected T doNewComponent(SatisfiedBOM satisfiedBOM) {
4546
}
4647

4748
@Override
48-
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
49-
if (componentClass != String.class) {
49+
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
50+
if (componentType != String.class) {
5051
return ImmutableSet.of();
5152
}
5253
return Sets.newLinkedHashSet(Iterables.transform(keys, new Function<String, Name<T>>() {

restx-factory/src/main/java/restx/factory/DefaultFactoryMachine.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.google.common.collect.ImmutableMap;
44
import com.google.common.collect.Sets;
55

6+
import java.lang.reflect.Type;
67
import java.util.Set;
8+
import restx.common.Types;
79

810
/**
911
* User: xavierhanin
@@ -37,10 +39,10 @@ public <T> MachineEngine<T> getEngine(Name<T> name) {
3739

3840
@Override
3941
@SuppressWarnings("unchecked")
40-
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
42+
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
4143
Set<Name<T>> names = Sets.newHashSet();
4244
for (Name<?> name : engines.keySet()) {
43-
if (componentClass.isAssignableFrom(name.getClazz())) {
45+
if (Types.isAssignableFrom(componentType, name.getType())) {
4446
names.add((Name<T>) name);
4547
}
4648
}

restx-factory/src/main/java/restx/factory/Factory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ private <T> BillOfMaterials getBillOfMaterialsFor(Name<T> name) {
12741274

12751275
private <T> Set<Name<T>> nameBuildableComponents(FactoryMachine machine, Class<T> componentClass) {
12761276
Set<Name<T>> buildableComponents = new LinkedHashSet<>();
1277-
for (Name<T> tName : machine.nameBuildableComponents(componentClass)) {
1277+
for (Name<T> tName : machine.<T>nameBuildableComponents(componentClass)) {
12781278
if (checkActive(tName)) {
12791279
buildableComponents.add(tName);
12801280
}

restx-factory/src/main/java/restx/factory/FactoryMachine.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package restx.factory;
22

3+
import java.lang.reflect.Type;
34
import java.util.Set;
45

56
/**
@@ -10,6 +11,6 @@
1011
public interface FactoryMachine {
1112
boolean canBuild(Name<?> name);
1213
<T> MachineEngine<T> getEngine(Name<T> name);
13-
<T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass);
14+
<T> Set<Name<T>> nameBuildableComponents(Type componentType);
1415
int priority();
1516
}

restx-factory/src/main/java/restx/factory/FactoryMachineWrapper.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.common.collect.Lists;
66
import com.google.common.collect.Maps;
77

8+
import java.lang.reflect.Type;
89
import java.util.List;
910
import java.util.Map;
1011
import java.util.Set;
@@ -108,8 +109,9 @@ public <T> MachineEngine<T> getEngine(Name<T> name) {
108109
return original.getEngine(name);
109110
}
110111

111-
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
112-
return original.nameBuildableComponents(componentClass);
112+
@Override
113+
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
114+
return original.nameBuildableComponents(componentType);
113115
}
114116

115117
@Override

restx-factory/src/main/java/restx/factory/NoopFactoryMachine.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package restx.factory;
22

3+
import java.lang.reflect.Type;
34
import java.util.Collections;
45
import java.util.Set;
56

@@ -23,7 +24,7 @@ public <T> MachineEngine<T> getEngine(Name<T> name) {
2324
}
2425

2526
@Override
26-
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
27+
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
2728
return Collections.emptySet();
2829
}
2930

restx-factory/src/main/java/restx/factory/SingleNameFactoryMachine.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package restx.factory;
22

3+
import java.lang.reflect.Type;
34
import java.util.Collections;
45
import java.util.Set;
6+
import restx.common.Types;
57

68
/**
79
* User: xavierhanin
@@ -34,8 +36,8 @@ public <T> MachineEngine<T> getEngine(Name<T> name) {
3436

3537
@Override
3638
@SuppressWarnings("unchecked")
37-
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
38-
if (componentClass.isAssignableFrom(name.getClazz())) {
39+
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
40+
if (Types.isAssignableFrom(componentType, name.getType())) {
3941
return Collections.singleton((Name<T>) name);
4042
} else {
4143
return Collections.emptySet();

restx-factory/src/main/java/restx/factory/SystemPropertyFactoryMachine.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.base.Objects;
44
import com.google.common.base.Optional;
55

6+
import java.lang.reflect.Type;
67
import java.util.LinkedHashSet;
78
import java.util.Map;
89
import java.util.Set;
@@ -30,8 +31,8 @@ protected T doNewComponent(SatisfiedBOM satisfiedBOM) {
3031

3132
@Override
3233
@SuppressWarnings("unchecked")
33-
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
34-
if (componentClass != String.class) {
34+
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
35+
if (componentType != String.class) {
3536
return emptySet();
3637
}
3738
Set<Name<T>> names = new LinkedHashSet<>();

0 commit comments

Comments
 (0)