Search in sources :

Example 1 with ClassTransformer

use of org.jboss.weld.resources.ClassTransformer in project core by weld.

the class SessionBeans method getEjbImplementationClass.

public static <T> EnhancedAnnotatedType<T> getEjbImplementationClass(EjbDescriptor<T> descriptor, BeanManagerImpl manager, EnhancedAnnotatedType<T> componentType) {
    InternalEjbDescriptor<T> ejbDescriptor = InternalEjbDescriptor.of(descriptor);
    if (ejbDescriptor.getBeanClass().equals(ejbDescriptor.getImplementationClass())) {
        // no special implementation class is used
        return componentType;
    }
    ClassTransformer transformer = manager.getServices().get(ClassTransformer.class);
    EnhancedAnnotatedType<T> implementationClass = cast(transformer.getEnhancedAnnotatedType(ejbDescriptor.getImplementationClass(), manager.getId()));
    manager.getServices().get(SlimAnnotatedTypeStore.class).put(implementationClass.slim());
    return implementationClass;
}
Also used : SlimAnnotatedTypeStore(org.jboss.weld.annotated.slim.SlimAnnotatedTypeStore) ClassTransformer(org.jboss.weld.resources.ClassTransformer)

Example 2 with ClassTransformer

use of org.jboss.weld.resources.ClassTransformer in project core by weld.

the class EjbSupportImpl method createNewSessionBeans.

@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));
        }
    }
}
Also used : EnhancedAnnotatedType(org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType) Type(java.lang.reflect.Type) SlimAnnotatedType(org.jboss.weld.annotated.slim.SlimAnnotatedType) SlimAnnotatedTypeStore(org.jboss.weld.annotated.slim.SlimAnnotatedTypeStore) ClassTransformer(org.jboss.weld.resources.ClassTransformer)

Example 3 with ClassTransformer

use of org.jboss.weld.resources.ClassTransformer in project core by weld.

the class AnnotatedTypesTest method testComparison.

/**
 * tests the AnnotatedTypes.compareAnnotatedTypes
 */
@Test
public void testComparison() throws SecurityException, NoSuchFieldException, NoSuchMethodException {
    // check that two weld classes on the same underlying are equal
    TypeStore ts = new TypeStore();
    ClassTransformer ct = new ClassTransformer(ts, new SharedObjectCache(), ReflectionCacheFactory.newInstance(ts), RegistrySingletonProvider.STATIC_INSTANCE);
    EnhancedAnnotatedType<Chair> chair1 = EnhancedAnnotatedTypeImpl.of(BackedAnnotatedType.of(Chair.class, ct.getSharedObjectCache(), ct.getReflectionCache(), RegistrySingletonProvider.STATIC_INSTANCE, AnnotatedTypeIdentifier.NULL_BDA_ID), ct);
    EnhancedAnnotatedType<Chair> chair2 = EnhancedAnnotatedTypeImpl.of(BackedAnnotatedType.of(Chair.class, ct.getSharedObjectCache(), ct.getReflectionCache(), RegistrySingletonProvider.STATIC_INSTANCE, AnnotatedTypeIdentifier.NULL_BDA_ID), ct);
    Assert.assertTrue(AnnotatedTypes.compareAnnotatedTypes(chair1, chair2));
    // check that a different implementation of annotated type is equal to the weld implementation
    TestAnnotatedTypeBuilder<Chair> builder = new TestAnnotatedTypeBuilder<Chair>(Chair.class);
    builder.addToClass(new DefaultLiteral());
    builder.addToField(Chair.class.getField("legs"), new ProducesLiteral());
    builder.addToMethod(Chair.class.getMethod("sit"), new ProducesLiteral());
    AnnotatedType<Chair> chair3 = builder.create();
    Assert.assertTrue(AnnotatedTypes.compareAnnotatedTypes(chair1, chair3));
    // check that the implementation returns false if a field annotation changes
    builder = new TestAnnotatedTypeBuilder<Chair>(Chair.class);
    builder.addToClass(new DefaultLiteral());
    builder.addToField(Chair.class.getField("legs"), new DefaultLiteral());
    builder.addToMethod(Chair.class.getMethod("sit"), new ProducesLiteral());
    chair3 = builder.create();
    Assert.assertFalse(AnnotatedTypes.compareAnnotatedTypes(chair1, chair3));
    // check that the implementation returns false if a class level annotation changes
    builder = new TestAnnotatedTypeBuilder<Chair>(Chair.class);
    builder.addToClass(new ProducesLiteral());
    builder.addToField(Chair.class.getField("legs"), new DefaultLiteral());
    builder.addToMethod(Chair.class.getMethod("sit"), new ProducesLiteral());
    chair3 = builder.create();
    Assert.assertFalse(AnnotatedTypes.compareAnnotatedTypes(chair1, chair3));
}
Also used : SharedObjectCache(org.jboss.weld.resources.SharedObjectCache) TypeStore(org.jboss.weld.metadata.TypeStore) ClassTransformer(org.jboss.weld.resources.ClassTransformer) TestAnnotatedTypeBuilder(org.jboss.weld.test.util.annotated.TestAnnotatedTypeBuilder) Test(org.testng.annotations.Test)

Example 4 with ClassTransformer

use of org.jboss.weld.resources.ClassTransformer in project HotswapAgent by HotswapProjects.

the class BeanReloadExecutor method createAnnotatedTypeForExistingBeanClass.

private static EnhancedAnnotatedType<?> createAnnotatedTypeForExistingBeanClass(String bdaId, Class<?> beanClass) {
    ClassTransformer classTransformer = getClassTransformer();
    SlimAnnotatedType<?> annotatedType = classTransformer.getBackedAnnotatedType(beanClass, bdaId);
    return EnhancedAnnotatedTypeImpl.of(annotatedType, classTransformer);
}
Also used : ClassTransformer(org.jboss.weld.resources.ClassTransformer)

Example 5 with ClassTransformer

use of org.jboss.weld.resources.ClassTransformer in project wildfly by wildfly.

the class InjectionTargets method createInjectionTarget.

/**
 * Creates a new InjectionTarget for a given class. If the interceptionSupport flag is set to true the resulting instance will support
 * interception (support provided by Weld). If an InjectionTarget is created for a component where interception support is implemented
 * through component's view (Jakarta Enterprise Beans, managed beans) the flag must be set to false.
 *
 * @param componentClass
 * @param bean
 * @param beanManager
 * @param interceptionSupport
 * @return
 */
public static <T> WeldInjectionTarget<T> createInjectionTarget(Class<?> componentClass, Bean<T> bean, BeanManagerImpl beanManager, boolean interceptionSupport) {
    final ClassTransformer transformer = beanManager.getServices().get(ClassTransformer.class);
    @SuppressWarnings("unchecked") final Class<T> clazz = (Class<T>) componentClass;
    EnhancedAnnotatedType<T> type = transformer.getEnhancedAnnotatedType(clazz, beanManager.getId());
    if (!type.getJavaClass().equals(componentClass)) {
        /*
             * Jasper loads a class with multiple classloaders which is not supported by Weld.
             * If this happens, use a combination of a bean archive identifier and class' classloader hashCode as the BDA ID.
             * This breaks AnnotatedType serialization but that does not matter as these are non-contextual components.
             */
        final ClassLoader classLoader = WildFlySecurityManager.isChecking() ? doPrivileged(new GetClassLoaderAction(componentClass)) : componentClass.getClassLoader();
        final String bdaId = beanManager.getId() + classLoader.hashCode();
        type = transformer.getEnhancedAnnotatedType(clazz, bdaId);
    }
    if (Beans.getBeanConstructor(type) == null) {
        /*
             * For example, AsyncListeners may be Jakarta Contexts and Dependency Injection incompatible as long as the application never calls javax.servletAsyncContext#createListener(Class)
             * and only instantiates the listener itself.
             */
        return beanManager.getInjectionTargetFactory(type).createNonProducibleInjectionTarget();
    }
    WeldInjectionTargetBuilder<T> builder = beanManager.createInjectionTargetBuilder(type);
    builder.setBean(bean);
    // because these are all EE components where resource injection is not handled by Weld
    builder.setResourceInjectionEnabled(false);
    if (interceptionSupport) {
        return builder.build();
    } else {
        // suppress interception/decoration because this is a component for which WF provides interception support
        return builder.setInterceptionEnabled(false).setTargetClassLifecycleCallbacksEnabled(false).setDecorationEnabled(false).build();
    }
}
Also used : GetClassLoaderAction(org.wildfly.security.manager.action.GetClassLoaderAction) ClassTransformer(org.jboss.weld.resources.ClassTransformer)

Aggregations

ClassTransformer (org.jboss.weld.resources.ClassTransformer)13 TypeStore (org.jboss.weld.metadata.TypeStore)7 SharedObjectCache (org.jboss.weld.resources.SharedObjectCache)7 SlimAnnotatedTypeStore (org.jboss.weld.annotated.slim.SlimAnnotatedTypeStore)3 Test (org.junit.Test)3 EnhancedAnnotatedMethod (org.jboss.weld.annotated.enhanced.EnhancedAnnotatedMethod)2 EnhancedAnnotatedType (org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType)2 ReflectionCache (org.jboss.weld.resources.ReflectionCache)2 Annotation (java.lang.annotation.Annotation)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 BeanAttributes (javax.enterprise.inject.spi.BeanAttributes)1 SlimAnnotatedType (org.jboss.weld.annotated.slim.SlimAnnotatedType)1 SpecializationAndEnablementRegistry (org.jboss.weld.bootstrap.SpecializationAndEnablementRegistry)1 SimpleServiceRegistry (org.jboss.weld.bootstrap.api.helpers.SimpleServiceRegistry)1 WeldConfiguration (org.jboss.weld.config.WeldConfiguration)1 ContextNotActiveException (org.jboss.weld.context.ContextNotActiveException)1 GlobalObserverNotifierService (org.jboss.weld.event.GlobalObserverNotifierService)1 ResourceInjectionFactory (org.jboss.weld.injection.ResourceInjectionFactory)1 InjectionTargetService (org.jboss.weld.injection.producer.InjectionTargetService)1