Java Code Examples for java.lang.reflect.Constructor#isVarArgs()

The following examples show how to use java.lang.reflect.Constructor#isVarArgs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScriptContext.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Object[] convertConstructors(Constructor[] constructors) {
    Object[] ret=new Object[constructors.length*6];
    int i=0;
    for (Constructor m : constructors) {
        Class[] parameterTypes;
        try {
            parameterTypes = m.getParameterTypes();
        }catch (Throwable e){ continue;}
        ret[i++]=m;
        ret[i++]=void.class;
        ret[i++]=null;
        ret[i++]= parameterTypes;
        Type[] genericParameterTypes = m.getGenericParameterTypes();
        for (int j=genericParameterTypes.length;j--!=0;){
            if(genericParameterTypes[j]==parameterTypes[j])
                genericParameterTypes[j]=null;
        }
        ret[i++]= genericParameterTypes;
        if(m.isVarArgs()&&parameterTypes[parameterTypes.length-1].isArray()){
            Type t=genericParameterTypes[genericParameterTypes.length-1];
            Class r=parameterTypes[parameterTypes.length-1];
            if(t==r) ret[i++]=r.getComponentType();
            else ret[i++]=((GenericArrayType)t).getGenericComponentType();
        }else ret[i++]=null;
    }
    return ret;
}
 
Example 2
Source File: Reflections.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** As {@link #invokeConstructorFromArgs(Class, Object...)} but allowing more configurable input;
 * in particular setAccessible allows private constructors to be used (not the default) */
@SuppressWarnings("unchecked")
public static <T> Maybe<T> invokeConstructorFromArgs(Reflections reflections, Class<? extends T> clazz, Object[] argsArray, boolean setAccessible) {
    for (Constructor<?> constructor : MutableList.<Constructor<?>>of().appendAll(Arrays.asList(clazz.getConstructors())).appendAll(Arrays.asList(clazz.getDeclaredConstructors()))) {
        Class<?>[] parameterTypes = constructor.getParameterTypes();
        if (constructor.isVarArgs()) {
            if (typesMatchUpTo(argsArray, parameterTypes, parameterTypes.length-1)) {
                Class<?> varargType = parameterTypes[parameterTypes.length-1].getComponentType();
                boolean varargsMatch = true;
                for (int i=parameterTypes.length-1; i<argsArray.length; i++) {
                    if (!Boxing.boxedType(varargType).isInstance(argsArray[i]) ||
                                            (varargType.isPrimitive() && argsArray[i]==null)) {
                        varargsMatch = false;
                        break;
                    }
                }
                if (varargsMatch) {
                    Object varargs = Array.newInstance(varargType, argsArray.length+1 - parameterTypes.length);
                    for (int i=parameterTypes.length-1; i<argsArray.length; i++) {
                        Boxing.setInArray(varargs, i+1-parameterTypes.length, argsArray[i], varargType);
                    }
                    Object[] newArgsArray = new Object[parameterTypes.length];
                    System.arraycopy(argsArray, 0, newArgsArray, 0, parameterTypes.length-1);
                    newArgsArray[parameterTypes.length-1] = varargs;
                    if (setAccessible) constructor.setAccessible(true);
                    return Maybe.of((T)reflections.loadInstance(constructor, newArgsArray));
                }
            }
        }
        if (typesMatch(argsArray, parameterTypes)) {
            if (setAccessible) constructor.setAccessible(true);
            return Maybe.of((T) reflections.loadInstance(constructor, argsArray));
        }
    }
    return Maybe.absent("Constructor not found");
}
 
Example 3
Source File: ClassUtil.java    From nesty with Apache License 2.0 5 votes vote down vote up
public static boolean hasDefaultConstructor(Class<?> clazz) {
   for (Constructor constructor : clazz.getDeclaredConstructors())
       if (!constructor.isVarArgs())
           return true;

    return false;
}
 
Example 4
Source File: ReflectiveConstructorExecutor.java    From java-technology-stack with MIT License 5 votes vote down vote up
public ReflectiveConstructorExecutor(Constructor<?> ctor) {
	this.ctor = ctor;
	if (ctor.isVarArgs()) {
		Class<?>[] paramTypes = ctor.getParameterTypes();
		this.varargsPosition = paramTypes.length - 1;
	}
	else {
		this.varargsPosition = null;
	}
}
 
Example 5
Source File: ReflectiveConstructorResolver.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Locate a constructor on the type. There are three kinds of match that might occur:
 * <ol>
 * <li>An exact match where the types of the arguments match the types of the constructor
 * <li>An in-exact match where the types we are looking for are subtypes of those defined on the constructor
 * <li>A match where we are able to convert the arguments into those expected by the constructor, according to the
 * registered type converter.
 * </ol>
 */
@Override
public ConstructorExecutor resolve(EvaluationContext context, String typeName, List<TypeDescriptor> argumentTypes)
		throws AccessException {

	try {
		TypeConverter typeConverter = context.getTypeConverter();
		Class<?> type = context.getTypeLocator().findType(typeName);
		Constructor<?>[] ctors = type.getConstructors();

		Arrays.sort(ctors, new Comparator<Constructor<?>>() {
			@Override
			public int compare(Constructor<?> c1, Constructor<?> c2) {
				int c1pl = c1.getParameterTypes().length;
				int c2pl = c2.getParameterTypes().length;
				return (c1pl < c2pl ? -1 : (c1pl > c2pl ? 1 : 0));
			}
		});

		Constructor<?> closeMatch = null;
		Constructor<?> matchRequiringConversion = null;

		for (Constructor<?> ctor : ctors) {
			Class<?>[] paramTypes = ctor.getParameterTypes();
			List<TypeDescriptor> paramDescriptors = new ArrayList<TypeDescriptor>(paramTypes.length);
			for (int i = 0; i < paramTypes.length; i++) {
				paramDescriptors.add(new TypeDescriptor(new MethodParameter(ctor, i)));
			}
			ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
			if (ctor.isVarArgs() && argumentTypes.size() >= paramTypes.length - 1) {
				// *sigh* complicated
				// Basically.. we have to have all parameters match up until the varargs one, then the rest of what is
				// being provided should be
				// the same type whilst the final argument to the method must be an array of that (oh, how easy...not) -
				// or the final parameter
				// we are supplied does match exactly (it is an array already).
				matchInfo = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
			}
			else if (paramTypes.length == argumentTypes.size()) {
				// worth a closer look
				matchInfo = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
			}
			if (matchInfo != null) {
				if (matchInfo.isExactMatch()) {
					return new ReflectiveConstructorExecutor(ctor);
				}
				else if (matchInfo.isCloseMatch()) {
					closeMatch = ctor;
				}
				else if (matchInfo.isMatchRequiringConversion()) {
					matchRequiringConversion = ctor;
				}
			}
		}

		if (closeMatch != null) {
			return new ReflectiveConstructorExecutor(closeMatch);
		}
		else if (matchRequiringConversion != null) {
			return new ReflectiveConstructorExecutor(matchRequiringConversion);
		}
		else {
			return null;
		}
	}
	catch (EvaluationException ex) {
		throw new AccessException("Failed to resolve constructor", ex);
	}
}
 
Example 6
Source File: ClassReader.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static long getMemberFlag(Constructor member){
    int flag=member.getModifiers();
    if(member.isSynthetic()) flag|=SYNTHETIC;
    if(member.isVarArgs()) flag|=VARARGS;
    return flag;
}
 
Example 7
Source File: ReflectiveConstructorResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Locate a constructor on the type. There are three kinds of match that might occur:
 * <ol>
 * <li>An exact match where the types of the arguments match the types of the constructor
 * <li>An in-exact match where the types we are looking for are subtypes of those defined on the constructor
 * <li>A match where we are able to convert the arguments into those expected by the constructor, according to the
 * registered type converter.
 * </ol>
 */
@Override
@Nullable
public ConstructorExecutor resolve(EvaluationContext context, String typeName, List<TypeDescriptor> argumentTypes)
		throws AccessException {

	try {
		TypeConverter typeConverter = context.getTypeConverter();
		Class<?> type = context.getTypeLocator().findType(typeName);
		Constructor<?>[] ctors = type.getConstructors();

		Arrays.sort(ctors, (c1, c2) -> {
			int c1pl = c1.getParameterCount();
			int c2pl = c2.getParameterCount();
			return (c1pl < c2pl ? -1 : (c1pl > c2pl ? 1 : 0));
		});

		Constructor<?> closeMatch = null;
		Constructor<?> matchRequiringConversion = null;

		for (Constructor<?> ctor : ctors) {
			Class<?>[] paramTypes = ctor.getParameterTypes();
			List<TypeDescriptor> paramDescriptors = new ArrayList<>(paramTypes.length);
			for (int i = 0; i < paramTypes.length; i++) {
				paramDescriptors.add(new TypeDescriptor(new MethodParameter(ctor, i)));
			}
			ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
			if (ctor.isVarArgs() && argumentTypes.size() >= paramTypes.length - 1) {
				// *sigh* complicated
				// Basically.. we have to have all parameters match up until the varargs one, then the rest of what is
				// being provided should be
				// the same type whilst the final argument to the method must be an array of that (oh, how easy...not) -
				// or the final parameter
				// we are supplied does match exactly (it is an array already).
				matchInfo = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
			}
			else if (paramTypes.length == argumentTypes.size()) {
				// worth a closer look
				matchInfo = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
			}
			if (matchInfo != null) {
				if (matchInfo.isExactMatch()) {
					return new ReflectiveConstructorExecutor(ctor);
				}
				else if (matchInfo.isCloseMatch()) {
					closeMatch = ctor;
				}
				else if (matchInfo.isMatchRequiringConversion()) {
					matchRequiringConversion = ctor;
				}
			}
		}

		if (closeMatch != null) {
			return new ReflectiveConstructorExecutor(closeMatch);
		}
		else if (matchRequiringConversion != null) {
			return new ReflectiveConstructorExecutor(matchRequiringConversion);
		}
		else {
			return null;
		}
	}
	catch (EvaluationException ex) {
		throw new AccessException("Failed to resolve constructor", ex);
	}
}
 
Example 8
Source File: SpelNodeImpl.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Generate code that handles building the argument values for the specified method. This method will take account
 * of whether the invoked method is a varargs method and if it is then the argument values will be appropriately
 * packaged into an array.
 * @param mv the method visitor where code should be generated
 * @param cf the current codeflow
 * @param member the method or constructor for which arguments are being setup
 * @param arguments the expression nodes for the expression supplied argument values
 */
protected static void generateCodeForArguments(MethodVisitor mv, CodeFlow cf, Member member, SpelNodeImpl[] arguments) {
	String[] paramDescriptors = null;
	boolean isVarargs = false;
	if (member instanceof Constructor) {
		Constructor<?> ctor = (Constructor<?>)member;
		paramDescriptors = CodeFlow.toDescriptors(ctor.getParameterTypes());
		isVarargs = ctor.isVarArgs();
	}
	else { // Method
		Method method = (Method)member;
		paramDescriptors = CodeFlow.toDescriptors(method.getParameterTypes());
		isVarargs = method.isVarArgs();
	}
	if (isVarargs) {
		// The final parameter may or may not need packaging into an array, or nothing may
		// have been passed to satisfy the varargs and so something needs to be built.
		int p = 0; // Current supplied argument being processed
		int childCount = arguments.length;
					
		// Fulfill all the parameter requirements except the last one
		for (p = 0; p < paramDescriptors.length - 1; p++) {
			generateCodeForArgument(mv, cf, arguments[p], paramDescriptors[p]);
		}
		
		SpelNodeImpl lastchild = (childCount == 0 ? null : arguments[childCount - 1]);
		String arraytype = paramDescriptors[paramDescriptors.length - 1];
		// Determine if the final passed argument is already suitably packaged in array
		// form to be passed to the method
		if (lastchild != null && lastchild.getExitDescriptor().equals(arraytype)) {
			generateCodeForArgument(mv, cf, lastchild, paramDescriptors[p]);
		}
		else {
			arraytype = arraytype.substring(1); // trim the leading '[', may leave other '['		
			// build array big enough to hold remaining arguments
			CodeFlow.insertNewArrayCode(mv, childCount - p, arraytype);
			// Package up the remaining arguments into the array
			int arrayindex = 0;
			while (p < childCount) {
				SpelNodeImpl child = arguments[p];
				mv.visitInsn(DUP);
				CodeFlow.insertOptimalLoad(mv, arrayindex++);
				generateCodeForArgument(mv, cf, child, arraytype);
				CodeFlow.insertArrayStore(mv, arraytype);
				p++;
			}
		}
	}
	else {
		for (int i = 0; i < paramDescriptors.length;i++) {
			generateCodeForArgument(mv, cf, arguments[i], paramDescriptors[i]);
		}
	}
}
 
Example 9
Source File: ConstructorUtils.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
/**
 * <p>Returns a new instance of the specified class choosing the right constructor
 * from the list of parameter types.</p>
 *
 * <p>This locates and calls a constructor.
 * The constructor signature must match the parameter types by assignment compatibility.</p>
 *
 * @param <T> the type to be constructed
 * @param cls  the class to be constructed, not {@code null}
 * @param args  the array of arguments, {@code null} treated as empty
 * @param parameterTypes  the array of parameter types, {@code null} treated as empty
 * @return new instance of {@code cls}, not {@code null}
 *
 * @throws NullPointerException if {@code cls} is {@code null}
 * @throws NoSuchMethodException if a matching constructor cannot be found
 * @throws IllegalAccessException if invocation is not permitted by security
 * @throws InvocationTargetException if an error occurs on invocation
 * @throws InstantiationException if an error occurs on instantiation
 * @see Constructor#newInstance
 */
public static <T> T invokeConstructor(final Class<T> cls, Object[] args, Class<?>[] parameterTypes)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
        InstantiationException {
    args = ArrayUtils.nullToEmpty(args);
    parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
    final Constructor<T> ctor = getMatchingAccessibleConstructor(cls, parameterTypes);
    if (ctor == null) {
        throw new NoSuchMethodException(
            "No such accessible constructor on object: " + cls.getName());
    }
    if (ctor.isVarArgs()) {
        final Class<?>[] methodParameterTypes = ctor.getParameterTypes();
        args = MethodUtils.getVarArgs(args, methodParameterTypes);
    }
    return ctor.newInstance(args);
}
 
Example 10
Source File: MethodKey.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isVarArgs(Constructor<?> app) {
    return app.isVarArgs();
}
 
Example 11
Source File: ReflectiveConstructorResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Locate a constructor on the type. There are three kinds of match that might occur:
 * <ol>
 * <li>An exact match where the types of the arguments match the types of the constructor
 * <li>An in-exact match where the types we are looking for are subtypes of those defined on the constructor
 * <li>A match where we are able to convert the arguments into those expected by the constructor, according to the
 * registered type converter.
 * </ol>
 */
@Override
@Nullable
public ConstructorExecutor resolve(EvaluationContext context, String typeName, List<TypeDescriptor> argumentTypes)
		throws AccessException {

	try {
		TypeConverter typeConverter = context.getTypeConverter();
		Class<?> type = context.getTypeLocator().findType(typeName);
		Constructor<?>[] ctors = type.getConstructors();

		Arrays.sort(ctors, (c1, c2) -> {
			int c1pl = c1.getParameterCount();
			int c2pl = c2.getParameterCount();
			return (c1pl < c2pl ? -1 : (c1pl > c2pl ? 1 : 0));
		});

		Constructor<?> closeMatch = null;
		Constructor<?> matchRequiringConversion = null;

		for (Constructor<?> ctor : ctors) {
			Class<?>[] paramTypes = ctor.getParameterTypes();
			List<TypeDescriptor> paramDescriptors = new ArrayList<>(paramTypes.length);
			for (int i = 0; i < paramTypes.length; i++) {
				paramDescriptors.add(new TypeDescriptor(new MethodParameter(ctor, i)));
			}
			ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
			if (ctor.isVarArgs() && argumentTypes.size() >= paramTypes.length - 1) {
				// *sigh* complicated
				// Basically.. we have to have all parameters match up until the varargs one, then the rest of what is
				// being provided should be
				// the same type whilst the final argument to the method must be an array of that (oh, how easy...not) -
				// or the final parameter
				// we are supplied does match exactly (it is an array already).
				matchInfo = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
			}
			else if (paramTypes.length == argumentTypes.size()) {
				// worth a closer look
				matchInfo = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
			}
			if (matchInfo != null) {
				if (matchInfo.isExactMatch()) {
					return new ReflectiveConstructorExecutor(ctor);
				}
				else if (matchInfo.isCloseMatch()) {
					closeMatch = ctor;
				}
				else if (matchInfo.isMatchRequiringConversion()) {
					matchRequiringConversion = ctor;
				}
			}
		}

		if (closeMatch != null) {
			return new ReflectiveConstructorExecutor(closeMatch);
		}
		else if (matchRequiringConversion != null) {
			return new ReflectiveConstructorExecutor(matchRequiringConversion);
		}
		else {
			return null;
		}
	}
	catch (EvaluationException ex) {
		throw new AccessException("Failed to resolve constructor", ex);
	}
}
 
Example 12
Source File: SpelNodeImpl.java    From bistoury with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Generate code that handles building the argument values for the specified method. This method will take account
 * of whether the invoked method is a varargs method and if it is then the argument values will be appropriately
 * packaged into an array.
 * @param mv the method visitor where code should be generated
 * @param cf the current codeflow
 * @param member the method or constructor for which arguments are being setup
 * @param arguments the expression nodes for the expression supplied argument values
 */
protected static void generateCodeForArguments(MethodVisitor mv, CodeFlow cf, Member member, SpelNodeImpl[] arguments) {
	String[] paramDescriptors = null;
	boolean isVarargs = false;
	if (member instanceof Constructor) {
		Constructor<?> ctor = (Constructor<?>)member;
		paramDescriptors = CodeFlow.toDescriptors(ctor.getParameterTypes());
		isVarargs = ctor.isVarArgs();
	}
	else { // Method
		Method method = (Method)member;
		paramDescriptors = CodeFlow.toDescriptors(method.getParameterTypes());
		isVarargs = method.isVarArgs();
	}
	if (isVarargs) {
		// The final parameter may or may not need packaging into an array, or nothing may
		// have been passed to satisfy the varargs and so something needs to be built.
		int p = 0; // Current supplied argument being processed
		int childCount = arguments.length;
					
		// Fulfill all the parameter requirements except the last one
		for (p = 0; p < paramDescriptors.length - 1; p++) {
			generateCodeForArgument(mv, cf, arguments[p], paramDescriptors[p]);
		}
		
		SpelNodeImpl lastchild = (childCount == 0 ? null : arguments[childCount - 1]);
		String arraytype = paramDescriptors[paramDescriptors.length - 1];
		// Determine if the final passed argument is already suitably packaged in array
		// form to be passed to the method
		if (lastchild != null && lastchild.getExitDescriptor().equals(arraytype)) {
			generateCodeForArgument(mv, cf, lastchild, paramDescriptors[p]);
		}
		else {
			arraytype = arraytype.substring(1); // trim the leading '[', may leave other '['		
			// build array big enough to hold remaining arguments
			CodeFlow.insertNewArrayCode(mv, childCount - p, arraytype);
			// Package up the remaining arguments into the array
			int arrayindex = 0;
			while (p < childCount) {
				SpelNodeImpl child = arguments[p];
				mv.visitInsn(DUP);
				CodeFlow.insertOptimalLoad(mv, arrayindex++);
				generateCodeForArgument(mv, cf, child, arraytype);
				CodeFlow.insertArrayStore(mv, arraytype);
				p++;
			}
		}
	}
	else {
		for (int i = 0; i < paramDescriptors.length;i++) {
			generateCodeForArgument(mv, cf, arguments[i], paramDescriptors[i]);
		}
	}
}
 
Example 13
Source File: MemberUtils.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
private Executable(final Constructor<?> constructor) {
    parameterTypes = constructor.getParameterTypes();
    isVarArgs = constructor.isVarArgs();
}
 
Example 14
Source File: NewElementHandler.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Calculates the value of this element
 * using the base class and the array of arguments.
 * By default, it creates an instance of the base class.
 * This method should be overridden in those handlers
 * that extend behavior of this element.
 *
 * @param type  the base class
 * @param args  the array of arguments
 * @return the value of this element
 * @throws Exception if calculation is failed
 */
ValueObject getValueObject(Class<?> type, Object[] args) throws Exception {
    if (type == null) {
        throw new IllegalArgumentException("Class name is not set");
    }
    Class<?>[] types = getArgumentTypes(args);
    Constructor<?> constructor = ConstructorFinder.findConstructor(type, types);
    if (constructor.isVarArgs()) {
        args = getArguments(args, constructor.getParameterTypes());
    }
    return ValueObjectImpl.create(constructor.newInstance(args));
}
 
Example 15
Source File: NewElementHandler.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Calculates the value of this element
 * using the base class and the array of arguments.
 * By default, it creates an instance of the base class.
 * This method should be overridden in those handlers
 * that extend behavior of this element.
 *
 * @param type  the base class
 * @param args  the array of arguments
 * @return the value of this element
 * @throws Exception if calculation is failed
 */
ValueObject getValueObject(Class<?> type, Object[] args) throws Exception {
    if (type == null) {
        throw new IllegalArgumentException("Class name is not set");
    }
    Class<?>[] types = getArgumentTypes(args);
    Constructor<?> constructor = ConstructorFinder.findConstructor(type, types);
    if (constructor.isVarArgs()) {
        args = getArguments(args, constructor.getParameterTypes());
    }
    return ValueObjectImpl.create(constructor.newInstance(args));
}
 
Example 16
Source File: NewElementHandler.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Calculates the value of this element
 * using the base class and the array of arguments.
 * By default, it creates an instance of the base class.
 * This method should be overridden in those handlers
 * that extend behavior of this element.
 *
 * @param type  the base class
 * @param args  the array of arguments
 * @return the value of this element
 * @throws Exception if calculation is failed
 */
ValueObject getValueObject(Class<?> type, Object[] args) throws Exception {
    if (type == null) {
        throw new IllegalArgumentException("Class name is not set");
    }
    Class<?>[] types = getArgumentTypes(args);
    Constructor<?> constructor = ConstructorFinder.findConstructor(type, types);
    if (constructor.isVarArgs()) {
        args = getArguments(args, constructor.getParameterTypes());
    }
    return ValueObjectImpl.create(constructor.newInstance(args));
}
 
Example 17
Source File: NewElementHandler.java    From jdk8u_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Calculates the value of this element
 * using the base class and the array of arguments.
 * By default, it creates an instance of the base class.
 * This method should be overridden in those handlers
 * that extend behavior of this element.
 *
 * @param type  the base class
 * @param args  the array of arguments
 * @return the value of this element
 * @throws Exception if calculation is failed
 */
ValueObject getValueObject(Class<?> type, Object[] args) throws Exception {
    if (type == null) {
        throw new IllegalArgumentException("Class name is not set");
    }
    Class<?>[] types = getArgumentTypes(args);
    Constructor<?> constructor = ConstructorFinder.findConstructor(type, types);
    if (constructor.isVarArgs()) {
        args = getArguments(args, constructor.getParameterTypes());
    }
    return ValueObjectImpl.create(constructor.newInstance(args));
}
 
Example 18
Source File: NewElementHandler.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Calculates the value of this element
 * using the base class and the array of arguments.
 * By default, it creates an instance of the base class.
 * This method should be overridden in those handlers
 * that extend behavior of this element.
 *
 * @param type  the base class
 * @param args  the array of arguments
 * @return the value of this element
 * @throws Exception if calculation is failed
 */
ValueObject getValueObject(Class<?> type, Object[] args) throws Exception {
    if (type == null) {
        throw new IllegalArgumentException("Class name is not set");
    }
    Class<?>[] types = getArgumentTypes(args);
    Constructor<?> constructor = ConstructorFinder.findConstructor(type, types);
    if (constructor.isVarArgs()) {
        args = getArguments(args, constructor.getParameterTypes());
    }
    return ValueObjectImpl.create(constructor.newInstance(args));
}
 
Example 19
Source File: NewElementHandler.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Calculates the value of this element
 * using the base class and the array of arguments.
 * By default, it creates an instance of the base class.
 * This method should be overridden in those handlers
 * that extend behavior of this element.
 *
 * @param type  the base class
 * @param args  the array of arguments
 * @return the value of this element
 * @throws Exception if calculation is failed
 */
ValueObject getValueObject(Class<?> type, Object[] args) throws Exception {
    if (type == null) {
        throw new IllegalArgumentException("Class name is not set");
    }
    Class<?>[] types = getArgumentTypes(args);
    Constructor<?> constructor = ConstructorFinder.findConstructor(type, types);
    if (constructor.isVarArgs()) {
        args = getArguments(args, constructor.getParameterTypes());
    }
    return ValueObjectImpl.create(constructor.newInstance(args));
}
 
Example 20
Source File: NewElementHandler.java    From Bytecoder with Apache License 2.0 3 votes vote down vote up
/**
 * Calculates the value of this element
 * using the base class and the array of arguments.
 * By default, it creates an instance of the base class.
 * This method should be overridden in those handlers
 * that extend behavior of this element.
 *
 * @param type  the base class
 * @param args  the array of arguments
 * @return the value of this element
 * @throws Exception if calculation is failed
 */
ValueObject getValueObject(Class<?> type, Object[] args) throws Exception {
    if (type == null) {
        throw new IllegalArgumentException("Class name is not set");
    }
    Class<?>[] types = getArgumentTypes(args);
    Constructor<?> constructor = ConstructorFinder.findConstructor(type, types);
    if (constructor.isVarArgs()) {
        args = getArguments(args, constructor.getParameterTypes());
    }
    return ValueObjectImpl.create(constructor.newInstance(args));
}