Record cannot get parameter names from constructors?
10
Looking to see how to use Records with reflection under Java 16 (and 15 same behavior)
public record RecordTest2(int id, int something, double total, LocalDateTime createdOn) {
public RecordTest2(int id, int something, double total) {
this(id, something, total, LocalDateTime.now());
}
}
Looking at the canonical constructor I do see the parameter names, but not in the other ones.
var recordTest2 = new RecordTest2(1, 2, 3.0, LocalDateTime.now());
Class<?> objectClass = recordTest2.getClass();
Constructor<?>[] constructors = objectClass.getConstructors();
for (Constructor<?> con : constructors) {
System.out.println(con.getName());
Parameter[] parameters = con.getParameters();
for (Parameter parameter : parameters) {
System.out.printf("param: %s\n", parameter.getName());
}
}
output:
net.sf.persism.dao.records.RecordTest2
param: arg0
param: arg1
param: arg2
net.sf.persism.dao.records.RecordTest2
param: id
param: something
param: total
param: createdOn
Any work-around to this?
Edit: Just to dot the "I" here. I can use the @ConstructorProperties annotation from java.beans.
java constructor java-record java-16
javac
followed byjava
works differently from howjshell
does! – Naman Apr 11 at 18:02