Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Add Java literal support for $L literal for issue #488 #913

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/main/java/com/squareup/javapoet/AnnotationSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/squareup/javapoet/CodeWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/com/squareup/javapoet/CodeWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"
);
}

}