Example usage for java.lang Class isAnonymousClass

List of usage examples for java.lang Class isAnonymousClass

Introduction

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

Prototype

public boolean isAnonymousClass() 

Source Link

Document

Returns true if and only if the underlying class is an anonymous class.

Usage

From source file:org.opt4j.config.ModuleAutoFinder.java

/**
 * Returns all not abstract classes that implement {@link PropertyModule}.
 * //w  w  w . java2 s. c o  m
 * @return all property modules
 */
protected Collection<Class<? extends Module>> getAll() {

    Starter starter = new Starter();
    Collection<File> files = starter.addPlugins();

    classLoader = ClassLoader.getSystemClassLoader();

    String paths = System.getProperty("java.class.path");
    StringTokenizer st = new StringTokenizer(paths, ";\n:");

    while (st.hasMoreTokens()) {
        String path = st.nextToken();
        File f = new File(path);

        if (f.exists()) {
            try {
                f = f.getCanonicalFile();
                files.add(f);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

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

    for (File file : files) {

        if (isJar(file)) {

            try {
                classes.addAll(getAllClasses(new ZipFile(file)));
            } catch (ZipException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (UnsupportedClassVersionError e) {
                System.err.println(file + " not supported: bad version number");
            }
        } else {
            classes.addAll(getAllClasses(file));
        }
    }

    List<Class<? extends Module>> modules = new ArrayList<Class<? extends Module>>();

    for (Class<?> clazz : classes) {
        if (Opt4JModule.class.isAssignableFrom(clazz) && !Modifier.isAbstract(clazz.getModifiers())) {
            Class<? extends Module> module = clazz.asSubclass(Module.class);
            Ignore i = module.getAnnotation(Ignore.class);

            if (i == null && !module.isAnonymousClass() && accept.transform(module)
                    && !ignore.transform(module)) {
                modules.add(module);
                invokeOut("Add module: " + module.toString());
            }
        }
    }

    return modules;

}

From source file:de.alpharogroup.lang.ClassExtensions.java

/**
 * Gets the {@link ClassType} from the given class.
 *
 * @param clazz//from  ww  w  .  j a v a2s.  c  om
 *            The class.
 * @return the {@link ClassType} from the given class.
 */
public static ClassType getClassType(final Class<?> clazz) {
    if (clazz.isArray()) {
        return ClassType.ARRAY;
    }
    if (isCollection(clazz)) {
        return ClassType.COLLECTION;
    }
    if (isMap(clazz)) {
        return ClassType.MAP;
    }
    if (clazz.isLocalClass()) {
        return ClassType.LOCAL;
    }
    if (clazz.isMemberClass()) {
        return ClassType.MEMBER;
    }
    if (clazz.isPrimitive()) {
        return ClassType.PRIMITIVE;
    }
    if (clazz.isAnnotation()) {
        return ClassType.ANNOTATION;
    }
    if (clazz.isEnum()) {
        return ClassType.ENUM;
    }
    if (clazz.isInterface()) {
        return ClassType.INTERFACE;
    }
    if (clazz.isSynthetic()) {
        return ClassType.SYNTHETIC;
    }
    if (clazz.isAnonymousClass()) {
        return ClassType.ANONYMOUS;
    }
    return ClassType.DEFAULT;
}

From source file:one.nio.serial.Repository.java

private static synchronized Serializer generateFor(Class<?> cls) {
    Serializer serializer = classMap.get(cls);
    if (serializer == null) {
        if (cls.isArray()) {
            get(cls.getComponentType());
            serializer = new ObjectArraySerializer(cls);
        } else if ((cls.getModifiers() & ENUM) != 0) {
            if (cls.getSuperclass() != Enum.class) {
                serializer = get(cls.getSuperclass());
                classMap.put(cls, serializer);
                return serializer;
            }/*from w  ww  .jav  a2  s  .  c o m*/
            serializer = new EnumSerializer(cls);
        } else if (Externalizable.class.isAssignableFrom(cls)) {
            serializer = new ExternalizableSerializer(cls);
        } else if (Collection.class.isAssignableFrom(cls) && !hasOptions(cls, FIELD_SERIALIZATION)) {
            serializer = new CollectionSerializer(cls);
        } else if (Map.class.isAssignableFrom(cls) && !hasOptions(cls, FIELD_SERIALIZATION)) {
            serializer = new MapSerializer(cls);
        } else if (Serializable.class.isAssignableFrom(cls)) {
            serializer = new GeneratedSerializer(cls);
        } else {
            serializer = new InvalidSerializer(cls);
        }

        serializer.generateUid();
        provideSerializer(serializer);

        if (cls.isAnonymousClass()) {
            log.warn("Trying to serialize anonymous class: " + cls.getName());
            anonymousClasses.incrementAndGet();
        }

        Renamed renamed = cls.getAnnotation(Renamed.class);
        if (renamed != null) {
            renamedClasses.put(renamed.from(), cls);
        }
    }
    return serializer;
}

From source file:com.feilong.core.lang.ClassUtilTest.java

/**
 *  class info map for LOGGER.//from w  w w  .j  av a  2s . c o m
 *
 * @param klass
 *            the klass
 * @return  <code>klass</code> nullempty, {@link Collections#emptyMap()}<br>
 */
public static Map<String, Object> getClassInfoMapForLog(Class<?> klass) {
    if (isNullOrEmpty(klass)) {
        return Collections.emptyMap();
    }

    Map<String, Object> map = new LinkedHashMap<String, Object>();

    map.put("clz.getCanonicalName()", klass.getCanonicalName());//"com.feilong.core.date.DatePattern"
    map.put("clz.getName()", klass.getName());//"com.feilong.core.date.DatePattern"
    map.put("clz.getSimpleName()", klass.getSimpleName());//"DatePattern"

    map.put("clz.getComponentType()", klass.getComponentType());
    // ?"". ,voidboolean?byte?char?short?int?long?float  double?.
    map.put("clz.isPrimitive()", klass.isPrimitive());

    // ?"".,.
    map.put("clz.isLocalClass()", klass.isLocalClass());
    // ?"?".?,?,?"""??".
    map.put("clz.isMemberClass()", klass.isMemberClass());

    //isSynthetic()?Class?"??".java??false,?true.,JVM???,java??"??"?
    map.put("clz.isSynthetic()", klass.isSynthetic());
    map.put("clz.isArray()", klass.isArray());
    map.put("clz.isAnnotation()", klass.isAnnotation());

    //??true.
    map.put("clz.isAnonymousClass()", klass.isAnonymousClass());
    map.put("clz.isEnum()", klass.isEnum());

    return map;
}

From source file:fr.exanpe.tapestry.tldgen.taglib.builder.StructureBuilder.java

/**
 * Builds the output taglib structure//from  w w w  .  j a  va2 s  . c o  m
 * 
 * @param rootPackage the root package to look the components for
 * @param supportedPackages all sub packages to scan
 * @param urls the urls used to scan the packages
 * @return the structure containing the information on the taglib to generate
 * @throws MojoExecutionException if any unexpected error occurs
 */
public Taglib build(String rootPackage, String[] supportedPackages, URL[] urls) throws MojoExecutionException {
    Taglib taglib = new Taglib();

    log.debug("Creating taglib object model...");

    for (String subPackage : supportedPackages) {
        String pkgname = rootPackage + "." + subPackage;

        log.debug("Processing taglib for full package named : " + pkgname);

        Reflections reflections = new Reflections(new ConfigurationBuilder()
                .filterInputsBy(new FilterBuilder.Include(FilterBuilder.prefix(pkgname))).setUrls(urls)
                .setScanners(new TypesScanner()));

        Store store = reflections.getStore();

        // Return classes anaylised by TypeScanner
        Multimap<String, String> classes = store.getStoreMap().values().iterator().next();

        log.debug(String.format("%s classes to analyse for %s package...", classes.keySet().size(), pkgname));

        // Loop on found classes
        for (final String s : classes.keySet()) {
            Class<?> c;
            try {
                log.debug(String.format("Load class %s into classloader", s));
                c = Thread.currentThread().getContextClassLoader().loadClass(s);
            } catch (ClassNotFoundException e) {
                // should not happen as it has just been parsed by Reflection...
                log.error(e);
                throw new MojoExecutionException("Class loader internal error for class :" + s, e);
            }

            if (!c.isAnnotation() && !c.isAnonymousClass() && !c.isEnum() && !c.isInterface()
                    && !c.isLocalClass() && !c.isMemberClass() && !c.isSynthetic()
                    && !Modifier.isAbstract(c.getModifiers())) {
                log.debug("Processing Tag : " + c.getName());

                Tag tag = buildTagFromClass(rootPackage, c);
                taglib.getTags().add(tag);
            }
        }
    }

    log.debug("Taglib object model completed");
    return taglib;
}

From source file:cn.webwheel.DefaultMain.java

@SuppressWarnings("unchecked")
private void autoMap(String root, String pkg, String name) {
    Class cls;
    try {/*from   w w  w  .  ja  v a2  s  .c o  m*/
        cls = Class.forName(pkg + "." + name);
    } catch (ClassNotFoundException e) {
        return;
    }
    if (cls.isMemberClass() && !Modifier.isStatic(cls.getModifiers())) {
        return;
    }
    if (cls.isAnonymousClass() || cls.isLocalClass() || !Modifier.isPublic(cls.getModifiers())
            || Modifier.isAbstract(cls.getModifiers())) {
        return;
    }

    name = name.replace('$', '.');

    for (Method method : cls.getMethods()) {

        String pathPrefix = pkg.substring(root.length()).replace('.', '/') + '/';
        String path = pathPrefix + name + '.' + method.getName();

        Action action = getAction(cls, method);
        if (action == null)
            continue;

        if (!action.value().isEmpty()) {
            if (action.value().startsWith("?")) {
                path = path + action.value();
            } else if (action.value().startsWith(".")) {
                path = pathPrefix + name + action.value();
            } else if (!action.value().startsWith("/")) {
                path = pathPrefix + action.value();
            } else {
                path = action.value();
            }
        }
        ActionBinder binder = map(path);
        if (!action.rest().isEmpty()) {
            String rest = action.rest();
            if (!rest.startsWith("/"))
                rest = pathPrefix + rest;
            binder = binder.rest(rest);
        }
        SetterConfig cfg = binder.with(cls, method);
        if (!action.charset().isEmpty()) {
            cfg = cfg.setCharset(action.charset());
        }
        if (action.fileUploadFileSizeMax() != 0) {
            cfg = cfg.setFileUploadFileSizeMax(action.fileUploadFileSizeMax());
        }
        if (action.fileUploadSizeMax() != 0) {
            cfg.setFileUploadSizeMax(action.fileUploadSizeMax());
        }
        if (action.setterPolicy() != SetterPolicy.Auto) {
            cfg.setSetterPolicy(action.setterPolicy());
        }
    }
}

From source file:com.discovery.darchrow.lang.ClassUtil.java

/**
 *  class info map for LOGGER./*from w  w  w  .  j  a v  a  2s  .  c o  m*/
 *
 * @param klass
 *            the clz
 * @return the map for log
 */
public static Map<String, Object> getClassInfoMapForLog(Class<?> klass) {
    if (Validator.isNullOrEmpty(klass)) {
        return null;
    }

    Map<String, Object> map = new LinkedHashMap<String, Object>();

    map.put("clz.getCanonicalName()", klass.getCanonicalName());//"com.feilong.core.date.DatePattern"
    map.put("clz.getName()", klass.getName());//"com.feilong.core.date.DatePattern"
    map.put("clz.getSimpleName()", klass.getSimpleName());//"DatePattern"

    map.put("clz.getComponentType()", klass.getComponentType());
    // ?? voidboolean?byte?char?short?int?long?float  double?
    map.put("clz.isPrimitive()", klass.isPrimitive());

    // ??,
    map.put("clz.isLocalClass()", klass.isLocalClass());
    // ????,??????
    map.put("clz.isMemberClass()", klass.isMemberClass());

    //isSynthetic()?Class????java??false?trueJVM???java??????
    map.put("clz.isSynthetic()", klass.isSynthetic());
    map.put("clz.isArray()", klass.isArray());
    map.put("clz.isAnnotation()", klass.isAnnotation());

    //??true
    map.put("clz.isAnonymousClass()", klass.isAnonymousClass());
    map.put("clz.isEnum()", klass.isEnum());

    return map;
}

From source file:cn.teamlab.wg.framework.struts2.json.PackageBasedActionConfigBuilder.java

/**
 * Interfaces, enums, annotations, and abstract classes cannot be
 * instantiated.// w  w  w  .jav  a2  s .  c  o m
 * 
 * @param actionClass
 *            class to check
 * @return returns true if the class cannot be instantiated or should be
 *         ignored
 */
protected boolean cannotInstantiate(Class<?> actionClass) {
    return actionClass.isAnnotation() || actionClass.isInterface() || actionClass.isEnum()
            || (actionClass.getModifiers() & Modifier.ABSTRACT) != 0 || actionClass.isAnonymousClass();
}

From source file:com.sunchenbin.store.feilong.core.lang.ClassUtil.java

/**
 *  class info map for LOGGER.//  ww  w .  java  2 s  . c o m
 *
 * @param klass
 *            the clz
 * @return the map for log
 */
public static Map<String, Object> getClassInfoMapForLog(Class<?> klass) {
    if (Validator.isNullOrEmpty(klass)) {
        return Collections.emptyMap();
    }

    Map<String, Object> map = new LinkedHashMap<String, Object>();

    map.put("clz.getCanonicalName()", klass.getCanonicalName());//"com.sunchenbin.store.feilong.core.date.DatePattern"
    map.put("clz.getName()", klass.getName());//"com.sunchenbin.store.feilong.core.date.DatePattern"
    map.put("clz.getSimpleName()", klass.getSimpleName());//"DatePattern"

    map.put("clz.getComponentType()", klass.getComponentType());
    // ?? ,voidboolean?byte?char?short?int?long?float  double?
    map.put("clz.isPrimitive()", klass.isPrimitive());

    // ??,
    map.put("clz.isLocalClass()", klass.isLocalClass());
    // ????,?,?????
    map.put("clz.isMemberClass()", klass.isMemberClass());

    //isSynthetic()?Class????java??false,?true,JVM???,java??????
    map.put("clz.isSynthetic()", klass.isSynthetic());
    map.put("clz.isArray()", klass.isArray());
    map.put("clz.isAnnotation()", klass.isAnnotation());

    //??true
    map.put("clz.isAnonymousClass()", klass.isAnonymousClass());
    map.put("clz.isEnum()", klass.isEnum());

    return map;
}

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

/**
 * Sets the required type for the Java class.
 *
 * @param type/*w  w  w .j  a va  2 s. c o 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;
}