@Test(expected = SeedException.class) @SuppressWarnings("unchecked") public void binding_definitions_with_duplicate_keys_via_qualifier_should_not_work() { resolveBindingDefinitions(TestType.class, CollectionQualifiedTestType2.class, CollectionQualifiedTestType2.class); }
@SuppressWarnings({"unchecked", "rawtypes"}) public static <T> Map<Key<T>, Class<? extends T>> resolveBindingDefinitions(Class<T> injecteeClass, Class<? extends T> firstImplClass, Class<? extends T>... restImplClasses) { Map<Key<T>, Class<? extends T>> typeLiterals = new HashMap<>(); List<Class<? extends T>> subClasses = new ArrayList<>(); if (firstImplClass != null) { subClasses.add(firstImplClass); } if (restImplClasses != null && restImplClasses.length > 0) { subClasses.addAll(Arrays.asList(restImplClasses)); } for (Class<? extends T> subClass : subClasses) { if (isBindable(subClass)) { Type resolvedType = TypeToken.of(subClass).getSupertype((Class) injecteeClass).getType(); TypeLiteral<T> parentTypeLiteral; if (resolvedType == null) { parentTypeLiteral = TypeLiteral.get(injecteeClass); } else { parentTypeLiteral = (TypeLiteral<T>) TypeLiteral.get(resolvedType); } Optional<Annotation> qualifier = Annotations.on(subClass) .traversingSuperclasses() .traversingInterfaces() .findAll() .filter(AnnotationPredicates.annotationAnnotatedWith(Qualifier.class, false)) .findFirst(); Key<T> key = qualifier.map(annotation -> Key.get(parentTypeLiteral, annotation)).orElseGet( () -> Key.get(parentTypeLiteral)); if (typeLiterals.containsKey(key)) { throw SeedException.createNew(CoreErrorCode.DUPLICATED_BINDING_KEY) .put("duplicatedKey", key) .put("firstClass", subClass.getName()) .put("secondClass", typeLiterals.get(key).getName()); } typeLiterals.put(key, subClass); } } return typeLiterals; }
BindingUtils { @SuppressWarnings({"unchecked", "rawtypes"}) public static <T> Map<Key<T>, Class<? extends T>> resolveBindingDefinitions(Class<T> injecteeClass, Class<? extends T> firstImplClass, Class<? extends T>... restImplClasses) { Map<Key<T>, Class<? extends T>> typeLiterals = new HashMap<>(); List<Class<? extends T>> subClasses = new ArrayList<>(); if (firstImplClass != null) { subClasses.add(firstImplClass); } if (restImplClasses != null && restImplClasses.length > 0) { subClasses.addAll(Arrays.asList(restImplClasses)); } for (Class<? extends T> subClass : subClasses) { if (isBindable(subClass)) { Type resolvedType = TypeToken.of(subClass).getSupertype((Class) injecteeClass).getType(); TypeLiteral<T> parentTypeLiteral; if (resolvedType == null) { parentTypeLiteral = TypeLiteral.get(injecteeClass); } else { parentTypeLiteral = (TypeLiteral<T>) TypeLiteral.get(resolvedType); } Optional<Annotation> qualifier = Annotations.on(subClass) .traversingSuperclasses() .traversingInterfaces() .findAll() .filter(AnnotationPredicates.annotationAnnotatedWith(Qualifier.class, false)) .findFirst(); Key<T> key = qualifier.map(annotation -> Key.get(parentTypeLiteral, annotation)).orElseGet( () -> Key.get(parentTypeLiteral)); if (typeLiterals.containsKey(key)) { throw SeedException.createNew(CoreErrorCode.DUPLICATED_BINDING_KEY) .put("duplicatedKey", key) .put("firstClass", subClass.getName()) .put("secondClass", typeLiterals.get(key).getName()); } typeLiterals.put(key, subClass); } } return typeLiterals; } }
BindingUtils { @SuppressWarnings({"unchecked", "rawtypes"}) public static <T> Map<Key<T>, Class<? extends T>> resolveBindingDefinitions(Class<T> injecteeClass, Class<? extends T> firstImplClass, Class<? extends T>... restImplClasses) { Map<Key<T>, Class<? extends T>> typeLiterals = new HashMap<>(); List<Class<? extends T>> subClasses = new ArrayList<>(); if (firstImplClass != null) { subClasses.add(firstImplClass); } if (restImplClasses != null && restImplClasses.length > 0) { subClasses.addAll(Arrays.asList(restImplClasses)); } for (Class<? extends T> subClass : subClasses) { if (isBindable(subClass)) { Type resolvedType = TypeToken.of(subClass).getSupertype((Class) injecteeClass).getType(); TypeLiteral<T> parentTypeLiteral; if (resolvedType == null) { parentTypeLiteral = TypeLiteral.get(injecteeClass); } else { parentTypeLiteral = (TypeLiteral<T>) TypeLiteral.get(resolvedType); } Optional<Annotation> qualifier = Annotations.on(subClass) .traversingSuperclasses() .traversingInterfaces() .findAll() .filter(AnnotationPredicates.annotationAnnotatedWith(Qualifier.class, false)) .findFirst(); Key<T> key = qualifier.map(annotation -> Key.get(parentTypeLiteral, annotation)).orElseGet( () -> Key.get(parentTypeLiteral)); if (typeLiterals.containsKey(key)) { throw SeedException.createNew(CoreErrorCode.DUPLICATED_BINDING_KEY) .put("duplicatedKey", key) .put("firstClass", subClass.getName()) .put("secondClass", typeLiterals.get(key).getName()); } typeLiterals.put(key, subClass); } } return typeLiterals; } private  BindingUtils(); }
BindingUtils { @SuppressWarnings({"unchecked", "rawtypes"}) public static <T> Map<Key<T>, Class<? extends T>> resolveBindingDefinitions(Class<T> injecteeClass, Class<? extends T> firstImplClass, Class<? extends T>... restImplClasses) { Map<Key<T>, Class<? extends T>> typeLiterals = new HashMap<>(); List<Class<? extends T>> subClasses = new ArrayList<>(); if (firstImplClass != null) { subClasses.add(firstImplClass); } if (restImplClasses != null && restImplClasses.length > 0) { subClasses.addAll(Arrays.asList(restImplClasses)); } for (Class<? extends T> subClass : subClasses) { if (isBindable(subClass)) { Type resolvedType = TypeToken.of(subClass).getSupertype((Class) injecteeClass).getType(); TypeLiteral<T> parentTypeLiteral; if (resolvedType == null) { parentTypeLiteral = TypeLiteral.get(injecteeClass); } else { parentTypeLiteral = (TypeLiteral<T>) TypeLiteral.get(resolvedType); } Optional<Annotation> qualifier = Annotations.on(subClass) .traversingSuperclasses() .traversingInterfaces() .findAll() .filter(AnnotationPredicates.annotationAnnotatedWith(Qualifier.class, false)) .findFirst(); Key<T> key = qualifier.map(annotation -> Key.get(parentTypeLiteral, annotation)).orElseGet( () -> Key.get(parentTypeLiteral)); if (typeLiterals.containsKey(key)) { throw SeedException.createNew(CoreErrorCode.DUPLICATED_BINDING_KEY) .put("duplicatedKey", key) .put("firstClass", subClass.getName()) .put("secondClass", typeLiterals.get(key).getName()); } typeLiterals.put(key, subClass); } } return typeLiterals; } private  BindingUtils(); @SuppressWarnings({"unchecked", "rawtypes"}) static Map<Key<T>, Class<? extends T>> resolveBindingDefinitions(Class<T> injecteeClass,
            Class<? extends T> firstImplClass, Class<? extends T>... restImplClasses); @SuppressWarnings("unchecked") static Map<Key<T>, Class<? extends T>> resolveBindingDefinitions(Class<T> injecteeClass,
            Collection<Class<? extends T>> implClasses); static boolean isBindable(Class<?> subClass); @SuppressWarnings("unchecked") static Key<T> resolveKey(Class<T> injecteeClass, Class<? extends T> genericImplClass,
            Type... typeVariableClasses); static boolean isProxy(Class<?> proxyClass); static Class<?> cleanProxy(Class<?> toClean); }
BindingUtils { @SuppressWarnings({"unchecked", "rawtypes"}) public static <T> Map<Key<T>, Class<? extends T>> resolveBindingDefinitions(Class<T> injecteeClass, Class<? extends T> firstImplClass, Class<? extends T>... restImplClasses) { Map<Key<T>, Class<? extends T>> typeLiterals = new HashMap<>(); List<Class<? extends T>> subClasses = new ArrayList<>(); if (firstImplClass != null) { subClasses.add(firstImplClass); } if (restImplClasses != null && restImplClasses.length > 0) { subClasses.addAll(Arrays.asList(restImplClasses)); } for (Class<? extends T> subClass : subClasses) { if (isBindable(subClass)) { Type resolvedType = TypeToken.of(subClass).getSupertype((Class) injecteeClass).getType(); TypeLiteral<T> parentTypeLiteral; if (resolvedType == null) { parentTypeLiteral = TypeLiteral.get(injecteeClass); } else { parentTypeLiteral = (TypeLiteral<T>) TypeLiteral.get(resolvedType); } Optional<Annotation> qualifier = Annotations.on(subClass) .traversingSuperclasses() .traversingInterfaces() .findAll() .filter(AnnotationPredicates.annotationAnnotatedWith(Qualifier.class, false)) .findFirst(); Key<T> key = qualifier.map(annotation -> Key.get(parentTypeLiteral, annotation)).orElseGet( () -> Key.get(parentTypeLiteral)); if (typeLiterals.containsKey(key)) { throw SeedException.createNew(CoreErrorCode.DUPLICATED_BINDING_KEY) .put("duplicatedKey", key) .put("firstClass", subClass.getName()) .put("secondClass", typeLiterals.get(key).getName()); } typeLiterals.put(key, subClass); } } return typeLiterals; } private  BindingUtils(); @SuppressWarnings({"unchecked", "rawtypes"}) static Map<Key<T>, Class<? extends T>> resolveBindingDefinitions(Class<T> injecteeClass,
            Class<? extends T> firstImplClass, Class<? extends T>... restImplClasses); @SuppressWarnings("unchecked") static Map<Key<T>, Class<? extends T>> resolveBindingDefinitions(Class<T> injecteeClass,
            Collection<Class<? extends T>> implClasses); static boolean isBindable(Class<?> subClass); @SuppressWarnings("unchecked") static Key<T> resolveKey(Class<T> injecteeClass, Class<? extends T> genericImplClass,
            Type... typeVariableClasses); static boolean isProxy(Class<?> proxyClass); static Class<?> cleanProxy(Class<?> toClean);  }