@Override public T getInstance() { return cast(reference.getInstance()); }
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()); }
if (ij.getAnnotated().getAnnotation(New.class) != null && ij.getQualifiers().size() > 1) { throw ValidatorLogger.LOG.newWithQualifiers(ij, Formats.formatAsStackTraceElement(ij)); if (ij.getType() instanceof TypeVariable<?>) { throw ValidatorLogger.LOG.injectionPointWithTypeVariable(ij, Formats.formatAsStackTraceElement(ij)); if (!(ij.getMember() instanceof Field) && ij.getAnnotated().isAnnotationPresent(Named.class) && ij.getAnnotated().getAnnotation(Named.class).value().equals("")) { throw ValidatorLogger.LOG.nonFieldInjectionPointCannotUseNamed(ij, Formats.formatAsStackTraceElement(ij)); if (ij.getAnnotated().isAnnotationPresent(Produces.class)) { if (bean != null) { throw BeanLogger.LOG.injectedFieldCannotBeProducer(ij.getAnnotated(), bean); } else { throw BeanLogger.LOG.injectedFieldCannotBeProducer(ij.getAnnotated(), Reflections.<AnnotatedField<?>>cast(ij.getAnnotated()).getDeclaringType()); checkScopeAnnotations(ij, beanManager.getServices().get(MetaAnnotationStore.class)); JtaApiAbstraction jtaApi = beanManager.getServices().get(JtaApiAbstraction.class); if (jtaApi.USER_TRANSACTION_CLASS.equals(ij.getType()) && (ij.getQualifiers().isEmpty() || ij.getQualifiers().contains(DefaultLiteral.INSTANCE)) && beanManager.getServices().get(EJBApiAbstraction.class).isSessionBeanWithContainerManagedTransactions(bean)) { throw ValidatorLogger.LOG.userTransactionInjectionIntoBeanWithContainerManagedTransactions(ij, Formats.formatAsStackTraceElement(ij));
@Override protected <T> ResourceReferenceFactory<T> getResourceReferenceFactory(InjectionPoint injectionPoint, JpaInjectionServices injectionServices, PersistenceApiAbstraction apiAbstraction) { if (!injectionPoint.getType().equals(apiAbstraction.ENTITY_MANAGER_CLASS)) { throw BeanLogger.LOG.invalidResourceProducerType(injectionPoint.getAnnotated(), apiAbstraction.ENTITY_MANAGER_CLASS); } return Reflections.<ResourceReferenceFactory<T>> cast(injectionServices .registerPersistenceContextInjectionPoint(injectionPoint)); }
public static <T, X> FieldInjectionPointAttributes<T, X> of(InjectionPoint ip) { if (ip instanceof FieldInjectionPointAttributes<?, ?>) { return Reflections.cast(ip); } if (!(ip.getAnnotated() instanceof AnnotatedField<?>) || !(ip.getMember() instanceof Field)) { throw BeanLogger.LOG.invalidInjectionPointType(ForwardingFieldInjectionPointAttributes.class, ip.getAnnotated()); } return new ForwardingFieldInjectionPointAttributes<T, X>(ip); }
public void validateMetadataInjectionPoint(InjectionPoint ij, Bean<?> bean, MessageCallback<DefinitionException> messageCallback) { // metadata injection points if (ij.getType().equals(InjectionPoint.class) && bean == null) { throw messageCallback.construct(ij, Formats.formatAsStackTraceElement(ij)); } if (ij.getType().equals(InjectionPoint.class) && !Dependent.class.equals(bean.getScope())) { throw ValidatorLogger.LOG.injectionIntoNonDependentBean(ij, Formats.formatAsStackTraceElement(ij)); } Class<?> rawType = Reflections.getRawType(ij.getType()); if (Bean.class.equals(rawType) || Interceptor.class.equals(rawType) || Decorator.class.equals(rawType)) { if (bean == null) { throw messageCallback.construct(ij, Formats.formatAsStackTraceElement(ij)); } if (bean instanceof AbstractClassBean<?>) { checkBeanMetadataInjectionPoint(bean, ij, AnnotatedTypes.getDeclaringAnnotatedType(ij.getAnnotated()).getBaseType()); } if (bean instanceof ProducerMethod<?, ?>) { ProducerMethod<?, ?> producerMethod = Reflections.cast(bean); checkBeanMetadataInjectionPoint(bean, ij, producerMethod.getAnnotated().getBaseType()); } } }
@Override protected InterceptionFactory<?> newInstance(InjectionPoint ip, CreationalContext<InterceptionFactory<?>> creationalContext) { AnnotatedParameter<?> annotatedParameter = (AnnotatedParameter<?>) ip.getAnnotated(); ParameterizedType parameterizedType = (ParameterizedType) annotatedParameter.getBaseType(); AnnotatedType<?> annotatedType = beanManager.createAnnotatedType(Reflections.getRawType(parameterizedType.getActualTypeArguments()[0])); return InterceptionFactoryImpl.of(beanManager, creationalContext, annotatedType); }
public static String getUnsatisfiedDependenciesAdditionalInfo(InjectionPoint ij, BeanManagerImpl beanManager) { Set<Bean<?>> beansMatchedByType = beanManager.getBeans(ij.getType(), Any.Literal.INSTANCE); if (beansMatchedByType.isEmpty()) { Class<?> rawType = Reflections.getRawType(ij.getType()); if (rawType != null) { MissingDependenciesRegistry missingDependenciesRegistry = beanManager.getServices().get(MissingDependenciesRegistry.class); String missingDependency = missingDependenciesRegistry.getMissingDependencyForClass(rawType.getName()); if (missingDependency != null) { return ValidatorLogger.LOG.unsatisfiedDependencyBecauseClassIgnored( rawType.getName(), missingDependency); } } } else { return ValidatorLogger.LOG.unsatisfiedDependencyBecauseQualifiersDontMatch( WeldCollections.toMultiRowString(beansMatchedByType)); } return ""; }
throw BeanLogger.LOG.nullNotAllowedFromProducer(getProducer(), Formats.formatAsStackTraceElement(getAnnotated().getJavaMember())); InjectionPoint injectionPoint = beanManager.getServices().get(CurrentInjectionPoint.class).peek(); if (injectionPoint != null) { Class<?> injectionPointRawType = Reflections.getRawType(injectionPoint.getType()); if (injectionPointRawType.isPrimitive()) { return cast(Defaults.getJlsDefaultValue(injectionPointRawType)); if (beanManager.isPassivatingScope(getScope())) { throw BeanLogger.LOG.nonSerializableProductError(getProducer(), Formats.formatAsStackTraceElement(getAnnotated().getJavaMember())); InjectionPoint injectionPoint = beanManager.getServices().get(CurrentInjectionPoint.class).peek(); if (injectionPoint != null && injectionPoint.getBean() != null && Beans.isPassivatingScope(injectionPoint.getBean(), beanManager)) { if (!(injectionPoint.getMember() instanceof Field) || !injectionPoint.isTransient()) { throw BeanLogger.LOG.unserializableProductInjectionError(this, Formats.formatAsStackTraceElement(getAnnotated().getJavaMember()), injectionPoint, Formats.formatAsStackTraceElement(injectionPoint.getMember()));
public static <X, Y extends X> AnnotatedMethod<X> of(AnnotatedMethod<X> originalMethod, UnbackedAnnotatedType<Y> declaringType, SharedObjectCache cache) { UnbackedAnnotatedType<X> downcastDeclaringType = cast(declaringType); return new UnbackedAnnotatedMethod<X>(originalMethod.getBaseType(), originalMethod.getTypeClosure(), originalMethod.getAnnotations(), downcastDeclaringType, originalMethod.getParameters(), originalMethod.getJavaMember(), cache); }
/** * Check all abstract methods are declared by the decorated types. * * @param type * @param beanManager * @param delegateType * @throws DefinitionException If any of the abstract methods is not declared by the decorated types */ public static <T> void checkAbstractMethods(Set<Type> decoratedTypes, EnhancedAnnotatedType<T> type, BeanManagerImpl beanManager) { if (decoratedTypes == null) { decoratedTypes = new HashSet<Type>(type.getInterfaceClosure()); decoratedTypes.remove(Serializable.class); } Set<MethodSignature> signatures = new HashSet<MethodSignature>(); for (Type decoratedType : decoratedTypes) { for (EnhancedAnnotatedMethod<?, ?> method : ClassTransformer.instance(beanManager) .getEnhancedAnnotatedType(Reflections.getRawType(decoratedType), beanManager.getId()).getEnhancedMethods()) { signatures.add(method.getSignature()); } } for (EnhancedAnnotatedMethod<?, ?> method : type.getEnhancedMethods()) { if (Reflections.isAbstract(((AnnotatedMethod<?>) method).getJavaMember())) { MethodSignature methodSignature = method.getSignature(); if (!signatures.contains(methodSignature)) { throw BeanLogger.LOG.abstractMethodMustMatchDecoratedType(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } } } }
public static <T, X> ForwardingParameterInjectionPointAttributes<T, X> of(InjectionPoint ip) { if (ip instanceof ForwardingParameterInjectionPointAttributes<?, ?>) { return Reflections.cast(ip); } if (!(ip.getAnnotated() instanceof AnnotatedParameter<?>)) { throw BeanLogger.LOG.invalidInjectionPointType(ForwardingParameterInjectionPointAttributes.class, ip.getAnnotated()); } return new ForwardingParameterInjectionPointAttributes<T, X>(ip); }
public MethodSignatureImpl(AnnotatedMethod<?> method) { this.methodName = method.getJavaMember().getName(); this.parameterTypes = new String[method.getParameters().size()]; for (int i = 0; i < method.getParameters().size(); i++) { parameterTypes[i] = Reflections.getRawType(method.getParameters().get(i).getBaseType()).getName(); } }
/** * Invokes the method * * @param instance The instance to invoke * @param parameters The method parameters * @return A reference to the instance */ public <X> X invoke(Object instance, Object... parameters) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { return cast(annotatedMethod.getJavaMember().invoke(instance, parameters)); }
public T call() throws Exception { if (instance == null) { Contextual<T> contextual = getBeanManager().getServices().get(ContextualStore.class).<Contextual<T>, T>getContextual(beanId); if (contextual instanceof EEResourceProducerField<?, ?>) { this.instance = Reflections.<EEResourceProducerField<?, T>>cast(contextual).createUnderlying(creationalContext); } else { throw BeanLogger.LOG.beanNotEeResourceProducer(contextual); } } return instance; }
/** * Validates the producer method */ protected void checkProducerMethod(EnhancedAnnotatedMethod<T, ? super X> method) { if (method.getEnhancedParameters(Observes.class).size() > 0) { throw BeanLogger.LOG.inconsistentAnnotationsOnMethod(PRODUCER_ANNOTATION, "@Observes", this.method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (method.getEnhancedParameters(Disposes.class).size() > 0) { throw BeanLogger.LOG.inconsistentAnnotationsOnMethod(PRODUCER_ANNOTATION, "@Disposes", this.method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (getDeclaringBean() instanceof SessionBean<?> && !Modifier.isStatic(method.slim().getJavaMember().getModifiers())) { boolean methodDeclaredOnTypes = false; // TODO use annotated item? for (Type type : getDeclaringBean().getTypes()) { Class<?> clazz = Reflections.getRawType(type); try { AccessController.doPrivileged(new GetMethodAction(clazz, method.getName(), method.getParameterTypesAsArray())); methodDeclaredOnTypes = true; break; } catch (PrivilegedActionException ignored) { } } if (!methodDeclaredOnTypes) { throw BeanLogger.LOG.methodNotBusinessMethod("Producer", this, getDeclaringBean(), Formats.formatAsStackTraceElement(method.getJavaMember())); } } }
public DecorationHelper(TargetBeanInstance originalInstance, Bean<?> bean, Class<T> proxyClassForDecorator, BeanManagerImpl beanManager, ContextualStore contextualStore, List<Decorator<?>> decorators) { this.originalInstance = Reflections.<T>cast(originalInstance.getInstance()); this.targetBeanInstance = originalInstance; this.beanManager = beanManager; this.contextualStore = contextualStore; this.decorators = new LinkedList<Decorator<?>>(decorators); this.proxyClassForDecorator = proxyClassForDecorator; this.bean = bean; this.instantiator = beanManager.getServices().get(ProxyInstantiator.class); counter = 0; }
@Override protected void checkRequiredTypeAnnotations(EnhancedAnnotatedParameter<?, ?> eventParameter) { Class<?> rawObserverType = Reflections.getRawType(getObservedType()); boolean isProcessAnnotatedType = rawObserverType.equals(ProcessAnnotatedType.class) || rawObserverType.equals(ProcessSyntheticAnnotatedType.class); if (!isProcessAnnotatedType && !requiredTypeAnnotations.isEmpty()) { throw EventLogger.LOG .invalidWithAnnotations(this, Formats.formatAsStackTraceElement(eventParameter.getDeclaringEnhancedCallable().getJavaMember())); } if (isProcessAnnotatedType && requiredTypeAnnotations.isEmpty()) { Type[] typeArguments = eventParameter.getActualTypeArguments(); if (typeArguments.length == 0 || Reflections.isUnboundedWildcard(typeArguments[0]) || Reflections.isUnboundedTypeVariable(typeArguments[0])) { EventLogger.LOG.unrestrictedProcessAnnotatedTypes(this); } } }
private Map<String, Object> getExternalConfigurationOptions(ServiceRegistry services) { // to stay compatible with older SPI versions we first check if ExternalConfiguration is available before using the class if (Reflections.isClassLoadable(EXTERNAL_CONFIGURATION_CLASS_NAME, WeldClassLoaderResourceLoader.INSTANCE)) { final ExternalConfiguration externalConfiguration = services.get(ExternalConfiguration.class); if (externalConfiguration != null) { return externalConfiguration.getConfigurationProperties(); } } return Collections.emptyMap(); }
@Override public void createNewSessionBeans(BeanDeployerEnvironment environment, BeanManagerImpl manager) { final SlimAnnotatedTypeStore store = manager.getServices().get(SlimAnnotatedTypeStore.class); final ClassTransformer classTransformer = manager.getServices().get(ClassTransformer.class); for (Type type : environment.getNewBeanTypes()) { Class<?> clazz = Reflections.getRawType(type); if (isEjb(clazz)) { EnhancedAnnotatedType<?> enhancedType = classTransformer.getEnhancedAnnotatedType(clazz, type, manager.getId()); InternalEjbDescriptor<?> descriptor = ejbDescriptors.getUnique(clazz); environment.addSessionBean(createNewSessionBean(enhancedType, descriptor, manager, store)); } } }