ADDITIONAL SYSTEM INFORMATION :
java compiler 11.0.11
A DESCRIPTION OF THE PROBLEM :
NPE while compiling code with empty diamond to infer generic type. See the code provided
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
compile the code
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
compiles and infers the type from the function argument
ACTUAL -
doesnt compile, NPE
---------- BEGIN SOURCE ----------
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.math.BigDecimal;
import java.util.Map;
public class Test {
private static final Gson GSON = new GsonBuilder().create();
public void testFunc() {
testFunc2(fromMongoJsonObject(new TypeToken<>() {
}));
}
void testFunc2(Map<Long, BigDecimal> aMap) {
}
private static <R> R fromMongoJsonObject(TypeToken<R> aTypeToken) {
return GSON.fromJson("Test", aTypeToken.getType());
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Specify the generic explicitly in diamond:
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.math.BigDecimal;
import java.util.Map;
public class Test {
private static final Gson GSON = new GsonBuilder().create();
public void testFunc() {
testFunc2(fromMongoJsonObject(new TypeToken<Map<Long, BigDecimal>>() {
}));
}
void testFunc2(Map<Long, BigDecimal> aMap) {
}
private static <R> R fromMongoJsonObject(TypeToken<R> aTypeToken) {
return GSON.fromJson("Test", aTypeToken.getType());
}
}
FREQUENCY : always