Close

Java Reflection - Class.getEnclosingMethod() Examples

Java Reflection Java Java API 


Class:

java.lang.Class

java.lang.Objectjava.lang.Objectjava.lang.Classjava.lang.Classjava.io.SerializableSerializablejava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.TypeTypejava.lang.reflect.AnnotatedElementAnnotatedElementLogicBig

Method:

public Method getEnclosingMethod()
                          throws SecurityException

This method returns a Method object representing the immediately enclosing method of the this class object. If this class is not enclosed in a method, this method returns null .


Examples


package com.logicbig.example.clazz;

public class GetEnclosingMethodExample {

public static void main(String[] args) {
GetEnclosingMethodExample test = new GetEnclosingMethodExample();
Runnable runnable = test.getMyRunnable();

System.out.println(runnable.getClass().getEnclosingMethod());
}

private Runnable getMyRunnable() {
return new Runnable() {
@Override
public void run() {
//do something
}
};
}
}

Output

private java.lang.Runnable com.logicbig.example.clazz.GetEnclosingMethodExample.getMyRunnable()




See Also