From ceb7a5bfa3b2427574256c46ea1a5bd664e3ec7b Mon Sep 17 00:00:00 2001 From: Augustin Peyrard Date: Sun, 12 Apr 2015 14:21:25 +0200 Subject: [PATCH] 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` 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()) ``` --- .../restx/config/ConsolidatedConfigFactoryMachine.java | 5 +++-- .../restx/config/ElementsFromConfigFactoryMachine.java | 7 ++++--- .../java/restx/factory/DeactivationFactoryMachine.java | 5 +++-- .../src/main/java/restx/factory/DefaultFactoryMachine.java | 6 ++++-- restx-factory/src/main/java/restx/factory/Factory.java | 2 +- .../src/main/java/restx/factory/FactoryMachine.java | 3 ++- .../src/main/java/restx/factory/FactoryMachineWrapper.java | 6 ++++-- .../src/main/java/restx/factory/NoopFactoryMachine.java | 3 ++- .../main/java/restx/factory/SingleNameFactoryMachine.java | 6 ++++-- .../java/restx/factory/SystemPropertyFactoryMachine.java | 5 +++-- .../main/java/restx/factory/WarehouseProvidersMachine.java | 6 ++++-- .../src/main/java/restx/jongo/JongoCollectionFactory.java | 3 ++- 12 files changed, 36 insertions(+), 21 deletions(-) diff --git a/restx-factory/src/main/java/restx/config/ConsolidatedConfigFactoryMachine.java b/restx-factory/src/main/java/restx/config/ConsolidatedConfigFactoryMachine.java index 3105d8e33..0e7649422 100644 --- a/restx-factory/src/main/java/restx/config/ConsolidatedConfigFactoryMachine.java +++ b/restx-factory/src/main/java/restx/config/ConsolidatedConfigFactoryMachine.java @@ -6,6 +6,7 @@ import com.google.common.base.Optional; import com.google.common.collect.Iterables; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -98,8 +99,8 @@ public BillOfMaterials getBillOfMaterial() { @Override @SuppressWarnings("unchecked") - public Set> nameBuildableComponents(Class componentClass) { - if (componentClass == RestxConfig.class) { + public Set> nameBuildableComponents(Type componentType) { + if (componentType == RestxConfig.class) { return Collections.singleton(Name.of((Class) RestxConfig.class)); } else { return Collections.emptySet(); diff --git a/restx-factory/src/main/java/restx/config/ElementsFromConfigFactoryMachine.java b/restx-factory/src/main/java/restx/config/ElementsFromConfigFactoryMachine.java index 8dc8f64fe..f5206e675 100644 --- a/restx-factory/src/main/java/restx/config/ElementsFromConfigFactoryMachine.java +++ b/restx-factory/src/main/java/restx/config/ElementsFromConfigFactoryMachine.java @@ -6,6 +6,7 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Sets; +import java.lang.reflect.Type; import java.util.Collections; import java.util.Set; import restx.common.ConfigElement; @@ -69,8 +70,8 @@ protected T doNewComponent(SatisfiedBOM satisfiedBOM) { @Override @SuppressWarnings("unchecked") - public Set> nameBuildableComponents(Class componentClass) { - if (String.class == componentClass) { + public Set> nameBuildableComponents(Type componentType) { + if (String.class == componentType) { return (Set) Sets.newHashSet(Iterables.transform(config.elements(), new Function>() { @Override @@ -78,7 +79,7 @@ public Name apply(ConfigElement input) { return Name.of(String.class, input.getKey()); } })); - } else if (ConfigElement.class == componentClass) { + } else if (ConfigElement.class == componentType) { return (Set) Sets.newHashSet(Iterables.transform(config.elements(), new Function>() { @Override diff --git a/restx-factory/src/main/java/restx/factory/DeactivationFactoryMachine.java b/restx-factory/src/main/java/restx/factory/DeactivationFactoryMachine.java index 980593f1e..998ab2fa4 100644 --- a/restx-factory/src/main/java/restx/factory/DeactivationFactoryMachine.java +++ b/restx-factory/src/main/java/restx/factory/DeactivationFactoryMachine.java @@ -6,6 +6,7 @@ import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import java.lang.reflect.Type; import java.util.Set; public class DeactivationFactoryMachine implements FactoryMachine { @@ -45,8 +46,8 @@ protected T doNewComponent(SatisfiedBOM satisfiedBOM) { } @Override - public Set> nameBuildableComponents(Class componentClass) { - if (componentClass != String.class) { + public Set> nameBuildableComponents(Type componentType) { + if (componentType != String.class) { return ImmutableSet.of(); } return Sets.newLinkedHashSet(Iterables.transform(keys, new Function>() { diff --git a/restx-factory/src/main/java/restx/factory/DefaultFactoryMachine.java b/restx-factory/src/main/java/restx/factory/DefaultFactoryMachine.java index 8c35236f2..59a5924de 100644 --- a/restx-factory/src/main/java/restx/factory/DefaultFactoryMachine.java +++ b/restx-factory/src/main/java/restx/factory/DefaultFactoryMachine.java @@ -3,7 +3,9 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.Sets; +import java.lang.reflect.Type; import java.util.Set; +import restx.common.Types; /** * User: xavierhanin @@ -37,10 +39,10 @@ public MachineEngine getEngine(Name name) { @Override @SuppressWarnings("unchecked") - public Set> nameBuildableComponents(Class componentClass) { + public Set> nameBuildableComponents(Type componentType) { Set> names = Sets.newHashSet(); for (Name name : engines.keySet()) { - if (componentClass.isAssignableFrom(name.getClazz())) { + if (Types.isAssignableFrom(componentType, name.getType())) { names.add((Name) name); } } diff --git a/restx-factory/src/main/java/restx/factory/Factory.java b/restx-factory/src/main/java/restx/factory/Factory.java index 89ea81463..9bd957ecb 100644 --- a/restx-factory/src/main/java/restx/factory/Factory.java +++ b/restx-factory/src/main/java/restx/factory/Factory.java @@ -1274,7 +1274,7 @@ private BillOfMaterials getBillOfMaterialsFor(Name name) { private Set> nameBuildableComponents(FactoryMachine machine, Class componentClass) { Set> buildableComponents = new LinkedHashSet<>(); - for (Name tName : machine.nameBuildableComponents(componentClass)) { + for (Name tName : machine.nameBuildableComponents(componentClass)) { if (checkActive(tName)) { buildableComponents.add(tName); } diff --git a/restx-factory/src/main/java/restx/factory/FactoryMachine.java b/restx-factory/src/main/java/restx/factory/FactoryMachine.java index eeaedfe9e..52bd2680b 100644 --- a/restx-factory/src/main/java/restx/factory/FactoryMachine.java +++ b/restx-factory/src/main/java/restx/factory/FactoryMachine.java @@ -1,5 +1,6 @@ package restx.factory; +import java.lang.reflect.Type; import java.util.Set; /** @@ -10,6 +11,6 @@ public interface FactoryMachine { boolean canBuild(Name name); MachineEngine getEngine(Name name); - Set> nameBuildableComponents(Class componentClass); + Set> nameBuildableComponents(Type componentType); int priority(); } diff --git a/restx-factory/src/main/java/restx/factory/FactoryMachineWrapper.java b/restx-factory/src/main/java/restx/factory/FactoryMachineWrapper.java index 67af84c58..c2fce853f 100644 --- a/restx-factory/src/main/java/restx/factory/FactoryMachineWrapper.java +++ b/restx-factory/src/main/java/restx/factory/FactoryMachineWrapper.java @@ -5,6 +5,7 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import java.lang.reflect.Type; import java.util.List; import java.util.Map; import java.util.Set; @@ -108,8 +109,9 @@ public MachineEngine getEngine(Name name) { return original.getEngine(name); } - public Set> nameBuildableComponents(Class componentClass) { - return original.nameBuildableComponents(componentClass); + @Override + public Set> nameBuildableComponents(Type componentType) { + return original.nameBuildableComponents(componentType); } @Override diff --git a/restx-factory/src/main/java/restx/factory/NoopFactoryMachine.java b/restx-factory/src/main/java/restx/factory/NoopFactoryMachine.java index 5b730172a..d3622f3ec 100644 --- a/restx-factory/src/main/java/restx/factory/NoopFactoryMachine.java +++ b/restx-factory/src/main/java/restx/factory/NoopFactoryMachine.java @@ -1,5 +1,6 @@ package restx.factory; +import java.lang.reflect.Type; import java.util.Collections; import java.util.Set; @@ -23,7 +24,7 @@ public MachineEngine getEngine(Name name) { } @Override - public Set> nameBuildableComponents(Class componentClass) { + public Set> nameBuildableComponents(Type componentType) { return Collections.emptySet(); } diff --git a/restx-factory/src/main/java/restx/factory/SingleNameFactoryMachine.java b/restx-factory/src/main/java/restx/factory/SingleNameFactoryMachine.java index 060076903..508225bc2 100644 --- a/restx-factory/src/main/java/restx/factory/SingleNameFactoryMachine.java +++ b/restx-factory/src/main/java/restx/factory/SingleNameFactoryMachine.java @@ -1,7 +1,9 @@ package restx.factory; +import java.lang.reflect.Type; import java.util.Collections; import java.util.Set; +import restx.common.Types; /** * User: xavierhanin @@ -34,8 +36,8 @@ public MachineEngine getEngine(Name name) { @Override @SuppressWarnings("unchecked") - public Set> nameBuildableComponents(Class componentClass) { - if (componentClass.isAssignableFrom(name.getClazz())) { + public Set> nameBuildableComponents(Type componentType) { + if (Types.isAssignableFrom(componentType, name.getType())) { return Collections.singleton((Name) name); } else { return Collections.emptySet(); diff --git a/restx-factory/src/main/java/restx/factory/SystemPropertyFactoryMachine.java b/restx-factory/src/main/java/restx/factory/SystemPropertyFactoryMachine.java index b996ec654..3f8025a76 100644 --- a/restx-factory/src/main/java/restx/factory/SystemPropertyFactoryMachine.java +++ b/restx-factory/src/main/java/restx/factory/SystemPropertyFactoryMachine.java @@ -3,6 +3,7 @@ import com.google.common.base.Objects; import com.google.common.base.Optional; +import java.lang.reflect.Type; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; @@ -30,8 +31,8 @@ protected T doNewComponent(SatisfiedBOM satisfiedBOM) { @Override @SuppressWarnings("unchecked") - public Set> nameBuildableComponents(Class componentClass) { - if (componentClass != String.class) { + public Set> nameBuildableComponents(Type componentType) { + if (componentType != String.class) { return emptySet(); } Set> names = new LinkedHashSet<>(); diff --git a/restx-factory/src/main/java/restx/factory/WarehouseProvidersMachine.java b/restx-factory/src/main/java/restx/factory/WarehouseProvidersMachine.java index bf2216205..fad6574c0 100644 --- a/restx-factory/src/main/java/restx/factory/WarehouseProvidersMachine.java +++ b/restx-factory/src/main/java/restx/factory/WarehouseProvidersMachine.java @@ -6,8 +6,10 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; +import java.lang.reflect.Type; import java.util.LinkedHashSet; import java.util.Set; +import restx.common.Types; /** * Date: 16/11/13 @@ -95,11 +97,11 @@ private Optional> findComponent(Name name) { @Override @SuppressWarnings("unchecked") - public Set> nameBuildableComponents(Class componentClass) { + public Set> nameBuildableComponents(Type componentType) { Set> names = new LinkedHashSet<>(); for (Warehouse provider : providers) { for (Name name : provider.listNames()) { - if (componentClass.isAssignableFrom(name.getClazz())) { + if (Types.isAssignableFrom(componentType, name.getType())) { names.add((Name) name); } } diff --git a/restx-jongo/src/main/java/restx/jongo/JongoCollectionFactory.java b/restx-jongo/src/main/java/restx/jongo/JongoCollectionFactory.java index 7da11720d..f5f191cf3 100644 --- a/restx-jongo/src/main/java/restx/jongo/JongoCollectionFactory.java +++ b/restx-jongo/src/main/java/restx/jongo/JongoCollectionFactory.java @@ -3,6 +3,7 @@ import org.jongo.Jongo; import restx.factory.*; +import java.lang.reflect.Type; import java.util.Collections; import java.util.Set; @@ -39,7 +40,7 @@ public BillOfMaterials getBillOfMaterial() { } @Override - public Set> nameBuildableComponents(Class componentClass) { + public Set> nameBuildableComponents(Type componentType) { return Collections.emptySet(); }