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

/**
 * Get an URL to a schema file. This implementation finds the schema file using the ClassLoader
 * that loaded the argument class./*from   w ww .  ja va  2  s  .c o m*/
 * 
 * @param resourceFileName
 * @param runningClass 
 * @return
 */
public static String getResourceUrlString(String resourceFileName, Class<?> runningClass) {
    String rtn = null;
    URL url = runningClass.getClassLoader().getResource(resourceFileName);
    if (url == null)
        throw new MissingResourceException("Resource not found: " + resourceFileName, runningClass.getName(),
                resourceFileName);
    rtn = url.toString();
    return rtn;
}

From source file:be.fedict.commons.eid.consumer.tlv.TlvParser.java

/**
 * Parses the given file using the meta-data annotations within the tlvClass
 * parameter.//from www  . j  av a 2  s  . co  m
 * 
 * @param <T>
 * @param file
 * @param tlvClass
 * @return
 */
public static <T> T parse(final byte[] file, final Class<T> tlvClass) {
    T t;
    try {
        t = parseThrowing(file, tlvClass);
    } catch (final Exception ex) {
        throw new RuntimeException("error parsing file: " + tlvClass.getName(), ex);
    }
    return t;
}

From source file:com.cloudera.recordservice.tests.ClusterController.java

/**
 * This method allows the caller to add environment variables to the JVM.
 * There is no easy way to do this through a simple call, such as there is to
 * read env variables using System.getEnv(variableName). Much of the method
 * was written with guidance from stack overflow:
 * http://stackoverflow.com/questions// w  w w .  j  a  v a  2  s  .c  o m
 * /318239/how-do-i-set-environment-variables-from-java
 */
protected static void setEnv(Map<String, String> newenv) {
    try {
        Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
        Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
        theEnvironmentField.setAccessible(true);
        Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
        env.putAll(newenv);
        Field theCaseInsensitiveEnvironmentField = processEnvironmentClass
                .getDeclaredField("theCaseInsensitiveEnvironment");
        theCaseInsensitiveEnvironmentField.setAccessible(true);
        Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
        cienv.putAll(newenv);
    } catch (NoSuchFieldException e) {
        try {
            Class[] classes = Collections.class.getDeclaredClasses();
            Map<String, String> env = System.getenv();
            for (Class cl : classes) {
                if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
                    Field field = cl.getDeclaredField("m");
                    field.setAccessible(true);
                    Object obj = field.get(env);
                    Map<String, String> map = (Map<String, String>) obj;
                    map.clear();
                    map.putAll(newenv);
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}

From source file:com.elasticbox.jenkins.k8s.util.TestUtils.java

public static void setEnv(Map<String, String> newenv) {
    try {//from   w  w w  .  ja v a  2  s. co m
        Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
        Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
        theEnvironmentField.setAccessible(true);
        Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
        env.putAll(newenv);
        Field theCaseInsensitiveEnvironmentField = processEnvironmentClass
                .getDeclaredField("theCaseInsensitiveEnvironment");
        theCaseInsensitiveEnvironmentField.setAccessible(true);
        Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
        cienv.putAll(newenv);
    } catch (NoSuchFieldException e) {
        try {
            Class[] classes = Collections.class.getDeclaredClasses();
            Map<String, String> env = System.getenv();
            for (Class cl : classes) {
                if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
                    Field field = cl.getDeclaredField("m");
                    field.setAccessible(true);
                    Object obj = field.get(env);
                    Map<String, String> map = (Map<String, String>) obj;
                    map.putAll(newenv);
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}

From source file:Main.java

/**
 * Returns a class name without "java.lang." and "java.util." prefix, also shows array types in a format like
 * {@code int[]}; useful for printing class names in error messages.
 * //from  w ww .  j a  v a2s .  c  o  m
 * @param pClass can be {@code null}, in which case the method returns {@code null}.
 * @param shortenFreeMarkerClasses if {@code true}, it will also shorten FreeMarker class names. The exact rules
 *     aren't specified and might change over time, but right now, {@code freemarker.ext.beans.NumberModel} for
 *     example becomes to {@code f.e.b.NumberModel}. 
 * 
 * @since 2.3.20
 */
public static String getShortClassName(Class pClass, boolean shortenFreeMarkerClasses) {
    if (pClass == null) {
        return null;
    } else if (pClass.isArray()) {
        return getShortClassName(pClass.getComponentType()) + "[]";
    } else {
        String cn = pClass.getName();
        if (cn.startsWith("java.lang.") || cn.startsWith("java.util.")) {
            return cn.substring(10);
        } else {
            if (shortenFreeMarkerClasses) {
                if (cn.startsWith("freemarker.template.")) {
                    return "f.t" + cn.substring(19);
                } else if (cn.startsWith("freemarker.ext.beans.")) {
                    return "f.e.b" + cn.substring(20);
                } else if (cn.startsWith("freemarker.core.")) {
                    return "f.c" + cn.substring(15);
                } else if (cn.startsWith("freemarker.ext.")) {
                    return "f.e" + cn.substring(14);
                } else if (cn.startsWith("freemarker.")) {
                    return "f" + cn.substring(10);
                }
                // Falls through
            }
            return cn;
        }
    }
}

From source file:com.anathema_roguelike.main.utilities.Utils.java

@SuppressWarnings("unchecked")
public static <T> Collection<Class<? extends T>> getSubclasses(Class<T> superclass, boolean includeAbstract,
        boolean includeSuperclass, Predicate<Class<? extends T>> predicate) {
    ArrayList<Class<? extends T>> ret = new ArrayList<>();

    if (subtypeCache.containsKey(superclass)) {
        ret = new ArrayList<>((ArrayList<Class<? extends T>>) subtypeCache.get(superclass));
    } else {/*from   w  w w.  j av  a  2s.c  om*/

        Rebound rebound = new Rebound("com.anathema_roguelike", includeAbstract);

        Set<Class<? extends T>> subTypes = rebound.getSubClassesOf(superclass);

        if (!includeSuperclass) {
            subTypes.remove(superclass);
        }

        ArrayList<Class<? extends T>> sorted = new ArrayList<>(subTypes);
        Collections.sort(sorted, new Comparator<Class<? extends T>>() {
            @Override
            public int compare(Class<? extends T> o1, Class<? extends T> o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });

        subtypeCache.put((Class<?>) superclass, (ArrayList<Class<? extends T>>) sorted);

        ret = new ArrayList<>(sorted);
    }

    return Collections2.filter(ret, predicate);
}

From source file:Main.java

/**
 * Gets a method and forces it to be accessible, even if it is not.
 *
 * @param clazz The class from which the method will be got.
 * @param methodName The name of the method.
 * @param parameterTypes The parameter types that the method must match.
 * @return The method, if it is found.// w w  w  .ja  va2  s .  c  om
 * @since 2.0.7
 */
public static Method getForcedAccessibleMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
    Method method;
    try {
        method = clazz.getMethod(methodName, parameterTypes);
    } catch (SecurityException e) {
        throw new RuntimeException("Cannot access method '" + methodName + "' in class '" + clazz.getName()
                + "' for security reasons", e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(
                "The method '" + methodName + "' in class '" + clazz.getName() + "' does not exist", e);
    }
    if (!method.isAccessible()) {
        method.setAccessible(true);
    }
    return method;
}

From source file:org.frat.common.converter.ConverterService.java

private static <T, F> ObjectConverter getCustomConverterInstance(
        Class<? extends ObjectConverter> customConverterClass) {
    if (customConverterClass == null) {
        return null;
    }//from   w  w  w . j av a2  s .  com
    String key = customConverterClass.getName();
    ObjectConverter converter = CACHED_CUSTOM_CONVERTER_MAP.get(key);
    if (converter == null) {
        synchronized (CACHED_CUSTOM_CONVERTER_MAP) {
            try {
                converter = ApplicationContextUtil.getBean(customConverterClass);
            } catch (BeansException e) {
                LOGGER.info(customConverterClass.getName() + " is not a component, need new instance.");
            }
            if (converter == null) {
                try {
                    converter = customConverterClass.newInstance();
                    CACHED_CUSTOM_CONVERTER_MAP.put(key, converter);
                } catch (InstantiationException e) {
                    return null;
                } catch (IllegalAccessException e) {
                    return null;
                }
            }
        }
    }
    return converter;
}

From source file:cc.redpen.validator.ValidatorFactory.java

static void registerValidator(Class<? extends Validator> clazz) {
    boolean deprecated = clazz.getAnnotation(Deprecated.class) == null ? false : true;
    if (deprecated) {
        LOG.warn(clazz.getName() + " is deprecated");
    }/*from www. ja  v  a 2s. c o  m*/
    validators.put(clazz.getSimpleName().replace("Validator", ""), createValidator(clazz));
}

From source file:com.fizzed.rocker.compiler.RockerUtil.java

public static String qualifiedClassName(Class<?> type) {
    return type.getName().replace('$', '.');
}