Skip to content

Instantly share code, notes, and snippets.

@syakuis
Last active April 5, 2022 01:58
Show Gist options
  • Save syakuis/8989075e29ae0f286a66fbafa2644703 to your computer and use it in GitHub Desktop.
Save syakuis/8989075e29ae0f286a66fbafa2644703 to your computer and use it in GitHub Desktop.
Reflect classes information
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
/**
* @author Seok Kyun. Choi.
* @since 2022-04-05
*/
@Slf4j
public class ClassesInfoTest {
@RequiredArgsConstructor
@Builder
@Getter
@EqualsAndHashCode
@ToString
public static class ClassInfo {
private final String name;
private final Class<?> type;
}
private List<ClassInfo> getClassInfo(Class<?> clazz) {
return Arrays.stream(clazz.getDeclaredFields())
.map(field -> ClassInfo.builder().name(field.getName()).type(field.getType()).build())
.collect(
Collectors.toList());
}
@Test
public void test() {
List<ClassInfo> classInfos = getClassInfo(Discount.class);
log.debug("{}", classInfos);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment