private UnbackedAnnotatedType(
     AnnotatedType<X> source, AnnotatedTypeIdentifier identifier, SharedObjectCache cache) {
   super(source.getBaseType(), source.getTypeClosure(), source.getAnnotations());
   this.javaClass = source.getJavaClass();
   ImmutableSet.Builder<AnnotatedConstructor<X>> constructors = ImmutableSet.builder();
   for (AnnotatedConstructor<X> constructor : source.getConstructors()) {
     constructors.add(UnbackedAnnotatedConstructor.of(constructor, this, cache));
   }
   this.constructors = constructors.build();
   ImmutableSet.Builder<AnnotatedMethod<? super X>> methods = ImmutableSet.builder();
   for (AnnotatedMethod<? super X> originalMethod : source.getMethods()) {
     methods.add(UnbackedAnnotatedMethod.of(originalMethod, this, cache));
   }
   this.methods = methods.build();
   ImmutableSet.Builder<AnnotatedField<? super X>> fields = ImmutableSet.builder();
   for (AnnotatedField<? super X> originalField : source.getFields()) {
     fields.add(UnbackedAnnotatedField.of(originalField, this, cache));
   }
   this.fields = fields.build();
   this.identifier = identifier;
 }
Esempio n. 2
0
  /*
   * Utility methods for field InjectionPoints
   */
  public List<Set<FieldInjectionPoint<?, ?>>> getFieldInjectionPoints(
      Bean<?> declaringBean, EnhancedAnnotatedType<?> type, BeanManagerImpl manager) {
    List<Set<FieldInjectionPoint<?, ?>>> injectableFieldsList =
        new ArrayList<Set<FieldInjectionPoint<?, ?>>>();

    if (type.slim() instanceof UnbackedAnnotatedType<?>) {
      // external AnnotatedTypes require special treatment
      Collection<EnhancedAnnotatedField<?, ?>> allFields = type.getEnhancedFields(Inject.class);

      for (Class<?> clazz = type.getJavaClass();
          clazz != null && clazz != Object.class;
          clazz = clazz.getSuperclass()) {
        ImmutableSet.Builder<FieldInjectionPoint<?, ?>> fields = ImmutableSet.builder();
        for (EnhancedAnnotatedField<?, ?> field : allFields) {
          if (!field.isStatic() && field.getJavaMember().getDeclaringClass().equals(clazz)) {
            addFieldInjectionPoint(field, fields, declaringBean, type.getJavaClass(), manager);
          }
        }
        injectableFieldsList.add(0, fields.build());
      }
    } else {
      for (EnhancedAnnotatedType<?> t = type;
          t != null && !t.getJavaClass().equals(Object.class);
          t = t.getEnhancedSuperclass()) {
        ImmutableSet.Builder<FieldInjectionPoint<?, ?>> fields = ImmutableSet.builder();
        for (EnhancedAnnotatedField<?, ?> annotatedField :
            t.getDeclaredEnhancedFields(Inject.class)) {
          if (!annotatedField.isStatic()) {
            addFieldInjectionPoint(
                annotatedField, fields, declaringBean, t.getJavaClass(), manager);
          }
        }
        injectableFieldsList.add(0, fields.build());
      }
    }
    return ImmutableList.copyOf(injectableFieldsList);
  }
Esempio n. 3
0
 public TypeDiscoveryConfiguration startExtensions(Iterable<Metadata<Extension>> extensions) {
   setExtensions(extensions);
   // TODO: we should fire BeforeBeanDiscovery to allow extensions to register additional scopes
   final Set<Class<? extends Annotation>> beanDefiningAnnotations =
       ImmutableSet.of(
           // built-in scopes
           Dependent.class,
           RequestScoped.class,
           ConversationScoped.class,
           SessionScoped.class,
           ApplicationScoped.class,
           javax.interceptor.Interceptor.class,
           javax.decorator.Decorator.class,
           // built-in stereotype
           Model.class,
           // meta-annotations
           NormalScope.class,
           Stereotype.class);
   return new TypeDiscoveryConfigurationImpl(beanDefiningAnnotations);
 }
Esempio n. 4
0
  /**
   * Extensions to Weld SE can subclass and override this method to customize the deployment before
   * weld boots up. For example, to add a custom ResourceLoader, you would subclass Weld like so:
   *
   * <pre>
   * public class MyWeld extends Weld {
   *     protected Deployment createDeployment(ResourceLoader resourceLoader, CDI11Bootstrap bootstrap) {
   *         return super.createDeployment(new MyResourceLoader(), bootstrap);
   *     }
   * }
   * </pre>
   *
   * <p>This could then be used as normal:
   *
   * <pre>
   * WeldContainer container = new MyWeld().initialize();
   * </pre>
   *
   * @param resourceLoader
   * @param bootstrap
   */
  protected Deployment createDeployment(ResourceLoader resourceLoader, CDI11Bootstrap bootstrap) {

    final Iterable<Metadata<Extension>> extensions =
        getExtensions(WeldResourceLoader.getClassLoader(), bootstrap);
    final TypeDiscoveryConfiguration typeDiscoveryConfiguration =
        bootstrap.startExtensions(extensions);
    final Deployment deployment;
    final Set<WeldBeanDeploymentArchive> beanArchives = new HashSet<WeldBeanDeploymentArchive>();
    final Map<Class<? extends Service>, Service> additionalServices = new HashMap<>();

    if (discoveryEnabled) {
      DiscoveryStrategy strategy =
          DiscoveryStrategyFactory.create(
              resourceLoader,
              bootstrap,
              ImmutableSet.<Class<? extends Annotation>>builder()
                  .addAll(typeDiscoveryConfiguration.getKnownBeanDefiningAnnotations())
                  // Add ThreadScoped manually as Weld SE doesn't support implicit bean archives
                  // without beans.xml
                  .add(ThreadScoped.class)
                  .build());
      beanArchives.addAll(strategy.performDiscovery());
      ClassFileServices classFileServices = strategy.getClassFileServices();
      if (classFileServices != null) {
        additionalServices.put(ClassFileServices.class, classFileServices);
      }
    }

    if (isSyntheticBeanArchiveRequired()) {
      ImmutableSet.Builder<String> beanClassesBuilder = ImmutableSet.builder();
      beanClassesBuilder.addAll(scanPackages());
      WeldBeanDeploymentArchive syntheticBeanArchive =
          new WeldBeanDeploymentArchive(
              WeldDeployment.SYNTHETIC_BDA_ID,
              beanClassesBuilder.build(),
              buildSyntheticBeansXml(),
              Collections.emptySet(),
              ImmutableSet.copyOf(beanClasses));
      beanArchives.add(syntheticBeanArchive);
    }

    if (beanArchives.isEmpty() && beanBuilders.isEmpty()) {
      throw WeldSELogger.LOG.weldContainerCannotBeInitializedNoBeanArchivesFound();
    }

    Multimap<String, BeanDeploymentArchive> problems =
        BeanArchives.findBeanClassesDeployedInMultipleBeanArchives(beanArchives);
    if (!problems.isEmpty()) {
      // Right now, we only log a warning for each bean class deployed in multiple bean archives
      for (Entry<String, Collection<BeanDeploymentArchive>> entry : problems.entrySet()) {
        WeldSELogger.LOG.beanClassDeployedInMultipleBeanArchives(
            entry.getKey(), WeldCollections.toMultiRowString(entry.getValue()));
      }
    }

    String isolation =
        AccessController.doPrivileged(
            new GetSystemPropertyAction(ARCHIVE_ISOLATION_SYSTEM_PROPERTY));

    if (isolation != null && Boolean.valueOf(isolation).equals(Boolean.FALSE)) {
      Set<WeldBeanDeploymentArchive> flatDeploymentArchives =
          new HashSet<WeldBeanDeploymentArchive>();
      flatDeploymentArchives.add(WeldBeanDeploymentArchive.merge(bootstrap, beanArchives));
      deployment =
          new WeldDeployment(resourceLoader, bootstrap, flatDeploymentArchives, extensions);
      CommonLogger.LOG.archiveIsolationDisabled();
    } else {
      deployment = new WeldDeployment(resourceLoader, bootstrap, beanArchives, extensions);
      CommonLogger.LOG.archiveIsolationEnabled();
    }

    deployment.getServices().addAll(additionalServices.entrySet());
    return deployment;
  }