/******************************************************************************************************************* * * Scans all the given role implementation classes and build a map of roles by owner class. * * @param roleImplementationTypes the types of role implementations to scan * ******************************************************************************************************************/ protected void scan (final @Nonnull Collection<Class<?>> roleImplementationTypes) { log.debug("scan({})", shortNames(roleImplementationTypes)); for (final Class<?> roleImplementationType : roleImplementationTypes) { for (final Class<?> datumType : findDatumTypesForRole(roleImplementationType)) { for (final Class<?> roleType : findAllImplementedInterfacesOf(roleImplementationType)) { if (!roleType.getName().equals("org.springframework.beans.factory.aspectj.ConfigurableObject")) { roleMapByDatumAndRole.add(new DatumAndRole(datumType, roleType), roleImplementationType); } } } } logRoles(); }
values.add(getBean(parameterType));
final Class<?> datumType = findTypeOf(datum); final List<ROLE_TYPE> roles = new ArrayList<>(); final Set<Class<? extends ROLE_TYPE>> roleImplementationTypes = findRoleImplementationsFor(datumType, roleType); contextType = findContextTypeForRole(roleImplementationType); log.trace(">>>> contexts: {}", shortIds(contextManager.getContexts())); final Object[] params = getParameterValues(parameterTypes, datumType, datum, contextType, context); roles.add(roleType.cast(constructor.newInstance(params))); break;
/******************************************************************************************************************* * * Finds all the interfaces implemented by a given class, including those eventually implemented by superclasses * and interfaces that are indirectly implemented (e.g. C implements I1, I1 extends I2). * * @param clazz the class to inspect * @return the implemented interfaces * ******************************************************************************************************************/ @Nonnull /* VisibleForTesting */ static SortedSet<Class<?>> findAllImplementedInterfacesOf (final @Nonnull Class<?> clazz) { final SortedSet<Class<?>> interfaces = new TreeSet<>(CLASS_COMPARATOR); interfaces.addAll(Arrays.asList(clazz.getInterfaces())); for (final Class<?> interface_ : interfaces) { interfaces.addAll(findAllImplementedInterfacesOf(interface_)); } if (clazz.getSuperclass() != null) { interfaces.addAll(findAllImplementedInterfacesOf(clazz.getSuperclass())); } return interfaces; }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test public void findTypeOf_must_return_orinal_class_for_Mockito_mocks() { // given final R1 nonMock = new RI1A(); final R2 mock = mock(R2.class); // when final Class<R1> nonMockType = RoleManagerSupport.findTypeOf(nonMock); final Class<R2> mockType = RoleManagerSupport.findTypeOf(mock); // then assertEquals(RI1A.class, nonMockType); assertEquals(R2.class, mockType); }
/******************************************************************************************************************* * * ******************************************************************************************************************/ private void logChanges (final @Nonnull DatumAndRole datumAndRole, final @Nonnull Set<Class<?>> before, final @Nonnull Set<Class<?>> after) { after.removeAll(before); if (!after.isEmpty()) { log.debug(">>>>>>> added implementations: {} -> {}", datumAndRole, after); if (log.isTraceEnabled()) // yes, trace { logRoles(); } } }
logChanges(datumAndRole, before, after);
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test(dataProvider = "classesAndExpectedInterfaces") public void must_correctly_find_implemented_interfaces (final @Nonnull Class<?> clazz, final @Nonnull Set<Class<?>> expectedInterfaces) { // given the mocks // when final SortedSet<Class<?>> actualInterfaces = RoleManagerSupport.findAllImplementedInterfacesOf(clazz); // then assertThat(actualInterfaces, is(expectedInterfaces)); }