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.
param: arg0 param: arg1 param: arg2
? – Hugelyjavac
versionOpenJDK Runtime Environment (build 16-ea+29-2091)
, the behavior is the same as that mentioned in the question. – Arcadiajavac -parameters
? – Barrajavac
followed byjava
works differently from howjshell
does! – Arcadiaprivate native Parameter[] getParameters0()
declared inExecutable.java
– ArcadiaJVM_GetMethodParameters(env, method)
– Barra