Get scala's case class parameters using java reflection API
Get scala's case class parameters using java reflection API
I need to get the member variables/parameters of scala case class in java. I tried using the java reflection API in the usual way but doesn't work with scala class.
List<Field> fields = getPrivateFields(myClass.class);
where myClass
is a scala case class. How do I achieve this in java?
myClass
public static List<Field> getPrivateFields(Class<?> theClass){
List<Field> privateFields = new ArrayList<Field>();
Field fields = theClass.getDeclaredFields();
for(Field field:fields){
if(Modifier.isPrivate(field.getModifiers())){
privateFields.add(field);
}
}
return privateFields;
}
getPrivateFields
Added getPrivateFields method. Also, I referred that code from one of the stackoverflow answers.
– Neha Saggam
Jul 3 at 6:56
And what is the problem? Do you get an empty list? Show your case class and the output of your program and state what you expected instead.
– cbley
Jul 3 at 10:09
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
It works perfectly fine for me. What is
getPrivateFields
doing and what exactly is the problem? Show a concrete example.– cbley
Jul 2 at 11:31