/** * Given a Factory Name return the generic type parameters of the factory (The actual repository class, and its properties class)o */ public static Type[] getEnvironmentRepositoryFactoryTypeParams(ConfigurableListableBeanFactory beanFactory, String factoryName) { MethodMetadata methodMetadata = (MethodMetadata) beanFactory.getBeanDefinition(factoryName).getSource(); Class<?> factoryClass = null; try { factoryClass = Class.forName(methodMetadata.getReturnTypeName()); } catch (ClassNotFoundException e) { throw new IllegalStateException(e); } Optional<AnnotatedType> annotatedFactoryType = Arrays.stream(factoryClass.getAnnotatedInterfaces()) .filter(i -> { ParameterizedType parameterizedType = (ParameterizedType) i.getType(); return parameterizedType.getRawType().equals(EnvironmentRepositoryFactory.class); }).findFirst(); ParameterizedType factoryParameterizedType = (ParameterizedType) annotatedFactoryType .orElse(factoryClass.getAnnotatedSuperclass()).getType(); return factoryParameterizedType.getActualTypeArguments(); }
for (AnnotatedType t : consumer.getClass().getAnnotatedInterfaces()) { annotation = t.getAnnotation(MyTypeAnnotation.class); if (annotation != null) {
@Retention(RetentionPolicy.RUNTIME) public @interface X { String[] value(); } @X({"a", "b", "c"}) interface AnInterface {} public static class TestClass implements AnInterface {} public static void main(String[] args) { // annotations are not inherited, empty array System.out.println(Arrays.toString(TestClass.class.getAnnotations())); // check if TestClass is annotated with X and get X.value() Arrays.stream(TestClass.class.getAnnotatedInterfaces()) .filter(type -> type.getType().equals(AnInterface.class)) .map(type -> (Class<AnInterface>) type.getType()) .findFirst() .ifPresent(anInterface -> { String[] value = anInterface.getAnnotation(X.class).value(); System.out.println(Arrays.toString(value)); }); }
private static void determineValueExtractorDefinitions(List<AnnotatedType> valueExtractorDefinitions, Class<?> extractorImplementationType) { if ( !ValueExtractor.class.isAssignableFrom( extractorImplementationType ) ) { return; } Class<?> superClass = extractorImplementationType.getSuperclass(); if ( superClass != null && !Object.class.equals( superClass ) ) { determineValueExtractorDefinitions( valueExtractorDefinitions, superClass ); } for ( Class<?> implementedInterface : extractorImplementationType.getInterfaces() ) { if ( !ValueExtractor.class.equals( implementedInterface ) ) { determineValueExtractorDefinitions( valueExtractorDefinitions, implementedInterface ); } } for ( AnnotatedType annotatedInterface : extractorImplementationType.getAnnotatedInterfaces() ) { if ( ValueExtractor.class.equals( ReflectionHelper.getClassFromType( annotatedInterface.getType() ) ) ) { valueExtractorDefinitions.add( annotatedInterface ); } } }
Arrays.stream(c.getAnnotatedInterfaces()). map(a -> a.getType()). filter(a -> a instanceof ParameterizedType).
private void hierarchy(Consumer<ContainerElementKey> sink) { final TypeVariable<?> var; if (typeArgumentIndex == null) { var = null; } else { var = containerClass.getTypeParameters()[typeArgumentIndex.intValue()]; } final Lazy<Set<ContainerElementKey>> round = new Lazy<>(LinkedHashSet::new); Stream .concat(Stream.of(containerClass.getAnnotatedSuperclass()), Stream.of(containerClass.getAnnotatedInterfaces())) .filter(AnnotatedParameterizedType.class::isInstance).map(AnnotatedParameterizedType.class::cast) .forEach(t -> { final AnnotatedType[] args = ((AnnotatedParameterizedType) t).getAnnotatedActualTypeArguments(); for (int i = 0; i < args.length; i++) { final Type boundArgumentType = args[i].getType(); if (boundArgumentType instanceof Class<?> || boundArgumentType.equals(var)) { round.get().add(new ContainerElementKey(t, Integer.valueOf(i))); } } }); round.optional().ifPresent(s -> { s.forEach(sink); // recurse: s.forEach(k -> k.hierarchy(sink)); }); }
private void hierarchy(Consumer<ContainerElementKey> sink) { final TypeVariable<?> var; if (typeArgumentIndex == null) { var = null; } else { var = containerClass.getTypeParameters()[typeArgumentIndex.intValue()]; } final Lazy<Set<ContainerElementKey>> round = new Lazy<>(LinkedHashSet::new); Stream .concat(Stream.of(containerClass.getAnnotatedSuperclass()), Stream.of(containerClass.getAnnotatedInterfaces())) .filter(AnnotatedParameterizedType.class::isInstance).map(AnnotatedParameterizedType.class::cast) .forEach(t -> { final AnnotatedType[] args = ((AnnotatedParameterizedType) t).getAnnotatedActualTypeArguments(); for (int i = 0; i < args.length; i++) { final Type boundArgumentType = args[i].getType(); if (boundArgumentType instanceof Class<?> || boundArgumentType.equals(var)) { round.get().add(new ContainerElementKey(t, Integer.valueOf(i))); } } }); round.optional().ifPresent(s -> { s.forEach(sink); // recurse: s.forEach(k -> k.hierarchy(sink)); }); }
final Lazy<Set<ContainerElementKey>> result = new Lazy<>(HashSet::new); Stream.of(extractorType.getAnnotatedInterfaces()).filter(AnnotatedParameterizedType.class::isInstance) .map(AnnotatedParameterizedType.class::cast) .filter(apt -> ValueExtractor.class.equals(((ParameterizedType) apt.getType()).getRawType()))
final Lazy<Set<ContainerElementKey>> result = new Lazy<>(HashSet::new); Stream.of(extractorType.getAnnotatedInterfaces()).filter(AnnotatedParameterizedType.class::isInstance) .map(AnnotatedParameterizedType.class::cast) .filter(apt -> ValueExtractor.class.equals(((ParameterizedType) apt.getType()).getRawType()))
String serviceInterface = impementedClass[0].getAnnotatedInterfaces()[0].getType().getTypeName(); String genericsList = serviceInterface.substring(serviceInterface.indexOf('<') + 1, serviceInterface.indexOf('>')); String[] entityClassesToScan = genericsList.replaceAll("\\,", "").split(" ");
String serviceInterface = impementedClass[0].getAnnotatedInterfaces()[0].getType().getTypeName(); String genericsList = serviceInterface.substring(serviceInterface.indexOf('<') + 1, serviceInterface.indexOf('>')); String[] entityClassesToScan = genericsList.replaceAll("\\,", "").split(" ");
allAnnotatedInterfaces = javaClass.getAnnotatedInterfaces(); } catch (MalformedParameterizedTypeException | TypeNotPresentException | NoClassDefFoundError t) {
AnnotatedType[] superInterfaces = clazz.getAnnotatedInterfaces(); AnnotatedType superClass = clazz.getAnnotatedSuperclass();