Skip to content

Instantly share code, notes, and snippets.

@yoshi389111
Created October 30, 2015 11:14
Show Gist options
  • Save yoshi389111/6c6b1e9c66073a72cdd4 to your computer and use it in GitHub Desktop.
Save yoshi389111/6c6b1e9c66073a72cdd4 to your computer and use it in GitHub Desktop.
Failed to getEnclosiongMethod() of local or anonymous class in lambda.
import java.lang.reflect.Method;
/**
* Failed to getEnclosiongMethod() of local or anonymous class in lambda.
*/
public class FailedToGetEnclosingMethod {
public static void main(String[] args) throws Exception {
System.out.println(System.getProperty("java.vendor"));
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("java.vm.version"));
System.out.println(System.getProperty("os.name"));
System.out.println(System.getProperty("os.arch"));
System.out.println(System.getProperty("os.version"));
System.out.println();
Runnable a = () -> {
class LocalClass {
}
Class<?> clazz = LocalClass.class;
// or
// Class<?> clazz = new Object(){}.getClass();
try {
clazz.getEnclosingMethod();
} catch (Error e) {
e.printStackTrace(System.out);
}
try {
System.out.println();
Object enclosingMethodInfo = getter(clazz, "getEnclosingMethodInfo");
Object name = getter(enclosingMethodInfo, "getName");
System.out.println("EnclosingMethod: " + name);
System.out.println();
Class<?> enclosingClass = clazz.getEnclosingClass();
for (Method method : enclosingClass.getDeclaredMethods()) {
if (method.getName().contains("lambda")) {
System.out.println(method);
}
}
} catch (Exception e) {
e.printStackTrace();
}
};
a.run();
}
/** invoke getter */
public static Object getter(Object target, String methodName) throws Exception {
Class<?> clazz = target.getClass();
Method method = clazz.getDeclaredMethod(methodName);
method.setAccessible(true);
return method.invoke(target);
}
}
Oracle Corporation
1.8.0_65
25.65-b01
Windows 7
x86
6.1
java.lang.InternalError: Enclosing method not found
at java.lang.Class.getEnclosingMethod(Unknown Source)
at FailedToGetEnclosingMethod.lambda$0(FailedToGetEnclosingMethod.java:25)
at FailedToGetEnclosingMethod.main(FailedToGetEnclosingMethod.java:51)
EnclosingMethod: lambda$1
private static void FailedToGetEnclosingMethod.lambda$0()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment