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:Main.java

/**
 * <p>Is the specified class an inner class or static nested class.</p>
 *
 * @param cls  the class to check, may be null
 * @return {@code true} if the class is an inner or static nested class,
 *  false if not or {@code null}/*from www  . ja  v  a2 s  .  com*/
 */
public static boolean isInnerClass(Class<?> cls) {
    return cls != null && cls.getEnclosingClass() != null;
}

From source file:Main.java

/**
 * <p>Is the specified class an inner class or static nested class.</p>
 *
 * @param cls  the class to check, may be null
 * @return {@code true} if the class is an inner or static nested class,
 *  false if not or {@code null}/*from w w w .ja v  a 2  s  .  c o m*/
 */
public static boolean isInnerClass(final Class<?> cls) {
    return cls != null && cls.getEnclosingClass() != null;
}

From source file:Main.java

public static String getProcessName(Class<?> clazz) {
    if (clazz.isAnonymousClass()) {
        return getProcessName(clazz.getEnclosingClass());
    }//from   ww  w.  ja  v a 2s .  co m
    return clazz.getSimpleName();
}

From source file:Main.java

/**
 * Helper method to determine caller outer class.
 * <p/>//  w w  w. jav a  2 s  .c o  m
 * Takes caller class and finds its top enclosing class (which is supposed to be test class).
 * 
 * @param level
 *            on which level this call is being made. 0 - call is made immediately in the method of HotSwapTool.
 * @return outer class reference
 */
private static Class<?> determineOuter(int level) {
    StackTraceElement[] stack = Thread.currentThread().getStackTrace();
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    // one for Thread#getStackTrace
    // one for #determineOuter
    // one for the caller
    String callerName = stack[level + 3].getClassName();
    try {
        Class<?> clazz = cl.loadClass(callerName);
        while (clazz.getEnclosingClass() != null) {
            clazz = clazz.getEnclosingClass();
        }
        return clazz;
    } catch (ClassNotFoundException e) {
        throw new IllegalArgumentException("Cannot find caller class: " + callerName, e);
    }
}

From source file:org.datalorax.populace.core.populate.instance.DefaultConstructorInstanceFactory.java

private static boolean isInnerClass(final Class<?> rawType) {
    return rawType.getEnclosingClass() != null && !Modifier.isStatic(rawType.getModifiers());
}

From source file:com.linecorp.armeria.server.docs.Specification.java

static Specification forServiceConfigs(Iterable<ServiceConfig> serviceConfigs,
        Map<Class<?>, ? extends TBase<?, ?>> sampleRequests) {

    final Map<Class<?>, Iterable<EndpointInfo>> map = new LinkedHashMap<>();

    for (ServiceConfig c : serviceConfigs) {
        c.service().as(ThriftService.class).ifPresent(service -> {
            for (Class<?> iface : service.interfaces()) {
                final Class<?> serviceClass = iface.getEnclosingClass();
                final List<EndpointInfo> endpoints = (List<EndpointInfo>) map.computeIfAbsent(serviceClass,
                        cls -> new ArrayList<>());

                c.pathMapping().exactPath()
                        .ifPresent(p -> endpoints.add(EndpointInfo.of(c.virtualHost().hostnamePattern(), p,
                                service.defaultSerializationFormat(), service.allowedSerializationFormats())));
            }/*from w ww  .ja va 2s  .  c  om*/
        });
    }

    return forServiceClasses(map, sampleRequests);
}

From source file:com.github.mbenson.privileged.weaver.FilesystemWeaver.java

private static Class<?> getOutermost(Class<?> type) {
    Class<?> enclosing = type.getEnclosingClass();
    return enclosing == null ? type : getOutermost(enclosing);
}

From source file:therian.operator.immutablecheck.DefaultImmutableChecker.java

private static void addImmutableTypeTo(final Set<Class<?>> target, final Class<?> type) {
    if (target.contains(type)) {
        return;/*from ww  w.  j ava2s.c o m*/
    }
    Class<?> c = type;
    while (c.isAnonymousClass()) {
        c = c.getEnclosingClass();
    }
    if (target.contains(c) && !target.equals(c)
            || StringUtils.startsWithAny(c.getSimpleName().toLowerCase(Locale.US), KNOWN_IMMUTABLE_PREFIXES)) {
        target.add(type);
    }
}

From source file:org.gearvrf.utility.Log.java

/**
 * Constructs debug {@code TAG} strings using a {@link Class}, so that
 * rename refactorings will keep the {@code TAG} up-to-date. Also handles
 * constructing fully-qualified {@code TAG} strings for nested classes.
 * <p>/*from  w w w . j  a  v a 2s . c om*/
 * Note that this involves a small amount of runtime overhead at start-up,
 * so if your code is particularly performance-sensitive, you may want to
 * stick to a manually constructed {@code TAG} string.
 * 
 * <p>
 * How you use it:
 * 
 * <pre>
 * class MyClass {
 *     ...
 *     private static final String TAG = Utility.tag(MyClass.class);
 * }
 * </pre>
 * 
 * @param clazz
 *            The {@link Class} of the class to build a {@code TAG} string
 *            for
 * @return Fully-qualified {@code TAG} string
 */
public static String tag(Class<?> clazz) {
    String result = clazz.getSimpleName();
    for (Class<?> outer = clazz.getEnclosingClass(); outer != null; outer = outer.getEnclosingClass()) {
        result = outer.getSimpleName() + "." + result;
    }
    return result;
}

From source file:com.l2jfree.network.mmocore.packethandlers.PacketDefinition.java

public static List<Class<?>> findClasses(Class<?> clazz) {
    while (clazz.getEnclosingClass() != null)
        clazz = clazz.getEnclosingClass();

    final List<Class<?>> classes = new ArrayList<Class<?>>();

    classes.add(clazz);/* w  ww  . j a va2  s  . c  om*/

    for (int i = 0; i < classes.size(); i++)
        for (Class<?> c : classes.get(i).getDeclaredClasses())
            if (!classes.contains(c))
                if (isInstanceable(c))
                    classes.add(c);

    return classes;
}