/**
  * Transforms the input Class object to a result by instantiation.
  *
  * @param input  the input object to transform
  * @return the transformed result
  */
 public Object transform(Object input)
 {
     try
     {
         if (input is java.lang.Class == false)
         {
             throw new FunctorException(
                       "InstantiateTransformer: Input object was not an is Class, it was a "
                       + (input == null ? "null object" : input.getClass().getName()));
         }
         java.lang.reflect.Constructor con = ((java.lang.Class)input).getConstructor(iParamTypes);
         return(con.newInstance(iArgs));
     }
     catch (java.lang.NoSuchMethodException)
     {
         throw new FunctorException("InstantiateTransformer: The constructor must exist and be public ");
     }
     catch (java.lang.InstantiationException ex)
     {
         throw new FunctorException("InstantiateTransformer: InstantiationException", ex);
     }
     catch (java.lang.IllegalAccessException ex)
     {
         throw new FunctorException("InstantiateTransformer: Constructor must be public", ex);
     }
     catch (java.lang.reflect.InvocationTargetException ex)
     {
         throw new FunctorException("InstantiateTransformer: Constructor threw an exception", ex);
     }
 }
 /**
  * Find the Constructor for the class specified.
  */
 private void findConstructor()
 {
     try
     {
         iConstructor = iClassToInstantiate.getConstructor(iParamTypes);
     }
     catch (java.lang.NoSuchMethodException)
     {
         throw new java.lang.IllegalArgumentException("InstantiateFactory: The constructor must exist and be public ");
     }
 }
Пример #3
0
        /*
         * Tries to find a suitable constructor and instantiate a new Permission
         * with specified parameters.
         *
         * @param targetType class of expected Permission instance
         * @param targetName name of expected Permission instance
         * @param targetActions actions of expected Permission instance
         * @return a new Permission instance
         * @throws IllegalArgumentException if no suitable constructor found
         * @throws Exception any exception thrown by Constructor.newInstance()
         */
        public static java.security.Permission instantiatePermission(java.lang.Class targetType,
                                                                     String targetName, String targetActions)
        {//throws Exception {
            // let's guess the best order for trying constructors
            java.lang.Class[][] argTypes = null;
            Object[][]          args     = null;
            if (targetActions != null)
            {
                argTypes = new java.lang.Class[][] { TWO_ARGS, ONE_ARGS, NO_ARGS };
                args     = new Object[][] {
                    new Object [] { targetName, targetActions },
                    new Object [] { targetName },
                    new Object [] {}
                };
            }
            else if (targetName != null)
            {
                argTypes = new java.lang.Class[][] { ONE_ARGS, TWO_ARGS, NO_ARGS };
                args     = new Object[][] {
                    new Object [] { targetName },
                    new Object [] { targetName, targetActions },
                    new Object [] {}
                };
            }
            else
            {
                argTypes = new java.lang.Class[][] { NO_ARGS, ONE_ARGS, TWO_ARGS };
                args     = new Object[][] {
                    new Object [] {},
                    new Object [] { targetName },
                    new Object [] { targetName, targetActions }
                };
            }

            // finally try to instantiate actual permission
            for (int i = 0; i < argTypes.Length; i++)
            {
                try
                {
                    java.lang.reflect.Constructor ctor = targetType.getConstructor(argTypes[i]);
                    return((java.security.Permission)ctor.newInstance(args[i]));
                }
                catch (java.lang.NoSuchMethodException ignore) { }
            }
            throw new java.lang.IllegalArgumentException("No suitable constructors found in permission class : " + targetType + ". Zero, one or two-argument constructor is expected");
        }
        /**
         * Converts the given value to the given type.  First, reflection is
         * is used to find a public constructor declared by the given class
         * that takes one argument, which must be the precise type of the
         * given value.  If such a constructor is found, a new object is
         * created by passing the given value to that constructor, and the
         * newly constructed object is returned.<P>
         *
         * If no such constructor exists, and the given type is a primitive
         * type, then the given value is converted to a string using its
         * {@link Object#toString() toString()} method, and that string is
         * parsed into the correct primitive type using, for instance,
         * {@link Integer#valueOf(String)} to convert the string into an
         * <code>int</code>.<P>
         *
         * If no special constructor exists and the given type is not a
         * primitive type, this method returns the original value.
         *
         * @param newType  the type to convert the value to
         * @param value  the value to convert
         * @return the converted value
         * @throws NumberFormatException if newType is a primitive type, and
         *  the string representation of the given value cannot be converted
         *  to that type
         * @throws InstantiationException  if the constructor found with
         *  reflection raises it
         * @throws InvocationTargetException  if the constructor found with
         *  reflection raises it
         * @throws IllegalAccessException  never
         * @throws IllegalArgumentException  never
         */
        protected Object convertType(java.lang.Class newType, Object value)
        {
            //throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

            // try call constructor
            java.lang.Class[] types = { value.getClass() };
            try
            {
                java.lang.reflect.Constructor constructor = newType.getConstructor(types);
                Object[] arguments = { value };
                return(constructor.newInstance(arguments));
            }
            catch (java.lang.NoSuchMethodException)
            {
                // try using the transformers
                Transformer transformer = getTypeTransformer(newType);
                if (transformer != null)
                {
                    return(transformer.transform(value));
                }
                return(value);
            }
        }
        /**
         * Find the Constructor for the class specified.
         */
        private void findConstructor()
        {
            try
            {
                iConstructor = iClassToInstantiate.getConstructor(iParamTypes);

            }
            catch (java.lang.NoSuchMethodException ex)
            {
                throw new java.lang.IllegalArgumentException("InstantiateFactory: The constructor must exist and be public ");
            }
        }