示例#1
0
        /// <summary>
        /// Uses the constructor represented by this {@code Constructor} object to
        /// create and initialize a new instance of the constructor's
        /// declaring class, with the specified initialization parameters.
        /// Individual parameters are automatically unwrapped to match
        /// primitive formal parameters, and both primitive and reference
        /// parameters are subject to method invocation conversions as necessary.
        ///
        /// <para>If the number of formal parameters required by the underlying constructor
        /// is 0, the supplied {@code initargs} array may be of length 0 or null.
        ///
        /// </para>
        /// <para>If the constructor's declaring class is an inner class in a
        /// non-static context, the first argument to the constructor needs
        /// to be the enclosing instance; see section 15.9.3 of
        /// <cite>The Java&trade; Language Specification</cite>.
        ///
        /// </para>
        /// <para>If the required access and argument checks succeed and the
        /// instantiation will proceed, the constructor's declaring class
        /// is initialized if it has not already been initialized.
        ///
        /// </para>
        /// <para>If the constructor completes normally, returns the newly
        /// created and initialized instance.
        ///
        /// </para>
        /// </summary>
        /// <param name="initargs"> array of objects to be passed as arguments to
        /// the constructor call; values of primitive types are wrapped in
        /// a wrapper object of the appropriate type (e.g. a {@code float}
        /// in a <seealso cref="java.lang.Float Float"/>)
        /// </param>
        /// <returns> a new object created by calling the constructor
        /// this object represents
        /// </returns>
        /// <exception cref="IllegalAccessException">    if this {@code Constructor} object
        ///              is enforcing Java language access control and the underlying
        ///              constructor is inaccessible. </exception>
        /// <exception cref="IllegalArgumentException">  if the number of actual
        ///              and formal parameters differ; if an unwrapping
        ///              conversion for primitive arguments fails; or if,
        ///              after possible unwrapping, a parameter value
        ///              cannot be converted to the corresponding formal
        ///              parameter type by a method invocation conversion; if
        ///              this constructor pertains to an enum type. </exception>
        /// <exception cref="InstantiationException">    if the class that declares the
        ///              underlying constructor represents an abstract class. </exception>
        /// <exception cref="InvocationTargetException"> if the underlying constructor
        ///              throws an exception. </exception>
        /// <exception cref="ExceptionInInitializerError"> if the initialization provoked
        ///              by this method fails. </exception>
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @CallerSensitive public T newInstance(Object... initargs) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        public T NewInstance(params Object[] initargs)
        {
            if (!@override)
            {
                if (!Reflection.quickCheckMemberAccess(Clazz, Modifiers_Renamed))
                {
                    Class caller = Reflection.CallerClass;
                    CheckAccess(caller, Clazz, AnnotatedElement_Fields.Null, Modifiers_Renamed);
                }
            }
            if ((Clazz.Modifiers & Modifier.ENUM) != 0)
            {
                throw new IllegalArgumentException("Cannot reflectively create enum objects");
            }
            ConstructorAccessor ca = ConstructorAccessor_Renamed;             // read volatile

            if (ca == AnnotatedElement_Fields.Null)
            {
                ca = AcquireConstructorAccessor();
            }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("unchecked") T inst = (T) ca.newInstance(initargs);
            T inst = (T)ca.newInstance(initargs);

            return(inst);
        }
示例#2
0
        // NOTE that there is no synchronization used here. It is correct
        // (though not efficient) to generate more than one
        // ConstructorAccessor for a given Constructor. However, avoiding
        // synchronization will probably make the implementation more
        // scalable.
        private ConstructorAccessor AcquireConstructorAccessor()
        {
            // First check to see if one has been created yet, and take it
            // if so.
            ConstructorAccessor tmp = AnnotatedElement_Fields.Null;

            if (Root_Renamed != AnnotatedElement_Fields.Null)
            {
                tmp = Root_Renamed.ConstructorAccessor;
            }
            if (tmp != AnnotatedElement_Fields.Null)
            {
                ConstructorAccessor_Renamed = tmp;
            }
            else
            {
                // Otherwise fabricate one and propagate it up to the root
                tmp = ReflectionFactory.newConstructorAccessor(this);
                ConstructorAccessor = tmp;
            }

            return(tmp);
        }