Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2014, 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.interceptor.proxy;
 
 import java.util.Map;
 import java.util.Set;
 
 
 
 import  edu.umd.cs.findbugs.annotations.SuppressWarnings;

Simple InvocationContext implementation whose proceed() invokes the target method directly without calling any interceptors. If this is not a method interception, a call to proceed() always returns null.

Author(s):
Marius Bogoevici
Jozef Hartinger
 
 public class SimpleInvocationContext implements InvocationContext {
 
     private final Map<StringObjectcontextData;
     private final Method method;
     private final Method proceed;
     private Object[] parameters;
     private final Object target;
     private final Object timer;
     private final Constructor<?> constructor;
 
     private static final Map<Class<?>, Set<Class<?>>> WIDENING_TABLE;
 
     static {
         Map<Class<?>, Set<Class<?>>> wideningTable = new HashMap<Class<?>, Set<Class<?>>>();
         wideningTable.put(byte.class, ImmutableSet.<Class<?>> of(short.classint.classlong.classfloat.classdouble.class));
         wideningTable.put(short.class, ImmutableSet.<Class<?>> of(int.classlong.classfloat.classdouble.class));
         wideningTable.put(char.class, ImmutableSet.<Class<?>> of(int.classlong.classfloat.classdouble.class));
         wideningTable.put(int.class, ImmutableSet.<Class<?>> of(long.classfloat.classdouble.class));
         wideningTable.put(long.class, ImmutableSet.<Class<?>> of(float.classdouble.class));
         wideningTable.put(float.class, Collections.<Class<?>> singleton(double.class));
          = Collections.unmodifiableMap(wideningTable);
 
     }
 
     public SimpleInvocationContext(Object targetMethod targetMethodMethod proceedObject[] parameters) {
         this(targettargetMethodproceednullparametersnullnew HashMap<StringObject>());
     }
 
     public SimpleInvocationContext(Constructor<?> constructorObject[] parametersMap<StringObjectcontextData) {
         this(nullnullnullconstructorparametersnullcontextData);
     }
 
     private SimpleInvocationContext(Object targetMethod methodMethod proceedConstructor<?> constructorObject[] parametersObject timerMap<StringObjectcontextData) {
         this. = target;
         this. = method;
         this. = proceed;
         this. = constructor;
         this. = parameters;
         this. = timer;
         this. = contextData;
     }
 
     @Override
     public Map<StringObjectgetContextData() {
         return ;
     }
 
     @Override
     public Method getMethod() {
         return ;
     }
 
     @Override
     @SuppressWarnings("EI_EXPOSE_REP")
     public Object[] getParameters() {
         if (this. != null || this. != null) {
             return ;
         } else {
             throw new IllegalStateException("Illegal invocation to getParameters() during lifecycle invocation");
         }
    }
    @Override
    public Object getTarget() {
        return ;
    }

    
Checks that the targetClass is widening the argument class

Parameters:
argumentClass
targetClass
Returns:
    private static boolean isWideningPrimitive(Class<?> argumentClassClass<?> targetClass) {
        return .containsKey(argumentClass) && .get(argumentClass).contains(targetClass);
    }
    @SuppressWarnings("EI_EXPOSE_REP")
    public void setParameters(Object[] params) {
        if (this. != null || this. != null) {
            // there is no requirement to do anything if params is null
            // but this is theoretically possible only if the target method has no arguments
            int newParametersCount = params == null ? 0 : params.length;
            Class<?>[] parameterTypes = null;
            if ( != null) {
                parameterTypes = .getParameterTypes();
            } else {
                parameterTypes = .getParameterTypes();
            }
            if (parameterTypes.length != newParametersCount) {
                throw new IllegalArgumentException("Wrong number of parameters: method has " + parameterTypes.length + ", attempting to set "
                        + newParametersCount + (params != null ? "" : " (argument was null)"));
            }
            if (params != null) {
                for (int i = 0; i < params.lengthi++) {
                    Class<?> methodParameterClass = parameterTypes[i];
                    if (params[i] != null) {
                        // identity ok
                        Class<?> newArgumentClass = params[i].getClass();
                        if (newArgumentClass.equals(methodParameterClass)) {
                            break;
                        }
                        if (newArgumentClass.isPrimitive()) {
                            // argument is primitive - never actually a case for interceptors
                            if (methodParameterClass.isPrimitive()) {
                                // widening primitive
                                if (!isWideningPrimitive(newArgumentClassmethodParameterClass)) {
                                    throwIAE(imethodParameterClassnewArgumentClass);
                                }
                            } else {
                                // boxing+widening reference
                                Class<?> boxedArgumentClass = Primitives.wrap(newArgumentClass);
                                if (!methodParameterClass.isAssignableFrom(boxedArgumentClass)) {
                                    throwIAE(imethodParameterClassnewArgumentClass);
                                }
                            }
                        } else {
                            // argument is non-primitive
                            if (methodParameterClass.isPrimitive()) {
                                // unboxing+widening primitive
                                Class<?> unboxedClass = Primitives.unwrap(newArgumentClass);
                                if (!unboxedClass.equals(methodParameterClass) && !isWideningPrimitive(unboxedClassmethodParameterClass)) {
                                    throwIAE(imethodParameterClassnewArgumentClass);
                                }
                            } else {
                                // widening reference
                                if (!methodParameterClass.isAssignableFrom(newArgumentClass)) {
                                    throwIAE(imethodParameterClassnewArgumentClass);
                                }
                            }
                        }
                    } else {
                        // null is never acceptable on a primitive type
                        if (parameterTypes[i].isPrimitive()) {
                            throw new IllegalArgumentException("Trying to set a null value on a " + parameterTypes[i].getName());
                        }
                    }
                }
                this. = params;
            }
        } else {
            throw new IllegalStateException("Illegal invocation to setParameters() during lifecycle invocation");
        }
    }
    private void throwIAE(int iClass<?> methodParameterClassClass<?> newArgumentClass) {
        throw new IllegalArgumentException("Incompatible parameter type on position: " + i + " :" + newArgumentClass + " (expected type was "
                + methodParameterClass.getName() + ")");
    }
    @Override
    public Object getTimer() {
        return ;
    }
    @Override
    public Constructor<?> getConstructor() {
        return ;
    }
    @Override
    public Object proceed() throws Exception {
        if ( != null) {
            return .invoke();
        } else {
            return null;
        }
    }
New to GrepCode? Check out our FAQ X