Skip to content

Commit a7c54fc

Browse files
committed
include record custom public properties (methods without parameters)
1 parent 31ada51 commit a7c54fc

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/main/java/jvm2dts/Converter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
import java.lang.annotation.Annotation;
66
import java.lang.reflect.*;
77
import java.lang.reflect.Type;
8-
import java.util.ArrayList;
9-
import java.util.LinkedHashMap;
10-
import java.util.List;
11-
import java.util.Map;
8+
import java.util.*;
129
import java.util.logging.Logger;
1310

1411
import static java.lang.reflect.Modifier.isPublic;
@@ -86,8 +83,9 @@ private String convertClass(Class<?> clazz) {
8683
new ClassReader(in).accept(new ClassAnnotationExtractor(methodAnnotations), ClassReader.SKIP_CODE);
8784

8885
var getters = clazz.isRecord() ?
89-
stream(clazz.getRecordComponents())
90-
.collect(toMap(RecordComponent::getName, RecordComponent::getAccessor)) :
86+
stream(clazz.getMethods())
87+
.filter(m -> !isStatic(m.getModifiers()) && m.getParameterCount() == 0 && m.getDeclaringClass() != Object.class && !Set.of("hashCode", "toString").contains(m.getName()))
88+
.collect(toMap(Method::getName, m -> m)) :
9189
stream(clazz.getMethods())
9290
.filter(m -> !isStatic(m.getModifiers()) && m.getParameterCount() == 0 && isLikeGetter(m.getName()))
9391
.collect(toMap(m -> toPropertyName(m.getName()), m -> m, (m1, m2) -> m1.getReturnType().isAssignableFrom(m2.getReturnType()) ? m2 : m1));

src/test/java/jvm2dts/ClassConverterTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ void record() {
7474
assertThat(converter.convert(Record.class)).isEqualTo("interface Record {" +
7575
"isCool: boolean; " +
7676
"hello: string; " +
77+
"notIncluded: number; " +
7778
"world: string;}");
7879
}
7980

@@ -153,4 +154,6 @@ interface AnyId {
153154
Object getId();
154155
}
155156

156-
record Record(String hello, String world, boolean isCool) {}
157+
record Record(String hello, String world, boolean isCool) {
158+
public int notIncluded() { return 0; }
159+
}

0 commit comments

Comments
 (0)