Why does this generic method need a cast on the return value?

D

david.karr

I'm constructing a generic method with a couple of type parameters,
and the method returns an instance of one of them. The function
parameters provided should allow the type parameters to be
determined. I've gotten all compile errors out of the generic method,
but I'm confused by what happens when I call the method. Eclipse
seems to think the method returns "Object", so it says it can't
convert the response without a cast.

The following is a very simplified version of my class, just to
demonstrate the problem. Note that I have two generic methods and
calls to them, one of them that doesn't display this symptom, and the
other that does. The cast on the return value of "submitRequest()" is
what I'm trying to get rid of.

---------------------------
package generics;

public class Generics {

public Generics() {
Bar bar;
try {
bar = doit(new Foo(), Bar.class);
} catch (Exception ex) {
ex.printStackTrace();
}

GenericResponseInfo genericResponseInfo =
// Why does this line need a cast?
(GenericResponseInfo) submitRequest(new GenericRequestInfo
(), 1000,
new ThingieFactory() {
public Thingie<GenericRequestInfo> createElem() {
return null;
}},
new ThingieFactory() {
public Thingie<GenericResponseInfo> createElem() {
return null;
}
});

}

private <A,B> B doit(A a, Class<B> bclass) throws
InstantiationException, IllegalAccessException {
return null;
}

private <REQINFO, RESPINFO> RESPINFO submitRequest
(REQINFO requestInfo,
int readTimeout,
ThingieFactory<REQINFO> requestFactory,
ThingieFactory<RESPINFO> responseFactory) {

Thingie<RESPINFO> responseInfoElem =
responseFactory.createElem();

RESPINFO result = responseInfoElem.getValue();

return (result);
}

private static interface ThingieFactory<T> {
Thingie<T> createElem();
}

private static class Thingie<T> {
public T getValue() { return null; }
}

private static class GenericRequestInfo {
}

private static class GenericResponseInfo {
}

public static class Foo {
}

public static class Bar {
}
}
-----------------
 
D

david.karr

I'm constructing a generic method with a couple of type parameters,
and the method returns an instance of one of them.  The function
parameters provided should allow the type parameters to be
determined.  I've gotten all compile errors out of the generic method,
but I'm confused by what happens when I call the method.  Eclipse
seems to think the method returns "Object", so it says it can't
convert the response without a cast.

The following is a very simplified version of my class, just to
demonstrate the problem.  Note that I have two generic methods and
calls to them, one of them that doesn't display this symptom, and the
other that does.  The cast on the return value of "submitRequest()" is
what I'm trying to get rid of.

---------------------------
package generics;

public class Generics {

    public Generics() {
        Bar bar;
        try {
            bar = doit(new Foo(), Bar.class);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        GenericResponseInfo genericResponseInfo =
            // Why does this line need a cast?
            (GenericResponseInfo) submitRequest(new GenericRequestInfo
(), 1000,
                new ThingieFactory() {
            public Thingie<GenericRequestInfo> createElem() {
                return null;
            }},
            new ThingieFactory() {
                public Thingie<GenericResponseInfo> createElem() {
                    return null;
                }
            });

    }

    private  <A,B> B doit(A a, Class<B> bclass) throws
InstantiationException, IllegalAccessException {
        return null;
    }

    private <REQINFO, RESPINFO> RESPINFO submitRequest
(REQINFO                   requestInfo,
            int                       readTimeout,
            ThingieFactory<REQINFO>  requestFactory,
            ThingieFactory<RESPINFO> responseFactory) {

        Thingie<RESPINFO>    responseInfoElem    =
responseFactory.createElem();

        RESPINFO    result  = responseInfoElem.getValue();

        return (result);
    }

    private static interface ThingieFactory<T> {
        Thingie<T>  createElem();
    }

    private static class Thingie<T> {
        public T getValue() { return null; }
    }

    private static class GenericRequestInfo {
    }

    private static class GenericResponseInfo {
    }

    public static class Foo {
    }

    public static class Bar {
    }}

-----------------

Note that I also tried providing explicit type arguments, like this:

this.<GenericRequestInfo, GenericResponseInfo>submitRequest(new
GenericRequestInfo(), 1000, ...

(without "this.", Eclipse thought "submitRequest" was a type, not a
method.)

This compiles, but it gives me a warning on the function call, saying
that it's an "unchecked invocation". I wouldn't think I would have to
pass the type arguments explicitly, as they are represented in the
actual parameters to the function.
 
M

Mike Schilling

This would be eaiser with an example that's properly indented and
doesn't involve inner classes, but I believe the problem is the
following:

Your generic method

private <REQINFO, RESPINFO> RESPINFO submitRequest(
REQINFO requestInfo,
int readTimeout,
ThingieFactory<REQINFO> requestFactory,
ThingieFactory<RESPINFO> responseFactory)

can infer RESPINFO only from the type parameters of its fourth
argument, which in this case is

new ThingieFactory()

This is a raw type, that is, it has no type parameters. Try

new ThingieFactory<GenericRequestInfo> ()
 
J

Joshua Cranmer

I'm constructing a generic method with a couple of type parameters,
and the method returns an instance of one of them. The function
parameters provided should allow the type parameters to be
determined. I've gotten all compile errors out of the generic method,
but I'm confused by what happens when I call the method. Eclipse
seems to think the method returns "Object", so it says it can't
convert the response without a cast.

Ultimately, your problem is one that results from determining the type
of the expressions.
submitRequest(new GenericRequestInfo(),

Here we have a type GenericRequestInfo we're trying to match.
1000,
int

new ThingieFactory() { ... },);
ThingieFactory$1

> new ThingieFactory() { ... });

and ThingieFactory$2.

Note that the superclass of ThingieFactory$1 is ThingieFactory, not
ThingieFactory<GenericRequestInfo>. I'm sure you wanted the later? If
so, you need to manually specify it (new ThingieFactory<GenericRequestInfo).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top