/src/main/java/org/byron4j/cookbook/javacore/reflection/ReflectionTest.java

https://github.com/Byron4j/CookBook · Java · 35 lines · 18 code · 8 blank · 9 comment · 1 complexity · b6fbf1794aecd04822df4611ac2b7600 MD5 · raw file

  1. package org.byron4j.cookbook.javacore.reflection;
  2. import sun.reflect.Reflection;
  3. /**
  4. * @program: cookbook
  5. * @author: Byron
  6. * @create: 2019/07/19
  7. */
  8. public class ReflectionTest {
  9. public static void main(String[] args) {
  10. Demo demo = new Demo();
  11. //demo.say();
  12. new SecurityManager() {
  13. {
  14. String name = getClassContext()[1].getSimpleName();
  15. System.err.println(name == null ? "null" : name);
  16. }
  17. };
  18. }
  19. }
  20. class Demo{
  21. public void say(){
  22. // Exception in thread "main" java.lang.InternalError: CallerSensitive annotation expected at frame 1
  23. // at sun.reflect.Reflection.getCallerClass(Native Method)
  24. // 改方法只有jdk中的类才能使用
  25. System.out.println(Reflection.getCallerClass());
  26. }
  27. }