Example usage for java.lang Class getEnclosingClass

List of usage examples for java.lang Class getEnclosingClass

Introduction

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

Prototype

@CallerSensitive
public Class<?> getEnclosingClass() throws SecurityException 

Source Link

Document

Returns the immediately enclosing class of the underlying class.

Usage

From source file:natalia.dymnikova.cluster.scheduler.impl.Codec.java

private static Object doSanitize(final Object f) {
    final Class<?> aClass = f.getClass();
    if (!isStatic(aClass.getModifiers())) {
        final Class<?> enclosingClass = aClass.getEnclosingClass();
        if (enclosingClass != null) {
            for (final Field field : aClass.getDeclaredFields()) {
                if (enclosingClass.equals(field.getType())) {
                    field.setAccessible(true);
                    try {
                        field.set(f, null);
                    } catch (final IllegalAccessException e) {
                        throw unchecked(e);
                    }//from  ww  w  .ja  v a2 s  . c  o  m
                }
            }
        }
    }
    return f;
}

From source file:org.datalorax.populace.core.populate.GraphPopulator.java

private static boolean isNotInnerClass(final Class<?> type) {
    return type.getEnclosingClass() == null || Modifier.isStatic(type.getModifiers());
}

From source file:Main.java

/**
 * Method for finding enclosing class for non-static inner classes
 *//*  ww w.  j a va 2  s  .  c om*/
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:com.anathema_roguelike.main.utilities.Utils.java

static String getProperty(Properties properties, Object obj, String defaultValue) {

    if (!(obj instanceof Class)) {

        if (obj instanceof String) {
            return (String) obj;
        }//  ww w  .j  av  a 2 s  .c o m

        obj = obj.getClass();
    }

    Class<?> cls = (Class<?>) obj;

    while (cls.isAnonymousClass()) {
        cls = cls.getEnclosingClass();
    }

    String property = properties.getProperty(cls.getSimpleName());

    if (property != null) {
        return property;
    } else {
        return defaultValue;
    }
}

From source file:org.evosuite.utils.NumberFormatter.java

/**
 * <p>/*from  w  w w .  j  a v  a 2 s  . c  om*/
 * getNumberString
 * </p>
 * 
 * @param value
 *            a {@link java.lang.Object} object.
 * @return a {@link java.lang.String} object.
 */
public static String getNumberString(Object value) {
    if (value == null)
        return "null";
    else if (value.getClass().equals(char.class) || value.getClass().equals(Character.class)) {
        // StringEscapeUtils fails to escape a single quote char
        if (Character.valueOf('\'').equals(value)) {
            return "'\\\''";
        } else {
            return "'" + StringEscapeUtils.escapeJava(Character.toString((Character) value)) + "'";
        }
    } else if (value.getClass().equals(String.class)) {
        return "\"" + StringEscapeUtils.escapeJava((String) value) + "\"";
    } else if (value.getClass().equals(float.class) || value.getClass().equals(Float.class)) {
        if (value.toString().equals("" + Float.NaN))
            return "Float.NaN";
        else if (value.toString().equals("" + Float.NEGATIVE_INFINITY))
            return "Float.NEGATIVE_INFINITY";
        else if (value.toString().equals("" + Float.POSITIVE_INFINITY))
            return "Float.POSITIVE_INFINITY";
        else if (((Float) value) < 0F)
            return "(" + value + "F)";
        else
            return value + "F";
    } else if (value.getClass().equals(double.class) || value.getClass().equals(Double.class)) {
        if (value.toString().equals("" + Double.NaN))
            return "Double.NaN";
        else if (value.toString().equals("" + Double.NEGATIVE_INFINITY))
            return "Double.NEGATIVE_INFINITY";
        else if (value.toString().equals("" + Double.POSITIVE_INFINITY))
            return "Double.POSITIVE_INFINITY";
        else if (((Double) value) < 0.0)
            return "(" + value + ")";
        else
            return value.toString();
    } else if (value.getClass().equals(long.class) || value.getClass().equals(Long.class)) {
        if (((Long) value) < 0)
            return "(" + value + "L)";
        else
            return value + "L";
    } else if (value.getClass().equals(byte.class) || value.getClass().equals(Byte.class)) {
        if (((Byte) value) < 0)
            return "(byte) (" + value + ")";
        else
            return "(byte)" + value;
    } else if (value.getClass().equals(short.class) || value.getClass().equals(Short.class)) {
        if (((Short) value) < 0)
            return "(short) (" + value + ")";
        else
            return "(short)" + value;
    } else if (value.getClass().equals(int.class) || value.getClass().equals(Integer.class)) {
        int val = ((Integer) value).intValue();
        if (val == Integer.MAX_VALUE)
            return "Integer.MAX_VALUE";
        else if (val == Integer.MIN_VALUE)
            return "Integer.MIN_VALUE";
        else if (((Integer) value) < 0)
            return "(" + value + ")";
        else
            return "" + val;
    } else if (value.getClass().isEnum() || value instanceof Enum) {
        // java.util.concurrent.TimeUnit is an example where the enum
        // elements are anonymous inner classes, and then isEnum does
        // not return true apparently? So we check using instanceof as well.

        Class<?> clazz = value.getClass();
        String className = clazz.getSimpleName();
        while (clazz.getEnclosingClass() != null) {
            String enclosingName = clazz.getEnclosingClass().getSimpleName();
            className = enclosingName + "." + className;
            clazz = clazz.getEnclosingClass();
        }

        // We have to do this here to avoid a double colon in the TimeUnit example
        if (!className.endsWith("."))
            className += ".";
        try {
            if (value.getClass().getField(value.toString()) != null)
                return className + value;
            else if (((Enum<?>) value).name() != null)
                return className + ((Enum<?>) value).name();
            else
                return "Enum.valueOf(" + className + "class, \"" + value + "\")";
        } catch (Exception e) {
            if (((Enum<?>) value).name() != null)
                return className + ((Enum<?>) value).name();
            else
                return "Enum.valueOf(" + className + "class /* " + e + " */, \"" + value + "\")";
            // return className + "valueOf(\"" + value + "\")";
        }
    } else if (value.getClass().equals(Boolean.class)) {
        return value.toString();
    } else {
        // This should not happen
        assert (false);
        return value.toString();
    }
}

From source file:com.anathema_roguelike.main.utilities.Utils.java

static Class<?> classify(Object obj) {
    Class<?> cls;

    if (obj instanceof Class) {
        cls = (Class<?>) obj;
    } else {//from w  ww  .java  2 s . c  o  m
        cls = obj.getClass();
    }

    while (cls.isAnonymousClass()) {
        cls = cls.getEnclosingClass();
    }

    return cls;
}

From source file:org.alfresco.module.org_alfresco_module_rm.api.PublicAPITestUtil.java

/**
 * Check if the given class is a part of the Alfresco public API.
 *
 * @param clazz The class to check.//from  w ww . j  a va2 s . co  m
 * @return {@code true} if the given class is annotated with {@link AlfrescoPublicApi}.
 */
private static boolean isPartOfPublicApi(Class<?> clazz) {
    if (clazz.getAnnotation(AlfrescoPublicApi.class) != null) {
        return true;
    }
    if (clazz.getEnclosingClass() != null) {
        return isPartOfPublicApi(clazz.getEnclosingClass());
    }
    return false;
}

From source file:com.haulmont.cuba.core.config.ConfigUtil.java

/**
 * Get the prefix associated with a configuration interface.
 * If the interface has an enclosing class, the
 * fully-qualified name of that class is used, or else the name of the
 * package containing the interface. In either of the latter two
 * cases, a '.' is appended to the name.
 *
 * @param configInterface The configuration interface.
 * @return The interface prefix.//from w  w w  .  j  a  va 2 s.  co m
 */
public static String getPropertyPrefix(Class<?> configInterface) {
    // Foo -> ""
    // foo.Bar -> "foo."
    // foo.Bar$Baz -> "foo.Bar."
    Class<?> enclosingClass = configInterface.getEnclosingClass();
    if (enclosingClass != null) {
        return enclosingClass.getName() + '.';
    } else {
        Package pkg = configInterface.getPackage();
        if (pkg != null) {
            return pkg.getName() + '.';
        } else {
            return "";
        }
    }
}

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./*from   w  ww  .  j a v  a 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 (type.getEnclosingClass() != null) {
            if (!Modifier.isStatic(type.getModifiers())) {
                return "non-static member class";
            }
        }
    } catch (SecurityException e) {
    } catch (NullPointerException e) {
    }
    return null;
}

From source file:com.github.dozermapper.core.util.MappingUtils.java

/**
 * Used to test if {@code srcFieldClass} is enum.
 *
 * @param srcFieldClass the source field to be tested.
 * @return {@code true} if and only if current running JRE is 1.5 or above, and
 * {@code srcFieldClass} is enum; otherwise return {@code false}.
 *//*  w w  w  .j ava  2 s.  com*/
public static boolean isEnumType(Class<?> srcFieldClass) {
    if (srcFieldClass.isAnonymousClass()) {
        //If srcFieldClass is anonymous class, replace srcFieldClass with its enclosing class.
        //This is used to ensure Dozer can get correct Enum type.
        srcFieldClass = srcFieldClass.getEnclosingClass();
    }
    return srcFieldClass.isEnum();
}