Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2009, Red Hat, Inc. and/or its affiliates, 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;

Author(s):
Marius Bogoevici
 
 public class InterceptorInvocationContext implements InvocationContext {
 
     private final Map<StringObjectcontextData;
 
     private final Method method;
 
     private Object[] parameters;
 
     private final Object target;
 
     private final InterceptionChain interceptionChain;
 
     private final Object timer;
 
     private final Constructor<?> constructor;
 
     private static final Map<Class<?>, Set<Class<?>>> WIDENING_TABLE;
 
     private static final Map<Class<?>, Class<?>> WRAPPER_CLASSES;
     private static final Map<Class<?>, Class<?>> REVERSE_WRAPPER_CLASSES;
 
     static {
         Map<Class<?>, Class<?>> wrapperClasses = new HashMap<Class<?>, Class<?>>();
         wrapperClasses.put(boolean.classBoolean.class);
         wrapperClasses.put(byte.classByte.class);
         wrapperClasses.put(char.classCharacter.class);
         wrapperClasses.put(short.classShort.class);
         wrapperClasses.put(int.classInteger.class);
         wrapperClasses.put(long.classLong.class);
         wrapperClasses.put(float.classFloat.class);
         wrapperClasses.put(double.classDouble.class);
 
          = Collections.unmodifiableMap(wrapperClasses);
 
         Map<Class<?>, Class<?>> reverseWrapperClasses = new HashMap<Class<?>, Class<?>>();
         for (Map.Entry<Class<?>, Class<?>> classEntry : wrapperClasses.entrySet()) {
             reverseWrapperClasses.put(classEntry.getValue(), classEntry.getKey());
         }
 
          = Collections.unmodifiableMap(reverseWrapperClasses);
 
         Map<Class<?>, Set<Class<?>>> wideningTable = new HashMap<Class<?>, Set<Class<?>>>();
         wideningTable.put(byte.classsetOf(short.classint.classlong.classfloat.classdouble.class));
         wideningTable.put(short.classsetOf(int.classlong.classfloat.classdouble.class));
         wideningTable.put(char.classsetOf(int.classlong.classfloat.classdouble.class));
         wideningTable.put(int.classsetOf(long.classfloat.classdouble.class));
         wideningTable.put(long.classsetOf(float.classdouble.class));
         wideningTable.put(float.class, Collections.<Class<?>>singleton(double.class));
          = Collections.unmodifiableMap(wideningTable);
 
     }
 
     private static Set<Class<?>> setOf(Class<?>... classes) {
         return new HashSet<Class<?>>(Arrays.asList(classes));
     }
 
     public InterceptorInvocationContext(InterceptionChain interceptionChainObject targetMethod targetMethodObject[] parameters) {
         this(interceptionChaintargettargetMethodnullparametersnull);
     }
 
     public InterceptorInvocationContext(InterceptionChain interceptionChainObject targetMethod targetMethodObject timer) {
        this(interceptionChaintargettargetMethodnullnulltimer);
    }
    public InterceptorInvocationContext(InterceptionChain interceptionChainConstructor<?> constructorObject[] parametersMap<StringObjectcontextData) {
        this(interceptionChainnullnullconstructorparametersnullcontextData);
    }
    private InterceptorInvocationContext(InterceptionChain interceptionChainObject targetMethod methodConstructor<?> constructorObject[] parametersObject timer) {
        this(interceptionChaintargetmethodconstructorparameterstimernew HashMap<StringObject>());
    }
    private InterceptorInvocationContext(InterceptionChain interceptionChainObject targetMethod methodConstructor<?> constructorObject[] parametersObject timerMap<StringObjectcontextData) {
        this. = interceptionChain;
        this. = target;
        this. = method;
        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 ;
    }
    @Override
    public Object proceed() throws Exception {
        try {
            return .invokeNextInterceptor(this);
        } catch (Exception e) {
            throw e;
        } catch (Throwable t) {
            throw new InterceptorException(t);
        }
    }

    
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);
    }
    private static Class<?> getWrapperClass(Class<?> primitiveClass) {
        if (!.containsKey(primitiveClass)) {
            return primitiveClass;
        } else {
            return .get(primitiveClass);
        }
    }
    private static Class<?> getPrimitiveClass(Class<?> wrapperClass) {
        if (!.containsKey(wrapperClass)) {
            return wrapperClass;
        } else {
            return .get(wrapperClass);
        }
    }
    @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<? extends ObjectnewArgumentClass = 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 = getWrapperClass(newArgumentClass);
                                if (!methodParameterClass.isAssignableFrom(boxedArgumentClass)) {
                                    throwIAE(imethodParameterClassnewArgumentClass);
                                }
                            }
                        } else {
                            // argument is non-primitive
                            if (methodParameterClass.isPrimitive()) {
                                // unboxing+widening primitive
                                Class<?> unboxedClass = getPrimitiveClass(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<? extends ObjectnewArgumentClass) {
        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 ;
    }
New to GrepCode? Check out our FAQ X