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:com.eucalyptus.simpleworkflow.common.client.WorkflowClientStandalone.java

private void handleClassFile(final File f, final JarEntry j) throws IOException, RuntimeException {
    final String classGuess = j.getName().replaceAll("/", ".").replaceAll("\\.class.{0,1}", "");
    try {// w ww. jav  a  2s  .c o m
        final Class candidate = ClassLoader.getSystemClassLoader().loadClass(classGuess);
        final Ats ats = Ats.inClassHierarchy(candidate);
        if ((this.allowedClassNames.isEmpty() || this.allowedClassNames.contains(candidate.getName())
                || this.allowedClassNames.contains(candidate.getCanonicalName())
                || this.allowedClassNames.contains(candidate.getSimpleName()))
                && (ats.has(Workflow.class) || ats.has(Activities.class))
                && !Modifier.isAbstract(candidate.getModifiers())
                && !Modifier.isInterface(candidate.getModifiers()) && !candidate.isLocalClass()
                && !candidate.isAnonymousClass()) {
            if (ats.has(Workflow.class)) {
                this.workflowClasses.add(candidate);
                LOG.debug("Discovered workflow implementation class: " + candidate.getName());
            } else {
                this.activityClasses.add(candidate);
                LOG.debug("Discovered activity implementation class: " + candidate.getName());
            }
        }
    } catch (final ClassNotFoundException e) {
        LOG.debug(e, e);
    }
}

From source file:org.evosuite.setup.TestClusterGenerator.java

public static boolean canUse(Class<?> c) {
    //if (Throwable.class.isAssignableFrom(c))
    //   return false;
    if (Modifier.isPrivate(c.getModifiers()))
        return false;

    if (!Properties.USE_DEPRECATED && c.isAnnotationPresent(Deprecated.class)) {
        logger.debug("Skipping deprecated class {}", c.getName());
        return false;
    }/*from w w w .j  a  v  a 2  s . c o  m*/

    if (c.isAnonymousClass()) {
        return false;
    }

    if (ANONYMOUS_MATCHER1.matcher(c.getName()).matches()) {
        logger.debug("{} looks like an anonymous class, ignoring it", c);
        return false;
    }

    if (ANONYMOUS_MATCHER2.matcher(c.getName()).matches()) {
        logger.debug("{} looks like an anonymous class, ignoring it", c);
        return false;
    }

    if (c.getName().startsWith("junit"))
        return false;

    if (isEvoSuiteClass(c) && !MockList.isAMockClass(c.getCanonicalName())) {
        return false;
    }

    if (c.getEnclosingClass() != null) {
        if (!canUse(c.getEnclosingClass()))
            return false;
    }

    if (c.getDeclaringClass() != null) {
        if (!canUse(c.getDeclaringClass()))
            return false;
    }

    // If the SUT is not in the default package, then
    // we cannot import classes that are in the default
    // package
    if (!c.isArray() && !c.isPrimitive() && !Properties.CLASS_PREFIX.isEmpty() && !c.getName().contains(".")) {
        return false;
    }

    if (Modifier.isPublic(c.getModifiers())) {
        return true;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(c.getModifiers())) {
        //              && !Modifier.isProtected(c.getModifiers())) {
        String packageName = ClassUtils.getPackageName(c);
        if (packageName.equals(Properties.CLASS_PREFIX)) {
            return true;
        }
    }

    logger.debug("Not public");
    return false;
}

From source file:org.broadinstitute.gatk.utils.help.GenericDocumentationHandler.java

/**
 * Attempt to instantiate class c, if possible.  Returns null if this proves impossible.
 *
 * @param c//from  w w w  . ja v a  2s . c  o m
 * @return
 */
private Object makeInstanceIfPossible(Class c) {
    Object instance = null;
    try {
        // don't try to make something where we will obviously fail
        if (!c.isEnum() && !c.isAnnotation() && !c.isAnonymousClass() && !c.isArray()
                && !c.isPrimitive() & JVMUtils.isConcrete(c)) {
            instance = c.newInstance();
            //System.out.printf("Created object of class %s => %s%n", c, instance);
            return instance;
        } else
            return null;
    } catch (IllegalAccessException e) {
    } catch (InstantiationException e) {
    } catch (ExceptionInInitializerError e) {
    } catch (SecurityException e) {
    }
    // this last one is super dangerous, but some of these methods catch ClassNotFoundExceptions
    // and rethrow then as RuntimeExceptions
    catch (RuntimeException e) {
    }

    return instance;
}

From source file:com.feilong.commons.core.lang.ClassUtil.java

/**
 *  class info map for log./*  w  ww .j a  v  a2  s. co m*/
 *
 * @param klass
 *            the clz
 * @return the map for log
 */
public static Map<String, Object> getClassInfoMapForLog(Class<?> klass) {

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

    //"clz.getCanonicalName()": "com.feilong.commons.core.date.DatePattern",
    //"clz.getName()": "com.feilong.commons.core.date.DatePattern",
    //"clz.getSimpleName()": "DatePattern",

    // getCanonicalName ( Java Language Specification ??) && getName
    //??class?array?
    // getName[[Ljava.lang.String?getCanonicalName?
    map.put("clz.getCanonicalName()", klass.getCanonicalName());
    map.put("clz.getName()", klass.getName());
    map.put("clz.getSimpleName()", klass.getSimpleName());

    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;
    //      Class<?> klass = this.getClass();
    //
    //      TypeVariable<?>[] typeParameters = klass.getTypeParameters();
    //      TypeVariable<?> typeVariable = typeParameters[0];
    //
    //      Type[] bounds = typeVariable.getBounds();
    //
    //      Type bound = bounds[0];
    //
    //      if (log.isDebugEnabled()){
    //         log.debug("" + (bound instanceof ParameterizedType));
    //      }
    //
    //      Class<T> modelClass = ReflectUtil.getGenericModelClass(klass);
    //      return modelClass;
}

From source file:com.zyf.framework.plugin.SqlSessionFactoryBean.java

/**
 * Build a {@code SqlSessionFactory} instance.
 *
 * The default implementation uses the standard MyBatis {@code XMLConfigBuilder} API to build a
 * {@code SqlSessionFactory} instance based on an Reader.
 * Since 1.3.0, it can be specified a {@link Configuration} instance directly(without config file).
 *
 * @return SqlSessionFactory/*from ww w .  jav  a 2  s.c  o m*/
 * @throws IOException if loading the config file failed
 */
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {

    Configuration configuration;

    XMLConfigBuilder xmlConfigBuilder = null;
    if (this.configuration != null) {
        configuration = this.configuration;
        if (configuration.getVariables() == null) {
            configuration.setVariables(this.configurationProperties);
        } else if (this.configurationProperties != null) {
            configuration.getVariables().putAll(this.configurationProperties);
        }
    } else if (this.configLocation != null) {
        xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null,
                this.configurationProperties);
        configuration = xmlConfigBuilder.getConfiguration();
    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(
                    "Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration");
        }
        configuration = new Configuration();
        if (this.configurationProperties != null) {
            configuration.setVariables(this.configurationProperties);
        }
    }

    if (this.objectFactory != null) {
        configuration.setObjectFactory(this.objectFactory);
    }

    if (this.objectWrapperFactory != null) {
        configuration.setObjectWrapperFactory(this.objectWrapperFactory);
    }

    if (this.vfs != null) {
        configuration.setVfsImpl(this.vfs);
    }

    if (hasLength(this.typeAliasesPackage)) {
        String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeAliasPackageArray) {
            configuration.getTypeAliasRegistry().registerAliases(packageToScan,
                    typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Scanned package: '" + packageToScan + "' for aliases");
            }
        }
    }

    if (!isEmpty(this.typeAliases)) {
        for (Class<?> typeAlias : this.typeAliases) {
            configuration.getTypeAliasRegistry().registerAlias(typeAlias);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Registered type alias: '" + typeAlias + "'");
            }
        }
    }

    if (!isEmpty(this.plugins)) {
        for (Interceptor plugin : this.plugins) {
            configuration.addInterceptor(plugin);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Registered plugin: '" + plugin + "'");
            }
        }
    }

    if (hasLength(this.typeHandlersPackage)) {
        String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeHandlersPackageArray) {
            configuration.getTypeHandlerRegistry().register(packageToScan);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Scanned package: '" + packageToScan + "' for type handlers");
            }
        }
    }

    if (!isEmpty(this.typeHandlers)) {
        for (TypeHandler<?> typeHandler : this.typeHandlers) {

            //TODO:
            ClazzTypeScan cts = typeHandler.getClass().getAnnotation(ClazzTypeScan.class);
            if (cts == null) {
                configuration.getTypeHandlerRegistry().register(typeHandler);
            } else {
                TypeScan typescan = cts.value();
                if (typeScanMap.containsKey(typescan.name())) {
                    String scanpath = typeScanMap.get(typescan.name());

                    ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
                    resolverUtil.find(new ResolverUtil.IsA(DescriptionID.class), scanpath);
                    Set<Class<? extends Class<?>>> handlerSet = resolverUtil.getClasses();
                    for (Class<?> type : handlerSet) {
                        try {
                            Constructor<?> c = typeHandler.getClass().getConstructor(Class.class);
                            c.newInstance(type);
                        } catch (NoSuchMethodException | SecurityException e) {
                            e.printStackTrace();
                        } catch (InstantiationException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }

                        if (!type.isAnonymousClass() && !type.isInterface()
                                && !Modifier.isAbstract(type.getModifiers())) {
                            configuration.getTypeHandlerRegistry().register(type, typeHandler.getClass());
                        }
                    }

                }
            }

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Registered type handler: '" + typeHandler + "'");
            }
        }
    }

    if (this.databaseIdProvider != null) {//fix #64 set databaseId before parse mapper xmls
        try {
            configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
        } catch (SQLException e) {
            throw new NestedIOException("Failed getting a databaseId", e);
        }
    }

    if (this.cache != null) {
        configuration.addCache(this.cache);
    }

    if (xmlConfigBuilder != null) {
        try {
            xmlConfigBuilder.parse();

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Parsed configuration file: '" + this.configLocation + "'");
            }
        } catch (Exception ex) {
            throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
        } finally {
            ErrorContext.instance().reset();
        }
    }

    if (this.transactionFactory == null) {
        this.transactionFactory = new SpringManagedTransactionFactory();
    }

    configuration.setEnvironment(new Environment(this.environment, this.transactionFactory, this.dataSource));

    if (!isEmpty(this.mapperLocations)) {
        for (Resource mapperLocation : this.mapperLocations) {
            if (mapperLocation == null) {
                continue;
            }

            try {
                XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
                        configuration, mapperLocation.toString(), configuration.getSqlFragments());
                xmlMapperBuilder.parse();
            } catch (Exception e) {
                throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
            } finally {
                ErrorContext.instance().reset();
            }

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Parsed mapper file: '" + mapperLocation + "'");
            }
        }

        // ThinkGem ?MapperXML?
        if (mapperRefresh.isEnabled()) {
            System.out.println("mapperRefresh loading.............");
            mapperRefresh.setConfiguration(configuration);
            mapperRefresh.setMapperLocations(mapperLocations);
            mapperRefresh.run();
        }

    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Property 'mapperLocations' was not specified or no matching resources found");
        }
    }

    return this.sqlSessionFactoryBuilder.build(configuration);
}

From source file:jp.co.acroquest.jsonic.JSON.java

protected <T> T create(Context context, Class<? extends T> c) throws Exception {
    Object instance = null;/*from w  w  w  . ja va 2  s  . c om*/

    JSONHint hint = context.getHint();
    if (hint != null && hint.type() != Object.class)
        c = hint.type().asSubclass(c);

    if (c.isInterface()) {
        if (SortedMap.class.equals(c)) {
            instance = new TreeMap<Object, Object>();
        } else if (Map.class.equals(c)) {
            instance = new LinkedHashMap<Object, Object>();
        } else if (SortedSet.class.equals(c)) {
            instance = new TreeSet<Object>();
        } else if (Set.class.equals(c)) {
            instance = new LinkedHashSet<Object>();
        } else if (List.class.equals(c)) {
            instance = new ArrayList<Object>();
        } else if (Collection.class.equals(c)) {
            instance = new ArrayList<Object>();
        } else if (Appendable.class.equals(c)) {
            instance = new StringBuilder();
        }
    } else if (Modifier.isAbstract(c.getModifiers())) {
        if (Calendar.class.equals(c)) {
            instance = Calendar.getInstance();
        }
    } else if ((c.isMemberClass() || c.isAnonymousClass()) && !Modifier.isStatic(c.getModifiers())) {
        Class<?> eClass = c.getEnclosingClass();
        Constructor<?> con = c.getDeclaredConstructor(eClass);
        con.setAccessible(true);
        if (context.contextObject != null && eClass.isAssignableFrom(context.contextObject.getClass())) {
            instance = con.newInstance(context.contextObject);
        } else {
            instance = con.newInstance((Object) null);
        }
    } else {
        if (Date.class.isAssignableFrom(c)) {
            try {
                Constructor<?> con = c.getDeclaredConstructor(long.class);
                con.setAccessible(true);
                instance = con.newInstance(0l);
            } catch (NoSuchMethodException e) {
                // no handle
            }
        }

        if (instance == null) {
            Constructor<?> con = c.getDeclaredConstructor();
            con.setAccessible(true);
            instance = con.newInstance();
        }
    }

    return c.cast(instance);
}