Coder Social home page Coder Social logo

Comments (6)

GoogleCodeExporter avatar GoogleCodeExporter commented on August 18, 2024
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 [email protected] on 12 Jul 2010 at 7:49

  • Changed state: WontFix
  • Added labels: ****
  • Removed labels: ****

from lambdaj.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 18, 2024
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 [email protected] on 13 Jul 2010 at 9:15

  • Added labels: ****
  • Removed labels: ****

from lambdaj.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 18, 2024
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 [email protected] on 13 Jul 2010 at 12:33

  • Changed state: Started
  • Added labels: ****
  • Removed labels: ****

from lambdaj.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 18, 2024
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 [email protected] on 13 Jul 2010 at 1:16

  • Added labels: ****
  • Removed labels: ****

Attachments:

from lambdaj.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 18, 2024
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 [email protected] on 14 Jul 2010 at 3:36

  • Changed state: WontFix
  • Added labels: ****
  • Removed labels: ****

from lambdaj.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 18, 2024
I completely agree. 
Thanks for your time to look at it.

Original comment by [email protected] on 15 Jul 2010 at 8:00

  • Added labels: ****
  • Removed labels: ****

from lambdaj.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.