Using reflection to inspect and modify classes at runtime In Hindi

Using reflection to inspect and modify classes at runtime In Hindi

जावा  प्रोग्रामिंग लैंग्वेज में रिफ्लेक्शन एक बेस्ट एडवांस जावा कांसेप्ट या फ़ीचर है, जो जावा डेवलपर को किसी यूजर डिफाइन प्रोग्राम के एक्सेक्यूशन या रनटाइम पर क्लास, मेथड, फ़ील्ड और उसके कंस्ट्रक्टर ऑब्जेक्ट को इंस्पेक्ट और मॉडिफ़ाई करने में हेल्प करता है।

Using reflection to inspect and modify classes at runtime In Hindi

रिफ्लेक्शन जावा में मुख्य रूप से java.lang.reflect पैकेज लाइब्रेरी का बिल्ट-इन फीचर्स है।

जावा प्रोग्राम में रिफ्लेक्शन फ़्रेमवर्क, एक डिपेंडेंसी इंजेक्शन, टेस्टिंग टूल, सीरियलाइज़ेशन लाइब्रेरी और ORM फ़्रेमवर्क में अधीक यूज़फुल मेथड है।

Getting Class Information in Java Programs.

जावा में एक यूजर डिफाइन क्लास ऑब्जेक्ट पाने के तीन कॉमन मेथड हैं।

Using .class in Java.

Class<?> cls = Employee.class;

Using the getClass() method.

Employee employee = new Employee();

Class<?> cls = employee.getClass();

Using Class.forName().

Class<?> cls =

Class.forName(“com.example.Employee”);

यहाँ इस Class.forName() फंक्शन मेथड तब यूज़फुल होता है, जब जावा रनटाइम पर क्लास का नाम एक स्ट्रिंग के रूप में जाना जाता है।

Viewing class information in a Java program.

यहाँ माना की हमारे पास एम्प्लॉई नाम की एक क्लास है.

import java.lang.reflect.Method;

class Employee {

    private int emp_id;

    private String emp_name;

    public Employee() {

    }

    public void preview() {

        System.out.println(“Employee”);

    }

    public void dept (String department) {

        System.out.println(“Employee ” + department);

    }

यहाँ हम इस कांसेप्ट में रिफ्लेक्शन का यूज़ करके इंस्पेक्ट कर सकते हैं।

    public static void main(String[] args) {

        Class<?> cls = Employee.class;

        // Inspect class name using Reflection

        System.out.println(cls.getSimpleName());

        // Create object and call preview()

        Employee emp = new Employee();

        emp.preview();

    }

}

Its output.

Employee

Employee

Inspecting Java Program Fields.

यह मौजूदा जावा प्रोग्राम में getDeclaredFields() फंक्शन मेथड क्लास में डिक्लेयर किए गए फील्ड्स वैल्यू को रिटर्न करता है।

Class<?> cls = Employee.class;

Field[] fields = cls.getDeclaredFields();

for (Field field : fields) {

System.out.println(field.getemp_name());

}

Its output.

id

emp_name

इसमें जावा यूजर क्लास फ़ील्ड के बारे में भी जानकारी एक्सेस कर सकते हैं.

for (Field field : fields) {

System.out.println(

field.getemp_name() + ” : ” +

field.getType()

);

}

Reading a private field in a Java program.

रिफ्लेक्शन, जावा यूजर को क्लास के एक्सेस-कंट्रोल/मॉड्यूल प्रतिबंधों के तहत, प्राइवेट डाटा मेंबर्स इन्फो को एक्सेस कर सकता है।

Employee employee = new Employee();

Field field =

Employee.class.getDeclaredField(“emp_name”);

field.setAccessible(true);

String emp_name =

(String) field.get(employee);

System.out.println(emp_name);

यहाँ ऊपर दिए गए प्रोग्राम में setAccessible(true) फंक्शन एक नॉर्मली इनएक्सेसिबल मेंबर के लिए रिफ्लेक्टिव एक्सेस का अनुरोध करता है। स्ट्रॉन्गली एनकैप्सुलेटेड मॉड्यूल में, इसे जरूरत के अनुसार प्रतिबंधित किया जा सकता है।

Modifying a Field in a Java Program.

जावा प्रोग्राम में रिफ्लेक्शन किसी क्लास के एक फ़ील्ड को मॉडिफाई भी कर सकता है।

Employee employee = new Employee();

Field field =

Employee.class.getDeclaredField(“emp_name”);

field.setAccessible(true);

field.set(Employee, “Siddhi”);

अब इसके बाद emp_name की नई वैल्यू है.

Siddhi

For example.

System.out.println(

field.get(employee)

);

Its output.

Siddhi

Verifying a method in Java.

किसी जावा प्रोग्राम में getDeclaredMethods() फंक्शन मेथड क्लास द्वारा डिक्लेअर एक मेथड को रिटर्न करता है।

Class<?> cls = Employee.class;

Method[] methods =

cls.getDeclaredMethods();

for (Method method : methods) {

System.out.println(method.getemp_name());

}

Its output.

preview

department

जावा यूजर इसमें रिटर्न टाइप भी देख सकते हैं.

for (Method method : methods) {

System.out.println(

method.getemp_name() +

” -> ” +

method.getReturnType()

);

}

Invoking a method using reflection in Java.

जावा प्रोग्राम में एक मेथड को इनवोक() फंक्शन का यूज़ करके डायनामिक रूप से कॉल किया जा सकता है।

Employee employee = new Employee();

Method method =

Employee.class.getDeclaredMethod(“preview”);

method.invoke(employee);

Its output.

Employee

इसमें यह एक ज़रूरी मेथड है.

method.invoke(object);

Invoking a method with parameters in Java.

यहाँ माना की हमारे पास एक क्लास कंडीशन है.

public void dept(String department) {

System.out.println(“employee ” + department);

}

We can invoke it using.

Employee employee = new Employee();

Method method =

Employee.class.getDeclaredMethod(

“employee”,

String.class

);

method.invoke(employee, “sales”);

Its output.

sales department

Here in this example.

String.class

denotes the parameter type.

Inspecting Constructors in Java.

जावा यूजर अपने प्रोग्राम में getDeclaredConstructors() फंक्शन मेथड का यूज़ करके कंस्ट्रक्टर्स को इंस्पेक्ट कर सकते हैं।

Class<?> cls = Employee.class;

Constructor<?>[] constructors =

cls.getDeclaredConstructors();

for (Constructor<?> constructor : constructors) {

System.out.println(constructor);

}

Creating objects using reflection in Java.

जावा प्रोग्राम में ऑब्जेक्ट क्रिएट करने के लिए कंस्ट्रक्टर मेथड का यूज़ किया जा सकता है।

Constructor<Employee> constructor =

Employee.class.getDeclaredConstructor();

Employee employee =

constructor.newInstance();

यह मौजूदा जावा एक्साम्प्ल में डायनामिक रूप से एक नया एम्प्लॉई ऑब्जेक्ट क्रिएट करता है।

Complete example of using reflection to inspect and modify classes at runtime.

    import java.lang.reflect.*;

    class Employee {

    private String emp_name = “Random”;

    public void preview() {

    System.out.println(“Employee Name -: ” + emp_name);

    }

    }

    public class Main {

    public static void main(String[] args)

    throws Exception {

    // here we Get employee Class object

    Class<?> cls = Employee.class;

    // here we Create a class object

    Employee employee =

    (Employee) cls.getDeclaredConstructor()

    .newInstance();

    // here we Access class field

    Field field =

    cls.getDeclaredField(“emp_name”);

    field.setAccessible(true);

    // here we Modify employee field

    field.set(employee, “Bhavishi Deora”);

    // here we Invoke method

    Method method =

    cls.getDeclaredMethod(“preview”);

    method.invoke(employee);

    }

    }

Its output.

Employee Name -: Bhavishi Deora

What’s in this?

Employee.class

↓

एम्प्लॉई क्लास की जानकारी पाएं

↓

नया क्लास ऑब्जेक्ट बनाएं

↓

प्राइवेट फ़ील्ड “emp_name” को ढूंढें

↓

क्लास फ़ील्ड को बदलें

↓

प्रीव्यू () मेथड को ढूंढें

↓

क्लास मेथड को इनवोक करें

Most popular important reflection java supported classes

#Most reflection classesPurpose of each reflection class
1.ClassIt Represents a user define class at java program execution or runtime
2.FieldIt Represents a user define custom class with its desire class data type field
3.MethodIt Represents a user define class method to call in reflection when user need
4.ConstructorIt Represents a user define custom class constructor data object method when user need
5.ModifierIt Provides detail information about user define class constructor modifiers task

Example of a Class field modification.

Modifier.isPrivate(field.getModifiers());

यह मौजूदा प्रोग्राम में चेक करता है कि कोई फ़ील्ड प्राइवेट है, या नहीं है।

GetMethods() vs. getDeclaredMethods() explanation in Java.

यह दोनों मेथड फंक्शन जावा रिफ्लेक्शन कांसेप्ट में ज़रूरी है।

getMethods() Java reflection concept.

यह जावा प्रोग्राम में पब्लिक फंक्शन मेथड है, जिसमें इनहेरिटेड पब्लिक मेथड भी इन्क्लुड हैं।

Method[] method =

Employee.class.getMethods();

getDeclaredMethods() Java reflection concept.

यह फंक्शन मेथड क्लास में डायरेक्ट डिक्लेयर किए गए मेथड को प्रोवाइड करता है, जिसमें नॉन-पब्लिक मेथड भी इन्क्लुड हैं।

Method[] method =

Employee.class.getDeclaredMethods();

Similarly.

getFields() function.

→ इसमें पब्लिक क्लास फ़ील्ड होते है.

getDeclaredFields() function.

→ किसी क्लास में डिक्लेयर किए गए फ़ील्ड होते है

getMethods() function.

→ यह क्लास पब्लिक मेथड है, जिसमें इनहेरिटेड मेथड भी शामिल होते हैं

getDeclaredMethods() function.

→ यह क्लास में डिक्लेयर किए गए मेथड होते है.

Advantages of reflection in Java.

  • जावा रिफ्लेक्शन प्रोग्राम रनटाइम पर क्लास के इंस्पेक्शन की परमिशन प्रोवाइड करते है।
  • जावा रिफ्लेक्शन क्लास मेथड को डायनामिक रूप से इनवोक कर सकता है।
  • रिफ्लेक्शन मेथड क्लास फ़ील्ड और कंस्ट्रक्टर को रिफ्लेक्टिव रूप से एक्सेस कर सकता है।
  • रिफ्लेक्शन फीचर्स क्लास फ्रेमवर्क और लाइब्रेरी के लिए यूज़फुल है।
  • रिफ्लेक्शन फीचर्स जावा में डायनामिक और फ़्लेक्सिबल एप्लिकेशन को सपोर्ट करता है।

Disadvantages of reflection in Java.

  • रिफ्लेक्शन से नॉर्मल जावा प्रोग्राम सोर्स कोड ज़्यादा कॉम्प्लेक्स बनता है।
  • रिफ्लेक्शन यूज़ करने से डायरेक्ट जावा क्लास मेथड कॉल प्रोसेस स्लो हो सकता है।
  • रिफ्लेक्शन क्लास डाटा एक्सेस की परमिशन होने पर नॉर्मल एनकैप्सुलेशन टास्क को बायपास कर सकता है।
  • रिफ्लेक्शन में जो एरर सामान्य रूप से कंपाइल टाइम पर कैच किये जाते हैं, वे रनटाइम पर एक्सपोज़ हो सकते हैं।
  • रिफ्लेक्शन कांसेप्ट एक स्ट्रांग जावा मॉड्यूल एनकैप्सुलेशन रिफ्लेक्टिव एक्सेस को ब्लॉक कर सकता है।

Conclusion of Using reflection to inspect and modify classes at runtime.

रिफ्लेक्शन फीचर जावा प्रोग्राम को रनटाइम पर यूजर डिफाइन क्लास डाटा टाइप एलिमेंट के साथ डायनामिक रूप से काम करने में हेल्प करता है.

Detailed Class Reflection Concept.

             ↓

  • Class Fields → इंस्पेक्ट / रीड / मॉडिफाई
  • Class Methods → इंस्पेक्ट / इनवोक
  • Class Constructors → इंस्पेक्ट / क्रिएट ऑब्जेक्ट्स

These are the main reflection APIs to remember in Java.

  • यूजर डिफाइन क्लास प्रोटोटाइप
  • क्लास फील्ड मेंबर डाटा टाइप
  • क्लास मेथड टाइप
  • क्लास कंस्ट्रक्टर ऑब्जेक्ट

Some of the most commonly used basic operations in Java reflection are.

  • getDeclaredFields() function method.
  • getDeclaredMethods() function method.
  • getDeclaredConstructors() function method.
  • getDeclaredField() function method.
  • getDeclaredMethod() function method.
  • getDeclaredConstructor() function method.
  • field.get() function method.
  • field.set() function method.
  • method.invoke() function method.
  • constructor.newInstance() function method.

Leave a Reply