The getDeclaredMethods()
method in Java, part of the java.lang.Class
class, is used to retrieve an array of Method
objects representing all the methods declared by the class or interface represented by the Class
object.
Table of Contents
- Introduction
getDeclaredMethods()
Method Syntax- Understanding
getDeclaredMethods()
- Examples
- Basic Usage
- Inspecting Method Details
- Real-World Use Case
- Conclusion
Introduction
The getDeclaredMethods()
method returns an array of Method
objects reflecting all the methods declared by the class or interface represented by the Class
object, including private, protected, default (package) access, and public methods. This method does not return inherited methods.
getDeclaredMethods() Method Syntax
The syntax for the getDeclaredMethods()
method is as follows:
public Method[] getDeclaredMethods() throws SecurityException
Parameters:
- This method does not take any parameters.
Returns:
- An array of
Method
objects representing all the declared methods of the class or interface.
Throws:
SecurityException
: If access to the method information is denied.
Understanding getDeclaredMethods()
The getDeclaredMethods()
method allows you to retrieve all methods declared in a class, regardless of their access level. This includes private, protected, package-private, and public methods. It does not include methods inherited from superclasses.
Examples
Basic Usage
To demonstrate the basic usage of getDeclaredMethods()
, we will create a class with different methods and retrieve all of them using this method.
Example
import java.lang.reflect.Method;
public class GetDeclaredMethodsExample {
public static void main(String[] args) {
Class<Person> personClass = Person.class;
Method[] methods = personClass.getDeclaredMethods();
for (Method method : methods) {
System.out.println("Method: " + method.getName());
}
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
private void privateMethod() {
System.out.println("Private method");
}
protected void protectedMethod() {
System.out.println("Protected method");
}
void packagePrivateMethod() {
System.out.println("Package-private method");
}
public void publicMethod() {
System.out.println("Public method");
}
}
Output:
Method: privateMethod
Method: protectedMethod
Method: packagePrivateMethod
Method: publicMethod
Inspecting Method Details
This example shows how to inspect additional details of each method, such as its return type and parameter types.
Example
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
public class InspectMethodDetailsExample {
public static void main(String[] args) {
Class<Person> personClass = Person.class;
Method[] methods = personClass.getDeclaredMethods();
for (Method method : methods) {
System.out.println("Method: " + method.getName());
System.out.println("Return type: " + method.getReturnType().getName());
Parameter[] parameters = method.getParameters();
for (Parameter parameter : parameters) {
System.out.println("Parameter: " + parameter.getName() + " of type " + parameter.getType().getName());
}
System.out.println();
}
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
private void privateMethod() {
System.out.println("Private method");
}
protected void protectedMethod() {
System.out.println("Protected method");
}
void packagePrivateMethod() {
System.out.println("Package-private method");
}
public void publicMethod() {
System.out.println("Public method");
}
}
Output:
Method: privateMethod
Return type: void
Method: protectedMethod
Return type: void
Method: packagePrivateMethod
Return type: void
Method: publicMethod
Return type: void
Real-World Use Case
Dynamic Method Invocation
In a real-world scenario, you might use the getDeclaredMethods()
method to dynamically invoke methods of an object. This is particularly useful in frameworks that require runtime manipulation of objects, such as testing frameworks or dependency injection frameworks.
Example
import java.lang.reflect.Method;
public class DynamicMethodInvocationExample {
public static void main(String[] args) {
try {
Person person = new Person("Alice", 30);
Class<Person> personClass = Person.class;
Method[] methods = personClass.getDeclaredMethods();
for (Method method : methods) {
method.setAccessible(true); // Make the private methods accessible
if (method.getParameterCount() == 0) {
System.out.println("Invoking method: " + method.getName());
method.invoke(person);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
private void privateMethod() {
System.out.println("Private method");
}
protected void protectedMethod() {
System.out.println("Protected method");
}
void packagePrivateMethod() {
System.out.println("Package-private method");
}
public void publicMethod() {
System.out.println("Public method");
}
}
Output:
Invoking method: privateMethod
Private method
Invoking method: protectedMethod
Protected method
Invoking method: packagePrivateMethod
Package-private method
Invoking method: publicMethod
Public method
Conclusion
The Class.getDeclaredMethods()
method in Java provides a way to retrieve all methods declared by a class, regardless of their access level. By using this method, you can dynamically access and manipulate methods of a class, making it particularly useful for reflection-based operations in frameworks and libraries.
Whether you are working with simple methods or handling complex dynamic method invocations, the getDeclaredMethods()
method offers a reliable way to access and work with methods at runtime.
Comments
Post a Comment
Leave Comment