Example usage for java.lang Class isMemberClass

List of usage examples for java.lang Class isMemberClass

Introduction

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

Prototype

public boolean isMemberClass() 

Source Link

Document

Returns true if and only if the underlying class is a member class.

Usage

From source file:org.soybeanMilk.SbmUtils.java

/**
 * ???//from  w w w .  j  a va  2  s  .  c  om
 * <pre>
 * class A&lt;T&gt;{}
 * class B extends A&lt;Integer&gt;{}
 * </pre>
 * <code>extractTypeVariablesInType(B.class, map)</code><code>map</code>
 * <pre>
 * T    -------&gt;    Integer
 * </pre>
 * @param source
 * @param container
 * @date 2012-5-14
 */
private static void extractTypeVariablesInType(Type source, Map<TypeVariable<?>, Type> variableTypesMap) {
    if (source == null)
        return;
    else if (source instanceof Class<?>) {
        Class<?> clazz = (Class<?>) source;

        //?
        Type[] genericInterfaces = clazz.getGenericInterfaces();
        if (genericInterfaces != null) {
            for (Type t : genericInterfaces)
                extractTypeVariablesInType(t, variableTypesMap);
        }

        //
        Type genericSuperType = clazz.getGenericSuperclass();
        Class<?> superClass = clazz.getSuperclass();
        while (superClass != null && !Object.class.equals(superClass)) {
            extractTypeVariablesInType(genericSuperType, variableTypesMap);

            genericSuperType = superClass.getGenericSuperclass();
            superClass = superClass.getSuperclass();
        }

        //
        Class<?> outerClass = clazz;
        while (outerClass.isMemberClass()) {
            Type genericOuterType = outerClass.getGenericSuperclass();
            extractTypeVariablesInType(genericOuterType, variableTypesMap);

            outerClass = outerClass.getEnclosingClass();
        }
    } else if (source instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) source;

        if (isClassType(pt.getRawType())) {
            Type[] actualArgTypes = pt.getActualTypeArguments();
            TypeVariable<?>[] typeVariables = narrowToClass(pt.getRawType()).getTypeParameters();

            for (int i = 0; i < actualArgTypes.length; i++) {
                TypeVariable<?> var = typeVariables[i];
                Type value = actualArgTypes[i];

                //????
                if (value instanceof TypeVariable<?>) {
                    Type actual = variableTypesMap.get(value);

                    if (actual != null)
                        value = actual;
                }

                variableTypesMap.put(var, value);
            }
        }

        //??
        extractTypeVariablesInType(pt.getRawType(), variableTypesMap);
    }
}

From source file:org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter.java

protected void registerEndpoint(String beanName, Endpoint<?> endpoint) {
    @SuppressWarnings("rawtypes")
    Class<? extends Endpoint> type = endpoint.getClass();
    if (AnnotationUtils.findAnnotation(type, ManagedResource.class) != null) {
        // Already managed
        return;/*from   ww  w . j av a 2s  .c  o  m*/
    }
    if (type.isMemberClass()
            && AnnotationUtils.findAnnotation(type.getEnclosingClass(), ManagedResource.class) != null) {
        // Nested class with @ManagedResource in parent
        return;
    }
    try {
        registerBeanNameOrInstance(getEndpointMBean(beanName, endpoint), beanName);
    } catch (MBeanExportException ex) {
        logger.error("Could not register MBean for endpoint [" + beanName + "]", ex);
    }
}

From source file:org.springframework.core.GenericTypeResolver.java

/**
 * Build a mapping of {@link TypeVariable#getName TypeVariable names} to
 * {@link Class concrete classes} for the specified {@link Class}. Searches
 * all super types, enclosing types and interfaces.
 *//*w  w w . ja v a 2s . co  m*/
public static Map<TypeVariable, Type> getTypeVariableMap(Class clazz) {
    Map<TypeVariable, Type> ref = typeVariableCache.get(clazz);
    Map<TypeVariable, Type> typeVariableMap = (ref != null ? ref : null);

    if (typeVariableMap == null) {
        typeVariableMap = new HashMap<TypeVariable, Type>();

        // interfaces
        extractTypeVariablesFromGenericInterfaces(clazz.getGenericInterfaces(), typeVariableMap);

        // super class
        Type genericType = clazz.getGenericSuperclass();
        Class type = clazz.getSuperclass();
        while (type != null && !Object.class.equals(type)) {
            if (genericType instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) genericType;
                populateTypeMapFromParameterizedType(pt, typeVariableMap);
            }
            extractTypeVariablesFromGenericInterfaces(type.getGenericInterfaces(), typeVariableMap);
            genericType = type.getGenericSuperclass();
            type = type.getSuperclass();
        }

        // enclosing class
        type = clazz;
        while (type.isMemberClass()) {
            genericType = type.getGenericSuperclass();
            if (genericType instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) genericType;
                populateTypeMapFromParameterizedType(pt, typeVariableMap);
            }
            type = type.getEnclosingClass();
        }

        typeVariableCache.put(clazz, typeVariableMap);
    }

    return typeVariableMap;
}

From source file:org.unitils.util.ReflectionUtils.java

/**
 * Creates an instance of the given type
 * /*  w  w  w . ja va 2 s.co  m*/
 * @param <T>
 *            The type of the instance
 * @param type
 *            The type of the instance
 * @param bypassAccessibility
 *            If true, no exception is thrown if the parameterless
 *            constructor is not public
 * @param argumentTypes
 *            The constructor arg types, not null
 * @param arguments
 *            The constructor args, not null
 * @return An instance of this type
 * @throws UnitilsException
 *             If an instance could not be created
 */
public static <T> T createInstanceOfType(Class<T> type, boolean bypassAccessibility, Class[] argumentTypes,
        Object[] arguments) {

    if (type.isMemberClass() && !isStatic(type.getModifiers())) {
        throw new UnitilsException(
                "Creation of an instance of a non-static innerclass is not possible using reflection. The type "
                        + type.getSimpleName()
                        + " is only known in the context of an instance of the enclosing class "
                        + type.getEnclosingClass().getSimpleName()
                        + ". Declare the innerclass as static to make construction possible.");
    }
    try {
        Constructor<T> constructor = type.getDeclaredConstructor(argumentTypes);
        if (bypassAccessibility) {
            constructor.setAccessible(true);
        }
        return constructor.newInstance(arguments);

    } catch (InvocationTargetException e) {
        throw new UnitilsException("Error while trying to create object of class " + type.getName(),
                e.getCause());

    } catch (Exception e) {
        throw new UnitilsException("Error while trying to create object of class " + type.getName(), e);
    }
}