티스토리 뷰

728x90
import java.lang.reflect.Field;
public class ToStringMaker {
  public ToStringMaker( Object o) {

  Class<? extends Object> c = o.getClass();
  Field[] fields = c.getDeclaredFields();
  for (Field field : fields) {
      String name = field.getName();  
    field.setAccessible(true);          
      try {
        System.out.format("%n%s: %s", name, field.get(o));
      } catch (IllegalArgumentException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      }
  }
 }}

https://stackoverflow.com/questions/44165554/use-reflection-api-to-get-field-names-and-values

 

Use reflection API to get field names and values

I'm creating a number of POJOs and have to create toString() methods for each. Instead of hand-coding each, I thought it would be easier to create a class to do the work for me using the reflection...

stackoverflow.com

// Access Private Field Using Reflection in Java
import java.lang.reflect.Field;
  
// Student class declaration
class Student {
  
    // private fields
    private String name;
    private int age;
  
    // Constructor
    public Student(String name, int age)
    {
        this.name = name;
        this.age = age;
    }
  
    // Getters and setters
    public String getName() { return name; }
  
    public void setName(String name) { this.name = name; }
  
    private int getAge() { return age; }
  
    public void setAge(int age) { this.age = age; }
  
    // Override toString method to get required
    // output at terminal
    @Override public String toString()
    {
        return "Employee [name=" + name + ", age=" + age
            + "]";
    }
}
  
// Program will throw an exception on online IDE
// Compile and run the program on offline IDE
class GFG {
  
    public static void main(String[] args)
    {
        try {
  
            // Student object created
            Student e = new Student("Kapil", 23);
  
            // Create Field object
            Field privateField
                = Student.class.getDeclaredField("name");
  
            // Set the accessibility as true
            privateField.setAccessible(true);
  
            // Store the value of private field in variable
            String name = (String)privateField.get(e);
  
            // Print the value
            System.out.println("Name of Student:" + name);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}​

https://www.geeksforgeeks.org/how-to-access-private-field-and-method-using-reflection-in-java/

 

How to Access Private Field and Method Using Reflection in Java? - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

728x90

'공부' 카테고리의 다른 글

[Regex] id, pw, email  (0) 2022.02.13
[Kafka] research  (0) 2022.02.04
[Java] Use reflection API to get field names and values  (0) 2022.02.04
[Tableau] 날짜 함수  (0) 2022.02.04
[Spark] write mode overwrite  (0) 2022.01.16
[Java] delete topic  (0) 2022.01.16
댓글
댓글쓰기 폼