Securing type on generic lambda expression

This applies to some extent to Java8 for retrieving a lambda setter from a class .

I am trying to get getter method for given field

public <T, R> IGetter<T, R> getGetter(Class<T> clazz, Field field) {

    Class<R> fieldType = null;
    try {
        fieldType = (Class<R>) field.getType();
    } catch(ClassCastException e) {
        error("Attempted to create a mistyped getter for the field " + field + "!");
    }

    return getGetter(clazz, field.getName(), fieldType);
}

This is the main method:

public <T, R> IGetter<T, R> getGetter(Class<T> clazz, String fieldName, Class<R> fieldType) {

    MethodHandles.Lookup caller = null;
    MethodHandle target = null;
    MethodType func = null;

    try {
        caller = MethodHandles.lookup();
        MethodType getter = MethodType.methodType(fieldType);
        target = caller.findVirtual(clazz, computeGetterName(fieldName), getter);
        func = target.type();
    } catch (NoSuchMethodException e) {
        error("Could not locate a properly named getter \"" + computeGetterName(fieldName) + "\"!");
    } catch (IllegalAccessException e) {
        error("Could not access \"" + computeGetterName(fieldName) + "\"!");
    }

    CallSite site = null;
    try {
        site = LambdaMetafactory.metafactory(
                caller,
                "get",
                MethodType.methodType(IGetter.class),
                func.generic(),
                target,
                func
        );
    } catch (LambdaConversionException e) {
        error("Could not convert the getter \"" + computeGetterName(fieldName) + "\" into a lambda expression!");
    }

    MethodHandle factory = site.getTarget();

    IGetter<T, R> r = null;
    try {
        r = (IGetter<T, R>) factory.invoke();
    } catch (Throwable throwable) {
        error("Casting the factory of \"" + computeGetterName(fieldName) + "\" failed!");
    }

    return r;
}

This does not compile due to type mismatch:

IGetter<TestEntity, Long> getter = accessorFactory.getGetter(TestEntity.class, "name", String.class);

This, however, compiles:

Field field = TestEntity.class.getDeclaredField("name");
IGetter<TestEntity, Long> getter = accessorFactory.getGetter(TestEntity.class, field);

And, to my surprise, this works using the recipient that was received above:

TestEntity testEntity = new TestEntity(1L, "Test");
System.out.println(getter.get(testEntity));

However, as soon as I do this:

Long value = getter.get(testEntity);

I get the following exception:

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
    at de.cyclonit.exercise.Main.main(Main.java:26)

Is there any way to catch this before?

Class TestEntity:

public class TestEntity {

    private Long id;

    private String name;


    public TestEntity(Long id, String name) {
    this.id = id;
        this.name = name;
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
}
+6
source share
1 answer

The problem is that your method

public <T, R> IGetter<T, R> getGetter(Class<T> clazz, Field field) {
    Class<R> fieldType = null;
    try {
        fieldType = (Class<R>) field.getType();
    } catch(ClassCastException e) {
        error("Attempted to create a mistyped getter for the field " + field + "!");
    }
    return getGetter(clazz, field.getName(), fieldType);
}

. Class Class, . Class<?> Class<R> - , "" , , .

- :

public <T, R> IGetter<T, R> getGetter(Class<T> clazz, Class<R> fieldType, Field field) {
    if(fieldType != field.getType()) {
        error("Attempted to create a mistyped getter for the field " + field + "!");
    }
    return getGetter(clazz, field.getName(), fieldType);
}

, , Field . getGetter , getters, . , .

...

+7

Source: https://habr.com/ru/post/1017273/


All Articles