Example usage for org.aspectj.lang.reflect ConstructorSignature getParameterTypes

List of usage examples for org.aspectj.lang.reflect ConstructorSignature getParameterTypes

Introduction

In this page you can find the example usage for org.aspectj.lang.reflect ConstructorSignature getParameterTypes.

Prototype

Class[] getParameterTypes();

Source Link

Usage

From source file:jenergy.agent.aop.aspectj.util.AspectjUtils.java

License:Apache License

/**
 * //from   w w  w.j  a  va 2 s . co m
 * @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);
}

From source file:se.crisp.codekvast.agent.lib.util.SignatureUtils.java

License:Open Source License

/**
 * Converts a java.lang.reflect.Constructor to a MethodSignature object.
 *
 * @param clazz       The class containing the method.
 * @param constructor The constructor to make a signature of.
 * @return A MethodSignature or null if the methodFilter stops the constructor.
 * @see #makeSignature(Class, Method)/*from  ww  w  .j  a va 2  s . c  om*/
 */
public static MethodSignature makeConstructorSignature(Class<?> clazz, Constructor constructor) {
    org.aspectj.lang.reflect.ConstructorSignature aspectjSignature = (org.aspectj.lang.reflect.ConstructorSignature) makeSignature(
            clazz, constructor);

    if (aspectjSignature == null) {
        return null;
    }

    return MethodSignature.builder().aspectjString(signatureToString(aspectjSignature, true))
            .declaringType(aspectjSignature.getDeclaringTypeName())
            .exceptionTypes(classArrayToString(aspectjSignature.getExceptionTypes()))
            .methodName(aspectjSignature.getName())
            .modifiers(Modifier.toString(aspectjSignature.getModifiers()))
            .packageName(aspectjSignature.getDeclaringType().getPackage().getName())
            .parameterTypes(classArrayToString(aspectjSignature.getParameterTypes())).returnType("").build();

}