Java Code Examples for org.springframework.expression.PropertyAccessor

The following examples show how to use org.springframework.expression.PropertyAccessor. 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
Source Project: spring-analysis-note   Source File: PropertyOrFieldReference.java    License: MIT License 6 votes vote down vote up
public boolean isWritableProperty(String name, TypedValue contextObject, EvaluationContext evalContext)
		throws EvaluationException {

	Object value = contextObject.getValue();
	if (value != null) {
		List<PropertyAccessor> accessorsToTry =
				getPropertyAccessorsToTry(contextObject.getValue(), evalContext.getPropertyAccessors());
		for (PropertyAccessor accessor : accessorsToTry) {
			try {
				if (accessor.canWrite(evalContext, value, name)) {
					return true;
				}
			}
			catch (AccessException ex) {
				// let others try
			}
		}
	}
	return false;
}
 
Example 2
Source Project: spring-analysis-note   Source File: PropertyAccessTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testAddingRemovingAccessors() {
	StandardEvaluationContext ctx = new StandardEvaluationContext();

	// reflective property accessor is the only one by default
	List<PropertyAccessor> propertyAccessors = ctx.getPropertyAccessors();
	assertEquals(1,propertyAccessors.size());

	StringyPropertyAccessor spa = new StringyPropertyAccessor();
	ctx.addPropertyAccessor(spa);
	assertEquals(2,ctx.getPropertyAccessors().size());

	List<PropertyAccessor> copy = new ArrayList<>();
	copy.addAll(ctx.getPropertyAccessors());
	assertTrue(ctx.removePropertyAccessor(spa));
	assertFalse(ctx.removePropertyAccessor(spa));
	assertEquals(1,ctx.getPropertyAccessors().size());

	ctx.setPropertyAccessors(copy);
	assertEquals(2,ctx.getPropertyAccessors().size());
}
 
Example 3
Source Project: java-technology-stack   Source File: PropertyOrFieldReference.java    License: MIT License 6 votes vote down vote up
public boolean isWritableProperty(String name, TypedValue contextObject, EvaluationContext evalContext)
		throws EvaluationException {

	Object value = contextObject.getValue();
	if (value != null) {
		List<PropertyAccessor> accessorsToTry =
				getPropertyAccessorsToTry(contextObject.getValue(), evalContext.getPropertyAccessors());
		for (PropertyAccessor accessor : accessorsToTry) {
			try {
				if (accessor.canWrite(evalContext, value, name)) {
					return true;
				}
			}
			catch (AccessException ex) {
				// let others try
			}
		}
	}
	return false;
}
 
Example 4
Source Project: java-technology-stack   Source File: PropertyAccessTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testAddingRemovingAccessors() {
	StandardEvaluationContext ctx = new StandardEvaluationContext();

	// reflective property accessor is the only one by default
	List<PropertyAccessor> propertyAccessors = ctx.getPropertyAccessors();
	assertEquals(1,propertyAccessors.size());

	StringyPropertyAccessor spa = new StringyPropertyAccessor();
	ctx.addPropertyAccessor(spa);
	assertEquals(2,ctx.getPropertyAccessors().size());

	List<PropertyAccessor> copy = new ArrayList<>();
	copy.addAll(ctx.getPropertyAccessors());
	assertTrue(ctx.removePropertyAccessor(spa));
	assertFalse(ctx.removePropertyAccessor(spa));
	assertEquals(1,ctx.getPropertyAccessors().size());

	ctx.setPropertyAccessors(copy);
	assertEquals(2,ctx.getPropertyAccessors().size());
}
 
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
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 7
Source Project: spring4-understanding   Source File: PropertyAccessTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testAddingRemovingAccessors() {
	StandardEvaluationContext ctx = new StandardEvaluationContext();

	// reflective property accessor is the only one by default
	List<PropertyAccessor> propertyAccessors = ctx.getPropertyAccessors();
	assertEquals(1,propertyAccessors.size());

	StringyPropertyAccessor spa = new StringyPropertyAccessor();
	ctx.addPropertyAccessor(spa);
	assertEquals(2,ctx.getPropertyAccessors().size());

	List<PropertyAccessor> copy = new ArrayList<PropertyAccessor>();
	copy.addAll(ctx.getPropertyAccessors());
	assertTrue(ctx.removePropertyAccessor(spa));
	assertFalse(ctx.removePropertyAccessor(spa));
	assertEquals(1,ctx.getPropertyAccessors().size());

	ctx.setPropertyAccessors(copy);
	assertEquals(2,ctx.getPropertyAccessors().size());
}
 
Example 8
@Test
@SuppressWarnings("unchecked")
public void converterCorrectlyInstalled() {
	Expression expression = this.pojo.getExpression();
	assertThat(expression.getValue("{\"a\": {\"b\": 5}}").toString()).isEqualTo("5");

	List<PropertyAccessor> propertyAccessors = TestUtils.getPropertyValue(
			this.evaluationContext, "propertyAccessors", List.class);

	assertThat(propertyAccessors)
			.hasAtLeastOneElementOfType(JsonPropertyAccessor.class);

	propertyAccessors = TestUtils.getPropertyValue(this.config.evaluationContext,
			"propertyAccessors", List.class);

	assertThat(propertyAccessors)
			.hasAtLeastOneElementOfType(JsonPropertyAccessor.class);
}
 
Example 9
Source Project: spring-analysis-note   Source File: AstUtils.java    License: MIT License 5 votes vote down vote up
/**
 * Determines the set of property resolvers that should be used to try and access a
 * property on the specified target type. The resolvers are considered to be in an
 * ordered list, however in the returned list any that are exact matches for the input
 * target type (as opposed to 'general' resolvers that could work for any type) are
 * placed at the start of the list. In addition, there are specific resolvers that
 * exactly name the class in question and resolvers that name a specific class but it
 * is a supertype of the class we have. These are put at the end of the specific resolvers
 * set and will be tried after exactly matching accessors but before generic accessors.
 * @param targetType the type upon which property access is being attempted
 * @return a list of resolvers that should be tried in order to access the property
 */
public static List<PropertyAccessor> getPropertyAccessorsToTry(
		@Nullable Class<?> targetType, List<PropertyAccessor> propertyAccessors) {

	List<PropertyAccessor> specificAccessors = new ArrayList<>();
	List<PropertyAccessor> generalAccessors = new ArrayList<>();
	for (PropertyAccessor resolver : propertyAccessors) {
		Class<?>[] targets = resolver.getSpecificTargetClasses();
		if (targets == null) {  // generic resolver that says it can be used for any type
			generalAccessors.add(resolver);
		}
		else {
			if (targetType != null) {
				int pos = 0;
				for (Class<?> clazz : targets) {
					if (clazz == targetType) {  // put exact matches on the front to be tried first?
						specificAccessors.add(pos++, resolver);
					}
					else if (clazz.isAssignableFrom(targetType)) {  // put supertype matches at the end of the
						// specificAccessor list
						generalAccessors.add(resolver);
					}
				}
			}
		}
	}
	List<PropertyAccessor> resolvers = new ArrayList<>(specificAccessors.size() + generalAccessors.size());
	resolvers.addAll(specificAccessors);
	resolvers.addAll(generalAccessors);
	return resolvers;
}
 
Example 10
Source Project: spring-analysis-note   Source File: PropertyOrFieldReference.java    License: MIT License 5 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	TypedValue tv = getValueInternal(state.getActiveContextObject(), state.getEvaluationContext(),
			state.getConfiguration().isAutoGrowNullReferences());
	PropertyAccessor accessorToUse = this.cachedReadAccessor;
	if (accessorToUse instanceof CompilablePropertyAccessor) {
		CompilablePropertyAccessor accessor = (CompilablePropertyAccessor) accessorToUse;
		setExitTypeDescriptor(CodeFlow.toDescriptor(accessor.getPropertyType()));
	}
	return tv;
}
 
Example 11
Source Project: spring-analysis-note   Source File: PropertyOrFieldReference.java    License: MIT License 5 votes vote down vote up
/**
 * Determines the set of property resolvers that should be used to try and access a property
 * on the specified target type. The resolvers are considered to be in an ordered list,
 * however in the returned list any that are exact matches for the input target type (as
 * opposed to 'general' resolvers that could work for any type) are placed at the start of the
 * list. In addition, there are specific resolvers that exactly name the class in question
 * and resolvers that name a specific class but it is a supertype of the class we have.
 * These are put at the end of the specific resolvers set and will be tried after exactly
 * matching accessors but before generic accessors.
 * @param contextObject the object upon which property access is being attempted
 * @return a list of resolvers that should be tried in order to access the property
 */
private List<PropertyAccessor> getPropertyAccessorsToTry(
		@Nullable Object contextObject, List<PropertyAccessor> propertyAccessors) {

	Class<?> targetType = (contextObject != null ? contextObject.getClass() : null);

	List<PropertyAccessor> specificAccessors = new ArrayList<>();
	List<PropertyAccessor> generalAccessors = new ArrayList<>();
	for (PropertyAccessor resolver : propertyAccessors) {
		Class<?>[] targets = resolver.getSpecificTargetClasses();
		if (targets == null) {
			// generic resolver that says it can be used for any type
			generalAccessors.add(resolver);
		}
		else if (targetType != null) {
			for (Class<?> clazz : targets) {
				if (clazz == targetType) {
					specificAccessors.add(resolver);
					break;
				}
				else if (clazz.isAssignableFrom(targetType)) {
					generalAccessors.add(resolver);
				}
			}
		}
	}
	List<PropertyAccessor> resolvers = new ArrayList<>(specificAccessors);
	generalAccessors.removeAll(specificAccessors);
	resolvers.addAll(generalAccessors);
	return resolvers;
}
 
Example 12
Source Project: spring-analysis-note   Source File: PropertyOrFieldReference.java    License: MIT License 5 votes vote down vote up
@Override
public TypedValue getValue() {
	TypedValue value =
			this.ref.getValueInternal(this.contextObject, this.evalContext, this.autoGrowNullReferences);
	PropertyAccessor accessorToUse = this.ref.cachedReadAccessor;
	if (accessorToUse instanceof CompilablePropertyAccessor) {
		this.ref.setExitTypeDescriptor(CodeFlow.toDescriptor(((CompilablePropertyAccessor) accessorToUse).getPropertyType()));
	}
	return value;
}
 
Example 13
Source Project: spring-analysis-note   Source File: SimpleEvaluationContext.java    License: MIT License 5 votes vote down vote up
private SimpleEvaluationContext(List<PropertyAccessor> accessors, List<MethodResolver> resolvers,
		@Nullable TypeConverter converter, @Nullable TypedValue rootObject) {

	this.propertyAccessors = accessors;
	this.methodResolvers = resolvers;
	this.typeConverter = (converter != null ? converter : new StandardTypeConverter());
	this.rootObject = (rootObject != null ? rootObject : TypedValue.NULL);
}
 
Example 14
Source Project: spring-analysis-note   Source File: SimpleEvaluationContext.java    License: MIT License 5 votes vote down vote up
/**
 * Create a {@code SimpleEvaluationContext} for the specified {@link PropertyAccessor}
 * delegates: typically a custom {@code PropertyAccessor} specific to a use case
 * (e.g. attribute resolution in a custom data structure), potentially combined with
 * a {@link DataBindingPropertyAccessor} if property dereferences are needed as well.
 * @param accessors the accessor delegates to use
 * @see DataBindingPropertyAccessor#forReadOnlyAccess()
 * @see DataBindingPropertyAccessor#forReadWriteAccess()
 */
public static Builder forPropertyAccessors(PropertyAccessor... accessors) {
	for (PropertyAccessor accessor : accessors) {
		if (accessor.getClass() == ReflectivePropertyAccessor.class) {
			throw new IllegalArgumentException("SimpleEvaluationContext is not designed for use with a plain " +
					"ReflectivePropertyAccessor. Consider using DataBindingPropertyAccessor or a custom subclass.");
		}
	}
	return new Builder(accessors);
}
 
Example 15
Source Project: spring-analysis-note   Source File: StandardEvaluationContext.java    License: MIT License 5 votes vote down vote up
private List<PropertyAccessor> initPropertyAccessors() {
	List<PropertyAccessor> accessors = this.propertyAccessors;
	if (accessors == null) {
		accessors = new ArrayList<>(5);
		accessors.add(new ReflectivePropertyAccessor());
		this.propertyAccessors = accessors;
	}
	return accessors;
}
 
Example 16
Source Project: java-technology-stack   Source File: AstUtils.java    License: MIT License 5 votes vote down vote up
/**
 * Determines the set of property resolvers that should be used to try and access a
 * property on the specified target type. The resolvers are considered to be in an
 * ordered list, however in the returned list any that are exact matches for the input
 * target type (as opposed to 'general' resolvers that could work for any type) are
 * placed at the start of the list. In addition, there are specific resolvers that
 * exactly name the class in question and resolvers that name a specific class but it
 * is a supertype of the class we have. These are put at the end of the specific resolvers
 * set and will be tried after exactly matching accessors but before generic accessors.
 * @param targetType the type upon which property access is being attempted
 * @return a list of resolvers that should be tried in order to access the property
 */
public static List<PropertyAccessor> getPropertyAccessorsToTry(
		@Nullable Class<?> targetType, List<PropertyAccessor> propertyAccessors) {

	List<PropertyAccessor> specificAccessors = new ArrayList<>();
	List<PropertyAccessor> generalAccessors = new ArrayList<>();
	for (PropertyAccessor resolver : propertyAccessors) {
		Class<?>[] targets = resolver.getSpecificTargetClasses();
		if (targets == null) {  // generic resolver that says it can be used for any type
			generalAccessors.add(resolver);
		}
		else {
			if (targetType != null) {
				int pos = 0;
				for (Class<?> clazz : targets) {
					if (clazz == targetType) {  // put exact matches on the front to be tried first?
						specificAccessors.add(pos++, resolver);
					}
					else if (clazz.isAssignableFrom(targetType)) {  // put supertype matches at the end of the
						// specificAccessor list
						generalAccessors.add(resolver);
					}
				}
			}
		}
	}
	List<PropertyAccessor> resolvers = new ArrayList<>(specificAccessors.size() + generalAccessors.size());
	resolvers.addAll(specificAccessors);
	resolvers.addAll(generalAccessors);
	return resolvers;
}
 
Example 17
Source Project: java-technology-stack   Source File: PropertyOrFieldReference.java    License: MIT License 5 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	TypedValue tv = getValueInternal(state.getActiveContextObject(), state.getEvaluationContext(),
			state.getConfiguration().isAutoGrowNullReferences());
	PropertyAccessor accessorToUse = this.cachedReadAccessor;
	if (accessorToUse instanceof CompilablePropertyAccessor) {
		CompilablePropertyAccessor accessor = (CompilablePropertyAccessor) accessorToUse;
		setExitTypeDescriptor(CodeFlow.toDescriptor(accessor.getPropertyType()));
	}
	return tv;
}
 
Example 18
Source Project: java-technology-stack   Source File: PropertyOrFieldReference.java    License: MIT License 5 votes vote down vote up
/**
 * Determines the set of property resolvers that should be used to try and access a property
 * on the specified target type. The resolvers are considered to be in an ordered list,
 * however in the returned list any that are exact matches for the input target type (as
 * opposed to 'general' resolvers that could work for any type) are placed at the start of the
 * list. In addition, there are specific resolvers that exactly name the class in question
 * and resolvers that name a specific class but it is a supertype of the class we have.
 * These are put at the end of the specific resolvers set and will be tried after exactly
 * matching accessors but before generic accessors.
 * @param contextObject the object upon which property access is being attempted
 * @return a list of resolvers that should be tried in order to access the property
 */
private List<PropertyAccessor> getPropertyAccessorsToTry(
		@Nullable Object contextObject, List<PropertyAccessor> propertyAccessors) {

	Class<?> targetType = (contextObject != null ? contextObject.getClass() : null);

	List<PropertyAccessor> specificAccessors = new ArrayList<>();
	List<PropertyAccessor> generalAccessors = new ArrayList<>();
	for (PropertyAccessor resolver : propertyAccessors) {
		Class<?>[] targets = resolver.getSpecificTargetClasses();
		if (targets == null) {
			// generic resolver that says it can be used for any type
			generalAccessors.add(resolver);
		}
		else if (targetType != null) {
			for (Class<?> clazz : targets) {
				if (clazz == targetType) {
					specificAccessors.add(resolver);
					break;
				}
				else if (clazz.isAssignableFrom(targetType)) {
					generalAccessors.add(resolver);
				}
			}
		}
	}
	List<PropertyAccessor> resolvers = new ArrayList<>(specificAccessors);
	generalAccessors.removeAll(specificAccessors);
	resolvers.addAll(generalAccessors);
	return resolvers;
}
 
Example 19
Source Project: java-technology-stack   Source File: PropertyOrFieldReference.java    License: MIT License 5 votes vote down vote up
@Override
public TypedValue getValue() {
	TypedValue value =
			this.ref.getValueInternal(this.contextObject, this.evalContext, this.autoGrowNullReferences);
	PropertyAccessor accessorToUse = this.ref.cachedReadAccessor;
	if (accessorToUse instanceof CompilablePropertyAccessor) {
		this.ref.setExitTypeDescriptor(CodeFlow.toDescriptor(((CompilablePropertyAccessor) accessorToUse).getPropertyType()));
	}
	return value;
}
 
Example 20
Source Project: java-technology-stack   Source File: SimpleEvaluationContext.java    License: MIT License 5 votes vote down vote up
private SimpleEvaluationContext(List<PropertyAccessor> accessors, List<MethodResolver> resolvers,
		@Nullable TypeConverter converter, @Nullable TypedValue rootObject) {

	this.propertyAccessors = accessors;
	this.methodResolvers = resolvers;
	this.typeConverter = (converter != null ? converter : new StandardTypeConverter());
	this.rootObject = (rootObject != null ? rootObject : TypedValue.NULL);
}
 
Example 21
Source Project: java-technology-stack   Source File: SimpleEvaluationContext.java    License: MIT License 5 votes vote down vote up
/**
 * Create a {@code SimpleEvaluationContext} for the specified {@link PropertyAccessor}
 * delegates: typically a custom {@code PropertyAccessor} specific to a use case
 * (e.g. attribute resolution in a custom data structure), potentially combined with
 * a {@link DataBindingPropertyAccessor} if property dereferences are needed as well.
 * @param accessors the accessor delegates to use
 * @see DataBindingPropertyAccessor#forReadOnlyAccess()
 * @see DataBindingPropertyAccessor#forReadWriteAccess()
 */
public static Builder forPropertyAccessors(PropertyAccessor... accessors) {
	for (PropertyAccessor accessor : accessors) {
		if (accessor.getClass() == ReflectivePropertyAccessor.class) {
			throw new IllegalArgumentException("SimpleEvaluationContext is not designed for use with a plain " +
					"ReflectivePropertyAccessor. Consider using DataBindingPropertyAccessor or a custom subclass.");
		}
	}
	return new Builder(accessors);
}
 
Example 22
Source Project: java-technology-stack   Source File: StandardEvaluationContext.java    License: MIT License 5 votes vote down vote up
private List<PropertyAccessor> initPropertyAccessors() {
	List<PropertyAccessor> accessors = this.propertyAccessors;
	if (accessors == null) {
		accessors = new ArrayList<>(5);
		accessors.add(new ReflectivePropertyAccessor());
		this.propertyAccessors = accessors;
	}
	return accessors;
}
 
Example 23
Source Project: lams   Source File: Indexer.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void setValue(Object newValue) {
	Class<?> contextObjectClass = getObjectClass(this.targetObject);
	try {
		if (Indexer.this.cachedWriteName != null && Indexer.this.cachedWriteName.equals(this.name) &&
				Indexer.this.cachedWriteTargetType != null &&
				Indexer.this.cachedWriteTargetType.equals(contextObjectClass)) {
			// It is OK to use the cached accessor
			Indexer.this.cachedWriteAccessor.write(this.evaluationContext, this.targetObject, this.name, newValue);
			return;
		}
		List<PropertyAccessor> accessorsToTry =
				AstUtils.getPropertyAccessorsToTry(contextObjectClass, this.evaluationContext.getPropertyAccessors());
		if (accessorsToTry != null) {
			for (PropertyAccessor accessor : accessorsToTry) {
				if (accessor.canWrite(this.evaluationContext, this.targetObject, this.name)) {
					Indexer.this.cachedWriteName = this.name;
					Indexer.this.cachedWriteTargetType = contextObjectClass;
					Indexer.this.cachedWriteAccessor = accessor;
					accessor.write(this.evaluationContext, this.targetObject, this.name, newValue);
					return;
				}
			}
		}
	}
	catch (AccessException ex) {
		throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_PROPERTY_WRITE,
				this.name, ex.getMessage());
	}
}
 
Example 24
Source Project: lams   Source File: AstUtils.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determines the set of property resolvers that should be used to try and access a
 * property on the specified target type. The resolvers are considered to be in an
 * ordered list, however in the returned list any that are exact matches for the input
 * target type (as opposed to 'general' resolvers that could work for any type) are
 * placed at the start of the list. In addition, there are specific resolvers that
 * exactly name the class in question and resolvers that name a specific class but it
 * is a supertype of the class we have. These are put at the end of the specific resolvers
 * set and will be tried after exactly matching accessors but before generic accessors.
 * @param targetType the type upon which property access is being attempted
 * @return a list of resolvers that should be tried in order to access the property
 */
public static List<PropertyAccessor> getPropertyAccessorsToTry(
		Class<?> targetType, List<PropertyAccessor> propertyAccessors) {

	List<PropertyAccessor> specificAccessors = new ArrayList<PropertyAccessor>();
	List<PropertyAccessor> generalAccessors = new ArrayList<PropertyAccessor>();
	for (PropertyAccessor resolver : propertyAccessors) {
		Class<?>[] targets = resolver.getSpecificTargetClasses();
		if (targets == null) {  // generic resolver that says it can be used for any type
			generalAccessors.add(resolver);
		}
		else {
			if (targetType != null) {
				int pos = 0;
				for (Class<?> clazz : targets) {
					if (clazz == targetType) {  // put exact matches on the front to be tried first?
						specificAccessors.add(pos++, resolver);
					}
					else if (clazz.isAssignableFrom(targetType)) {  // put supertype matches at the end of the
						// specificAccessor list
						generalAccessors.add(resolver);
					}
				}
			}
		}
	}
	List<PropertyAccessor> resolvers = new LinkedList<PropertyAccessor>();
	resolvers.addAll(specificAccessors);
	resolvers.addAll(generalAccessors);
	return resolvers;
}
 
Example 25
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	TypedValue tv = getValueInternal(state.getActiveContextObject(), state.getEvaluationContext(),
			state.getConfiguration().isAutoGrowNullReferences());
	PropertyAccessor accessorToUse = this.cachedReadAccessor;
	if (accessorToUse instanceof CompilablePropertyAccessor) {
		CompilablePropertyAccessor accessor = (CompilablePropertyAccessor) accessorToUse;
		this.exitTypeDescriptor = CodeFlow.toDescriptor(accessor.getPropertyType());
	}
	return tv;
}
 
Example 26
/**
 * Determines the set of property resolvers that should be used to try and access a property
 * on the specified target type. The resolvers are considered to be in an ordered list,
 * however in the returned list any that are exact matches for the input target type (as
 * opposed to 'general' resolvers that could work for any type) are placed at the start of the
 * list. In addition, there are specific resolvers that exactly name the class in question
 * and resolvers that name a specific class but it is a supertype of the class we have.
 * These are put at the end of the specific resolvers set and will be tried after exactly
 * matching accessors but before generic accessors.
 * @param contextObject the object upon which property access is being attempted
 * @return a list of resolvers that should be tried in order to access the property
 */
private List<PropertyAccessor> getPropertyAccessorsToTry(Object contextObject, List<PropertyAccessor> propertyAccessors) {
	Class<?> targetType = (contextObject != null ? contextObject.getClass() : null);

	List<PropertyAccessor> specificAccessors = new ArrayList<PropertyAccessor>();
	List<PropertyAccessor> generalAccessors = new ArrayList<PropertyAccessor>();
	for (PropertyAccessor resolver : propertyAccessors) {
		Class<?>[] targets = resolver.getSpecificTargetClasses();
		if (targets == null) {
			// generic resolver that says it can be used for any type
			generalAccessors.add(resolver);
		}
		else if (targetType != null) {
			for (Class<?> clazz : targets) {
				if (clazz == targetType) {
					specificAccessors.add(resolver);
					break;
				}
				else if (clazz.isAssignableFrom(targetType)) {
					generalAccessors.add(resolver);
				}
			}
		}
	}
	List<PropertyAccessor> resolvers = new ArrayList<PropertyAccessor>();
	resolvers.addAll(specificAccessors);
	generalAccessors.removeAll(specificAccessors);
	resolvers.addAll(generalAccessors);
	return resolvers;
}
 
Example 27
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	PropertyAccessor accessorToUse = this.cachedReadAccessor;
	if (!(accessorToUse instanceof CompilablePropertyAccessor)) {
		throw new IllegalStateException("Property accessor is not compilable: " + accessorToUse);
	}
	((CompilablePropertyAccessor) accessorToUse).generateCode(this.name, mv, cf);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 28
@Override
public TypedValue getValue() {
	TypedValue value =
			this.ref.getValueInternal(this.contextObject, this.evalContext, this.autoGrowNullReferences);
	PropertyAccessor accessorToUse = this.ref.cachedReadAccessor;
	if (accessorToUse instanceof CompilablePropertyAccessor) {
		this.ref.exitTypeDescriptor =
				CodeFlow.toDescriptor(((CompilablePropertyAccessor) accessorToUse).getPropertyType());
	}
	return value;
}
 
Example 29
private synchronized void initializePropertyAccessors() {
	if (this.propertyAccessors == null) {
		List<PropertyAccessor> defaultAccessors = new ArrayList<PropertyAccessor>();
		defaultAccessors.add(new ReflectivePropertyAccessor());
		this.propertyAccessors = defaultAccessors;
	}
}
 
Example 30
Source Project: spring4-understanding   Source File: Indexer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void setValue(Object newValue) {
	Class<?> contextObjectClass = getObjectClass(this.targetObject);
	try {
		if (Indexer.this.cachedWriteName != null && Indexer.this.cachedWriteName.equals(this.name) &&
				Indexer.this.cachedWriteTargetType != null &&
				Indexer.this.cachedWriteTargetType.equals(contextObjectClass)) {
			// It is OK to use the cached accessor
			Indexer.this.cachedWriteAccessor.write(this.evaluationContext, this.targetObject, this.name, newValue);
			return;
		}
		List<PropertyAccessor> accessorsToTry =
				AstUtils.getPropertyAccessorsToTry(contextObjectClass, this.evaluationContext.getPropertyAccessors());
		if (accessorsToTry != null) {
			for (PropertyAccessor accessor : accessorsToTry) {
				if (accessor.canWrite(this.evaluationContext, this.targetObject, this.name)) {
					Indexer.this.cachedWriteName = this.name;
					Indexer.this.cachedWriteTargetType = contextObjectClass;
					Indexer.this.cachedWriteAccessor = accessor;
					accessor.write(this.evaluationContext, this.targetObject, this.name, newValue);
					return;
				}
			}
		}
	}
	catch (AccessException ex) {
		throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_PROPERTY_WRITE,
				this.name, ex.getMessage());
	}
}