org.springframework.expression.AccessException Java Examples

The following examples show how to use org.springframework.expression.AccessException. 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: BeanReference.java    From spring4-understanding with 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 #2
Source File: ReflectivePropertyAccessor.java    From java-technology-stack with 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 #3
Source File: BeanReference.java    From lams with 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 #4
Source File: ReflectivePropertyAccessor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 #5
Source File: ReflectiveConstructorExecutor.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 #6
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 #7
Source File: MethodReference.java    From lams with 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 #8
Source File: PropertyOrFieldReference.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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 #9
Source File: MethodReference.java    From spring4-understanding with 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 #10
Source File: ReflectivePropertyAccessor.java    From spring-analysis-note with 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 #11
Source File: ReflectivePropertyAccessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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 #12
Source File: MapPropertyAccessor.java    From kork with 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
Source File: Concat.java    From piper with 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 #14
Source File: ReflectivePropertyAccessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 #15
Source File: EvalTag.java    From spring-analysis-note with 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 File: MethodReference.java    From spring-analysis-note with 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
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 #18
Source File: MethodReference.java    From spring4-understanding with 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
Source File: ReflectivePropertyAccessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 File: EvalTag.java    From spring4-understanding with 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 File: EvaluationTests.java    From spring4-understanding with 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 File: Range.java    From piper with 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 File: Join.java    From piper with 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
Source File: ReflectivePropertyAccessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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 File: SpelReproTests.java    From spring-analysis-note with MIT License 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));
}
 
Example #26
Source File: ExpressionLanguageScenarioTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException {
}
 
Example #27
Source File: BeanFactoryAccessor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException {
	throw new AccessException("Beans in a BeanFactory are read-only");
}
 
Example #28
Source File: MapAccessTests.java    From java-technology-stack with MIT License 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));
}
 
Example #29
Source File: ScenariosForSpringSecurity.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void write(EvaluationContext context, Object target, String name, Object newValue)
		throws AccessException {
}
 
Example #30
Source File: CustomEvaluationContextConfigurerTest.java    From docx-stamper with MIT License 4 votes vote down vote up
@Override
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
  return false;
}