Example usage for java.lang Class getName

List of usage examples for java.lang Class getName

Introduction

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

Prototype

public String getName() 

Source Link

Document

Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String .

Usage

From source file:Main.java

/**
 * Convenience method for obtaining most non-null human readable properties
 * of a JComponent.  Array properties are not included.
 * <P>//w ww. j  av a  2  s .  c  om
 * Implementation note:  The returned value is a HashMap.  This is subject
 * to change, so callers should code against the interface Map.
 *
 * @param component the component whose proerties are to be determined
 * @return the class and value of the properties
 */
public static Map<Object, Object> getProperties(JComponent component) {
    Map<Object, Object> retVal = new HashMap<Object, Object>();
    Class<?> clazz = component.getClass();
    Method[] methods = clazz.getMethods();
    Object value = null;
    for (Method method : methods) {
        if (method.getName().matches("^(is|get).*") && method.getParameterTypes().length == 0) {
            try {
                Class returnType = method.getReturnType();
                if (returnType != void.class && !returnType.getName().startsWith("[")
                        && !setExclude.contains(method.getName())) {
                    String key = method.getName();
                    value = method.invoke(component);
                    if (value != null && !(value instanceof Component)) {
                        retVal.put(key, value);
                    }
                }
                // ignore exceptions that arise if the property could not be accessed
            } catch (IllegalAccessException ex) {
            } catch (IllegalArgumentException ex) {
            } catch (InvocationTargetException ex) {
            }
        }
    }
    return retVal;
}

From source file:com.taobao.itest.listener.ResourceLocationProcessingUtil.java

public static String[] generateDefaultLocations(Class<?> clazz, String suffix) {
    return new String[] {
            ResourceUtils.CLASSPATH_URL_PREFIX + "/" + clazz.getName().replace('.', '/') + suffix };
}

From source file:org.codegist.crest.io.http.HttpClientFactory.java

public static HttpClient create(CRestConfig crestConfig, Class<?> source) {
    HttpClient httpClient = crestConfig.get(source.getName() + HTTP_CLIENT);
    if (httpClient != null) {
        return httpClient;
    }//from  w  w w .j ava  2 s  . c om

    int concurrencyLevel = crestConfig.getConcurrencyLevel();
    if (concurrencyLevel > 1) {
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(concurrencyLevel));
        ConnManagerParams.setMaxTotalConnections(params, concurrencyLevel);

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), HTTP_PORT));
        schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), HTTPS_PORT));

        ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
        httpClient = new DefaultHttpClient(cm, params);
    } else {
        httpClient = new DefaultHttpClient();
    }
    ((DefaultHttpClient) httpClient).setRoutePlanner(new ProxySelectorRoutePlanner(
            httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault()));
    return httpClient;
}

From source file:br.com.bea.androidtools.api.model.EntityUtils.java

public static final <E extends Entity<?>> List<Field> columnFields(final Class<E> targetClass) {
    if (null == EntityUtils.columnsCache.get(targetClass.getName().hashCode())) {
        final List<Field> fields = new LinkedList<Field>();
        for (final Field field : targetClass.getDeclaredFields())
            if (field.isAnnotationPresent(Column.class))
                fields.add(field);//from ww w.  j a  v a2 s.  c o  m
        EntityUtils.columnsCache.put(targetClass.getName().hashCode(), fields);
    }
    return EntityUtils.columnsCache.get(targetClass.getName().hashCode());
}

From source file:Main.java

private static boolean isBaseType(Class<?> o) {

    if (o == null)
        return true;

    String className = o.getName();
    if (className == null)
        return false;
    if (o.isPrimitive())
        return true;
    if (o == Byte.class)
        return true;
    if (o == Character.class)
        return true;
    if (o == Short.class)
        return true;
    if (o == Integer.class)
        return true;
    if (o == Long.class)
        return true;
    if (o == Float.class)
        return true;
    if (o == Double.class)
        return true;
    if (o == Boolean.class)
        return true;
    if (o == String.class)
        return true;
    if (o == StringBuffer.class)
        return true;
    if (o == StringBuilder.class)
        return true;
    if (o == BigInteger.class)
        return true;
    if (o == BigDecimal.class)
        return true;
    return false;
}

From source file:Main.java

private static URL getRootUrlForClass(Class<?> cls) {
    ClassLoader classLoader = cls.getClassLoader();
    String resource = cls.getName().replace('.', '/') + ".class";
    if (classLoader == null) {
        // A null class loader means the bootstrap class loader. In this case we use the
        // system class loader. This is safe since we can assume that the system class
        // loader uses parent first as delegation policy.
        classLoader = ClassLoader.getSystemClassLoader();
    }/*from w  ww .  j a  va  2s .c  om*/
    URL url = classLoader.getResource(resource);
    if (url == null) {
        return null;
    }
    String file = url.getFile();
    if (file.endsWith(resource)) {
        try {
            return new URL(url.getProtocol(), url.getHost(), url.getPort(),
                    file.substring(0, file.length() - resource.length()));
        } catch (MalformedURLException ex) {
            return null;
        }
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Given an input class object, return a string which consists of the
 * class's package name as a pathname, i.e., all dots ('.') are replaced by
 * slashes ('/'). Neither a leading nor trailing slash is added. The result
 * could be concatenated with a slash and the name of a resource and fed
 * directly to {@code ClassLoader.getResource()}. For it to be fed to
 * {@code Class.getResource} instead, a leading slash would also have
 * to be prepended to the returned value.
 *
 * @param clazz the input class. A {@code null} value or the default
 *              (empty) package will result in an empty string ("") being returned.
 * @return a path which represents the package name
 * @see ClassLoader#getResource/*w ww. j a v  a2 s  .c om*/
 * @see Class#getResource
 */
public static String classPackageAsResourcePath(Class<?> clazz) {
    if (clazz == null) {
        return "";
    }
    String className = clazz.getName();
    int packageEndIndex = className.lastIndexOf(PACKAGE_SEPARATOR);
    if (packageEndIndex == -1) {
        return "";
    }
    String packageName = className.substring(0, packageEndIndex);
    return packageName.replace(PACKAGE_SEPARATOR, PATH_SEPARATOR);
}

From source file:br.com.bea.androidtools.api.model.EntityUtils.java

public static final <E extends ValueObject> List<Field> metadataFields(final Class<E> targetClass) {
    if (null == EntityUtils.metadatasCache.get(targetClass.getName().hashCode())) {
        final List<Field> fields = new LinkedList<Field>();
        for (final Field field : targetClass.getDeclaredFields())
            if (field.isAnnotationPresent(Metadata.class))
                fields.add(field);/* w  w  w  .  j  av  a 2 s . c o  m*/
        EntityUtils.metadatasCache.put(targetClass.getName().hashCode(), fields);
    }
    return EntityUtils.metadatasCache.get(targetClass.getName().hashCode());
}

From source file:com.wavemaker.commons.util.SpringUtils.java

public static void throwSpringNotInitializedError(Class<?> uninitializedBean) {
    throw new AssertionError("Spring has not initialized " + uninitializedBean.getName());
}

From source file:Main.java

/**
 * Because of a BUG of Android (API 13-),
 * get signature info by using "getPackageArchiveInfo" of "PackageManager"
 * always causes "NullPointerException".
 * Lack of code in method "getPackageArchiveInfo":
 *     if ((flags & GET_SIGNATURES) != 0)
 *     {//from www . j  a va  2s .  co  m
 *         packageParser.collectCertificates(pkg, 0);
 *     }
 */
@SuppressWarnings("unchecked")
public static PackageInfo getPackageArchiveInfo(String archiveFilePath, int flags) {
    try {
        Class packageParserClass = Class.forName("android.content.pm.PackageParser");
        Class[] innerClasses = packageParserClass.getDeclaredClasses();
        Class packageParserPackageClass = null;
        for (Class innerClass : innerClasses) {
            if (0 == innerClass.getName().compareTo("android.content.pm.PackageParser$Package")) {
                packageParserPackageClass = innerClass;
                break;
            }
        }
        Constructor packageParserConstructor = packageParserClass.getConstructor(String.class);
        Method parsePackageMethod = packageParserClass.getDeclaredMethod("parsePackage", File.class,
                String.class, DisplayMetrics.class, int.class);
        Method collectCertificatesMethod = packageParserClass.getDeclaredMethod("collectCertificates",
                packageParserPackageClass, int.class);
        Method generatePackageInfoMethod = packageParserClass.getDeclaredMethod("generatePackageInfo",
                packageParserPackageClass, int[].class, int.class, long.class, long.class);
        packageParserConstructor.setAccessible(true);
        parsePackageMethod.setAccessible(true);
        collectCertificatesMethod.setAccessible(true);
        generatePackageInfoMethod.setAccessible(true);

        Object packageParser = packageParserConstructor.newInstance(archiveFilePath);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        displayMetrics.setToDefaults();

        final File sourceFile = new File(archiveFilePath);

        Object pkg = parsePackageMethod.invoke(packageParser, sourceFile, archiveFilePath, displayMetrics, 0);
        if (pkg == null) {
            return null;
        }

        if ((flags & PackageManager.GET_SIGNATURES) != 0) {
            collectCertificatesMethod.invoke(packageParser, pkg, 0);
        }

        return (PackageInfo) generatePackageInfoMethod.invoke(null, pkg, null, flags, 0, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}