0

I have a class with a lot of static classes inside, generated from a .xsd file. Is there a way to extract a interface of all static classes, keeping the original format (with inner interfaces)?

Single example:

Class:

public class Main {
  int a;
  public void do(){
    ...
  }

  public static class Inner {
    int b;
    public void foo(){
      ...
    }
  }
}

Interface:

public interface IMain {
  void do();
  interface IInner {
    void foo();
  }
}
marciel.deg
  • 400
  • 3
  • 17
  • 2
    This is awkward for an interface. What are you actually trying to accomplish? – Makoto Mar 02 '18 at 19:50
  • Search for "Java nested interface" e.g. https://www.javatpoint.com/nested-interface – IEE1394 Mar 02 '18 at 19:57
  • 2
    Use reflection, e.g. see https://stackoverflow.com/questions/471749/can-i-discover-a-java-class-declared-inner-classes-using-reflection for how to get the nested classes and interfaces. Then recursively do reflection of those to get their declared methods etc. – Janus Varmarken Mar 02 '18 at 19:59

1 Answers1

0

With Janus help, I create a single function:

public void printInterface(Class<?> clazz) {
    System.err.println("public interface I" + clazz.getSimpleName() + "{");
    for (Method m : clazz.getDeclaredMethods()) {
        String params = "";
        for (Parameter p : m.getParameters()) {
            if (params != "")
                params += ", ";
            params += p.getType().getSimpleName() + " value";
        }
        System.err.println("  " + m.getReturnType().getSimpleName() + " " + m.getName() + "(" + params + ");");
    }
    for (Class<?> c : clazz.getDeclaredClasses()) {
        printInterface(c);
    }
    System.err.println("}");
}
marciel.deg
  • 400
  • 3
  • 17