Java Code Examples for org.springframework.expression.AccessException

The following examples show how to use org.springframework.expression.AccessException. These examples are extracted from open source projects. 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
@Override
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
	if (target == null) {
		return false;
	}

	Class<?> type = (target instanceof Class ? (Class<?>) target : target.getClass());
	if (type.isArray()) {
		return false;
	}

	if (this.member instanceof Method) {
		Method method = (Method) this.member;
		String getterName = "get" + StringUtils.capitalize(name);
		if (getterName.equals(method.getName())) {
			return true;
		}
		getterName = "is" + StringUtils.capitalize(name);
		return getterName.equals(method.getName());
	}
	else {
		Field field = (Field) this.member;
		return field.getName().equals(name);
	}
}
 
Example 2
Source Project: lams   Source File: MethodReference.java    License: GNU General Public License v2.0 6 votes vote down vote up
private MethodExecutor findAccessorForMethod(String name, List<TypeDescriptor> argumentTypes,
		Object targetObject, EvaluationContext evaluationContext) throws SpelEvaluationException {

	List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
	if (methodResolvers != null) {
		for (MethodResolver methodResolver : methodResolvers) {
			try {
				MethodExecutor methodExecutor = methodResolver.resolve(
						evaluationContext, targetObject, name, argumentTypes);
				if (methodExecutor != null) {
					return methodExecutor;
				}
			}
			catch (AccessException ex) {
				throw new SpelEvaluationException(getStartPosition(), ex,
						SpelMessage.PROBLEM_LOCATING_METHOD, name, targetObject.getClass());
			}
		}
	}

	throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_NOT_FOUND,
			FormatHelper.formatMethodForMessage(name, argumentTypes),
			FormatHelper.formatClassNameForMessage(
					targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass()));
}
 
Example 3
Source Project: spring4-understanding   Source File: MethodReference.java    License: Apache License 2.0 6 votes vote down vote up
private MethodExecutor findAccessorForMethod(String name, List<TypeDescriptor> argumentTypes,
		Object targetObject, EvaluationContext evaluationContext) throws SpelEvaluationException {

	List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
	if (methodResolvers != null) {
		for (MethodResolver methodResolver : methodResolvers) {
			try {
				MethodExecutor methodExecutor = methodResolver.resolve(
						evaluationContext, targetObject, name, argumentTypes);
				if (methodExecutor != null) {
					return methodExecutor;
				}
			}
			catch (AccessException ex) {
				throw new SpelEvaluationException(getStartPosition(), ex,
						SpelMessage.PROBLEM_LOCATING_METHOD, name, targetObject.getClass());
			}
		}
	}

	throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_NOT_FOUND,
			FormatHelper.formatMethodForMessage(name, argumentTypes),
			FormatHelper.formatClassNameForMessage(
					targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass()));
}
 
Example 4
Source Project: spring-analysis-note   Source File: ReflectivePropertyAccessor.java    License: MIT License 6 votes vote down vote up
@Nullable
private TypeDescriptor getTypeDescriptor(EvaluationContext context, Object target, String name) {
	Class<?> type = (target instanceof Class ? (Class<?>) target : target.getClass());

	if (type.isArray() && name.equals("length")) {
		return TypeDescriptor.valueOf(Integer.TYPE);
	}
	PropertyCacheKey cacheKey = new PropertyCacheKey(type, name, target instanceof Class);
	TypeDescriptor typeDescriptor = this.typeDescriptorCache.get(cacheKey);
	if (typeDescriptor == null) {
		// Attempt to populate the cache entry
		try {
			if (canRead(context, target, name) || canWrite(context, target, name)) {
				typeDescriptor = this.typeDescriptorCache.get(cacheKey);
			}
		}
		catch (AccessException ex) {
			// Continue with null type descriptor
		}
	}
	return typeDescriptor;
}
 
Example 5
public boolean isWritableProperty(String name, TypedValue contextObject, EvaluationContext evalContext)
		throws EvaluationException {

	List<PropertyAccessor> accessorsToTry =
			getPropertyAccessorsToTry(contextObject.getValue(), evalContext.getPropertyAccessors());
	if (accessorsToTry != null) {
		for (PropertyAccessor accessor : accessorsToTry) {
			try {
				if (accessor.canWrite(evalContext, contextObject.getValue(), name)) {
					return true;
				}
			}
			catch (AccessException ex) {
				// let others try
			}
		}
	}
	return false;
}
 
Example 6
/**
 * 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 7
@Override
public TypedValue execute(EvaluationContext context, Object... arguments) throws AccessException {
	try {
		ReflectionHelper.convertArguments(
				context.getTypeConverter(), arguments, this.ctor, this.varargsPosition);
		if (this.ctor.isVarArgs()) {
			arguments = ReflectionHelper.setupArgumentsForVarargsInvocation(
					this.ctor.getParameterTypes(), arguments);
		}
		ReflectionUtils.makeAccessible(this.ctor);
		return new TypedValue(this.ctor.newInstance(arguments));
	}
	catch (Exception ex) {
		throw new AccessException("Problem invoking constructor: " + this.ctor, ex);
	}
}
 
Example 8
Source Project: lams   Source File: BeanReference.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	BeanResolver beanResolver = state.getEvaluationContext().getBeanResolver();
	if (beanResolver == null) {
		throw new SpelEvaluationException(
				getStartPosition(), SpelMessage.NO_BEAN_RESOLVER_REGISTERED, this.beanName);
	}

	try {
		return new TypedValue(beanResolver.resolve(state.getEvaluationContext(), this.beanName));
	}
	catch (AccessException ex) {
		throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_BEAN_RESOLUTION,
			this.beanName, ex.getMessage());
	}
}
 
Example 9
Source Project: java-technology-stack   Source File: ReflectivePropertyAccessor.java    License: MIT License 6 votes vote down vote up
@Nullable
private TypeDescriptor getTypeDescriptor(EvaluationContext context, Object target, String name) {
	Class<?> type = (target instanceof Class ? (Class<?>) target : target.getClass());

	if (type.isArray() && name.equals("length")) {
		return TypeDescriptor.valueOf(Integer.TYPE);
	}
	PropertyCacheKey cacheKey = new PropertyCacheKey(type, name, target instanceof Class);
	TypeDescriptor typeDescriptor = this.typeDescriptorCache.get(cacheKey);
	if (typeDescriptor == null) {
		// Attempt to populate the cache entry
		try {
			if (canRead(context, target, name) || canWrite(context, target, name)) {
				typeDescriptor = this.typeDescriptorCache.get(cacheKey);
			}
		}
		catch (AccessException ex) {
			// Continue with null type descriptor
		}
	}
	return typeDescriptor;
}
 
Example 10
Source Project: spring4-understanding   Source File: BeanReference.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	BeanResolver beanResolver = state.getEvaluationContext().getBeanResolver();
	if (beanResolver == null) {
		throw new SpelEvaluationException(
				getStartPosition(), SpelMessage.NO_BEAN_RESOLVER_REGISTERED, this.beanName);
	}

	try {
		return new TypedValue(beanResolver.resolve(state.getEvaluationContext(), this.beanName));
	}
	catch (AccessException ex) {
		throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_BEAN_RESOLUTION,
			this.beanName, ex.getMessage());
	}
}
 
Example 11
@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 12
Source Project: kork   Source File: MapPropertyAccessor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public TypedValue read(final EvaluationContext context, final Object target, final String name)
    throws AccessException {
  try {
    return super.read(context, target, name);
  } catch (AccessException ae) {
    if (allowUnknownKeys) {
      return TypedValue.NULL;
    }
    throw ae;
  }
}
 
Example 13
@Override
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
	if (target == null) {
		return false;
	}
	Class<?> type = (target instanceof Class ? (Class<?>) target : target.getClass());
	PropertyCacheKey cacheKey = new PropertyCacheKey(type, name, target instanceof Class);
	if (this.writerCache.containsKey(cacheKey)) {
		return true;
	}
	Method method = findSetterForProperty(name, type, target);
	if (method != null) {
		// Treat it like a property
		Property property = new Property(type, null, method);
		TypeDescriptor typeDescriptor = new TypeDescriptor(property);
		this.writerCache.put(cacheKey, method);
		this.typeDescriptorCache.put(cacheKey, typeDescriptor);
		return true;
	}
	else {
		Field field = findField(name, type, target);
		if (field != null) {
			this.writerCache.put(cacheKey, field);
			this.typeDescriptorCache.put(cacheKey, new TypeDescriptor(field));
			return true;
		}
	}
	return false;
}
 
Example 14
Source Project: piper   Source File: Concat.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public TypedValue execute (EvaluationContext aContext, Object aTarget, Object... aArguments) throws AccessException {
  List<?> l1 = (List<?>) aArguments[0];
  List<?> l2 = (List<?>) aArguments[1];
  List<Object> joined = new ArrayList<>(l1.size()+l2.size());
  joined.addAll(l1);
  joined.addAll(l2);
  return new TypedValue(joined);
}
 
Example 15
Source Project: spring-analysis-note   Source File: EvalTag.java    License: MIT License 5 votes vote down vote up
@Override
public TypedValue read(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
	Object implicitVar = resolveImplicitVariable(name);
	if (implicitVar != null) {
		return new TypedValue(implicitVar);
	}
	return new TypedValue(this.pageContext.findAttribute(name));
}
 
Example 16
Source Project: spring-analysis-note   Source File: MethodReference.java    License: MIT License 5 votes vote down vote up
private MethodExecutor findAccessorForMethod(List<TypeDescriptor> argumentTypes, Object targetObject,
		EvaluationContext evaluationContext) throws SpelEvaluationException {

	AccessException accessException = null;
	List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
	for (MethodResolver methodResolver : methodResolvers) {
		try {
			MethodExecutor methodExecutor = methodResolver.resolve(
					evaluationContext, targetObject, this.name, argumentTypes);
			if (methodExecutor != null) {
				return methodExecutor;
			}
		}
		catch (AccessException ex) {
			accessException = ex;
			break;
		}
	}

	String method = FormatHelper.formatMethodForMessage(this.name, argumentTypes);
	String className = FormatHelper.formatClassNameForMessage(
			targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass());
	if (accessException != null) {
		throw new SpelEvaluationException(
				getStartPosition(), accessException, SpelMessage.PROBLEM_LOCATING_METHOD, method, className);
	}
	else {
		throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_NOT_FOUND, method, className);
	}
}
 
Example 17
private TypeDescriptor getTypeDescriptor(EvaluationContext context, Object target, String name) {
	if (target == null) {
		return null;
	}
	Class<?> type = (target instanceof Class ? (Class<?>) target : target.getClass());

	if (type.isArray() && name.equals("length")) {
		return TypeDescriptor.valueOf(Integer.TYPE);
	}
	CacheKey cacheKey = new CacheKey(type, name, target instanceof Class);
	TypeDescriptor typeDescriptor = this.typeDescriptorCache.get(cacheKey);
	if (typeDescriptor == null) {
		// attempt to populate the cache entry
		try {
			if (canRead(context, target, name)) {
				typeDescriptor = this.typeDescriptorCache.get(cacheKey);
			}
			else if (canWrite(context, target, name)) {
				typeDescriptor = this.typeDescriptorCache.get(cacheKey);
			}
		}
		catch (AccessException ex) {
			// continue with null type descriptor
		}
	}
	return typeDescriptor;
}
 
Example 18
Source Project: spring4-understanding   Source File: MethodReference.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Decode the AccessException, throwing a lightweight evaluation exception or, if the
 * cause was a RuntimeException, throw the RuntimeException directly.
 */
private void throwSimpleExceptionIfPossible(Object value, AccessException ex) {
	if (ex.getCause() instanceof InvocationTargetException) {
		Throwable rootCause = ex.getCause().getCause();
		if (rootCause instanceof RuntimeException) {
			throw (RuntimeException) rootCause;
		}
		throw new ExpressionInvocationTargetException(getStartPosition(),
				"A problem occurred when trying to execute method '" + this.name +
				"' on object of type [" + value.getClass().getName() + "]", rootCause);
	}
}
 
Example 19
@Override
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
	if (target == null) {
		return false;
	}
	Class<?> type = (target instanceof Class ? (Class<?>) target : target.getClass());
	CacheKey cacheKey = new CacheKey(type, name, target instanceof Class);
	if (this.writerCache.containsKey(cacheKey)) {
		return true;
	}
	Method method = findSetterForProperty(name, type, target);
	if (method != null) {
		// Treat it like a property
		Property property = new Property(type, null, method);
		TypeDescriptor typeDescriptor = new TypeDescriptor(property);
		this.writerCache.put(cacheKey, method);
		this.typeDescriptorCache.put(cacheKey, typeDescriptor);
		return true;
	}
	else {
		Field field = findField(name, type, target);
		if (field != null) {
			this.writerCache.put(cacheKey, field);
			this.typeDescriptorCache.put(cacheKey, new TypeDescriptor(field));
			return true;
		}
	}
	return false;
}
 
Example 20
Source Project: spring4-understanding   Source File: EvalTag.java    License: Apache License 2.0 5 votes vote down vote up
private Object resolveImplicitVariable(String name) throws AccessException {
	if (this.variableResolver == null) {
		return null;
	}
	try {
		return this.variableResolver.resolveVariable(name);
	}
	catch (Exception ex) {
		throw new AccessException(
				"Unexpected exception occurred accessing '" + name + "' as an implicit variable", ex);
	}
}
 
Example 21
Source Project: spring4-understanding   Source File: EvaluationTests.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object resolve(EvaluationContext context, String beanName)
		throws AccessException {
	if (beanName.equals("foo") || beanName.equals("bar")) {
		return new Spr9751_2();
	}
	throw new AccessException("not heard of "+beanName);
}
 
Example 22
Source Project: piper   Source File: Range.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public TypedValue execute(EvaluationContext aContext, Object aTarget, Object... aArguments) throws AccessException {
  List<Integer> value = IntStream.rangeClosed((int)aArguments[0], (int)aArguments[1])
      .boxed()
      .collect(Collectors.toList());
  return new TypedValue(value);
}
 
Example 23
Source Project: piper   Source File: Join.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public TypedValue execute (EvaluationContext aContext, Object aTarget, Object... aArguments) throws AccessException {
  String separator = (String) aArguments[0];
  List<?> values = (List<?>) aArguments[1];
  String str = values.stream()
                     .map(String::valueOf)
                     .collect(Collectors.joining(separator));
  return new TypedValue(str);
}
 
Example 24
private TypeDescriptor getTypeDescriptor(EvaluationContext context, Object target, String name) {
	if (target == null) {
		return null;
	}
	Class<?> type = (target instanceof Class ? (Class<?>) target : target.getClass());

	if (type.isArray() && name.equals("length")) {
		return TypeDescriptor.valueOf(Integer.TYPE);
	}
	PropertyCacheKey cacheKey = new PropertyCacheKey(type, name, target instanceof Class);
	TypeDescriptor typeDescriptor = this.typeDescriptorCache.get(cacheKey);
	if (typeDescriptor == null) {
		// attempt to populate the cache entry
		try {
			if (canRead(context, target, name)) {
				typeDescriptor = this.typeDescriptorCache.get(cacheKey);
			}
			else if (canWrite(context, target, name)) {
				typeDescriptor = this.typeDescriptorCache.get(cacheKey);
			}
		}
		catch (AccessException ex) {
			// continue with null type descriptor
		}
	}
	return typeDescriptor;
}
 
Example 25
Source Project: spring4-understanding   Source File: SpelReproTests.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
	return getMap(target).containsKey(name);
}
 
Example 26
Source Project: spring-analysis-note   Source File: EnvironmentAccessor.java    License: MIT License 4 votes vote down vote up
/**
 * Read-only: no-op.
 */
@Override
public void write(EvaluationContext context, @Nullable Object target, String name, @Nullable Object newValue)
		throws AccessException {
}
 
Example 27
@Override
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
  return false;
}
 
Example 28
Source Project: spring-analysis-note   Source File: BeanFactoryAccessor.java    License: MIT License 4 votes vote down vote up
@Override
public TypedValue read(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
	Assert.state(target instanceof BeanFactory, "Target must be of type BeanFactory");
	return new TypedValue(((BeanFactory) target).getBean(name));
}
 
Example 29
Source Project: java-technology-stack   Source File: SpelReproTests.java    License: MIT License 4 votes vote down vote up
@Override
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
	return (((Map<?, ?>) target).containsKey(name));
}
 
Example 30
Source Project: spring4-understanding   Source File: SpelReproTests.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
	return new TypedValue(((Map<?, ?>) target).get(name));
}