protected static void validateResourceInjectionPointType(Class<?> resourceType, InjectionPoint injectionPoint) { Class<?> injectionPointRawType = Reflections.getRawType(injectionPoint.getType()); HierarchyDiscovery discovery = new HierarchyDiscovery(resourceType); for (Type type : discovery.getTypeClosure()) { if (Reflections.getRawType(type).equals(injectionPointRawType)) { return; } } // type autoboxing if (resourceType.isPrimitive() && BOXED_TYPES.get(resourceType).equals(injectionPointRawType)) { return; } else if (injectionPointRawType.isPrimitive() && BOXED_TYPES.get(injectionPointRawType).equals(resourceType)) { return; } throw BeanLogger.LOG.invalidResourceProducerType(injectionPoint.getAnnotated(), resourceType.getName()); }
private static boolean isAssignableFrom(ParameterizedType type1, Class<?> type2) { Class<?> rawType1 = Reflections.getRawType(type1); // raw types have to be assignable if (!isAssignableFrom(rawType1, type2)) { return false; } // this is a raw type with missing type arguments if (!Types.getCanonicalType(type2).equals(type2)) { return true; } return matches(type1, new HierarchyDiscovery(type2)); }
if (Types.isArray(observedType) && Types.isArray(eventType)) { final Type observedComponentType = Types.getArrayComponentType(observedType); for (Type type : new HierarchyDiscovery(Types.getArrayComponentType(eventType)).getTypeClosure()) { if (matchesNoBoxing(observedComponentType, type)) { return true; return observedType.equals(Reflections.getRawType(eventType));
protected void discoverTypes(Type type, boolean rawGeneric) { if (!rawGeneric) { rawGeneric = Types.isRawGenericType(type); } if (type instanceof Class<?>) { Class<?> clazz = (Class<?>) type; this.types.put(clazz, clazz); discoverFromClass(clazz, rawGeneric); } else if (rawGeneric) { discoverTypes(Reflections.getRawType(type), rawGeneric); } else if (type instanceof GenericArrayType) { GenericArrayType arrayType = (GenericArrayType) type; Type genericComponentType = arrayType.getGenericComponentType(); Class<?> rawComponentType = Reflections.getRawType(genericComponentType); if (rawComponentType != null) { Class<?> arrayClass = Array.newInstance(rawComponentType, 0).getClass(); this.types.put(arrayClass, type); discoverFromClass(arrayClass, rawGeneric); } } else if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type rawType = (parameterizedType).getRawType(); if (rawType instanceof Class<?>) { Class<?> clazz = (Class<?>) rawType; processTypeVariables(clazz.getTypeParameters(), parameterizedType.getActualTypeArguments()); this.types.put(clazz, type); discoverFromClass(clazz, rawGeneric); } } }
protected Type getEventType(Class<?> runtimeType) { Type resolvedType = runtimeType; if (Types.containsTypeVariable(resolvedType)) { /* * If the container is unable to resolve the parameterized type of the event object, it uses the specified type to infer the parameterized type of the event types. */ resolvedType = injectionPointTypeHierarchy.resolveType(resolvedType); } if (Types.containsTypeVariable(resolvedType)) { /* * Examining the hierarchy of the specified type did not help. This may still be one of the cases when combining the * event type and the specified type reveals the actual values for type variables. Let's try that. */ Type canonicalEventType = Types.getCanonicalType(runtimeType); TypeResolver objectTypeResolver = new EventObjectTypeResolverBuilder(injectionPointTypeHierarchy.getResolver() .getResolvedTypeVariables(), new HierarchyDiscovery(canonicalEventType).getResolver() .getResolvedTypeVariables()).build(); resolvedType = objectTypeResolver.resolveType(canonicalEventType); } return resolvedType; }
this.observerMethod = initMethodInjectionPoint(observer, declaringBean, manager); EnhancedAnnotatedParameter<?, ? super X> eventParameter = observer.getEnhancedParameters(Observes.class).get(0); this.eventType = new HierarchyDiscovery(declaringBean.getBeanClass()).resolveType(eventParameter.getBaseType()); this.id = createId(observer, declaringBean); this.bindings = manager.getServices().get(SharedObjectCache.class) transactionPhase = ObserverFactory.getTransactionalPhase(observer); ImmutableSet.Builder<WeldInjectionPointAttributes<?, ?>> injectionPoints = ImmutableSet.builder(); for (ParameterInjectionPoint<?, ?> injectionPoint : observerMethod.getParameterInjectionPoints()) { if (injectionPoint instanceof SpecialParameterInjectionPoint) { continue; injectionPoints.add(injectionPoint); this.injectionPoints = injectionPoints.build(); this.isStatic = observer.isStatic(); this.eventMetadataRequired = initMetadataRequired(this.injectionPoints);
ImmutableSet.Builder<Type> types = ImmutableSet.builder(); HierarchyDiscovery beanClassDiscovery = HierarchyDiscovery.forNormalizedType(ejbDescriptor.getBeanClass()); for (BusinessInterfaceDescriptor<?> businessInterfaceDescriptor : ejbDescriptor.getLocalBusinessInterfaces()) { Type resolvedLocalInterface = beanClassDiscovery.resolveType(Types.getCanonicalType(businessInterfaceDescriptor.getInterface())); SessionBeanHierarchyDiscovery interfaceDiscovery = new SessionBeanHierarchyDiscovery(resolvedLocalInterface); if (beanClassDiscovery.getTypeMap().containsKey(businessInterfaceDescriptor.getInterface())) { types.addAll(getTypedTypes(typeMap, annotated.getJavaClass(), annotated.getAnnotation(Typed.class))); } else { typeMap.put(Object.class, Object.class); types.addAll(typeMap.values()); return getLegalBeanTypes(types.build(), annotated);
@Override public BeanAttributesConfigurator<T> addTransitiveTypeClosure(Type type) { checkArgumentNotNull(type); this.types.addAll(Beans.getLegalBeanTypes(new HierarchyDiscovery(type).getTypeClosure(), type)); return this; }
public ContextBean(ContextHolder<T> contextHolder, BeanManagerImpl beanManager) { super(new StringBeanIdentifier(BeanIdentifiers.forBuiltInBean(beanManager, contextHolder.getType(), null)), beanManager, contextHolder.getType()); this.context = contextHolder.getContext(); this.types = HierarchyDiscovery.forNormalizedType(contextHolder.getType()).getTypeClosure(); this.qualifiers = contextHolder.getQualifiers(); }
private boolean isRouteHandler(AnnotatedType<?> annotatedType) { if (!Reflections.isTopLevelOrStaticNestedClass(annotatedType.getJavaClass())) { LOGGER.warn("Ignoring {0} - class annotated with @WebRoute must be top-level or static nested class", annotatedType.getJavaClass()); return false; } Set<Type> types = new HierarchyDiscovery(annotatedType.getBaseType()).getTypeClosure(); for (Type type : types) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; if (parameterizedType.getRawType().equals(Handler.class)) { Type[] arguments = parameterizedType.getActualTypeArguments(); if (arguments.length == 1 && arguments[0].equals(RoutingContext.class)) { return true; } } } } LOGGER.warn("Ignoring {0} - class annotated with @WebRoute must implement io.vertx.core.Handler<RoutingContext>", annotatedType.getJavaClass()); return false; }
protected void discoverFromClass(Class<?> clazz, boolean rawGeneric) { if (clazz.getSuperclass() != null) { discoverTypes(processAndResolveType(clazz.getGenericSuperclass(), clazz.getSuperclass()), rawGeneric); } discoverInterfaces(clazz, rawGeneric); }
private static boolean matches(ParameterizedType type1, HierarchyDiscovery type2) { for (Type type : type2.getTypeClosure()) { if (type instanceof ParameterizedType && matches(type1, (ParameterizedType) type)) { return true; } } return false; }
public HierarchyDiscovery(Type type, TypeResolver resolver) { this.types = new HashMap<Class<?>, Type>(); this.resolver = resolver; this.resolvedTypeVariables = resolver.getResolvedTypeVariables(); discoverTypes(type, false); this.typeClosure = ImmutableSet.copyOf(types.values()); }
protected Type processAndResolveType(Type superclass, Class<?> rawSuperclass) { if (superclass instanceof ParameterizedType) { ParameterizedType parameterizedSuperclass = (ParameterizedType) superclass; processTypeVariables(rawSuperclass.getTypeParameters(), parameterizedSuperclass.getActualTypeArguments()); return resolveType(parameterizedSuperclass); } else if (superclass instanceof Class<?>) { // this is not a parameterized type, nothing to resolve return superclass; } throw new RuntimeException("Unexpected type: " + superclass); }
protected void discoverInterfaces(Class<?> clazz, boolean rawGeneric) { Type[] genericInterfaces = clazz.getGenericInterfaces(); Class<?>[] interfaces = clazz.getInterfaces(); if (genericInterfaces.length == interfaces.length) { // this branch should execute every time! for (int i = 0; i < interfaces.length; i++) { discoverTypes(processAndResolveType(genericInterfaces[i], interfaces[i]), rawGeneric); } } }
private EventImpl(InjectionPoint injectionPoint, BeanManagerImpl beanManager) { super(injectionPoint, null, beanManager); this.injectionPointTypeHierarchy = new HierarchyDiscovery(getType()); this.cachedObservers = new ConcurrentHashMap<Class<?>, CachedObservers>(DEFAULT_CACHE_CAPACITY); }
private void processTypeVariable(TypeVariable<?> variable, Type value) { if (value instanceof TypeVariable<?>) { value = resolveType(value); } this.resolvedTypeVariables.put(variable, value); }
private void discoverBusinessInterfaces(Map<Class<?>, Class<?>> typeToBusinessInterfaceMap, Set<Class<?>> businessInterfaces) { for (Class<?> businessInterfaceClass : businessInterfaces) { for (Class<?> type : HierarchyDiscovery.forNormalizedType(businessInterfaceClass).getTypeMap().keySet()) { typeToBusinessInterfaceMap.put(type, businessInterfaceClass); } } } }
private void processTypeVariables(TypeVariable<?>[] variables, Type[] values) { for (int i = 0; i < variables.length; i++) { processTypeVariable(variables[i], values[i]); } }
public HierarchyDiscovery(Type type, TypeResolver resolver) { this.types = new HashMap<Class<?>, Type>(); this.resolver = resolver; this.resolvedTypeVariables = resolver.getResolvedTypeVariables(); discoverTypes(type, false); }