Skip to content

Commit

Permalink
Add explicit casts to deficient numeric types
Browse files Browse the repository at this point in the history
To avoid `possible lossy conversion from int to byte/short` errors when
using `AnnotationValue`s for `byte` or `short` in some contexts, e.g.:

```
  void f() {
    g(0);
  }

  void g(byte b) {}
```

```
error: incompatible types: possible lossy conversion from int to byte
    g(0);
      ^
```
RELNOTES=n/a
PiperOrigin-RevId: 386478953
  • Loading branch information
cushon authored and Google Java Core Libraries committed Jul 23, 2021
1 parent 22db69d commit df56d18
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 12 additions & 0 deletions common/src/main/java/com/google/auto/common/AnnotationOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ private String formatType(TypeMirror typeMirror) {
return null;
}

@Override
public @Nullable Void visitByte(byte b, StringBuilder sb) {
sb.append("(byte) ").append(b);
return null;
}

@Override
public @Nullable Void visitShort(short s, StringBuilder sb) {
sb.append("(short) ").append(s);
return null;
}

@Override
public @Nullable Void visitChar(char c, StringBuilder sb) {
appendQuoted(sb, c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,10 @@ public void toSourceString() {
.put("intValues", "{1, 2}")
.put("longValue", "6L")
.put("longValues", "{3L, 4L}")
.put("byteValue", "7")
.put("byteValues", "{8, 9}")
.put("shortValue", "10")
.put("shortValues", "{11, 12}")
.put("byteValue", "(byte) 7")
.put("byteValues", "{(byte) 8, (byte) 9}")
.put("shortValue", "(short) 10")
.put("shortValues", "{(short) 11, (short) 12}")
.put("floatValue", "13.0F")
.put("floatValues", "{14.0F, 15.0F}")
.put("doubleValue", "16.0")
Expand Down

0 comments on commit df56d18

Please sign in to comment.