Skip to content

Instantly share code, notes, and snippets.

@Randgalt
Created October 18, 2020 13:34
Show Gist options
  • Save Randgalt/86fe893bfd1d657798263080c77a1ac3 to your computer and use it in GitHub Desktop.
Save Randgalt/86fe893bfd1d657798263080c77a1ac3 to your computer and use it in GitHub Desktop.
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
public class GetRecordComponents
{
public static class Component
{
private final String name;
private final Class<?> type;
private final String genericSignature;
private final Type genericType;
private final Method accessor;
public Component(String name, Class<?> type, String genericSignature, Type genericType, Method accessor)
{
this.name = name;
this.type = type;
this.genericSignature = genericSignature;
this.genericType = genericType;
this.accessor = accessor;
}
public String getName()
{
return name;
}
public Class<?> getType()
{
return type;
}
public String getGenericSignature()
{
return genericSignature;
}
public Type getGenericType()
{
return genericType;
}
public Method getAccessor()
{
return accessor;
}
@Override
public String toString()
{
return "Component{" +
"name='" + name + '\'' +
", type=" + type +
", genericSignature='" + genericSignature + '\'' +
", genericType=" + genericType +
", accessor=" + accessor +
'}';
}
@Override
public boolean equals(Object o)
{
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }
Component component = (Component) o;
return name.equals(component.name) &&
type.equals(component.type) &&
genericSignature.equals(component.genericSignature) &&
genericType.equals(component.genericType) &&
accessor.equals(component.accessor);
}
@Override
public int hashCode()
{
return Objects.hash(name, type, genericSignature, genericType, accessor);
}
}
public static Optional<List<Component>> get(Class<?> clazz)
{
// public RecordComponent[] getRecordComponents() {
try {
List<Component> components = new ArrayList<>();
Method method = Class.class.getMethod("getRecordComponents");
Object[] recordComponents = (Object[]) method.invoke(clazz);
for (Object component : recordComponents) {
String name = (String) component.getClass().getMethod("getName").invoke(component);
Class<?> type = (Class<?>) component.getClass().getMethod("getType").invoke(component);
String genericSignature = (String) component.getClass().getMethod("getGenericSignature").invoke(component);
Type genericType = (Type) component.getClass().getMethod("getGenericType").invoke(component);
Method accessor = (Method) component.getClass().getMethod("getAccessor").invoke(component);
components.add(new Component(name, type, genericSignature, genericType, accessor));
}
return Optional.of(Collections.unmodifiableList(components));
}
catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
// ignore
}
return Optional.empty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment