1

The following code compiles in JDK6. Fails in JDK7 with compilation error.

java: incompatible types
  required: com.jdk7.IExporter<O>
  found:    com.jdk7.IExporter<java.lang.Object>

Compiler is 1.7.0_10, from Oracle.

$ javac -version
javac 1.7.0_10

Code

package com.jdk7;

public class GenericIn7 {

    public <O> IExporter<O> getExporter(Class<O> objType) {
        final IExporter<O> localExporter = 
                determineExporter(getPersistentInterface(objType));
        return null;
    }

    private <O> IExporter<O> determineExporter(Class<O> persistentInterface) {
        return null;
    }

    protected <O, I extends O> Class<O> getPersistentInterface(Class<I> clazz) {

        return null;
    }
}

class IExporter<T> {
}

[For sake of completeness, replacing generic with IExporter and other changes make it compile. ]

Jayan
  • 18,003
  • 15
  • 89
  • 143
  • Is that both using plain javac? Ie, no ecj or anything? – fge Jan 22 '13 at 05:05
  • 1
    Bug fix in Java for incompatibility http://www.oracle.com/technetwork/java/javase/compatibility-417013.html#jdk7 – user1983527 Jan 22 '13 at 05:08
  • 1
    @ user1983527 : Thanks for the help. I checked when posting the question. Could not really connect the error with anything there. – Jayan Jan 22 '13 at 05:16

1 Answers1

3

Looks like the type inference just isn't working as you expect on this line:

final IExporter<O> localExporter = 
            determineExporter(getPersistentInterface(objType));

I think the problem is what O is being resolved to in getPersistentInterface(). It is being called as if you had called this.<Object, O>getPersistentInterface(...). If you are explicit:

final IExporter<O> localExporter = 
            determineExporter(this.<O, O>getPersistentInterface(objType));

it works fine.

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • @ Mark Peters: Thank you. Is this bug? IntelliJ does not show any warning when editing. – Jayan Jan 22 '13 at 05:13
  • 1
    @Jayan: I'm not sure if it's a bug. It's not overly rare to find IntelliJ and javac disagreeing when inferring generic parameters. I find the JLS surrounding generic nuances pretty hard to parse, so I'm not going to try to apply it to this situation :-). One test might be to try it using ecj and see if you get the same result. Java has never been stellar at type inference. It works great when the hint comes from an assignment, OK in simple expressions, and not very well when the hint comes from another generic method's argument types. – Mark Peters Jan 22 '13 at 05:34
  • @ Mark Peters : Thank you. Good to know more about this. – Jayan Jan 22 '13 at 07:35