예제 #1
0
  public static <T> WeldMethod<?, ?> findDecoratorMethod(
      WeldDecorator<T> decorator,
      Map<MethodSignature, WeldMethod<?, ?>> decoratorMethods,
      Method method) {
    // try the signature first, might be simpler
    MethodSignature key = new MethodSignatureImpl(method);
    if (decoratorMethods.containsKey(key)) {
      return decoratorMethods.get(key);
    }
    // try all methods
    for (WeldMethod<?, ?> decoratorMethod : decoratorMethods.values()) {
      if (method.getParameterTypes().length == decoratorMethod.getParameters().size()
          && method.getName().equals(decoratorMethod.getName())) {
        boolean parameterMatch = true;
        for (int i = 0; parameterMatch && i < method.getParameterTypes().length; i++) {
          parameterMatch =
              parameterMatch
                  && decoratorMethod.getParameterTypesAsArray()[i].isAssignableFrom(
                      method.getParameterTypes()[i]);
        }
        if (parameterMatch) {
          return decoratorMethod;
        }
      }
    }

    return null;
  }
예제 #2
0
  protected void initInterceptionModelForType() {
    ClassMetadata<?> classMetadata =
        beanManager.getInterceptorMetadataReader().getClassMetadata(getType());

    InterceptionModelBuilder<ClassMetadata<?>, ?> builder =
        InterceptionModelBuilder.<ClassMetadata<?>>newBuilderFor(classMetadata);

    // initialize CDI interceptors
    Set<Annotation> classBindingAnnotations =
        flattenInterceptorBindings(beanManager, getWeldAnnotated().getAnnotations());
    for (Class<? extends Annotation> annotation : getStereotypes()) {
      classBindingAnnotations.addAll(
          flattenInterceptorBindings(beanManager, beanManager.getStereotypeDefinition(annotation)));
    }
    if (classBindingAnnotations.size() > 0) {
      if (Beans.findInterceptorBindingConflicts(beanManager, classBindingAnnotations)) {
        throw new DeploymentException(CONFLICTING_INTERCEPTOR_BINDINGS, getType());
      }

      Annotation[] classBindingAnnotationsArray =
          classBindingAnnotations.toArray(new Annotation[0]);

      List<Interceptor<?>> resolvedPostConstructInterceptors =
          beanManager.resolveInterceptors(
              InterceptionType.POST_CONSTRUCT, classBindingAnnotationsArray);
      builder
          .interceptPostConstruct()
          .with(toSerializableContextualArray(resolvedPostConstructInterceptors));

      List<Interceptor<?>> resolvedPreDestroyInterceptors =
          beanManager.resolveInterceptors(
              InterceptionType.PRE_DESTROY, classBindingAnnotationsArray);
      builder
          .interceptPreDestroy()
          .with(toSerializableContextualArray(resolvedPreDestroyInterceptors));

      List<Interceptor<?>> resolvedPrePassivateInterceptors =
          beanManager.resolveInterceptors(
              InterceptionType.PRE_PASSIVATE, classBindingAnnotationsArray);
      builder
          .interceptPrePassivate()
          .with(toSerializableContextualArray(resolvedPrePassivateInterceptors));

      List<Interceptor<?>> resolvedPostActivateInterceptors =
          beanManager.resolveInterceptors(
              InterceptionType.POST_ACTIVATE, classBindingAnnotationsArray);
      builder
          .interceptPostActivate()
          .with(toSerializableContextualArray(resolvedPostActivateInterceptors));
    }
    List<WeldMethod<?, ?>> businessMethods = Beans.getInterceptableMethods(getWeldAnnotated());
    for (WeldMethod<?, ?> method : businessMethods) {
      Set<Annotation> methodBindingAnnotations = new HashSet<Annotation>(classBindingAnnotations);
      methodBindingAnnotations.addAll(
          flattenInterceptorBindings(beanManager, method.getAnnotations()));
      if (methodBindingAnnotations.size() > 0) {
        if (Beans.findInterceptorBindingConflicts(beanManager, classBindingAnnotations)) {
          throw new DeploymentException(
              CONFLICTING_INTERCEPTOR_BINDINGS, getType() + "." + method.getName() + "()");
        }

        List<Interceptor<?>> methodBoundInterceptors =
            beanManager.resolveInterceptors(
                InterceptionType.AROUND_INVOKE,
                methodBindingAnnotations.toArray(new Annotation[] {}));
        if (methodBoundInterceptors != null && methodBoundInterceptors.size() > 0) {
          if (method.isFinal()) {
            throw new DefinitionException(
                FINAL_INTERCEPTED_BEAN_METHOD_NOT_ALLOWED,
                method,
                methodBoundInterceptors.get(0).getBeanClass().getName());
          }
          builder
              .interceptAroundInvoke(Reflections.<AnnotatedMethod<T>>cast(method).getJavaMember())
              .with(toSerializableContextualArray(methodBoundInterceptors));
        }

        methodBoundInterceptors =
            beanManager.resolveInterceptors(
                InterceptionType.AROUND_TIMEOUT,
                methodBindingAnnotations.toArray(new Annotation[] {}));
        if (methodBoundInterceptors != null && methodBoundInterceptors.size() > 0) {
          if (method.isFinal()) {
            throw new DefinitionException(
                FINAL_INTERCEPTED_BEAN_METHOD_NOT_ALLOWED,
                method,
                methodBoundInterceptors.get(0).getBeanClass().getName());
          }
          builder
              .interceptAroundTimeout(Reflections.<AnnotatedMethod<T>>cast(method).getJavaMember())
              .with(toSerializableContextualArray(methodBoundInterceptors));
        }
      }
    }

    // initialize EJB3 interceptors
    Class<?>[] classDeclaredInterceptors = null;
    if (getWeldAnnotated()
        .isAnnotationPresent(InterceptionUtils.getInterceptorsAnnotationClass())) {
      Annotation interceptorsAnnotation =
          getType().getAnnotation(InterceptionUtils.getInterceptorsAnnotationClass());
      classDeclaredInterceptors = SecureReflections.extractValues(interceptorsAnnotation);
    }

    if (classDeclaredInterceptors != null) {
      for (Class<?> clazz : classDeclaredInterceptors) {
        builder
            .interceptAll()
            .with(beanManager.getInterceptorMetadataReader().getInterceptorMetadata(clazz));
      }
    }

    for (WeldMethod<?, ?> method : businessMethods) {
      boolean excludeClassInterceptors =
          method.isAnnotationPresent(
              InterceptionUtils.getExcludeClassInterceptorsAnnotationClass());
      Class<?>[] methodDeclaredInterceptors = null;
      if (method.isAnnotationPresent(InterceptionUtils.getInterceptorsAnnotationClass())) {
        methodDeclaredInterceptors =
            SecureReflections.extractValues(
                method.getAnnotation(InterceptionUtils.getInterceptorsAnnotationClass()));
      }
      if (excludeClassInterceptors) {
        builder.ignoreGlobalInterceptors(
            Reflections.<AnnotatedMethod<T>>cast(method).getJavaMember());
      }
      if (methodDeclaredInterceptors != null && methodDeclaredInterceptors.length > 0) {
        List<InterceptorMetadata<?>> methodDeclaredInterceptorMetadatas =
            new ArrayList<InterceptorMetadata<?>>();
        for (Class<?> clazz : methodDeclaredInterceptors) {
          methodDeclaredInterceptorMetadatas.add(
              beanManager.getInterceptorMetadataReader().getInterceptorMetadata(clazz));
        }
        if (method.isFinal()) {
          throw new DefinitionException(
              FINAL_INTERCEPTED_BEAN_METHOD_NOT_ALLOWED,
              method,
              methodDeclaredInterceptors[0].getName());
        }
        if (method.isAnnotationPresent(
            beanManager.getServices().get(EJBApiAbstraction.class).TIMEOUT_ANNOTATION_CLASS)) {
          builder
              .interceptAroundTimeout(Reflections.<AnnotatedMethod<T>>cast(method).getJavaMember())
              .with(methodDeclaredInterceptorMetadatas.toArray(new InterceptorMetadata[] {}));
        } else {
          builder
              .interceptAroundInvoke(Reflections.<AnnotatedMethod<T>>cast(method).getJavaMember())
              .with(methodDeclaredInterceptorMetadatas.toArray(new InterceptorMetadata[] {}));
        }
      }
    }
    InterceptionModel<ClassMetadata<?>, ?> interceptionModel = builder.build();

    if (interceptionModel.getAllInterceptors().size() > 0
        || hasSerializationOrInvocationInterceptorMethods) {
      if (getWeldAnnotated().isFinal()) {
        throw new DefinitionException(FINAL_BEAN_CLASS_WITH_INTERCEPTORS_NOT_ALLOWED, this);
      }
      beanManager
          .getInterceptorModelRegistry()
          .put(getType(), ((InterceptionModel<ClassMetadata<?>, ?>) interceptionModel));
    }
  }