Search in sources :

Example 1 with PropertyAccessor

use of cn.taketoday.expression.PropertyAccessor in project today-infrastructure by TAKETODAY.

the class SpelReproTests method propertyAccessOnNullTarget_SPR5663.

@Test
void propertyAccessOnNullTarget_SPR5663() throws AccessException {
    PropertyAccessor accessor = new ReflectivePropertyAccessor();
    EvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
    assertThat(accessor.canRead(context, null, "abc")).isFalse();
    assertThat(accessor.canWrite(context, null, "abc")).isFalse();
    assertThatIllegalStateException().isThrownBy(() -> accessor.read(context, null, "abc"));
    assertThatIllegalStateException().isThrownBy(() -> accessor.write(context, null, "abc", "foo"));
}
Also used : PropertyAccessor(cn.taketoday.expression.PropertyAccessor) ReflectivePropertyAccessor(cn.taketoday.expression.spel.support.ReflectivePropertyAccessor) EvaluationContext(cn.taketoday.expression.EvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) ReflectivePropertyAccessor(cn.taketoday.expression.spel.support.ReflectivePropertyAccessor) Test(org.junit.jupiter.api.Test)

Example 2 with PropertyAccessor

use of cn.taketoday.expression.PropertyAccessor in project today-infrastructure by TAKETODAY.

the class AstUtils method getPropertyAccessorsToTry.

/**
 * 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) {
    ArrayList<PropertyAccessor> specificAccessors = new ArrayList<>();
    ArrayList<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) {
                        // put exact matches on the front to be tried first?
                        specificAccessors.add(resolver);
                    } else if (clazz.isAssignableFrom(targetType)) {
                        // put supertype matches at the end of the
                        // specificAccessor list
                        generalAccessors.add(resolver);
                    }
                }
            }
        }
    }
    ArrayList<PropertyAccessor> resolvers = new ArrayList<>(specificAccessors.size() + generalAccessors.size());
    resolvers.addAll(specificAccessors);
    resolvers.addAll(generalAccessors);
    return resolvers;
}
Also used : PropertyAccessor(cn.taketoday.expression.PropertyAccessor) ArrayList(java.util.ArrayList)

Example 3 with PropertyAccessor

use of cn.taketoday.expression.PropertyAccessor in project today-framework by TAKETODAY.

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(cn.taketoday.expression.PropertyAccessor) EvaluationContext(cn.taketoday.expression.EvaluationContext) Test(org.junit.jupiter.api.Test)

Example 4 with PropertyAccessor

use of cn.taketoday.expression.PropertyAccessor in project today-framework by TAKETODAY.

the class SpelReproTests method propertyAccessOnNullTarget_SPR5663.

@Test
void propertyAccessOnNullTarget_SPR5663() throws AccessException {
    PropertyAccessor accessor = new ReflectivePropertyAccessor();
    EvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
    assertThat(accessor.canRead(context, null, "abc")).isFalse();
    assertThat(accessor.canWrite(context, null, "abc")).isFalse();
    assertThatIllegalStateException().isThrownBy(() -> accessor.read(context, null, "abc"));
    assertThatIllegalStateException().isThrownBy(() -> accessor.write(context, null, "abc", "foo"));
}
Also used : PropertyAccessor(cn.taketoday.expression.PropertyAccessor) ReflectivePropertyAccessor(cn.taketoday.expression.spel.support.ReflectivePropertyAccessor) EvaluationContext(cn.taketoday.expression.EvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) ReflectivePropertyAccessor(cn.taketoday.expression.spel.support.ReflectivePropertyAccessor) Test(org.junit.jupiter.api.Test)

Example 5 with PropertyAccessor

use of cn.taketoday.expression.PropertyAccessor in project today-framework by TAKETODAY.

the class PropertyAccessTests method addingAndRemovingAccessors.

@Test
void addingAndRemovingAccessors() {
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    // reflective property accessor is the only one by default
    assertThat(ctx.getPropertyAccessors()).hasSize(1);
    StringyPropertyAccessor spa = new StringyPropertyAccessor();
    ctx.addPropertyAccessor(spa);
    assertThat(ctx.getPropertyAccessors()).hasSize(2);
    List<PropertyAccessor> copy = new ArrayList<>(ctx.getPropertyAccessors());
    assertThat(ctx.removePropertyAccessor(spa)).isTrue();
    assertThat(ctx.removePropertyAccessor(spa)).isFalse();
    assertThat(ctx.getPropertyAccessors()).hasSize(1);
    ctx.setPropertyAccessors(copy);
    assertThat(ctx.getPropertyAccessors()).hasSize(2);
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) PropertyAccessor(cn.taketoday.expression.PropertyAccessor) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Aggregations

PropertyAccessor (cn.taketoday.expression.PropertyAccessor)8 Test (org.junit.jupiter.api.Test)6 EvaluationContext (cn.taketoday.expression.EvaluationContext)4 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)4 ArrayList (java.util.ArrayList)4 ReflectivePropertyAccessor (cn.taketoday.expression.spel.support.ReflectivePropertyAccessor)2