-1

Type mismatch: cannot convert from BytecodeCodeProcessor<new AbstractBytecodeCodeVisitor(){}> to BytecodeCodeProcessor<AbstractBytecodeCodeVisitor>

public abstract class AbstractBytecodeCodeVisitor {
}
public class BytecodeCodeProcessor
     <T extends AbstractBytecodeCodeVisitor> {

     public BytecodeCodeProcessor(ClassSourceResult classSourceResult, 
     T visitor) {

     }
}
BytecodeCodeProcessor<AbstractBytecodeCodeVisitor> processor = 
        new BytecodeCodeProcessor<>(classSourceResult, 
new AbstractBytecodeCodeVisitor() {

});
Krab
  • 6,526
  • 6
  • 41
  • 78
  • You should review [The Java Tutorials. Generics](http://docs.oracle.com/javase/tutorial/java/generics/). You cannot create new instances on a generic definition. – Luiggi Mendoza Nov 02 '14 at 15:13
  • 1
    http://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-arent-javas-generics-implicitly-p – Sotirios Delimanolis Nov 02 '14 at 15:20
  • @LuiggiMendoza: i don't see where the error is. What does it mean "you cannot create new instances on a generic definition"? – Krab Nov 02 '14 at 15:22

1 Answers1

0

The anonymous class instantiated via new AbstractBytecodeCodeVisitor() {} is a subclass of AbstractBytecodeCodeVisitor. This anonymous subclass is not equal to the generic type parameter specified by BytecodeCodeProcessor<AbstractBytecodeCodeVisitor>. Anonymous AbstractBytecodeCodeVisitor != AbstractBytecodeCodeVisitor, thus the compilation error. The code can be fixed in numerous ways, some of which are listed in the following link.

Generics and anonymous classes (bug or feature?)

One solution:

BytecodeCodeProcessor<AbstractBytecodeCodeVisitor> processor = 
    new BytecodeCodeProcessor<AbstractBytecodeCodeVisitor>(
        classSourceResult, new AbstractBytecodeCodeVisitor() {}
    );
Community
  • 1
  • 1
jaco0646
  • 15,303
  • 7
  • 59
  • 83
  • It's not `BytecodeCodeProcessor` that is anonymous. – Sotirios Delimanolis Nov 02 '14 at 16:03
  • Nevertheless, filling in the diamond solves the compilation error. – jaco0646 Nov 02 '14 at 16:19
  • For a different reason than the one you brought up. With the type argument, no type has to be inferred because you explicitly declare it. Without it, it's inferred to a type that makes the expression not assignable to `BytecodeCodeProcessor`, in Java 7. Java 8's improved type inference has fixed this problem. – Sotirios Delimanolis Nov 02 '14 at 16:32
  • Java 8 gives a compilation error as well (on the diamond). Another SO topic, closer to the OP's problem: http://stackoverflow.com/questions/1642874/generics-and-anonymous-classes-bug-or-feature – jaco0646 Nov 02 '14 at 16:46
  • I get no error in Java 8. I used OP's code as is. (Post it if you got it.) I would prefer if you edited your question to include the concepts discussed in the link you posted just now (or in the linked duplicate). – Sotirios Delimanolis Nov 02 '14 at 16:52
  • And also explain why what you suggest works. – Sotirios Delimanolis Nov 02 '14 at 16:53
  • Edited. The error I see in Netbeans is similar to the OP's: _incompatible types: Main.BytecodeCodeProcessor<> cannot be converted to Main.BytecodeCodeProcessor_. I'm not running the very latest version of Java 8: build 1.8.0-b132. – jaco0646 Nov 02 '14 at 17:40
  • You are or you aren't? It compiles fine for me with jdk1.8.0_20's `javac` which contains `java` build 1.8.0_20-b26. I would double check that Netbeans isn't using java 7. – Sotirios Delimanolis Nov 02 '14 at 17:44