diff --git a/src/main/java/com/squareup/javapoet/AnnotationSpec.java b/src/main/java/com/squareup/javapoet/AnnotationSpec.java index 918c4839e..1fa6db18f 100644 --- a/src/main/java/com/squareup/javapoet/AnnotationSpec.java +++ b/src/main/java/com/squareup/javapoet/AnnotationSpec.java @@ -229,9 +229,6 @@ Builder addMemberForValue(String memberName, Object value) { if (value instanceof String) { return addMember(memberName, "$S", value); } - if (value instanceof Float) { - return addMember(memberName, "$Lf", value); - } if (value instanceof Character) { return addMember(memberName, "'$L'", characterLiteralWithoutSingleQuotes((char) value)); } diff --git a/src/main/java/com/squareup/javapoet/CodeWriter.java b/src/main/java/com/squareup/javapoet/CodeWriter.java index da6d3c8e5..9adfb84ec 100644 --- a/src/main/java/com/squareup/javapoet/CodeWriter.java +++ b/src/main/java/com/squareup/javapoet/CodeWriter.java @@ -363,6 +363,10 @@ private void emitLiteral(Object o) throws IOException { } else if (o instanceof CodeBlock) { CodeBlock codeBlock = (CodeBlock) o; emit(codeBlock); + } else if (o instanceof Long) { + emitAndIndent(String.valueOf(o) + "L"); + } else if (o instanceof Float) { + emitAndIndent(String.valueOf(o) + "f"); } else { emitAndIndent(String.valueOf(o)); } diff --git a/src/test/java/com/squareup/javapoet/AnnotationSpecTest.java b/src/test/java/com/squareup/javapoet/AnnotationSpecTest.java index 97c1e6e90..ec14efcec 100644 --- a/src/test/java/com/squareup/javapoet/AnnotationSpecTest.java +++ b/src/test/java/com/squareup/javapoet/AnnotationSpecTest.java @@ -313,7 +313,7 @@ public class IsAnnotated { + " a = 5,\n" + " b = 6,\n" + " c = 7,\n" - + " d = 8,\n" + + " d = 8L,\n" + " e = 9.0f,\n" + " f = 11.1,\n" + " g = {\n" diff --git a/src/test/java/com/squareup/javapoet/CodeWriterTest.java b/src/test/java/com/squareup/javapoet/CodeWriterTest.java index 331d00039..9325fce68 100644 --- a/src/test/java/com/squareup/javapoet/CodeWriterTest.java +++ b/src/test/java/com/squareup/javapoet/CodeWriterTest.java @@ -2,6 +2,7 @@ import org.junit.Test; +import javax.lang.model.element.Modifier; import java.io.IOException; import static com.google.common.truth.Truth.assertThat; @@ -20,4 +21,27 @@ public void emptyLineInJavaDocDosEndings() throws IOException { " * B\n" + " */\n"); } + + @Test + public void floatLiteralInInitializer() throws IOException { + float a = 7.0f; + float b = 8.0f; + FieldSpec fieldSpec = FieldSpec.builder(Float[].class, "floatArray", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL). + initializer("new Float[] { $L, $L }", a, b).build(); + assertThat(fieldSpec.toString()).isEqualTo( + "public static final java.lang.Float[] floatArray = new Float[] { 7.0f, 8.0f };\n" + ); + } + + @Test + public void LongLiteralInInitializer() throws IOException { + Long a = 11111111111L; + Long b = 22222222222L; + FieldSpec fieldSpec = FieldSpec.builder(Long[].class, "longArray", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL). + initializer("new Long[] { $L, $L }", a, b).build(); + assertThat(fieldSpec.toString()).isEqualTo( + "public static final java.lang.Long[] longArray = new Long[] { 11111111111L, 22222222222L };\n" + ); + } + } \ No newline at end of file