diff --git a/src/main/java/com/firefly/dp/FactoryProducer.java b/src/main/java/com/firefly/dp/FactoryProducer.java index 9a540d5..f223fbe 100644 --- a/src/main/java/com/firefly/dp/FactoryProducer.java +++ b/src/main/java/com/firefly/dp/FactoryProducer.java @@ -2,28 +2,30 @@ import com.firefly.dp.annotations.FactoryMember; import com.firefly.dp.helper.FactoryMemberHelper; +import lombok.Setter; import java.lang.annotation.Annotation; import java.util.List; import java.util.Optional; import java.util.stream.Stream; +@Setter public class FactoryProducer { private List factoryMembers; - public T getImplemention(Class clazz, String value) { + public T getImplemention(Class clazz, String value) throws IllegalAccessException, InstantiationException { - List implementedClazz = new FactoryMemberHelper(this.factoryMembers, clazz).accumulateByParent(); - for (Object o : implementedClazz) { - Annotation[] annotations = o.getClass().getAnnotations(); + List implementedClazz = new FactoryMemberHelper(this.factoryMembers, clazz).accumulateByParent(); + for (Class o : implementedClazz) { + Annotation[] annotations = o.getAnnotations(); Optional first = Stream.of(annotations) .filter(annotation -> annotation.annotationType().equals(FactoryMember.class)) .findFirst(); if (first.isPresent()) { FactoryMember annotation = (FactoryMember) first.get(); if (annotation.key().equalsIgnoreCase(value)) - return (T) o; + return (T) o.newInstance(); } } diff --git a/src/main/java/com/firefly/dp/helper/FactoryMemberHelper.java b/src/main/java/com/firefly/dp/helper/FactoryMemberHelper.java index f719318..d45bac8 100644 --- a/src/main/java/com/firefly/dp/helper/FactoryMemberHelper.java +++ b/src/main/java/com/firefly/dp/helper/FactoryMemberHelper.java @@ -12,7 +12,7 @@ public class FactoryMemberHelper { private Class parentClass; - public List accumulateByParent() { + public List accumulateByParent() { return this.fireFlyClazz.stream() .filter(clazz -> parentClass.isAssignableFrom(clazz)) .collect(Collectors.toList());