private void checkNoArgsConstructor(EnhancedAnnotatedType<T> type) { if (!beanManager.getServices().get(ProxyInstantiator.class).isUsingConstructor()) { return; } EnhancedAnnotatedConstructor<T> constructor = type.getNoArgsEnhancedConstructor(); if (constructor == null) { throw BeanLogger.LOG.decoratedHasNoNoargsConstructor(this); } else if (constructor.isPrivate()) { throw BeanLogger.LOG .decoratedNoargsConstructorIsPrivate(this, Formats.formatAsStackTraceElement(constructor.getJavaMember())); } }
private static void validatePseudoScopedInjectionPoint(InjectionPoint ij, BeanManagerImpl beanManager, Set<Object> dependencyPath, Set<Bean<?>> validatedBeans) { Set<Bean<?>> resolved = beanManager.getBeans(ij); Bean<?> bean = null; try { bean = beanManager.resolve(resolved); } catch (AmbiguousResolutionException ex) { throw ValidatorLogger.LOG.injectionPointHasAmbiguousDependencies(ij, Formats.formatAnnotations(ij.getQualifiers()), Formats.formatInjectionPointType(ij.getType()), Formats.formatAsStackTraceElement(ij), WeldCollections.toMultiRowString(resolved)); } if (bean != null) { if (!(bean instanceof AbstractBuiltInBean<?>)) { if (!ij.isDelegate()) { boolean normalScoped = beanManager.isNormalScope(bean.getScope()); if (!normalScoped && !(bean instanceof SessionBean)) { reallyValidatePseudoScopedBean(bean, beanManager, dependencyPath, validatedBeans); } } } } }
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()));
if (ij.isDelegate()) { return; // do not validate delegate injection points as these are special Set<?> resolvedBeans = beanManager.getBeanResolver().resolve(beanManager.getBeans(ij)); if (!isInjectionPointSatisfied(ij, resolvedBeans, beanManager)) { throw ValidatorLogger.LOG.injectionPointHasUnsatisfiedDependencies( ij, Formats.formatAnnotations(ij.getQualifiers()), Formats.formatInjectionPointType(ij.getType()), Formats.formatAsStackTraceElement(ij), InjectionPoints.getUnsatisfiedDependenciesAdditionalInfo(ij, beanManager)); throw ValidatorLogger.LOG.injectionPointHasAmbiguousDependencies( ij, Formats.formatAnnotations(ij.getQualifiers()), Formats.formatInjectionPointType(ij.getType()), Formats.formatAsStackTraceElement(ij), WeldCollections.toMultiRowString(resolvedBeans)); if (beanManager.isNormalScope(resolvedBean.getScope())) { UnproxyableResolutionException ue = Proxies.getUnproxyableTypeException(ij.getType(), resolvedBean, beanManager.getServices()); if (ue != null) { throw ValidatorLogger.LOG.injectionPointHasNonProxyableDependencies(ij, Formats.formatAsStackTraceElement(ij), ue);
public void validateEventMetadataInjectionPoint(InjectionPoint ip) { if (EventMetadata.class.equals(ip.getType()) && ip.getQualifiers().contains(DefaultLiteral.INSTANCE)) { throw ValidatorLogger.LOG.eventMetadataInjectedOutsideOfObserver(ip, Formats.formatAsStackTraceElement(ip)); } }
public void validateInjectionPointForDefinitionErrors(InjectionPoint ij, Bean<?> bean, BeanManagerImpl beanManager) { // check that UserTransaction is not injected into a SessionBean with container-managed transactions if (bean instanceof SessionBean<?>) { SessionBean<?> sessionBean = (SessionBean<?>) bean; if (UserTransaction.class.equals(ij.getType()) && (ij.getQualifiers().isEmpty() || ij.getQualifiers().contains(Default.Literal.INSTANCE)) && hasContainerManagedTransactions(sessionBean)) { throw ValidatorLogger.LOG.userTransactionInjectionIntoBeanWithContainerManagedTransactions(ij, Formats.formatAsStackTraceElement(ij)); } } }
private static void checkFacadeInjectionPoint(InjectionPoint injectionPoint, Class<?> type) { Type injectionPointType = injectionPoint.getType(); if (injectionPointType instanceof Class<?> && type.equals(injectionPointType)) { throw ValidatorLogger.LOG.injectionPointMustHaveTypeParameter(injectionPoint, Formats.formatAsStackTraceElement(injectionPoint)); } if (injectionPointType instanceof ParameterizedType && !injectionPoint.isDelegate()) { ParameterizedType parameterizedType = (ParameterizedType) injectionPointType; if (type.equals(parameterizedType.getRawType())) { if (parameterizedType.getActualTypeArguments()[0] instanceof TypeVariable<?>) { throw ValidatorLogger.LOG.injectionPointWithTypeVariable(injectionPoint, Formats.formatAsStackTraceElement(injectionPoint)); } if (parameterizedType.getActualTypeArguments()[0] instanceof WildcardType) { throw ValidatorLogger.LOG.injectionPointHasWildcard(injectionPoint, Formats.formatAsStackTraceElement(injectionPoint)); } } } }
/** * 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 String formatAnnotatedType(AnnotatedType<?> type) { return Formats.formatSimpleClassName(type) + " " + Formats.addSpaceIfNeeded(Formats.formatModifiers(type.getJavaClass().getModifiers())) + Formats.formatAnnotations(type.getAnnotations()) + " class " + type.getJavaClass().getName() + Formats.formatActualTypeArguments(type.getBaseType()); }
private <T> void addContainerLifecycleEvent(T event, Object info, BeanManagerImpl beanManagerImpl) { ResolvedObservers<?> resolvedObservers = null; Type eventType = null; if (event instanceof AbstractContainerEvent) { AbstractContainerEvent containerEvent = (AbstractContainerEvent) event; eventType = containerEvent.getEventType(); resolvedObservers = beanManagerImpl.getGlobalLenientObserverNotifier().resolveObserverMethods(eventType); } else if (event instanceof ProcessAnnotatedTypeImpl) { ProcessAnnotatedTypeImpl<?> processAnnotatedTypeEvent = (ProcessAnnotatedTypeImpl<?>) event; eventType = ProcessAnnotatedType.class; info = Formats.formatType(processAnnotatedTypeEvent.getOriginalAnnotatedType().getBaseType(), false); resolvedObservers = beanManagerImpl.getGlobalLenientObserverNotifier().resolveObserverMethods( ProcessAnnotatedTypeEventResolvable.of(processAnnotatedTypeEvent, beanManagerImpl.getServices().get(RequiredAnnotationDiscovery.class))); } if (resolvedObservers != null && eventType != null) { probe.addEvent(new EventInfo(eventType, Collections.emptySet(), info, null, Reflections.cast(resolvedObservers.getAllObservers()), true, System.currentTimeMillis(), false)); } }
@Override protected DefinitionException producerWithParameterizedTypeWithTypeVariableBeanTypeMustBeDependent(AnnotatedMember<?> member) { return BeanLogger.LOG.producerMethodWithTypeVariableReturnTypeMustBeDependent(member, Formats.formatAsStackTraceElement(member.getJavaMember())); } }
@Override protected DefinitionException producerWithParameterizedTypeWithTypeVariableBeanTypeMustBeDependent(AnnotatedMember<?> member) { return BeanLogger.LOG.producerFieldWithTypeVariableBeanTypeMustBeDependent(member, Formats.formatAsStackTraceElement(member.getJavaMember())); } }
private Class<? extends Annotation> initScope() { if (scope != null) { return scope; } if (!stereotypes.isEmpty()) { MetaAnnotationStore metaAnnotationStore = beanManager.getServices().get(MetaAnnotationStore.class); Set<Annotation> possibleScopeTypes = new HashSet<>(); for (Class<? extends Annotation> stereotype : stereotypes) { StereotypeModel<? extends Annotation> model = metaAnnotationStore.getStereotype(stereotype); if (model.isValid()) { possibleScopeTypes.add(model.getDefaultScopeType()); } else { throw BeanManagerLogger.LOG.notStereotype(stereotype); } } if (possibleScopeTypes.size() == 1) { return possibleScopeTypes.iterator().next().annotationType(); } else { throw BeanLogger.LOG.multipleScopesFoundFromStereotypes(BeanAttributesConfigurator.class.getSimpleName(), Formats.formatTypes(stereotypes, false), possibleScopeTypes, ""); } } return Dependent.class; }
protected void checkDelegateInjectionPoints() { for (InjectionPoint injectionPoint : getInjectionPoints()) { if (injectionPoint.isDelegate()) { throw BeanLogger.LOG.delegateNotOnDecorator(injectionPoint, Formats.formatAsStackTraceElement(injectionPoint)); } } }
@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); } } }
protected boolean initScopeFromStereotype() { Set<Annotation> possibleScopes = mergedStereotypes.getPossibleScopes(); if (possibleScopes.size() == 1) { this.scope = possibleScopes.iterator().next().annotationType(); return true; } else if (possibleScopes.size() > 1) { String stack; Class<?> declaringClass; if (annotated instanceof EnhancedAnnotatedMember) { EnhancedAnnotatedMember<?, ?, ?> member = (EnhancedAnnotatedMember<?, ?, ?>) annotated; declaringClass = member.getDeclaringType().getJavaClass(); stack = "\n at " + Formats.formatAsStackTraceElement(member.getJavaMember()); } else { declaringClass = annotated.getJavaClass(); stack = ""; } throw BeanLogger.LOG.multipleScopesFoundFromStereotypes(Formats.formatType(declaringClass, false), Formats.formatTypes(mergedStereotypes.getStereotypes(), false), possibleScopes, stack); } else { return false; } }
/** * * @param pkg This param is completely ignored * @return the formatted version */ public static String version(@Deprecated Package pkg) { String version = null; String timestamp = null; // First try the weld-build.properties file Properties buildProperties = getBuildProperties(); if (buildProperties != null) { version = buildProperties.getProperty(BUILD_PROPERTIES_VERSION); timestamp = buildProperties.getProperty(BUILD_PROPERTIES_TIMESTAMP); } if (version == null) { // If needed use the manifest info version = getManifestImplementationVersion(); } return version(version, timestamp); }
@Override public String toString() { return "Decorator [" + getBeanClass().toString() + "] decorates [" + Formats.formatTypes(getDecoratedTypes()) + "] with delegate type [" + Formats.formatType(getDelegateType()) + "] and delegate qualifiers [" + Formats.formatAnnotations(getDelegateQualifiers()) + "]"; } }
public Bean<T> complete() { if (createCallback == null) { // not callback specified, Weld does not know how to instantiate this new custom bean throw BeanLogger.LOG.noCallbackSpecifiedForCustomBean("bean [" + beanClass.toString() + ", with types: " + Formats.formatTypes(attributes.types) + ", and qualifiers: " + Formats.formatAnnotations(attributes.qualifiers) + "]"); } return new ImmutableBean<>(this); }
private static <D extends GenericDeclaration> String formatTypeVariable(TypeVariable<D> typeVariable, boolean simpleNames) { Type[] bounds = typeVariable.getBounds(); if (Reflections.isEmptyBoundArray(bounds)) { return typeVariable.getName(); } return typeVariable.getName() + UPPER_BOUND + formatType(bounds[0], simpleNames); }