Skip to content

Instantly share code, notes, and snippets.

@everton
Last active March 8, 2020 06:05
Show Gist options
  • Save everton/a8cc2f50498c43349eafbcc8b279be97 to your computer and use it in GitHub Desktop.
Save everton/a8cc2f50498c43349eafbcc8b279be97 to your computer and use it in GitHub Desktop.
import java.lang.invoke.MethodHandles;
class Base {
public static class CurrentClassGetter extends SecurityManager {
public Class<?>[] getClassContext() {
return super.getClassContext();
}
}
public static void find(int id) {
// Class base = MethodHandles.lookup().lookupClass();
// Class base = new Object(){}.getClass().getEnclosingClass();
// System.out.println("Find " + base.getName() + " with id: " + id);
// System.out.println(new Throwable().getStackTrace()[2]);
System.out.println(new CurrentClassGetter().getClassContext()[2]);
}
}
class Student extends Base {
}
class HelloWorld {
public static void main(String args[]) {
Student.find(666);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment