How to reference a generic return type with multiple bounds

While the type parameters of a generic method can be restricted by bounds, such as extends Foo & Bar, they are ultimately decided by the caller. When you call getFooBar(), the call site already knows what T is being resolved to. Often, these type parameters will be inferred by the compiler, which is why you don't usually need to specify them, like this:

FooBar.<FooAndBar>getFooBar();

But even when T is inferred to be FooAndBar, that's really whats happening behind the scenes.

So, to answer your question, such a syntax like this:

Foo&Bar bothFooAndBar = FooBar.getFooBar();

Would never be useful in practice. The reason is that the caller must already know what T is. Either T is some concrete type:

FooAndBar bothFooAndBar = FooBar.<FooAndBar>getFooBar(); // T is FooAndBar

Or, T is an unresolved type parameter, and we're in its scope:

<U extends Foo & Bar> void someGenericMethod() {
    U bothFooAndBar = FooBar.<U>getFooBar(); // T is U
}

Another example of that:

class SomeGenericClass<V extends Foo & Bar> {
    void someMethod() {
        V bothFooAndBar = FooBar.<V>getFooBar(); // T is V
    }
}

Technically, that wraps up the answer. But I'd also like to point out that your example method getFooBar is inherently unsafe. Remember that the caller decides what T gets to be, not the method. Since getFooBar doesn't take any parameters related to T, and because of type erasure, its only options would be to return null or to "lie" by making an unchecked cast, risking heap pollution. A typical workaround would be for getFooBar to take a Class<T> argument, or else a FooFactory<T> for example.

Update

It turns out I was wrong when I asserted that the caller of getFooBar must always know what T is. As @MiserableVariable points out, there are some situations where the type argument of a generic method is inferred to be a wildcard capture, rather than a concrete type or type variable. See his answer for a great example of a getFooBar implementation that uses a proxy to drive home his point that T is unknown.

As we discussed in the comments, an example using getFooBar created confusion because it takes no arguments to infer T from. Certain compilers throw an error on a contextless call to getFooBar() while others are fine with it. I thought that the inconsistent compile errors - along with the fact that calling FooBar.<?>getFooBar() is illegal - validated my point, but these turned out to be red herrings.

Based on @MiserableVariable's answer, I put together an new example that uses a generic method with an argument, to remove the confusion. Assume we have interfaces Foo and Bar and an implementation FooBarImpl:

interface Foo { }
interface Bar { }
static class FooBarImpl implements Foo, Bar { }

We also have a simple container class that wraps an instance of some type implementing Foo and Bar. It declares a silly static method unwrap that takes a FooBarContainer and returns its referent:

static class FooBarContainer<T extends Foo & Bar> {

    private final T fooBar;

    public FooBarContainer(T fooBar) {
        this.fooBar = fooBar;
    }

    public T get() {
        return fooBar;
    }

    static <T extends Foo & Bar> T unwrap(FooBarContainer<T> fooBarContainer) {
        return fooBarContainer.get();
    }
}

Now let's say we have a wildcard parameterized type of FooBarContainer:

FooBarContainer<?> unknownFooBarContainer = ...;

We're allowed to pass unknownFooBarContainer into unwrap. This shows my earlier assertion was wrong, because the call site doesn't know what T is - only that it is some type within the bounds extends Foo & Bar.

FooBarContainer.unwrap(unknownFooBarContainer); // T is a wildcard capture, ?

As I noted, calling unwrap with a wildcard is illegal:

FooBarContainer.<?>unwrap(unknownFooBarContainer); // compiler error

I can only guess that this is because wildcard captures can never match each other - the ? argument provided at the call site is ambiguous, with no way of saying that it should specifically match the wildcard in the type of unknownFooBarContainer.

So, here's the use case for the syntax the OP is asking about. Calling unwrap on unknownFooBarContainer returns a reference of type ? extends Foo & Bar. We can assign that reference to Foo or Bar, but not both:

Foo foo = FooBarContainer.unwrap(unknownFooBarContainer);
Bar bar = FooBarContainer.unwrap(unknownFooBarContainer);

If for some reason unwrap were expensive and we only wanted to call it once, we would be forced to cast:

Foo foo = FooBarContainer.unwrap(unknownFooBarContainer);
Bar bar = (Bar)foo;

So this is where the hypothetical syntax would come in handy:

Foo&Bar fooBar = FooBarContainer.unwrap(unknownFooBarContainer);

This is just one fairly obscure use case. There would be pretty far-ranging implications for allowing such a syntax, both good and bad. It would open up room for abuse where it wasn't needed, and it's completely understandable why the language designers didn't implement such a thing. But I still think it's interesting to think about.


A note about heap pollution

(Mostly for @MiserableVariable) Here's a walkthrough of how an unsafe method like getFooBar causes heap pollution, and its implications. Given the following interface and implementations:

interface Foo { }

static class Foo1 implements Foo {
    public void foo1Method() { }
}

static class Foo2 implements Foo { }

Let's implement an unsafe method getFoo, similar to getFooBar but simplified for this example:

@SuppressWarnings("unchecked")
static <T extends Foo> T getFoo() {
    //unchecked cast - ClassCastException is not thrown here if T is wrong
    return (T)new Foo2();
}

public static void main(String[] args) {
    Foo1 foo1 = getFoo(); //ClassCastException is thrown here
}

Here, when the new Foo2 is cast to T, it is "unchecked", meaning because of type erasure the runtime doesn't know it should fail, even though it should in this case since T was Foo1. Instead, the heap is "polluted", meaning references are pointing to objects they shouldn't have been allowed to.

The failure happens after the method returns, when the Foo2 instance tries to get assigned to the foo1 reference, which has the reifiable type Foo1.

You're probably thinking, "Okay so it blew up at the call site instead of the method, big deal." But it can easily get more complicated when more generics are involved. For example:

static <T extends Foo> List<T> getFooList(int size) {
    List<T> fooList = new ArrayList<T>(size);
    for (int i = 0; i < size; i++) {
        T foo = getFoo();
        fooList.add(foo);
    }
    return fooList;
}

public static void main(String[] args) {

    List<Foo1> foo1List = getFooList(5);

    // a bunch of things happen

    //sometime later maybe, depending on state
    foo1List.get(0).foo1Method(); //ClassCastException is thrown here
}

Now it doesn't blow up at the call site. It blows up sometime later when the contents of foo1List get used. This is how heap pollution gets harder to debug, because the exception stacktrace doesn't point you to the actual problem.

It gets even more complicated when the caller is in generic scope itself. Imagine instead of getting a List<Foo1> we're getting a List<T>, putting it in a Map<K, List<T>> and returning it to yet another method. You get the idea I hope.


There are cases where a called method returning a value can be used by the caller without knowing the concrete type. It is even likely that such a type does not exist at all, it is only a proxy:

import java.lang.reflect.*;

interface Foo {}
interface Bar {}

class FooBar1 implements Foo, Bar {public String toString() { return "FooBar1"; }}
class FooBar2 implements Foo, Bar {public String toString() { return "FooBar2"; }}   

class FooBar {
    static <T extends Foo & Bar> T getFooBar1() { return (T) new FooBar1(); }
    static <T extends Foo & Bar> T getFooBar2() { return (T) new FooBar2(); }
    static <T extends Foo & Bar> T getFooBar() { 
        return (T) 
        Proxy.newProxyInstance(
            Foo.class.getClassLoader(),
            new Class[] { Foo.class, Bar.class },
            new InvocationHandler() {
                public Object invoke(Object proxy, Method method, Object[] args) {
                    return "PROXY!!!";}});
    }

    static <U extends Foo & Bar> void show(U u) { System.out.println(u); }

    public static void main(String[] args) {
        show(getFooBar1());
        show(getFooBar2());
        show(getFooBar());      
    }

}

Both FooBar1 and FooBar2 implement Foo and Bar. In main, the calls to getFooBar1 and getFooBar2 can be assigned to a variable, though there isn't a strong reason for it to know IMHO.

But getFooBar is the interesting case, which uses a proxy. In practice, it may be the only instance of a an object that implements the two interfaces. A different method (show here) can be used with a temporary in a type-safer manner, but it cannot be assigned to a variable without the FooBarWrapper hack described in the question. It is not even possible to create a generic wrapper, class Wrapper<T extends U & V> is not allowed.

The only trouble seems be defining a syntax, other type checking mechanisms seem to be in place, at least in Oracle javac 1.7.0.


Like @Paul Bellora mentioned in his answer, the type get resolved by the caller, since essentially it will now what it is calling. I would just like to add to his answer with an use case where I think the usage of the syntax could be of benefit.

There are always alternatives that avoid using such syntax. I cannot think of a single example that this is utterly necessary. However I can think of a use case of a particular situation that this syntax could be used conveniently, although I didn't even used it myself. I know its not the best example out there but it can get to the point.

Case

Recently I've been working in the development of an user interface. In this application I use a library to manage my GUI elements. In addition to the features of the library, I created a custom interface that defines a View in my application that has inputs for a specific type of data, lets say, input of coordinates. That interface would look like:

public interface CoordinateView extends View
{
    Coordinate getCoordinate();
    //Maybe more stuff
} 


I have several windows across my application that implement this interface. Now lets say that for some reason I want to store in a model the last coordinate submitted in a window and close the window right after. For this I can attach a handler to the window button that submits the form, the handler will get triggered when the user closes the Window. I could achieve that by simply adding the handler anonymously in every window, like:

public MyWindow extends Window implements CoordinateView, OtherInterface
{
    private Button submitButton;

    public MyWindow()
    {
        super();
        //Create all the elements

        submitButton.addClickHandler(
            new ClickHandler()
            {
                @Override
                onCLick(ClickEvent e)
                {
                    getModel().add(getCoordinate());
                    destroy();
                }
            });  
   }
}

However, this design is not desirable for me, it is not modular enough. Considering I have a decent amount of windows with this behavior, changing it could get rather tedious. So I rather extract the anonymous method in a class so that it would be easier to change and maintain. But the problem is that the destroy() method is not defined in any interface, is just part of window and the getCoordinate() method is defined in the interface I defined.

Usage

In this case I could use multiple bounds like the following:

public class MyController <T extends Window & CoordinateView> implements ClickHandler
{
    private T windowWithCoordinates;

    public MyController (T window)
    {
        windowWithCoordinates = window;
    }

    @Override
    onClick(ClickEvent e)
    {
        getModel().add(windowWithCoordinates.getCoordinate());
        windowWithCoordinate.destroy();
    }
}

Then the code in the windows will now be:

public MyWindow extends Window implements CoordinateView, OtherInterface
{
    private Button submitButton;

    public MyWindow()
    {
        super();
        //Create all the elements

        submitButton.addClickHandler(new MyController<MyWindow>(this));

    }
}

Notice that the behaviour will remain the same, the code is just a cohesive as it used to be. Its only more modular, but it didn't required the creation of an additional interface to be able to extract it properly.

Alternative

Alternatively, I could have defined an additional interface extending CoordinateView and define a method to close the window.

public interface CoordinateWindow extends CoordinateView
{
    void destroy();
}

Having the window implement this more specific interface instead of making unnecessary use of generic parameters in the extracted controller:

public class MyController implements ClickHandler
{
    private CoordinateWindow windowWithCoordinates;

    public MyController (CoordinateWindow window)
    {
        windowWithCoordinates = window;
    }

    @Override
    onClick(ClickEvent e)
    {
        getModel().add(windowWithCoordinates.getCoordinate());
        windowWithCoordinate.destroy();
    }
}


public MyWindow extends Window implements CoordinateWindow
{
    private Button submitButton;

    public MyWindow()
    {
        super();
        //Create all the elements  
        submitButton.addClickHandler(new MyController(this));                  
    }

    @Override
    void destroy()
    {
        this.destroy();
    }
}

This approach for some can be seen as much cleaner than the previous and even more reusable since now it could be added to other "windows" outside of the specified hierarchy. Personally, I prefer this approach as well. However, it may result in a little more coding since a new interface has to be defined just for the sake of getting a access to a desired method.

In conclusion, although I personally don't recommend it I think using generic types with multiple bounds could help in coupling definitions while reducing the amount of code.