covariant return types

View: New views
2 Messages — Rating Filter:   Alert me  

covariant return types

by Peter Murray-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Greetings Janino Community,

I am bumping into a problem with janino when using covariant return types.  I am trying to generate a class which implements interfaces which themselves declare methods with covariant return types.  Something like:

    public static interface Foo
    {
        public Number getFoo();
    }
   
    public static interface Bar extends Foo
    {
        public Integer getFoo();
    }

If, I generate a class like this:

    public static void main(String[]args) throws Exception
    {
        String code = "public Integer getFoo(){return 1;} ";
       
        ClassBodyEvaluator cbe = new ClassBodyEvaluator();
        cbe.setParentClassLoader(GeneratedObjectFactory.class.getClassLoader());
        cbe.setImplementedTypes(new Class[]{Bar.class});
        cbe.setClassName("BarImpl");
        cbe.cook(code);
        System.out.println("Compiled");
        Foo thingie = (Foo) cbe.getClazz().newInstance();
        Number b = thingie.getFoo();
        System.out.println("Should be 1: " + b);
    }   

I get the exception: Line 1, Column 1: Non-abstract class "Bar" must implement method "java.lang.Number com.subx.general.core.iapi.factory.GeneratedObjectFactory$Foo.getFoo()"

Interestingly enough, if I replace the code string with this: "public Integer getFoo(){return 1;} public Number getFoo() {return 1;}".  Janino compiles and java executes this just fine.

What I'm wondering is: Is it a reasonable practice to manually generate the covariant method versions for Janino?

Also, I noticed that the front page of supported features vs. unsupported features does not mention covariant return types (or did I miss it)?

Thanks for a great tool!  Janino ROCKS!

Cheers,

--
-pete
peter m. murray
pete@...

Re: covariant return types

by Arno Unkrig :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Peter,

> What I'm wondering is: Is it a reasonable practice to manually generate
> the covariant method versions for Janino?

Well... effectively that is what JDK 5 does behind the scenes. So it IS
a reasonable workaround.

> Also, I noticed that the front page of supported features vs.
> unsupported features does not mention covariant return types (or did I
> miss it)?

You're right. I will add CRT under "Limitations".


CU

Arno


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email