当前位置: 首页>>代码示例>>Java>>正文


Java Constructor.isVarArgs方法代码示例

本文整理汇总了Java中java.lang.reflect.Constructor.isVarArgs方法的典型用法代码示例。如果您正苦于以下问题:Java Constructor.isVarArgs方法的具体用法?Java Constructor.isVarArgs怎么用?Java Constructor.isVarArgs使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.lang.reflect.Constructor的用法示例。


在下文中一共展示了Constructor.isVarArgs方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: ReflectiveConstructorExecutor

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public ReflectiveConstructorExecutor(Constructor<?> ctor) {
	this.ctor = ctor;
	if (ctor.isVarArgs()) {
		Class<?>[] paramTypes = ctor.getParameterTypes();
		this.varargsPosition = paramTypes.length - 1;
	}
	else {
		this.varargsPosition = null;
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:11,代码来源:ReflectiveConstructorExecutor.java

示例2: resolve

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
 * 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 (new Integer(c1pl)).compareTo(c2pl);
			}
		});

		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);
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:78,代码来源:ReflectiveConstructorResolver.java

示例3: getValueObject

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
 * 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));
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:24,代码来源:NewElementHandler.java


注:本文中的java.lang.reflect.Constructor.isVarArgs方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。