Search in sources :

Example 6 with PropertyAccessor

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
    }
}
Also used : ReflectivePropertyAccessor(org.springframework.expression.spel.support.ReflectivePropertyAccessor) PropertyAccessor(org.springframework.expression.PropertyAccessor) AccessException(org.springframework.expression.AccessException) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ReflectivePropertyAccessor(org.springframework.expression.spel.support.ReflectivePropertyAccessor) Test(org.junit.Test)

Example 7 with PropertyAccessor

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
    }
}
Also used : PropertyAccessor(org.springframework.expression.PropertyAccessor) EvaluationContext(org.springframework.expression.EvaluationContext) Test(org.junit.Test)

Example 8 with PropertyAccessor

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;
}
Also used : DataBindingPropertyAccessor(org.springframework.expression.spel.support.DataBindingPropertyAccessor) PropertyAccessor(org.springframework.expression.PropertyAccessor) SimpleEvaluationContext(org.springframework.expression.spel.support.SimpleEvaluationContext) MapAccessor(org.springframework.context.expression.MapAccessor) Method(java.lang.reflect.Method)

Example 9 with PropertyAccessor

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));
}
Also used : PropertyAccessor(org.springframework.expression.PropertyAccessor) EvaluationContext(org.springframework.expression.EvaluationContext) Test(org.junit.jupiter.api.Test)

Example 10 with PropertyAccessor

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);
}
Also used : ReflectivePropertyAccessor(org.springframework.expression.spel.support.ReflectivePropertyAccessor) PropertyAccessor(org.springframework.expression.PropertyAccessor) CompilablePropertyAccessor(org.springframework.expression.spel.CompilablePropertyAccessor) CompilablePropertyAccessor(org.springframework.expression.spel.CompilablePropertyAccessor)

Aggregations

PropertyAccessor (org.springframework.expression.PropertyAccessor)17 ReflectivePropertyAccessor (org.springframework.expression.spel.support.ReflectivePropertyAccessor)7 Test (org.junit.Test)5 CompilablePropertyAccessor (org.springframework.expression.spel.CompilablePropertyAccessor)5 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)5 ArrayList (java.util.ArrayList)4 EvaluationContext (org.springframework.expression.EvaluationContext)4 Method (java.lang.reflect.Method)3 Test (org.junit.jupiter.api.Test)3 AccessException (org.springframework.expression.AccessException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 MapAccessor (org.springframework.context.expression.MapAccessor)2 EvaluationException (org.springframework.expression.EvaluationException)2 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)2 List (java.util.List)1 Map (java.util.Map)1 BeansException (org.springframework.beans.BeansException)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 ApplicationContext (org.springframework.context.ApplicationContext)1