HelenBlack / lambdaj

Automatically exported from code.google.com/p/lambdaj
Apache License 2.0
0 stars 0 forks source link

ClassCastException when using generics #32

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. create a class a generic property (e.g. BaseEntity with property id which 
takes a generic type T)
2. create a subclass of BaseEntity (e.g. Person<Long>) --> thus id is of type 
Long 
3. do a on(Person.class).getId()

--> classCastException java.lang.ClassCastException: 
net.sf.cglib.empty.Object$$EnhancerByCGLIB$$3565501f

I'm using version 2.2. 
It seems that ProxyArgument does not 'see' the type is Long instead of Object...

It is behaving very weird, since 'inspecting on(Person.class).getId() during a 
debugging session does workd, but assigning it to a variable, does give the 
classcast...
I know: very weird, but I hope you can help..

Thanks

Original issue reported on code.google.com by pieter.d...@gmail.com on 12 Jul 2010 at 1:37

GoogleCodeExporter commented 8 years ago
The one you described is exactly the expected behavior. It is a variant of the 
first of the 2 lambdaj known limitations explained here:

http://code.google.com/p/lambdaj/wiki/KnownLimitations

In more details, in your example, due to the lack of generics reification, 
lambdaj cannot recognize at runtime that the method getId() returns a Long 
instead of an Object. So it generates the proxy of an Object and when it tries 
to cast it to a Long it throws the ClassCastException you are experiencing.

I am sorry, but for what I know there is no solution for this issue. Let me 
know if my explanation is clear or if you have any question (and possibly any 
suggestion).

Thanks a lot
Mario

Original comment by mario.fu...@gmail.com on 12 Jul 2010 at 7:49

GoogleCodeExporter commented 8 years ago
I understand...

However, I was able to find correct type information for creating a proxy..

I did modify the invoke method on ProxyArgument with the following:

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        Class<?> returnType = method.getReturnType();

        if (method.getName().equals("hashCode")) return invocationSequence.hashCode();
        if (method.getName().equals("equals")) return invocationSequence.equals(args[0]);

        Type type = method.getGenericReturnType();
        if(type instanceof TypeVariable) {
            TypeVariable typeVar = (TypeVariable) type;
            String name = typeVar.getName(); //something like T, K, etc

            //now let's find the type for T
            GenericDeclaration genericDeclaration = typeVar.getGenericDeclaration();
            TypeVariable<?>[] typeVariables = genericDeclaration.getTypeParameters();
            int index = -1;
            for (int i = 0, typeVariablesLength = typeVariables.length; i < typeVariablesLength; i++) {
                TypeVariable<?> typeVariable = typeVariables[i];
                if (typeVariable.getName().equals(name)) {
                    //found it, tye type located at index i
                    index = i;
                    break;
                }
            }

            //actual type argument is thus also at this index 
            Type genericSuperclass = proxiedClass.getGenericSuperclass();
            if (index > -1 && genericSuperclass instanceof ParameterizedType) {
                ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
                Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();

                returnType = (Class<?>) actualTypeArguments[index];
            }
        }

        // Adds this invocation to the current invocation sequence and creates a new proxy propagating the invocation sequence
        return createArgument(returnType, new InvocationSequence(invocationSequence.get(), new Invocation(proxiedClass, method, args)));
    }

Original comment by pieter.d...@gmail.com on 13 Jul 2010 at 9:15

GoogleCodeExporter commented 8 years ago
Thanks a lot Pieter!

I will try your code immediately. It could be a great improvement.
Could you also post the test case you used to try it?

Thanks again
Mario

Original comment by mario.fu...@gmail.com on 13 Jul 2010 at 12:33

GoogleCodeExporter commented 8 years ago
Here it is.

Note that I think the fix will fail in the case where the superclass of the 
proxied class does not define the generic types but one of his superclasses (I 
should loop through all superclasses, but I don't have time for the moment...)

Pieter

Original comment by pieter.d...@gmail.com on 13 Jul 2010 at 1:16

Attachments:

GoogleCodeExporter commented 8 years ago
Hi Pieter,

I played with your solution and I must admit that it is very smart, but in the 
end I decided to take it off lambdaj because I am afraid it could lead to a non 
completely deterministic and transparent behavior. Let me quickly justify my 
decision.

As you wrote your solution doesn't work when the generic is in a 
super-superclass, but this could be easily fixable. Unfortunately there could 
be other corner cases where to find the right class to proxy couldn't be that 
easy. For example the generic could be in an implemented interface instead of 
in an extended class. Probably the worst case is when you have a 
MyInterface1<T> and a MyInterface2<T> and your class is defined as it follows:

class MyClass implements MyInterface1<String>, MyInterface2<Long>

In this case it wouldn't be so easy to decide if a given T is a Long or a 
String. Moreover the generic could be only in the signature of the invoked 
method and not defined at Class-level declaration (as it actually happens with 
many lambdaj methods).

In the end it doesn't have any chance to work with something very usual like an 
ArrayList<String> while it works with a class declared as:

MyList extends ArrayList<String>

and to be honest this behavior could be hardly understandable.

I hope you can agree or at least understand my decision. Anyway thanks a lot 
for your collaboration.

Best Regards,
Mario

Original comment by mario.fu...@gmail.com on 14 Jul 2010 at 3:36

GoogleCodeExporter commented 8 years ago
I completely agree. 
Thanks for your time to look at it.

Original comment by pieter.d...@gmail.com on 15 Jul 2010 at 8:00