use of org.springframework.expression.PropertyAccessor in project spring-framework by spring-projects.
the class SpelReproTests method accessingNullPropertyViaReflection_SPR5663.
@Test
public void accessingNullPropertyViaReflection_SPR5663() throws AccessException {
PropertyAccessor propertyAccessor = new ReflectivePropertyAccessor();
EvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
assertFalse(propertyAccessor.canRead(context, null, "abc"));
assertFalse(propertyAccessor.canWrite(context, null, "abc"));
try {
propertyAccessor.read(context, null, "abc");
fail("Should have failed with an AccessException");
} catch (AccessException ae) {
// success
}
try {
propertyAccessor.write(context, null, "abc", "foo");
fail("Should have failed with an AccessException");
} catch (AccessException ae) {
// success
}
}
use of org.springframework.expression.PropertyAccessor in project spring-framework by spring-projects.
the class ReflectionHelperTests method testOptimalReflectivePropertyResolver.
@Test
public void testOptimalReflectivePropertyResolver() throws Exception {
ReflectivePropertyAccessor rpr = new ReflectivePropertyAccessor();
Tester t = new Tester();
t.setProperty("hello");
EvaluationContext ctx = new StandardEvaluationContext(t);
// assertTrue(rpr.canRead(ctx, t, "property"));
// assertEquals("hello",rpr.read(ctx, t, "property").getValue());
// assertEquals("hello",rpr.read(ctx, t, "property").getValue()); // cached accessor used
PropertyAccessor optA = rpr.createOptimalAccessor(ctx, t, "property");
assertTrue(optA.canRead(ctx, t, "property"));
assertFalse(optA.canRead(ctx, t, "property2"));
try {
optA.canWrite(ctx, t, "property");
fail();
} catch (UnsupportedOperationException uoe) {
// success
}
try {
optA.canWrite(ctx, t, "property2");
fail();
} catch (UnsupportedOperationException uoe) {
// success
}
assertEquals("hello", optA.read(ctx, t, "property").getValue());
// cached accessor used
assertEquals("hello", optA.read(ctx, t, "property").getValue());
try {
optA.getSpecificTargetClasses();
fail();
} catch (UnsupportedOperationException uoe) {
// success
}
try {
optA.write(ctx, t, "property", null);
fail();
} catch (UnsupportedOperationException uoe) {
// success
}
optA = rpr.createOptimalAccessor(ctx, t, "field");
assertTrue(optA.canRead(ctx, t, "field"));
assertFalse(optA.canRead(ctx, t, "field2"));
try {
optA.canWrite(ctx, t, "field");
fail();
} catch (UnsupportedOperationException uoe) {
// success
}
try {
optA.canWrite(ctx, t, "field2");
fail();
} catch (UnsupportedOperationException uoe) {
// success
}
assertEquals(3, optA.read(ctx, t, "field").getValue());
// cached accessor used
assertEquals(3, optA.read(ctx, t, "field").getValue());
try {
optA.getSpecificTargetClasses();
fail();
} catch (UnsupportedOperationException uoe) {
// success
}
try {
optA.write(ctx, t, "field", null);
fail();
} catch (UnsupportedOperationException uoe) {
// success
}
}
use of org.springframework.expression.PropertyAccessor in project spring-integration by spring-projects.
the class IntegrationSimpleEvaluationContextFactoryBean method getObject.
@Override
public SimpleEvaluationContext getObject() throws Exception {
Collection<PropertyAccessor> accessors = getPropertyAccessors().values();
PropertyAccessor[] accessorArray = accessors.toArray(new PropertyAccessor[accessors.size() + 2]);
accessorArray[accessors.size()] = new MapAccessor();
accessorArray[accessors.size() + 1] = DataBindingPropertyAccessor.forReadOnlyAccess();
SimpleEvaluationContext evaluationContext = SimpleEvaluationContext.forPropertyAccessors(accessorArray).withTypeConverter(getTypeConverter()).withInstanceMethods().build();
for (Entry<String, Method> functionEntry : getFunctions().entrySet()) {
evaluationContext.setVariable(functionEntry.getKey(), functionEntry.getValue());
}
return evaluationContext;
}
use of org.springframework.expression.PropertyAccessor in project spring-framework by spring-projects.
the class ReflectionHelperTests method testOptimalReflectivePropertyAccessor.
@Test
public void testOptimalReflectivePropertyAccessor() throws Exception {
ReflectivePropertyAccessor reflective = new ReflectivePropertyAccessor();
Tester tester = new Tester();
tester.setProperty("hello");
EvaluationContext ctx = new StandardEvaluationContext(tester);
assertThat(reflective.canRead(ctx, tester, "property")).isTrue();
assertThat(reflective.read(ctx, tester, "property").getValue()).isEqualTo("hello");
// cached accessor used
assertThat(reflective.read(ctx, tester, "property").getValue()).isEqualTo("hello");
PropertyAccessor property = reflective.createOptimalAccessor(ctx, tester, "property");
assertThat(property.canRead(ctx, tester, "property")).isTrue();
assertThat(property.canRead(ctx, tester, "property2")).isFalse();
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> property.canWrite(ctx, tester, "property"));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> property.canWrite(ctx, tester, "property2"));
assertThat(property.read(ctx, tester, "property").getValue()).isEqualTo("hello");
// cached accessor used
assertThat(property.read(ctx, tester, "property").getValue()).isEqualTo("hello");
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(property::getSpecificTargetClasses);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> property.write(ctx, tester, "property", null));
PropertyAccessor field = reflective.createOptimalAccessor(ctx, tester, "field");
assertThat(field.canRead(ctx, tester, "field")).isTrue();
assertThat(field.canRead(ctx, tester, "field2")).isFalse();
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> field.canWrite(ctx, tester, "field"));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> field.canWrite(ctx, tester, "field2"));
assertThat(field.read(ctx, tester, "field").getValue()).isEqualTo(3);
// cached accessor used
assertThat(field.read(ctx, tester, "field").getValue()).isEqualTo(3);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(field::getSpecificTargetClasses);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> field.write(ctx, tester, "field", null));
}
use of org.springframework.expression.PropertyAccessor in project spring-framework by spring-projects.
the class PropertyOrFieldReference method generateCode.
@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);
}
Aggregations