Example usage for java.lang Class getEnclosingMethod

List of usage examples for java.lang Class getEnclosingMethod

Introduction

In this page you can find the example usage for java.lang Class getEnclosingMethod.

Prototype

@CallerSensitive
public Method getEnclosingMethod() throws SecurityException 

Source Link

Document

If this Class object represents a local or anonymous class within a method, returns a java.lang.reflect.Method Method object representing the immediately enclosing method of the underlying class.

Usage

From source file:Main.java

public static void main(String[] args) {

    Class cls;
    cls = (new MyObject()).ClassAObject().getClass();

    System.out.print("Method = ");
    System.out.println(cls.getEnclosingMethod());
}

From source file:Main.java

/**
 * Method for finding enclosing class for non-static inner classes
 *//*w  w w .  j av  a 2s.c o  m*/
public static Class<?> getOuterClass(Class<?> type) {
    // as above, GAE has some issues...
    try {
        // one more: method locals, anonymous, are not good:
        if (type.getEnclosingMethod() != null) {
            return null;
        }
        if (!Modifier.isStatic(type.getModifiers())) {
            return type.getEnclosingClass();
        }
    } catch (SecurityException e) {
    } catch (NullPointerException e) {
    }
    return null;
}

From source file:Main.java

public static String isLocalType(Class<?> type) {
    /* As per [JACKSON-187], GAE seems to throw SecurityExceptions
     * here and there... and GAE itself has a bug, too
     * (see []). Bah./*  ww w .j av a2  s. com*/
     */
    try {
        // one more: method locals, anonymous, are not good:
        if (type.getEnclosingMethod() != null) {
            return "local/anonymous";
        }

        /* But how about non-static inner classes? Can't construct
         * easily (theoretically, we could try to check if parent
         * happens to be enclosing... but that gets convoluted)
         */
        if (type.getEnclosingClass() != null) {
            if (!Modifier.isStatic(type.getModifiers())) {
                return "non-static member class";
            }
        }
    } catch (SecurityException e) {
    } catch (NullPointerException e) {
    }
    return null;
}

From source file:Main.java

public static String isLocalType(Class<?> type, boolean allowNonStatic) {
    /* As per [JACKSON-187], GAE seems to throw SecurityExceptions
     * here and there... and GAE itself has a bug, too
     * (see []). Bah. So we need to catch some wayward exceptions on GAE
     *///from ww  w . ja va 2 s.co  m
    try {
        // one more: method locals, anonymous, are not good:
        if (type.getEnclosingMethod() != null) {
            return "local/anonymous";
        }

        /* But how about non-static inner classes? Can't construct
         * easily (theoretically, we could try to check if parent
         * happens to be enclosing... but that gets convoluted)
         */
        if (!allowNonStatic) {
            if (type.getEnclosingClass() != null) {
                if (!Modifier.isStatic(type.getModifiers())) {
                    return "non-static member class";
                }
            }
        }
    } catch (SecurityException e) {
    } catch (NullPointerException e) {
    }
    return null;
}

From source file:Main.java

/**
 * @since 1.9/*from ww  w . j a v a  2  s  . c o m*/
 */
public static String isLocalType(Class<?> type, boolean allowNonStatic) {
    /* As per [JACKSON-187], GAE seems to throw SecurityExceptions
     * here and there... and GAE itself has a bug, too
     * (see []). Bah. So we need to catch some wayward exceptions on GAE
     */
    try {
        // one more: method locals, anonymous, are not good:
        if (type.getEnclosingMethod() != null) {
            return "local/anonymous";
        }

        /* But how about non-static inner classes? Can't construct
         * easily (theoretically, we could try to check if parent
         * happens to be enclosing... but that gets convoluted)
         */
        if (!allowNonStatic) {
            if (type.getEnclosingClass() != null) {
                if (!Modifier.isStatic(type.getModifiers())) {
                    return "non-static member class";
                }
            }
        }
    } catch (SecurityException e) {
    } catch (NullPointerException e) {
    }
    return null;
}