Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2015, Red Hat, Inc., and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * http://www.apache.org/licenses/LICENSE-2.0
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.jboss.weld.injection;
 
 import java.util.List;
 
 
Encapsulates various strategies for invoking a method injection point. The default implementation supports all the possible scenarios including dependency injection to parameters. In addition, there are optimized implementations for commonly used scenarios such as:

Author(s):
Jozef Hartinger
Martin Kouba
 
 public abstract class MethodInvocationStrategy {
 
 
 
 
 
 
     protected final Class<? extends RuntimeExceptionexceptionTypeToThrow;
 
     MethodInvocationStrategy(Class<? extends RuntimeExceptionexceptionTypeToThrow) {
         this. = exceptionTypeToThrow;
     }
 
         this. = null;
     }
 
     public static MethodInvocationStrategy forDisposer(MethodInjectionPoint<?, ?> methodBeanManagerImpl manager) {
         List<? extends ParameterInjectionPoint<?, ?>> parameters = method.getParameterInjectionPoints();
         if (parameters.size() == 1 && parameters.get(0).getAnnotated().isAnnotationPresent(Observes.class)) {
             return ;
         } else {
             return ;
         }
     }
 
     public static MethodInvocationStrategy forObserver(MethodInjectionPoint<?, ?> methodBeanManagerImpl manager) {
         List<? extends ParameterInjectionPoint<?, ?>> parameters = method.getParameterInjectionPoints();
         if (parameters.size() == 1 && parameters.get(0).getAnnotated().isAnnotationPresent(Observes.class)) {
             return ;
         } else if (parameters.size() == 2) {
             if (parameters.get(0).getAnnotated().isAnnotationPresent(Observes.class)) {
                 if (BeanManager.class.equals(parameters.get(1).getType())) {
                     return ;
                 } else if (EventMetadata.class.equals(parameters.get(1).getType())) {
                     return new EventPlusMetadataStrategy(manager);
                 }
             }
         }
         return ;
     }
 
     public abstract <T> void invoke(Object receiverMethodInjectionPoint<?, ?> method, T instanceBeanManagerImpl manager,
             CreationalContext<?> creationalContext);

    
The default general-purpose invocation strategy.
 
     private static class DefaultMethodInvocationStrategy extends MethodInvocationStrategy {
 
         public DefaultMethodInvocationStrategy(Class<? extends RuntimeExceptionexceptionTypeToThrow) {
             super(exceptionTypeToThrow);
        }
        @Override
        public <T> void invoke(Object receiverMethodInjectionPoint<?, ?> method, T instanceBeanManagerImpl managerCreationalContext<?> creationalContext) {
            boolean release = creationalContext == null;
            if (release) {
                creationalContext = manager.createCreationalContext(null);
            }
            try {
                method.invoke(receiverinstancemanagercreationalContext);
            } finally {
                if (release) {
                    creationalContext.release();
                }
            }
        }
    }

    
Optimized invocation strategy that only supports methods with a single special parameter (e.g. the event parameter).
    private static class SimpleMethodInvocationStrategy extends MethodInvocationStrategy {
        public SimpleMethodInvocationStrategy(Class<? extends RuntimeExceptionexceptionTypeToThrow) {
            super(exceptionTypeToThrow);
        }
        @Override
        public <T> void invoke(Object receiverMethodInjectionPoint<?, ?> method, T instanceBeanManagerImpl managerCreationalContext<?> creationalContext) {
            method.invoke(receiverinstancemanagercreationalContext);
        }
    }

    
Optimized invocation strategy that supports methods with exactly two parameters: special parameter plus javax.enterprise.inject.spi.BeanManager injection point.
    private static class SpecialParamPlusBeanManagerStrategy extends MethodInvocationStrategy {
        public SpecialParamPlusBeanManagerStrategy(Class<? extends RuntimeExceptionexceptionTypeToThrow) {
            super(exceptionTypeToThrow);
        }
        @Override
        public <T> void invoke(Object receiverMethodInjectionPoint<?, ?> method, T instanceBeanManagerImpl managerCreationalContext<?> creationalContext) {
            method.invoke(receivernew Object[] { instancenew BeanManagerProxy(manager) }, );
        }
    }

    
Optimized invocation strategy that supports observer methods with exactly two parameters: event parameter plus javax.enterprise.inject.spi.EventMetadata injection point.
    private static class EventPlusMetadataStrategy extends MethodInvocationStrategy {
        private final CurrentEventMetadata metadata;
        private EventPlusMetadataStrategy(BeanManagerImpl manager) {
            this. = manager.getServices().get(CurrentEventMetadata.class);
        }
        @Override
        public <T> void invoke(Object receiverMethodInjectionPoint<?, ?> method, T instanceBeanManagerImpl managerCreationalContext<?> creationalContext) {
            method.invoke(receivernew Object[] { instance.peek() }, ObserverException.class);
        }
    }
New to GrepCode? Check out our FAQ X