org.springframework.expression.ConstructorExecutor Java Examples

The following examples show how to use org.springframework.expression.ConstructorExecutor. 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: ConstructorReference.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Go through the list of registered constructor resolvers and see if any can find a
 * constructor that takes the specified set of arguments.
 * @param typeName the type trying to be constructed
 * @param argumentTypes the types of the arguments supplied that the constructor must take
 * @param state the current state of the expression
 * @return a reusable ConstructorExecutor that can be invoked to run the constructor or null
 * @throws SpelEvaluationException if there is a problem locating the constructor
 */
private ConstructorExecutor findExecutorForConstructor(String typeName,
		List<TypeDescriptor> argumentTypes, ExpressionState state) throws SpelEvaluationException {

	EvaluationContext evalContext = state.getEvaluationContext();
	List<ConstructorResolver> ctorResolvers = evalContext.getConstructorResolvers();
	for (ConstructorResolver ctorResolver : ctorResolvers) {
		try {
			ConstructorExecutor ce = ctorResolver.resolve(state.getEvaluationContext(), typeName, argumentTypes);
			if (ce != null) {
				return ce;
			}
		}
		catch (AccessException ex) {
			throw new SpelEvaluationException(getStartPosition(), ex,
					SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typeName,
					FormatHelper.formatMethodForMessage("", argumentTypes));
		}
	}
	throw new SpelEvaluationException(getStartPosition(), SpelMessage.CONSTRUCTOR_NOT_FOUND, typeName,
			FormatHelper.formatMethodForMessage("", argumentTypes));
}
 
Example #2
Source File: ConstructorReference.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Go through the list of registered constructor resolvers and see if any can find a
 * constructor that takes the specified set of arguments.
 * @param typeName the type trying to be constructed
 * @param argumentTypes the types of the arguments supplied that the constructor must take
 * @param state the current state of the expression
 * @return a reusable ConstructorExecutor that can be invoked to run the constructor or null
 * @throws SpelEvaluationException if there is a problem locating the constructor
 */
private ConstructorExecutor findExecutorForConstructor(String typeName,
		List<TypeDescriptor> argumentTypes, ExpressionState state) throws SpelEvaluationException {

	EvaluationContext evalContext = state.getEvaluationContext();
	List<ConstructorResolver> ctorResolvers = evalContext.getConstructorResolvers();
	for (ConstructorResolver ctorResolver : ctorResolvers) {
		try {
			ConstructorExecutor ce = ctorResolver.resolve(state.getEvaluationContext(), typeName, argumentTypes);
			if (ce != null) {
				return ce;
			}
		}
		catch (AccessException ex) {
			throw new SpelEvaluationException(getStartPosition(), ex,
					SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typeName,
					FormatHelper.formatMethodForMessage("", argumentTypes));
		}
	}
	throw new SpelEvaluationException(getStartPosition(), SpelMessage.CONSTRUCTOR_NOT_FOUND, typeName,
			FormatHelper.formatMethodForMessage("", argumentTypes));
}
 
Example #3
Source File: ConstructorReference.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Go through the list of registered constructor resolvers and see if any can find a
 * constructor that takes the specified set of arguments.
 * @param typeName the type trying to be constructed
 * @param argumentTypes the types of the arguments supplied that the constructor must take
 * @param state the current state of the expression
 * @return a reusable ConstructorExecutor that can be invoked to run the constructor or null
 * @throws SpelEvaluationException if there is a problem locating the constructor
 */
private ConstructorExecutor findExecutorForConstructor(String typeName,
		List<TypeDescriptor> argumentTypes, ExpressionState state)
		throws SpelEvaluationException {

	EvaluationContext evalContext = state.getEvaluationContext();
	List<ConstructorResolver> ctorResolvers = evalContext.getConstructorResolvers();
	if (ctorResolvers != null) {
		for (ConstructorResolver ctorResolver : ctorResolvers) {
			try {
				ConstructorExecutor ce = ctorResolver.resolve(state.getEvaluationContext(), typeName, argumentTypes);
				if (ce != null) {
					return ce;
				}
			}
			catch (AccessException ex) {
				throw new SpelEvaluationException(getStartPosition(), ex,
						SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typeName,
						FormatHelper.formatMethodForMessage("", argumentTypes));
			}
		}
	}
	throw new SpelEvaluationException(getStartPosition(), SpelMessage.CONSTRUCTOR_NOT_FOUND, typeName,
			FormatHelper.formatMethodForMessage("", argumentTypes));
}
 
Example #4
Source File: ConstructorReference.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Go through the list of registered constructor resolvers and see if any can find a
 * constructor that takes the specified set of arguments.
 * @param typeName the type trying to be constructed
 * @param argumentTypes the types of the arguments supplied that the constructor must take
 * @param state the current state of the expression
 * @return a reusable ConstructorExecutor that can be invoked to run the constructor or null
 * @throws SpelEvaluationException if there is a problem locating the constructor
 */
private ConstructorExecutor findExecutorForConstructor(String typeName,
		List<TypeDescriptor> argumentTypes, ExpressionState state)
		throws SpelEvaluationException {

	EvaluationContext evalContext = state.getEvaluationContext();
	List<ConstructorResolver> ctorResolvers = evalContext.getConstructorResolvers();
	if (ctorResolvers != null) {
		for (ConstructorResolver ctorResolver : ctorResolvers) {
			try {
				ConstructorExecutor ce = ctorResolver.resolve(state.getEvaluationContext(), typeName, argumentTypes);
				if (ce != null) {
					return ce;
				}
			}
			catch (AccessException ex) {
				throw new SpelEvaluationException(getStartPosition(), ex,
						SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typeName,
						FormatHelper.formatMethodForMessage("", argumentTypes));
			}
		}
	}
	throw new SpelEvaluationException(getStartPosition(), SpelMessage.CONSTRUCTOR_NOT_FOUND, typeName,
			FormatHelper.formatMethodForMessage("", argumentTypes));
}
 
Example #5
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 #6
Source File: ConstructorInvocationTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public ConstructorExecutor resolve(EvaluationContext context, String typeName,
		List<TypeDescriptor> argumentTypes) throws AccessException {
	throw new UnsupportedOperationException("Auto-generated method stub");
}
 
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: ConstructorInvocationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public ConstructorExecutor resolve(EvaluationContext context, String typeName,
		List<TypeDescriptor> argumentTypes) throws AccessException {
	throw new UnsupportedOperationException("Auto-generated method stub");
}
 
Example #9
Source File: ReflectiveConstructorResolver.java    From lams with GNU General Public License v2.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 #10
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 #11
Source File: ConstructorInvocationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public ConstructorExecutor resolve(EvaluationContext context, String typeName,
		List<TypeDescriptor> argumentTypes) throws AccessException {
	throw new UnsupportedOperationException("Auto-generated method stub");
}