Java Class New Instance newInstance(ProceedingJoinPoint thisJoinPoint, Class clazz, Object... newArgs)

Here you can find the source of newInstance(ProceedingJoinPoint thisJoinPoint, Class clazz, Object... newArgs)

Description

new Instance

License

Apache License

Parameter

Parameter Description
thisJoinPoint The reference to the constructor joinpoint.
clazz The class to invoke the same constructor of the joinpoint.
newArgs The arguments to be passed to the constructor call. In this case, the constructor arguments will be: the arguments of the original constructor defined by the joinpoint, and the newArgs.
T The type returned by the constructor call.

Exception

Parameter Description
NoSuchMethodException If a matching method is not found.
SecurityException If a security manager, <em>s</em>, is present and any of the following conditions is met:<ul><li>invocation of s.checkMemberAccess(this, Member.PUBLIC) denies access to the constructor</li><li>the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation ofs.checkPackageAccess() denies access to the package of this class.</li></ul>
InstantiationException If the class that declares the underlying constructor represents an abstract class.
IllegalAccessException If the Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
IllegalArgumentException If the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, afterpossible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocationconversion; if this constructor pertains to an Enum type.
InvocationTargetException If the underlying constructor throws an exception.
ClassCastException If it is not a constructor joinpoint.

Return

A new object created by calling the constructor of the given class.

Declaration

public static <T> T newInstance(ProceedingJoinPoint thisJoinPoint, Class<T> clazz, Object... newArgs)
        throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException,
        IllegalArgumentException, InvocationTargetException 

Method Source Code

//package com.java2s;
/**/*from   w  w  w. jav  a  2s .c  om*/
 * Copyright 2013 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.
 *
 *    Contributors:
 *          Alessandro Ferreira Leite - the initial implementation.
 */

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.reflect.ConstructorSignature;

public class Main {
    /**
     * 
     * @param thisJoinPoint
     *            The reference to the constructor joinpoint.
     * @param clazz
     *            The class to invoke the same constructor of the joinpoint.
     * @param newArgs
     *            The arguments to be passed to the constructor call. In this case, the constructor arguments will be: the arguments of the original
     *            constructor defined by the joinpoint, and the newArgs.
     * @param <T>
     *            The type returned by the constructor call.
     * @return A new object created by calling the constructor of the given class.
     * @throws NoSuchMethodException
     *             If a matching method is not found.
     * @throws SecurityException
     *             If a security manager, <em>s</em>, is present and any of the following conditions is met:
     *             <ul>
     *             <li>invocation of s.checkMemberAccess(this, Member.PUBLIC) denies access to the constructor</li>
     *             <li>the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of
     *             s.checkPackageAccess() denies access to the package of this class.</li>
     *             </ul>
     * @throws InstantiationException
     *             If the class that declares the underlying constructor represents an abstract class.
     * @throws IllegalAccessException
     *             If the Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
     * @throws 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 {@link Enum} type.
     * @throws InvocationTargetException
     *             If the underlying constructor throws an exception.
     * @throws ClassCastException
     *             If it is not a constructor joinpoint.
     * @see ConstructorSignature
     */
    public static <T> T newInstance(ProceedingJoinPoint thisJoinPoint, Class<T> clazz, Object... newArgs)
            throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {
        ConstructorSignature signature = (ConstructorSignature) thisJoinPoint.getSignature();

        Class<?>[] parameterTypes = new Class[signature.getParameterTypes().length
                + (newArgs != null ? newArgs.length : 0)];
        Object[] newConstructorArgs = new Object[parameterTypes.length];

        for (int i = 0; i < signature.getParameterTypes().length; i++) {
            parameterTypes[i] = signature.getParameterTypes()[i];
            newConstructorArgs[i] = thisJoinPoint.getArgs()[i];
        }

        for (int i = 0, j = newConstructorArgs.length - newArgs.length; i < newArgs.length; i++, j++) {
            parameterTypes[j] = newArgs[i].getClass();
            newConstructorArgs[j] = newArgs[i];
        }

        Constructor<T> constructor = clazz.getConstructor(parameterTypes);
        constructor.setAccessible(true);
        return constructor.newInstance(newConstructorArgs);
    }
}

Related

  1. newInstance(final String className, ClassLoader cl)
  2. newInstance(final String className, final Object[] args)
  3. newInstance(final String fullyQualifiedClass)
  4. newInstance(Map> providerMap, String provider, Class[] paramsClass, Object[] paramValues)
  5. newInstance(Object bean, String propertyName, Class clazz)
  6. newInstance(String aclass)
  7. newInstance(String className)
  8. newInstance(String className)
  9. newInstance(String className)