Esempio n. 1
0
 private void validateWsEndpointAnnotatedType(AnnotatedType<TranslatorEndpoint> annotatedType) {
   assertEquals(annotatedType.getBaseType(), TranslatorEndpoint.class);
   // translate()
   assertEquals(annotatedType.getMethods().size(), 1);
   Set<Type> typeClosure = annotatedType.getTypeClosure();
   // Translator, TranslatorEndpoint, Object
   assertTrue(
       typeSetMatches(typeClosure, Translator.class, TranslatorEndpoint.class, Object.class));
 }
  private void generateClassHeader(JavaWriter out) throws IOException {
    out.println();
    out.println("public class " + getClassName() + "<T>");

    if (hasNoInterfaceView()) out.println("  extends " + getBeanType().getJavaClass().getName());

    out.print("  implements SessionProxyFactory<T>");
    out.print(",\n    " + CandiEnhancedBean.class.getName());

    for (AnnotatedType<? super X> apiType : getLocalApi()) {
      out.print(",\n    ");
      out.printType(apiType.getBaseType());
    }

    for (AnnotatedType<? super X> apiType : getRemoteApi()) {
      out.print(",\n    ");
      out.printType(apiType.getBaseType());
    }

    out.println();
  }
 @Test
 @SpecAssertion(section = PAT, id = "ba")
 public void testGetAnnotatedType() {
   AnnotatedType<Dog> annotatedType = ProcessAnnotatedTypeObserver.getDogAnnotatedType();
   assertEquals(annotatedType.getBaseType(), Dog.class);
   Set<AnnotatedMethod<? super Dog>> annotatedMethods = annotatedType.getMethods();
   assertEquals(annotatedMethods.size(), 3);
   for (AnnotatedMethod<? super Dog> annotatedMethod : annotatedMethods) {
     Set<String> validMethodNames =
         new HashSet<String>(Arrays.asList("bite", "live", "drinkMilk"));
     if (!validMethodNames.contains(annotatedMethod.getJavaMember().getName())) {
       fail("Invalid method name found" + annotatedMethod.getJavaMember().getName());
     }
   }
 }
  private void introspectInject(AnnotatedType<X> type, ArrayList<ConfigProgram> injectProgramList) {
    Class<?> rawType = (Class<?>) type.getBaseType();

    if (rawType == null || Object.class.equals(rawType)) return;

    // Class<?> parentClass = rawType.getSuperclass();

    // configureClassResources(injectList, type);

    introspectInjectClass(type, injectProgramList);
    introspectInjectField(type, injectProgramList);
    introspectInjectMethod(type, injectProgramList);

    ResourceProgramManager resourceManager = _cdiManager.getResourceManager();

    resourceManager.buildInject(rawType, injectProgramList);
  }
 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;
 }
  private void validatePassivating(Bean<?> bean) {
    Type baseType = _annotatedType.getBaseType();

    Class<?> cl = getBeanManager().createTargetBaseType(baseType).getRawClass();
    boolean isStateful = _annotatedType.isAnnotationPresent(Stateful.class);

    if (!Serializable.class.isAssignableFrom(cl) && !isStateful) {
      throw new ConfigException(
          L.l(
              "'{0}' is an invalid @{1} bean because it's not serializable for {2}.",
              cl.getSimpleName(), bean.getScope().getSimpleName(), bean));
    }

    for (InjectionPoint ip : bean.getInjectionPoints()) {
      if (ip.isTransient()) continue;

      Type type = ip.getType();

      if (ip.getBean() instanceof CdiStatefulBean) continue;

      if (type instanceof Class<?>) {
        Class<?> ipClass = (Class<?>) type;

        if (!ipClass.isInterface()
            && !Serializable.class.isAssignableFrom(ipClass)
            && !getBeanManager().isNormalScope(ip.getBean().getScope())) {
          throw new ConfigException(
              L.l(
                  "'{0}' is an invalid @{1} bean because '{2}' value {3} is not serializable for {4}.",
                  cl.getSimpleName(),
                  bean.getScope().getSimpleName(),
                  ip.getType(),
                  ip.getMember().getName(),
                  bean));
        }
      }
    }
  }
  /** Called for implicit introspection. */
  private void introspect(AnnotatedType<X> beanType) {
    Class<X> cl = (Class<X>) beanType.getBaseType();

    introspectConstructor(beanType);
  }
  /** Binds parameters */
  private CandiProducer<X> build() {
    Thread thread = Thread.currentThread();
    ClassLoader oldLoader = thread.getContextClassLoader();

    try {
      thread.setContextClassLoader(getBeanManager().getClassLoader());

      introspect();

      Class<X> cl = (Class<X>) _annotatedType.getBaseType();

      if (_beanCtor == null) {
        // XXX:
        AnnotatedType<X> beanType = _annotatedType;

        if (beanType == null) beanType = ReflectionAnnotatedFactory.introspectType(cl);

        introspectConstructor(beanType);
      }

      Class<X> instanceClass = null;

      if (_isGenerateInterception) {
        if (!_annotatedType.isAnnotationPresent(javax.interceptor.Interceptor.class)
            && !_annotatedType.isAnnotationPresent(javax.decorator.Decorator.class)) {
          CandiBeanGenerator<X> bean = new CandiBeanGenerator<X>(getBeanManager(), _annotatedType);
          bean.introspect();

          instanceClass = (Class<X>) bean.generateClass();
        }

        if (instanceClass == cl && isSerializeHandle()) {
          instanceClass = SerializationAdapter.gen(instanceClass);
        }
      }

      if (instanceClass != null && instanceClass != _instanceClass) {
        try {
          if (_javaCtor != null) {
            _javaCtor =
                (Constructor<X>) getConstructor(instanceClass, _javaCtor.getParameterTypes());
            _javaCtor.setAccessible(true);
          }
        } catch (Exception e) {
          // server/2423
          log.log(Level.FINE, e.toString(), e);
          // throw ConfigException.create(e);
        }
      }

      ConfigProgram[] injectProgram = introspectInject(_annotatedType);
      ConfigProgram[] initProgram = introspectPostConstruct(_annotatedType);

      ArrayList<ConfigProgram> destroyList = new ArrayList<ConfigProgram>();
      introspectDestroy(destroyList, _annotatedType);
      ConfigProgram[] destroyProgram = new ConfigProgram[destroyList.size()];
      destroyList.toArray(destroyProgram);

      Arg[] args = null;

      if (_beanCtor != null) args = introspectArguments(_beanCtor, _beanCtor.getParameters());

      CandiProducer<X> producer =
          new CandiProducer<X>(
              _bean,
              instanceClass,
              _javaCtor,
              args,
              injectProgram,
              initProgram,
              destroyProgram,
              _injectionPointSet);

      return producer;
    } finally {
      thread.setContextClassLoader(oldLoader);
    }
  }