1. 如果是数组的Class,返回的方法数组长度为 0。

    log.info("{}", new int[]{}.getClass().getDeclaredMethods().length); // 0
    log.info("{}", new Stack[]{}.getClass().getDeclaredMethods().length); // 0

  1. 如果是 void 或 原生数据类型的Class返回的方法数组长度为 0。

    log.info("{}", byte.class.getDeclaredMethods().length); // 0
    log.info("{}", void.class.getDeclaredMethods().length); // 0

  2. 如果是其他普通类、抽象类、接口,可拿到当前类或接口的所有方法,如 private、protected、default, public、含静态方法、合成方法、桥接方法,除构造方法。
  • 普通类

    log.info("{}", Stream.of(Dog.class.getDeclaredMethods())
                  .map(Method::getName).collect(Collectors.joining(",")));
          // defaultStaticMethod,privateStaticMethod,protectedStaticMethod,publicStaticMethod,
          // protectedMethod,defaultMethod,privateMethod,publicMethod
    public class Dog {
      private void privateMethod() {
    
      }
      private static void privateStatusMethod() {
    
      }
      protected void protectedMethod() {
    
      }
      protected static void protectedStaticMethod() {
    
      }
      void defaultMethod() {
    
      }
      static void defaultStaticMethod() {
    
      }
      public void publicMethod() {
    
      }
      public static void publicStaticMethod() {
    
      }
    }

  • 抽象类

    log.info("{}", Stream.of(DogAbstract.class.getDeclaredMethods())
                  .map(Method::getName).collect(Collectors.joining(",")));
          // defaultStaticMethod,privateStatusMethod,protectedStaticMethod,publicStaticMethod,
          // protectedMethod,defaultMethod,privateMethod,publicMethod,
          // defaultAbstractMethod,protectedAbstractMethod,publicAbstractMethod
    public abstract class DogAbstract {
    
      private void privateMethod() {
    
      }
      abstract void defaultAbstractMethod();
      protected abstract void protectedAbstractMethod();
      public abstract void publicAbstractMethod();
      private static void privateStaticMethod() {
    
      }
      protected void protectedMethod() {
    
      }
      protected static void protectedStaticMethod() {
    
      }
      void defaultMethod() {
    
      }
      static void defaultStaticMethod() {
    
      }
      public void publicMethod() {
    
      }
      public static void publicStaticMethod() {
    
      }
    }

  • 接口

    log.info("{}", Stream.of(DogInterface.class.getDeclaredMethods())
    .map(Method::getName).collect(Collectors.joining(",")));
          // defaultMethod,staticMethod,publicAbstractMethod
public interface DogInterface {
    default void defaultMethod() {

    }
    static void staticMethod() {

    }
    void publicAbstractMethod();
}

  • 返回类型的协变可产生桥接方法、合成方法
    协变即是子类方法的返回类型父类方法返回类型子类

    Stream.of(MoveChild.class.getDeclaredMethods()).forEach(e -> {
              String modifier = Modifier.toString(e.getModifiers());
              Class<?> returnType = e.getReturnType();
              Class<?>[] parameterTypes = e.getParameterTypes();
              String name = e.getName();
              StringBuilder sb = new StringBuilder();
              sb.append(modifier).append(" ").append(returnType).append(" ").append(name)
                      .append("(")
                      .append(Stream.of(parameterTypes).map(f -> f.toString()).collect(Collectors.joining(",")))
                      .append(")").append(" ").append("isBridge:").append(e.isBridge()).append(" ").append("isSynthetic:").append(e.isSynthetic());
              log.info("{}", sb);
    
          });
    // public class java.lang.Integer get() isBridge:false isSynthetic:false
    // public volatile class java.lang.Number get() isBridge:true isSynthetic:true
public class Move {
    public Number get() {
        return null;
    }
}
public class MoveChild extends Move{
    @Override
    public Integer get() {
        return null;
    }
}

在JVM内部,方法签名由返回类型+方法+方法参数确定一个方法。


  • 内部类会可产生合成方法
    内部类其实只是一个在编译之后和别的普通的类没有区别,但却可以访问宿主类的私有成员变量和私有方法,这就是合法方法做到的。

    Stream.of(Outer.class.getDeclaredMethods()).forEach(e -> {
              String modifier = Modifier.toString(e.getModifiers());
              Class<?> returnType = e.getReturnType();
              Class<?>[] parameterTypes = e.getParameterTypes();
              String name = e.getName();
              StringBuilder sb = new StringBuilder();
              sb.append(modifier).append(" ").append(returnType).append(" ").append(name)
                      .append("(")
                      .append(Stream.of(parameterTypes).map(f -> f.toString()).collect(Collectors.joining(",")))
                      .append(")").append(" ").append("isBridge:").append(e.isBridge()).append(" ").append("isSynthetic:").append(e.isSynthetic());
              log.info("{}", sb);
          });
    
    // static class java.lang.String access$200(class com.dld.hll.reflection.Outer) isBridge:false isSynthetic:true
    // static void access$300(class com.dld.hll.reflection.Outer,class java.lang.String) isBridge:false isSynthetic:true
    // private void print(class java.lang.String) isBridge:false isSynthetic:false
public class Outer {
    private String name;
    private void print(String name) {
        System.out.println(name);
    }
    public void run() {
        Inner inner = new Inner();
        inner.printOuter();
    }
    private class Inner {
        private void printOuter() {
            Outer.this.print(Outer.this.name);
        }
    }
}

编译之后表现为:

public class Outer {
    private String name;
    private void print(String name) {
        System.out.println(name);
    }
    public void run() {
//        Inner inner = new Inner();
        Inner inner = new Inner(this);
        inner.printOuter();
    }

    static String access$200(Outer outer) {
        return outer.name;
    }
    static void access$300(Outer outer, String name) {
        outer.print(name);
    }

    private class Inner {
        Outer this$0;
        private Inner(Outer outer) {
            super();
            this.this$0 = outer;
        }
        private void printOuter() {
//            Outer.this.print(Outer.this.name);
            Outer.access$300(this$0, Outer.access$200(this$0));
        }
    }
}

  • 泛型的协变可产生桥接方法、合成方法

    Stream.of(Response.class.getDeclaredMethods()).forEach(e -> {
              String modifier = Modifier.toString(e.getModifiers());
              Class<?> returnType = e.getReturnType();
              Class<?>[] parameterTypes = e.getParameterTypes();
              String name = e.getName();
              StringBuilder sb = new StringBuilder();
              sb.append(modifier).append(" ").append(returnType).append(" ").append(name)
                      .append("(")
                      .append(Stream.of(parameterTypes).map(f -> f.toString()).collect(Collectors.joining(",")))
                      .append(")").append(" ").append("isBridge:").append(e.isBridge()).append(" ").append("isSynthetic:").append(e.isSynthetic());
              log.info("{}", sb);
          });
    
    // public class java.lang.Integer get() isBridge:false isSynthetic:false
    // public volatile class java.lang.Number get() isBridge:true isSynthetic:true
    public interface ResponseGenericInterface<T> {
    
      void get(T t);
    }
public class Response implements ResponseGenericInterface<String> {
    @Override
    public void get(String s) {

    }
}

NewBie
10 声望0 粉丝