reflection - Getting relative inner class name in Java -


package u.v;  class x {     static class xx {         static class xxx { }     } } 

while can canonical ("absolute") name of inner class

public class a{     public static void main(string[] args) {         system.out.println(x.xx.xxx.class.getcanonicalname());   //u.v.x.xx.xxx     } } 

and can last component of class name

    public static void main(string[] args) {         system.out.println(x.xx.xxx.class.getsimplename());   //xxx     } 

how go around elegantly getting relative class name?

utils.relativeclassname(x.xx.xxx.class, x.class);   //xx.xxx 

you can use function. +1 .

public static string getrelativeclassname(class<?> inner, class<?> outer) {   int length = outer.getcanonicalname().length();   return inner.getcanonicalname().substring(length+1); } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -