Example usage for java.lang Class isLocalClass

List of usage examples for java.lang Class isLocalClass

Introduction

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

Prototype

public boolean isLocalClass() 

Source Link

Document

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

Usage

From source file:hu.bme.mit.sette.common.validator.reflection.ClassValidator.java

/**
 * Sets the required type for the Java class.
 *
 * @param type//from w  w w.j a v  a2s .co  m
 *            the required type for the Java class.
 * @return this object
 */
public ClassValidator type(final ClassType type) {
    Validate.notNull(type, "The type must not be null");

    if (getSubject() != null) {
        Class<?> javaClass = getSubject();
        boolean isTypeValid = false;

        switch (type) {
        case CLASS:
            isTypeValid = !javaClass.isInterface() && !javaClass.isEnum() && !javaClass.isAnnotation()
                    && !javaClass.isPrimitive() && !javaClass.isArray();
            break;

        case REGULAR_CLASS:
            isTypeValid = !javaClass.isInterface() && !javaClass.isEnum() && !javaClass.isAnnotation()
                    && !javaClass.isPrimitive() && !javaClass.isArray() && !javaClass.isMemberClass()
                    && !javaClass.isAnonymousClass() && !javaClass.isLocalClass();
            break;

        case MEMBER_CLASS:
            isTypeValid = !javaClass.isInterface() && !javaClass.isEnum() && !javaClass.isAnnotation()
                    && !javaClass.isPrimitive() && !javaClass.isArray() && javaClass.isMemberClass();
            break;

        case ANONYMOUS_CLASS:
            isTypeValid = !javaClass.isInterface() && !javaClass.isEnum() && !javaClass.isAnnotation()
                    && !javaClass.isPrimitive() && !javaClass.isArray() && javaClass.isAnonymousClass();
            break;

        case LOCAL_CLASS:
            isTypeValid = !javaClass.isInterface() && !javaClass.isEnum() && !javaClass.isAnnotation()
                    && !javaClass.isPrimitive() && !javaClass.isArray() && javaClass.isLocalClass();
            break;

        case INTERFACE:
            isTypeValid = javaClass.isInterface();
            break;

        case REGULAR_INTERFACE:
            isTypeValid = javaClass.isInterface() && !javaClass.isMemberClass();
            break;

        case MEMBER_INTERFACE:
            isTypeValid = javaClass.isInterface() && javaClass.isMemberClass();
            break;

        case ENUM:
            isTypeValid = javaClass.isEnum();
            break;

        case REGULAR_ENUM:
            isTypeValid = javaClass.isEnum() && !javaClass.isMemberClass();
            break;

        case MEMBER_ENUM:
            isTypeValid = javaClass.isEnum() && javaClass.isMemberClass();
            break;

        case ANNOTATION:
            isTypeValid = javaClass.isAnnotation();
            break;

        case REGULAR_ANNOTATION:
            isTypeValid = javaClass.isAnnotation() && !javaClass.isMemberClass();
            break;

        case MEMBER_ANNOTATION:
            isTypeValid = javaClass.isAnnotation() && javaClass.isMemberClass();
            break;

        case PRIMITIVE:
            isTypeValid = javaClass.isPrimitive();
            break;

        case ARRAY:
            isTypeValid = javaClass.isArray();
            break;

        default:
            throw new UnsupportedOperationException("Unknown class type: " + type);
        }

        if (!isTypeValid) {
            this.addException(
                    String.format("The Java class must have the specified type\n" + "(type: [%s])", type));
        }
    }

    return this;
}

From source file:org.obsidian.test.TestAbstract.java

public <T> void appendDynamicImports(Class<T> classToImport) {
    boolean shouldBeIgnored = false;

    //Make sure: not already in dynamic imports
    for (String s : getDynamicImports()) {
        if (s.compareToIgnoreCase(classToImport.getName().replaceAll("\\$", ".")) == 0) {
            shouldBeIgnored = true;/*from   w  ww. j a  va 2  s  .  c  o  m*/
        }
    }

    if (!classToImport.isPrimitive() && !classToImport.isArray()) {
        //make sure: not in imports from constants
        for (String s : Content.IMPORTS) {
            if (s.compareToIgnoreCase(classToImport.getName()) == 0) {
                shouldBeIgnored = true;
            }

        }

        //make sure: not in same package
        if (classToImport.getPackage().toString()
                .compareToIgnoreCase(classTested.getPackage().toString()) == 0) {

            //#### Patch Submitted by Michael Cole (micole.3@gmail.com) 2/13/13
            if (!(classToImport.isMemberClass() || classToImport.isEnum() || classToImport.isLocalClass())) {
                shouldBeIgnored = true;
            }

        }

        //make sure not private
        if (Modifier.isPrivate(classToImport.getModifiers())) {
            shouldBeIgnored = true;
        }

        //make sure: not importing from java.lang unless at least 3 deep 
        //ex)java.lang.reflect
        String[] packageStructure = classToImport.getPackage().getName().split("\\.");
        int packageDepth = packageStructure.length;

        //if dataInputStream java.lang
        if (packageStructure[0].compareToIgnoreCase("java") == 0
                && packageStructure[1].compareToIgnoreCase("lang") == 0) {

            //and less than three deep
            if (packageDepth < 3) {
                shouldBeIgnored = true;
                classToImport.getName();
            }
        }
    } else {
        shouldBeIgnored = true;
        if (classToImport.isArray() && !classToImport.getComponentType().isPrimitive()) {
            appendDynamicImports(classToImport.getComponentType());
        }
    }

    //if: not already in imports and not in same package
    if (!shouldBeIgnored) {
        //add to dynamic imports
        String importName = classToImport.getName();
        importName = importName.replaceAll("\\$", ".");
        getDynamicImports().add(importName);
    }
}