As part of an investigation into the parameters of a method, I tried the new Pattern Matching for switch (Preview). Using a traditional condition, it works perfectly:
Method firstMethod = BitSet.class.getDeclaredMethods()[0];
Parameter firstParameter = firstMethod.getParameters()[0];
if (firstParameter.getType() == Integer.class) {
System.out.println("Integer");
}
When I tried to refactor it to use a switch
statement, it did not compile:
Method firstMethod = BitSet.class.getDeclaredMethods()[0];
Parameter firstParameter = firstMethod.getParameters()[0];
switch (firstParameter.getType()) {
case Integer.class: System.out.println("Integer");
case int.class: System.out.println("int");
default: System.out.println("other");
}
The error is:
error: incompatible types: Class<Integer> cannot be converted to Class<CAP#1>
case Integer.class: System.out.println("Integer");
^
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
Is this something that can't be done, or is it just a syntax error?