Skip to content

Commit 878b914

Browse files
Handle null constants
1 parent 4539825 commit 878b914

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ public static Semanticdb.Constant doubleConst(Double value) {
190190
.build();
191191
}
192192

193+
public static Semanticdb.Constant nullConst() {
194+
return Semanticdb.Constant.newBuilder()
195+
.setNullConstant(Semanticdb.NullConstant.newBuilder())
196+
.build();
197+
}
198+
193199
public static Semanticdb.Constant floatConst(Float value) {
194200
return Semanticdb.Constant.newBuilder()
195201
.setFloatConstant(Semanticdb.FloatConstant.newBuilder().setValue(value))

semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@ private Semanticdb.Tree annotationParameter(ExpressionTree expr) {
118118
// Literals can either be a primitive or String
119119
Object value = ((LiteralTree) expr).getValue();
120120
final Semanticdb.Constant constant;
121-
if (value instanceof String) constant = stringConst((String) value);
121+
// Technically, annotation parameter values cannot be null,
122+
// according to JLS: https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.7.1
123+
// But this codepath is still possible to hit when compiling invalid code - and
124+
// we should handle the null const case in order to fail more gracefully
125+
if (value == null) constant = nullConst();
126+
else if (value instanceof String) constant = stringConst((String) value);
122127
else if (value instanceof Boolean) constant = booleanConst((Boolean) value);
123128
else if (value instanceof Byte) constant = byteConst((Byte) value);
124129
else if (value instanceof Short) constant = shortConst((Short) value);

0 commit comments

Comments
 (0)