001    /*****************************************************************************
002     * Copyright (C) PicoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     * Original Code By: Centerline Computers, Inc.                              *
008     *****************************************************************************/
009    package org.picocontainer.injectors;
010    
011    import java.lang.reflect.AccessibleObject;
012    import java.lang.reflect.Constructor;
013    import java.lang.reflect.Field;
014    import java.lang.reflect.Method;
015    
016    /**
017     * 
018     * @author Michael Rimov
019     */
020    public class PrimitiveMemberChecker {
021    
022        /**
023         * Checks if the target argument is primative.
024         * @param member target member instance, may be constructor, field, or method.
025         * @param i parameter index.
026         * @return true if the target object's "i"th parameter is a primitive (ie, int, float, etc)
027         * @throws UnsupportedOperationException if for some reason the member parameter
028         * is not a Constructor, Method, or Field.  
029         * @throws ArrayIndexOutOfBoundsException if 'i' is an inappropriate index for the
030         * given parameters.  For example, i should never be anything but zero for a field.
031         */
032        public static boolean isPrimitiveArgument(AccessibleObject member, int i) throws ArrayIndexOutOfBoundsException, UnsupportedOperationException {
033            Class[] types;
034            if (member instanceof Constructor) {
035                types = ((Constructor)member).getParameterTypes();           
036            } else if (member instanceof Method) {
037                types = ((Method)member).getParameterTypes();
038            } else if (member instanceof Field) {
039                types = new Class[1];
040                types[0] = ((Field)member).getType();
041            } else {
042                //Should be field/constructor/method only.
043                throw new UnsupportedOperationException("Unsupported member type: " + member.getClass());
044            }
045            
046            if (i >= types.length) {
047                throw new ArrayIndexOutOfBoundsException("Index i > types array length "
048                    + types.length + " for member " + member);
049            }
050            
051            if (types[i].isPrimitive()) {
052                return true;
053            }
054            
055            return false;
056        }
057    
058    }