Skip to content

Commit d0371ae

Browse files
committed
test that custom Nullable annotation works on both classes and records
1 parent 0bf2b81 commit d0371ae

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/main/java/jvm2dts/Converter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static boolean isLikeGetter(String methodName) {
114114
return (methodName.startsWith("get") || methodName.startsWith("is")) && !methodName.equals("getClass");
115115
}
116116

117-
private void processProperty(String propertyName, Method method, StringBuilder out, Map<String, List<String>> classAnnotations) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
117+
private void processProperty(String propertyName, Method method, StringBuilder out, Map<String, List<String>> methodAnnotations) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
118118
var fieldBuffer = new StringBuilder();
119119

120120
if (propertyName == null) return;
@@ -139,8 +139,8 @@ else if (annotationName.equals("JsonProperty")) {
139139

140140
fieldBuffer.append(propertyName);
141141

142-
if (!classAnnotations.isEmpty())
143-
for (var annotation : classAnnotations.getOrDefault(method.getName(), emptyList()))
142+
if (!methodAnnotations.isEmpty())
143+
for (var annotation : methodAnnotations.getOrDefault(method.getName(), emptyList()))
144144
if (annotation.contains("Nullable;"))
145145
fieldBuffer.append("?");
146146

@@ -247,7 +247,7 @@ public ClassAnnotationExtractor(Map<String, List<String>> annotations) {
247247
}
248248

249249
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
250-
if (!isPublic(access) || !isLikeGetter(name)) return null;
250+
if (!isPublic(access)) return null;
251251
return new MethodAnnotationExtractor(annotations.computeIfAbsent(name, k -> new ArrayList<>()));
252252
}
253253
}

src/test/java/jvm2dts/ClassConverterTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import org.jetbrains.annotations.Nullable;
66
import org.junit.jupiter.api.Test;
77

8+
import java.lang.annotation.Retention;
89
import java.util.List;
910
import java.util.Optional;
1011
import java.util.UUID;
1112

13+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
1214
import static java.util.Collections.emptyMap;
1315
import static org.assertj.core.api.Assertions.assertThat;
1416

@@ -75,18 +77,22 @@ void realClass() {
7577
@Test
7678
void record() {
7779
assertThat(converter.convert(Record.class)).isEqualTo("interface Record {" +
78-
"isCool: boolean; " +
79-
"hello: string; " +
80-
"\"@key\": string; " +
8180
"notIncluded: boolean; " +
82-
"world: string;}");
81+
"\"@key\": string; " +
82+
"hello: string; " +
83+
"world?: string; " +
84+
"isCool: boolean;" +
85+
"}");
8386
}
8487

8588
@Test
8689
void emptyTypes() {
8790
assertThat(converter.convert(Empty.class)).isNull();
8891
assertThat(converter.convert(OnlyPrivate.class)).isNull();
8992
}
93+
94+
@Retention(RUNTIME)
95+
@interface Nullable {}
9096
}
9197

9298
@SuppressWarnings("unused")
@@ -108,7 +114,7 @@ interface WrapperTypes {
108114
Long getLong();
109115
Boolean isBoolean();
110116
@Nullable Float getFloat();
111-
@Nullable Double getDouble();
117+
@ClassConverterTest.Nullable Double getDouble();
112118
Optional<String> getOptional();
113119
}
114120

@@ -159,7 +165,7 @@ interface AnyId {
159165
Object getId();
160166
}
161167

162-
record Record(String hello, String world, boolean isCool) {
168+
record Record(String hello, @ClassConverterTest.Nullable String world, boolean isCool) {
163169
@JsonProperty public boolean notIncluded() { return false; }
164170
@JsonProperty("@key") public String key() { return ""; }
165171
}

0 commit comments

Comments
 (0)