org.springframework.expression.PropertyAccessor Java Examples

The following examples show how to use org.springframework.expression.PropertyAccessor. 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: SpelExpressionConverterConfigurationTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@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 #2
Source File: PropertyAccessTests.java    From java-technology-stack with 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 File: PropertyOrFieldReference.java    From java-technology-stack with 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 File: PropertyAccessTests.java    From spring-analysis-note with 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
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 #6
Source File: PropertyOrFieldReference.java    From spring4-understanding with Apache License 2.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 #7
Source File: PropertyOrFieldReference.java    From spring-analysis-note with 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 #8
Source File: PropertyAccessTests.java    From spring4-understanding with 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 #9
Source File: AstUtils.java    From lams with 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 #10
Source File: SimpleEvaluationContext.java    From spring-analysis-note with 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 #11
Source File: AstUtils.java    From java-technology-stack with 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 #12
Source File: PropertyOrFieldReference.java    From java-technology-stack with 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 #13
Source File: PropertyOrFieldReference.java    From java-technology-stack with 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 #14
Source File: PropertyOrFieldReference.java    From spring-analysis-note with 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 #15
Source File: SimpleEvaluationContext.java    From java-technology-stack with 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 #16
Source File: SimpleEvaluationContext.java    From java-technology-stack with 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 #17
Source File: StandardEvaluationContext.java    From java-technology-stack with 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 #18
Source File: PropertyOrFieldReference.java    From spring-analysis-note with 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 #19
Source File: Indexer.java    From lams with 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 #20
Source File: PropertyOrFieldReference.java    From spring4-understanding with Apache License 2.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 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 #21
Source File: PropertyOrFieldReference.java    From lams with GNU General Public License v2.0 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;
		this.exitTypeDescriptor = CodeFlow.toDescriptor(accessor.getPropertyType());
	}
	return tv;
}
 
Example #22
Source File: StandardEvaluationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private synchronized void initializePropertyAccessors() {
	if (this.propertyAccessors == null) {
		List<PropertyAccessor> defaultAccessors = new ArrayList<PropertyAccessor>();
		defaultAccessors.add(new ReflectivePropertyAccessor());
		this.propertyAccessors = defaultAccessors;
	}
}
 
Example #23
Source File: PropertyOrFieldReference.java    From lams with 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 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 #24
Source File: PropertyOrFieldReference.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 #25
Source File: PropertyOrFieldReference.java    From lams with GNU General Public License v2.0 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.exitTypeDescriptor =
				CodeFlow.toDescriptor(((CompilablePropertyAccessor) accessorToUse).getPropertyType());
	}
	return value;
}
 
Example #26
Source File: StandardEvaluationContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private synchronized void initializePropertyAccessors() {
	if (this.propertyAccessors == null) {
		List<PropertyAccessor> defaultAccessors = new ArrayList<PropertyAccessor>();
		defaultAccessors.add(new ReflectivePropertyAccessor());
		this.propertyAccessors = defaultAccessors;
	}
}
 
Example #27
Source File: Indexer.java    From spring4-understanding with 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());
	}
}
 
Example #28
Source File: AstUtils.java    From spring4-understanding with Apache License 2.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 #29
Source File: PropertyOrFieldReference.java    From spring4-understanding with Apache License 2.0 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;
		this.exitTypeDescriptor = CodeFlow.toDescriptor(accessor.getPropertyType());
	}
	return tv;
}
 
Example #30
Source File: PropertyOrFieldReference.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
}