/** * 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(); }
Class<?> c = o.getClass(); AnnotatedType type = c.getAnnotatedSuperclass(); System.out.println(Arrays.toString(type.getAnnotations()));
import java.lang.annotation.*; public class AnnotationTest { @Target(ElementType.TYPE_USE) @Retention(RetentionPolicy.RUNTIME) @interface First { } @Target(ElementType.TYPE_USE) @Retention(RetentionPolicy.RUNTIME) @interface Second { } class A { } class B extends @First @Second A { } public static void main(String[] args) { Annotation[] anns = B.class.getAnnotatedSuperclass().getAnnotations(); System.out.printf("There are %d annotations on B's use of its superclass.%n", anns.length); for (Annotation a : anns) System.out.println(a.annotationType().getName()); } }
public TO getTo() throws Exception{ if (to == null) { try{ to = ((Class<TO>)((ParameterizedType)this.getClass(). getGenericSuperclass()).getActualTypeArguments()[0]).newInstance(); }catch(ClassCastException cce){ cce.printStackTrace(); to = ((Class<TO>)((ParameterizedType)(((Class<TO>) this.getClass().getAnnotatedSuperclass().getType()).getGenericSuperclass())) .getActualTypeArguments()[0]).newInstance(); } } return to; }
private AnnotatedType getSourceType() { return GenericTypeReflector.getTypeParameter(getClass().getAnnotatedSuperclass(), AbstractTypeAdapter.class.getTypeParameters()[0]); } }
private AnnotatedType getTypeArguments(int index) { return GenericTypeReflector.getTypeParameter(getClass().getAnnotatedSuperclass(), CachingMapper.class.getTypeParameters()[index]); } }
public AbstractTypeSubstitutingMapper() { substituteType = GenericTypeReflector.getTypeParameter(getClass().getAnnotatedSuperclass(), AbstractTypeSubstitutingMapper.class.getTypeParameters()[0]); }
List<?> token = new ArrayList<@NonNull String>() {}; fullType("", token.getClass().getAnnotatedSuperclass());
public TO getTo() throws Exception { if(to == null) { try { to = ((Class<TO>) ((ParameterizedType) this.getClass().getGenericSuperclass()) .getActualTypeArguments()[0]).newInstance(); } catch(ClassCastException cce) { cce.printStackTrace(); to = ((Class<TO>) ((ParameterizedType) (((Class<TO>) this.getClass() .getAnnotatedSuperclass().getType()).getGenericSuperclass())) .getActualTypeArguments()[0]).newInstance(); } } return to; }
private AnnotatedType extractType() { AnnotatedType t = getClass().getAnnotatedSuperclass(); if (!(t instanceof AnnotatedParameterizedType)) { throw new RuntimeException("Invalid TypeToken; must specify type parameters"); } AnnotatedParameterizedType pt = (AnnotatedParameterizedType) t; if (((ParameterizedType) pt.getType()).getRawType() != TypeToken.class) { throw new RuntimeException("Invalid TypeToken; must directly extend TypeToken"); } return pt.getAnnotatedActualTypeArguments()[0]; }
/** * Gets the POJO Rules to be used by the RuleBook via reflection of the specified package. * @return a List of POJO Rules */ protected List<Class<?>> getPojoRules() { Reflections reflections = new Reflections(_package); List<Class<?>> rules = reflections .getTypesAnnotatedWith(com.deliveredtechnologies.rulebook.annotation.Rule.class).stream() .filter(rule -> rule.getAnnotatedSuperclass() != null) // Include classes only, exclude interfaces, etc. .filter(rule -> _subPkgMatch.test(rule.getPackage().getName())) .collect(Collectors.toList()); rules.sort(comparingInt(aClass -> getAnnotation(com.deliveredtechnologies.rulebook.annotation.Rule.class, aClass).order())); return rules; } }
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)); }); }
annotatedSuperclass = javaClass.getAnnotatedSuperclass(); } catch (MalformedParameterizedTypeException | TypeNotPresentException | NoClassDefFoundError t) {
AnnotatedType superClass = clazz.getAnnotatedSuperclass();