How do i access the get methods of the custom object. I am able to extract the primitive methods but not able to do when it comes to Non-Primitive Objects.
I am trying to do it as follows.
public class DataExtraction {
public void showTheData(Object student) throws IOException {
Class classofStudent = student.getClass();
Method[] methodsOfStudent = classofStudent.getDeclaredMethods();
for(Method method:methodsOfStudent)
{
if(isGetType(method))
{
if(method.getReturnType()==String.class)
{
try(InputStream is = new FileInputStream("ObjectFileReaderPrimitive.properties"))
{
//InputStream is = new FileInputStream("ObjectFileReaderPrimitive.properties");
Properties properties = new Properties();
properties.load(is);
System.out.println(properties.getProperty(method.getName()));
}
}
else
try(InputStream is = new FileInputStream("ObjectFileReaderNonPrimitive.properties"))
{
Class innerObjectClass = method.getReturnType().getClass();
Method[] methodsOfinnerObject = innerObjectClass.getDeclaredMethods();
for(Method methodofInnerClass : methodsOfinnerObject) {
if(isGetType(method))
{
Properties properties = new Properties();
properties.load(is);
System.out.println(properties.getProperty(methodofInnerClass.getName()));
}
}}
}
}}
private boolean isGetType(Method method) {
if(method.getName().startsWith("get"))
return true;
return false;
}
}
Where the student class is as follows-:
package com.sample;
public class Student {
private String id;
private String section;
private Address address;
public Student(String id, String section, Address address) {
super();
this.id = id;
this.section = section;
this.address = address;
}
public Student() {
super();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSection() {
return section;
}
public void setSection(String section) {
this.section = section;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Student [id=" + id + ", section=" + section + ", address=" + address + "]";
}
}
Please help me out if anyone knows how to achieve this . Its very urjent any type of help will be much appreciated.
Aucun commentaire:
Enregistrer un commentaire