get Enclosing Instance Name - Android java.lang.reflect

Android examples for java.lang.reflect:Nested Class

Description

get Enclosing Instance Name

Demo Code


//package com.java2s;

public class Main {
    public static String getEnclosingInstanceName(Object innerInstance) {
        int depth = -1;
        Class<?> enclosing = innerInstance.getClass().getEnclosingClass();
        while (enclosing != null) {
            ++depth;//w w w.  j  a  va 2 s .c  om
            enclosing = enclosing.getEnclosingClass();
        }

        return (depth == -1) ? null : "this$" + depth;
    }
}

Related Tutorials