Skip to content

Commit

Permalink
Fix - Receiving the Implentationm
Browse files Browse the repository at this point in the history
  • Loading branch information
itsaviu committed Jun 10, 2019
1 parent e38d9dc commit 91f2e74
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/main/java/com/firefly/dp/FactoryProducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Class> factoryMembers;

public <T> T getImplemention(Class<T> clazz, String value) {
public <T> T getImplemention(Class<T> clazz, String value) throws IllegalAccessException, InstantiationException {

List<Object> implementedClazz = new FactoryMemberHelper(this.factoryMembers, clazz).accumulateByParent();
for (Object o : implementedClazz) {
Annotation[] annotations = o.getClass().getAnnotations();
List<Class> implementedClazz = new FactoryMemberHelper(this.factoryMembers, clazz).accumulateByParent();
for (Class o : implementedClazz) {
Annotation[] annotations = o.getAnnotations();
Optional<Annotation> 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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class FactoryMemberHelper {

private Class parentClass;

public List<Object> accumulateByParent() {
public List<Class> accumulateByParent() {
return this.fireFlyClazz.stream()
.filter(clazz -> parentClass.isAssignableFrom(clazz))
.collect(Collectors.toList());
Expand Down

0 comments on commit 91f2e74

Please sign in to comment.