Java Code Examples for org.springframework.expression.EvaluationContext#getTypeConverter()

The following examples show how to use org.springframework.expression.EvaluationContext#getTypeConverter() . 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: ScenariosForSpringSecurity.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, List<TypeDescriptor> arguments)
		throws AccessException {
	if (name.equals("hasRole")) {
		return new HasRoleExecutor(context.getTypeConverter());
	}
	return null;
}
 
Example 2
Source File: ScenariosForSpringSecurity.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, List<TypeDescriptor> arguments)
		throws AccessException {
	if (name.equals("hasRole")) {
		return new HasRoleExecutor(context.getTypeConverter());
	}
	return null;
}
 
Example 3
Source File: ScenariosForSpringSecurity.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, List<TypeDescriptor> arguments)
		throws AccessException {
	if (name.equals("hasRole")) {
		return new HasRoleExecutor(context.getTypeConverter());
	}
	return null;
}
 
Example 4
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 5
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 6
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 7
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);
	}
}