org.springframework.expression.spel.support.ReflectionHelper Java Examples

The following examples show how to use org.springframework.expression.spel.support.ReflectionHelper. 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 6 votes vote down vote up
@Override
public TypedValue execute(EvaluationContext context, Object target, Object... arguments)
		throws AccessException {
	try {
		Method m = HasRoleExecutor.class.getMethod("hasRole", String[].class);
		Object[] args = arguments;
		if (args != null) {
			ReflectionHelper.convertAllArguments(tc, args, m);
		}
		if (m.isVarArgs()) {
			args = ReflectionHelper.setupArgumentsForVarargsInvocation(m.getParameterTypes(), args);
		}
		return new TypedValue(m.invoke(null, args), new TypeDescriptor(new MethodParameter(m,-1)));
	}
	catch (Exception ex) {
		throw new AccessException("Problem invoking hasRole", ex);
	}
}
 
Example #2
Source File: ScenariosForSpringSecurity.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public TypedValue execute(EvaluationContext context, Object target, Object... arguments)
		throws AccessException {
	try {
		Method m = HasRoleExecutor.class.getMethod("hasRole", String[].class);
		Object[] args = arguments;
		if (args != null) {
			ReflectionHelper.convertAllArguments(tc, args, m);
		}
		if (m.isVarArgs()) {
			args = ReflectionHelper.setupArgumentsForVarargsInvocation(m.getParameterTypes(), args);
		}
		return new TypedValue(m.invoke(null, args), new TypeDescriptor(new MethodParameter(m,-1)));
	}
	catch (Exception ex) {
		throw new AccessException("Problem invoking hasRole", ex);
	}
}
 
Example #3
Source File: ScenariosForSpringSecurity.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public TypedValue execute(EvaluationContext context, Object target, Object... arguments)
		throws AccessException {
	try {
		Method m = HasRoleExecutor.class.getMethod("hasRole", String[].class);
		Object[] args = arguments;
		if (args != null) {
			ReflectionHelper.convertAllArguments(tc, args, m);
		}
		if (m.isVarArgs()) {
			args = ReflectionHelper.setupArgumentsForVarargsInvocation(m.getParameterTypes(), args);
		}
		return new TypedValue(m.invoke(null, args), new TypeDescriptor(new MethodParameter(m,-1)));
	}
	catch (Exception ex) {
		throw new AccessException("Problem invoking hasRole", ex);
	}
}
 
Example #4
Source File: FunctionReference.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Execute a function represented as a java.lang.reflect.Method.
 * @param state the expression evaluation state
 * @param method the method to invoke
 * @return the return value of the invoked Java method
 * @throws EvaluationException if there is any problem invoking the method
 */
private TypedValue executeFunctionJLRMethod(ExpressionState state, Method method) throws EvaluationException {
	this.method = null;
	Object[] functionArgs = getArguments(state);

	if (!method.isVarArgs() && method.getParameterTypes().length != functionArgs.length) {
		throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION,
				functionArgs.length, method.getParameterTypes().length);
	}
	// Only static methods can be called in this way
	if (!Modifier.isStatic(method.getModifiers())) {
		throw new SpelEvaluationException(getStartPosition(),
				SpelMessage.FUNCTION_MUST_BE_STATIC, ClassUtils.getQualifiedMethodName(method), this.name);
	}

	this.argumentConversionOccurred = false;
	// Convert arguments if necessary and remap them for varargs if required
	if (functionArgs != null) {
		TypeConverter converter = state.getEvaluationContext().getTypeConverter();
		this.argumentConversionOccurred = ReflectionHelper.convertAllArguments(converter, functionArgs, method);
	}
	if (method.isVarArgs()) {
		functionArgs = ReflectionHelper.setupArgumentsForVarargsInvocation(
				method.getParameterTypes(), functionArgs);
	}

	try {
		ReflectionUtils.makeAccessible(method);
		Object result = method.invoke(method.getClass(), functionArgs);
		if (!argumentConversionOccurred) {
			this.method = method;
			this.exitTypeDescriptor = CodeFlow.toDescriptor(method.getReturnType());
		}
		return new TypedValue(result, new TypeDescriptor(new MethodParameter(method, -1)).narrow(result));
	}
	catch (Exception ex) {
		throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL,
				this.name, ex.getMessage());
	}
}
 
Example #5
Source File: FunctionReference.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Execute a function represented as a {@code java.lang.reflect.Method}.
 * @param state the expression evaluation state
 * @param method the method to invoke
 * @return the return value of the invoked Java method
 * @throws EvaluationException if there is any problem invoking the method
 */
private TypedValue executeFunctionJLRMethod(ExpressionState state, Method method) throws EvaluationException {
	Object[] functionArgs = getArguments(state);

	if (!method.isVarArgs()) {
		int declaredParamCount = method.getParameterCount();
		if (declaredParamCount != functionArgs.length) {
			throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION,
					functionArgs.length, declaredParamCount);
		}
	}
	if (!Modifier.isStatic(method.getModifiers())) {
		throw new SpelEvaluationException(getStartPosition(),
				SpelMessage.FUNCTION_MUST_BE_STATIC, ClassUtils.getQualifiedMethodName(method), this.name);
	}

	// Convert arguments if necessary and remap them for varargs if required
	TypeConverter converter = state.getEvaluationContext().getTypeConverter();
	boolean argumentConversionOccurred = ReflectionHelper.convertAllArguments(converter, functionArgs, method);
	if (method.isVarArgs()) {
		functionArgs = ReflectionHelper.setupArgumentsForVarargsInvocation(
				method.getParameterTypes(), functionArgs);
	}
	boolean compilable = false;

	try {
		ReflectionUtils.makeAccessible(method);
		Object result = method.invoke(method.getClass(), functionArgs);
		compilable = !argumentConversionOccurred;
		return new TypedValue(result, new TypeDescriptor(new MethodParameter(method, -1)).narrow(result));
	}
	catch (Exception ex) {
		throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL,
				this.name, ex.getMessage());
	}
	finally {
		if (compilable) {
			this.exitTypeDescriptor = CodeFlow.toDescriptor(method.getReturnType());
			this.method = method;
		}
		else {
			this.exitTypeDescriptor = null;
			this.method = null;
		}
	}
}
 
Example #6
Source File: FunctionReference.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Execute a function represented as a {@code java.lang.reflect.Method}.
 * @param state the expression evaluation state
 * @param method the method to invoke
 * @return the return value of the invoked Java method
 * @throws EvaluationException if there is any problem invoking the method
 */
private TypedValue executeFunctionJLRMethod(ExpressionState state, Method method) throws EvaluationException {
	Object[] functionArgs = getArguments(state);

	if (!method.isVarArgs()) {
		int declaredParamCount = method.getParameterCount();
		if (declaredParamCount != functionArgs.length) {
			throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION,
					functionArgs.length, declaredParamCount);
		}
	}
	if (!Modifier.isStatic(method.getModifiers())) {
		throw new SpelEvaluationException(getStartPosition(),
				SpelMessage.FUNCTION_MUST_BE_STATIC, ClassUtils.getQualifiedMethodName(method), this.name);
	}

	// Convert arguments if necessary and remap them for varargs if required
	TypeConverter converter = state.getEvaluationContext().getTypeConverter();
	boolean argumentConversionOccurred = ReflectionHelper.convertAllArguments(converter, functionArgs, method);
	if (method.isVarArgs()) {
		functionArgs = ReflectionHelper.setupArgumentsForVarargsInvocation(
				method.getParameterTypes(), functionArgs);
	}
	boolean compilable = false;

	try {
		ReflectionUtils.makeAccessible(method);
		Object result = method.invoke(method.getClass(), functionArgs);
		compilable = !argumentConversionOccurred;
		return new TypedValue(result, new TypeDescriptor(new MethodParameter(method, -1)).narrow(result));
	}
	catch (Exception ex) {
		throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL,
				this.name, ex.getMessage());
	}
	finally {
		if (compilable) {
			this.exitTypeDescriptor = CodeFlow.toDescriptor(method.getReturnType());
			this.method = method;
		}
		else {
			this.exitTypeDescriptor = null;
			this.method = null;
		}
	}
}
 
Example #7
Source File: FunctionReference.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Execute a function represented as a java.lang.reflect.Method.
 * @param state the expression evaluation state
 * @param method the method to invoke
 * @return the return value of the invoked Java method
 * @throws EvaluationException if there is any problem invoking the method
 */
private TypedValue executeFunctionJLRMethod(ExpressionState state, Method method) throws EvaluationException {
	this.method = null;
	Object[] functionArgs = getArguments(state);

	if (!method.isVarArgs() && method.getParameterTypes().length != functionArgs.length) {
		throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION,
				functionArgs.length, method.getParameterTypes().length);
	}
	// Only static methods can be called in this way
	if (!Modifier.isStatic(method.getModifiers())) {
		throw new SpelEvaluationException(getStartPosition(),
				SpelMessage.FUNCTION_MUST_BE_STATIC,
				method.getDeclaringClass().getName() + "." + method.getName(), this.name);
	}

	argumentConversionOccurred = false;
	// Convert arguments if necessary and remap them for varargs if required
	if (functionArgs != null) {
		TypeConverter converter = state.getEvaluationContext().getTypeConverter();
		argumentConversionOccurred = ReflectionHelper.convertAllArguments(converter, functionArgs, method);
	}
	if (method.isVarArgs()) {
		functionArgs =
				ReflectionHelper.setupArgumentsForVarargsInvocation(method.getParameterTypes(), functionArgs);
	}

	try {
		ReflectionUtils.makeAccessible(method);
		Object result = method.invoke(method.getClass(), functionArgs);
		if (!argumentConversionOccurred) {
			this.method = method;
			this.exitTypeDescriptor = CodeFlow.toDescriptor(method.getReturnType());
		}
		return new TypedValue(result, new TypeDescriptor(new MethodParameter(method, -1)).narrow(result));
	}
	catch (Exception ex) {
		throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL,
				this.name, ex.getMessage());
	}
}