Example usage for java.lang Class getSimpleName

List of usage examples for java.lang Class getSimpleName

Introduction

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

Prototype

public String getSimpleName() 

Source Link

Document

Returns the simple name of the underlying class as given in the source code.

Usage

From source file:com.vmware.photon.controller.common.dcp.ServiceHostUtils.java

/**
 * Logs the contents of all factories on a host.
 *
 * @param host/*  w  ww . j a  v a 2 s  . c  om*/
 * @throws Throwable
 */
public static <H extends ServiceHost & DcpHostInfoProvider> void dumpHost(H host, String referrer)
        throws Throwable {
    logger.info(String.format("host: %s - %s", host.getId(), host.getPort()));
    for (Class factory : host.getFactoryServices()) {
        try {
            Operation op = Operation
                    .createGet(UriUtils.buildExpandLinksQueryUri(UriUtils.buildUri(host, factory)));
            Operation result = sendRequestAndWait(host, op, referrer);
            logger.info(String.format("%s: %s: %s", host.getPort(), factory.getSimpleName(),
                    Utils.toJson(result.getBodyRaw())));
        } catch (Throwable ex) {
            logger.info(String.format("Could not get service: %s", factory.getCanonicalName()));
        }
    }
}

From source file:net.minecraftforge.common.crafting.CraftingHelper.java

private static <T> T getClassInstance(String clsName, Class<T> expected) {
    try {//w w  w. j  a  v  a2 s .  c o  m
        Class<?> cls = Class.forName(clsName);
        if (!expected.isAssignableFrom(cls))
            throw new JsonSyntaxException("Class '" + clsName + "' is not an " + expected.getSimpleName());
        return (T) cls.newInstance();
    } catch (ClassNotFoundException e) {
        throw new JsonSyntaxException("Could not find " + expected.getSimpleName() + ": " + clsName, e);
    } catch (InstantiationException | IllegalAccessException e) {
        throw new JsonSyntaxException("Could not instantiate " + expected.getSimpleName() + ": " + clsName, e);
    }
}

From source file:com.evolveum.midpoint.repo.sql.util.RUtil.java

public static String getTableName(Class hqlType) {
    Table tableAnnotation = (Table) hqlType.getAnnotation(Table.class); // TODO what about performance here? (synchronized call)
    if (tableAnnotation != null && StringUtils.isNotEmpty(tableAnnotation.name())) {
        return tableAnnotation.name();
    }/*from   w ww. j  a va 2s  .c o  m*/
    MidPointNamingStrategy namingStrategy = new MidPointNamingStrategy();
    return namingStrategy.classToTableName(hqlType.getSimpleName());
}

From source file:com.jkoolcloud.tnt4j.streams.custom.kafka.interceptors.reporters.metrics.MetricsReporter.java

private static String getMetricsKey(String topic, Integer partition, Class<? extends TopicMetrics> metricsClass,
        String op) {//from  w w w . java 2 s.  c  o  m
    return topic + ":" + partition + ":" + metricsClass.getSimpleName() + ":" + op; // NON-NLS
}

From source file:com.buaa.cfs.utils.NetUtils.java

/**
 * Get the socket factory for the given class according to its configuration parameter
 * <tt>hadoop.rpc.socket.factory.class.&lt;ClassName&gt;</tt>. When no such parameter exists then fall back on the
 * default socket factory as configured by <tt>hadoop.rpc.socket.factory.class.default</tt>. If this default socket
 * factory is not configured, then fall back on the JVM default socket factory.
 *
 * @param conf  the configuration//from www  .  j  ava 2 s  . c o  m
 * @param clazz the class (usually a {@link })
 *
 * @return a socket factory
 */
public static SocketFactory getSocketFactory(Configuration conf, Class<?> clazz) {

    SocketFactory factory = null;

    String propValue = conf.get("hadoop.rpc.socket.factory.class." + clazz.getSimpleName());
    if ((propValue != null) && (propValue.length() > 0))
        factory = getSocketFactoryFromProperty(conf, propValue);

    if (factory == null)
        factory = getDefaultSocketFactory(conf);

    return factory;
}

From source file:iing.uabc.edu.mx.persistencia.util.JSON.java

public static <T> Object parse(String jsonString, Class<T> clazz) {
    //The resulting object represented as the json String
    Object result = null;/*from w  w w  .j  ava 2  s  . c o  m*/

    try ( //For reading all the json string tree
            JsonParser parser = Json.createParser(new StringReader(jsonString))) {

        System.out.println("------------------Parsing Start--------------------");
        Event evt = parser.next();
        if (evt == Event.START_OBJECT) {
            System.out.println("Nuevo Objeto: " + clazz.getSimpleName());

            //Prepare POJO for json to POJO parsing
            BeanManager<T> manager = new BeanManager<>(clazz);
            //Is an object
            result = parseObject(parser, manager);
        } else if (evt == Event.START_ARRAY) {
            //Is an array
            System.out.println("Nueva Colleccion de clase: " + clazz.getSimpleName());

            //Prepare collection for json to Collection parsing
            CollectionManager<T> manager = new CollectionManager<>(new ArrayList<>(), clazz);

            result = parseArray(parser, manager);
        }

        System.out.println("------------------Parsing End--------------------");
    }

    return result;
}

From source file:com.heliosphere.athena.base.resource.bundle.ResourceBundleManager.java

/**
 * Retrieves a message from a resource bundle from its key.
 * <p>//from  ww w . j ava2  s.  com
 * @param key Resource key to retrieve.
 * @param parameters Parameters to inject while formatting the message.
 * @return The formatted message.
 */
@SuppressWarnings("nls")
private static final String retrieve(final Enum<? extends IBundle> key, final Object... parameters) {
    if (!isInitialized) {
        initialize();
    }

    final Class<? extends IBundle> bundleClass = key.getDeclaringClass();

    if (BUNDLES.containsKey(bundleClass)) {
        try {
            return MessageFormat.format(BUNDLES.get(bundleClass).getString(((IBundle) key).getKey()),
                    parameters);
        } catch (MissingResourceException e) {
            throw new ResourceBundleException(BundleAthenaBase.ResourceBundleInvalidKey,
                    bundleClass.getSimpleName(), key.name(), bundleClass.getEnumConstants()[0].getKey(),
                    getLocale(), e);
        }
    }

    return "Resource bundle key cannot be found [bundle=" + bundleClass.getName() + ", key=" + key.name() + "]";
}

From source file:com.heliosphere.demeter.base.resource.bundle.ResourceBundleManager.java

/**
 * Retrieves a message from a resource bundle from its key.
 * <p>// w w w  .  j a va2 s. co m
 * @param key Resource key to retrieve.
 * @param parameters Parameters to inject while formatting the message.
 * @return The formatted message.
 */
@SuppressWarnings("nls")
private static final String retrieve(final Enum<? extends IBundle> key, final Object... parameters) {
    if (!isInitialized) {
        initialize();
    }

    final Class<? extends IBundle> bundleClass = key.getDeclaringClass();

    if (BUNDLES.containsKey(bundleClass)) {
        try {
            return MessageFormat.format(BUNDLES.get(bundleClass).getString(((IBundle) key).getKey()),
                    parameters);
        } catch (MissingResourceException e) {
            throw new ResourceBundleException(BundleDemeterBase.ResourceBundleInvalidKey,
                    bundleClass.getSimpleName(), key.name(), bundleClass.getEnumConstants()[0].getKey(),
                    getLocale(), e);
        }
    }

    return "Resource bundle key cannot be found [bundle=" + bundleClass.getName() + ", key=" + key.name() + "]";
}

From source file:cross.applicationContext.ReflectionApplicationContextGenerator.java

/**
 * Creates an xml element using the given document for element creation.
 *
 * @param generator//from ww w  . j  a v a 2s. c  o  m
 * @param document the document use for element creation
 * @param beanDescriptor
 * @return the modified element
 */
public static Element createElement(ReflectionApplicationContextGenerator generator, Document document,
        BeanDescriptor beanDescriptor) {
    Class<?> javaClass = beanDescriptor.clazz;
    System.out.println("Building bean element for " + javaClass.getName());
    //get the name of the class
    String className = javaClass.getSimpleName();

    //get the name of the package
    String packageName = javaClass.getPackage().getName();

    //create <bean /> element
    Element beanElement = document.createElement("bean");
    String classNameLower = toLowerCaseName(className);
    beanElement.setAttribute("id", classNameLower);
    String classAttr = (packageName == null) ? className : packageName + "." + className;
    beanElement.setAttribute("class", classAttr);
    beanElement.setAttribute("scope", beanDescriptor.scope);
    //constructors are not supported
    //get all the class' properties from the public fields and setter methods.

    for (Method method : javaClass.getMethods()) {
        checkMutableProperties(method, javaClass, beanDescriptor.obj, beanDescriptor.properties);
    }
    //sort by name
    Collections.sort(beanDescriptor.properties, new Comparator<ObjectProperty>() {
        @Override
        public int compare(ObjectProperty t, ObjectProperty t1) {
            return t.name.compareTo(t1.name);
        }
    });
    List<String> blackList = Arrays.asList("workflow", "progress", "cvResolver");
    //add all properties as <property /> elements
    for (ObjectProperty p : beanDescriptor.properties) {
        if (!blackList.contains(p.name)) {
            Element propertyElement = document.createElement("property");
            propertyElement.setAttribute("name", p.name);
            Comment propertyCommentElement = document
                    .createComment(AnnotationInspector.getDescriptionFor(javaClass, p.name));
            boolean append = true;
            if (p.type.startsWith("java.lang.")) {
                String shortType = p.type.substring("java.lang.".length());
                if (primitives.contains(shortType) || wrappers.contains(shortType)) {
                    propertyElement.setAttribute("value", p.value);
                }
            } else if (primitives.contains(p.type) || wrappers.contains(p.type)) {
                propertyElement.setAttribute("value", p.value);
            } else if ("Array".equals(p.type) || "List".equals(p.type) || "java.util.List".equals(p.type)) {
                Element listElement = document.createElement("list");
                String genericType = p.genericType;
                propertyElement.appendChild(listElement);
            } else if ("Set".equals(p.type) || "java.util.Set".equals(p.type)) {
                Element listElement = document.createElement("set");
                propertyElement.appendChild(listElement);
            } else if ("Map".equals(p.type) || "java.util.Map".equals(p.type)) {
                Element listElement = document.createElement("map");
                propertyElement.appendChild(listElement);
            } else if ("Properties".equals(p.type) || "java.util.Properties".equals(p.type)) {
                Element listElement = document.createElement("props");
                propertyElement.appendChild(listElement);
            } else {
                try {
                    //                    System.err.println("Skipping ref!");
                    Set<BeanDefinition> beanDefinitions = getImplementationsOf(Class.forName(p.type), "cross",
                            "maltcms", "net.sf.maltcms");
                    BeanDefinition first = null;
                    for (BeanDefinition bd : beanDefinitions) {
                        generator.addBean(bd.getBeanClassName());
                        if (first == null) {
                            first = bd;
                        }
                    }
                    if (first != null) {
                        String simpleName = first.getBeanClassName()
                                .substring(first.getBeanClassName().lastIndexOf(".") + 1);
                        propertyElement.setAttribute("ref",
                                generator.classToElement.get(toLowerCaseName(simpleName)).id);
                    }
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ReflectionApplicationContextGenerator.class.getName()).log(Level.SEVERE,
                            null, ex);
                }
                append = true;
                //                    try {
                //                        generator.addBean(p.type);
                //                        Class<?> c = Class.forName(p.type);
                //                        List<Object> objects = generator.classToObject.get(c);
                //                        if (objects != null && !objects.isEmpty()) {
                //                            propertyElement.setAttribute("ref", generator.buildBeanElement(objects.get(0)).id);
                //                        } else {
                //                            append = false;
                //                        }
                //                    } catch (ClassNotFoundException ex) {
                //                        Logger.getLogger(ReflectionApplicationContextGenerator.class.getName()).log(Level.SEVERE, null, ex);
                //                    }

            }
            if (append) {
                beanElement.appendChild(propertyCommentElement);
                beanElement.appendChild(propertyElement);
            } else {
                beanElement.appendChild(propertyCommentElement);
                Comment comment = document.createComment("<property name=\"" + p.name + "\" ref=\"\"/>");
                beanElement.appendChild(comment);
            }
        }
    }
    return beanElement;
}

From source file:com.wegas.core.Helper.java

/**
 * @param <T>/*  ww  w  .  j av a 2  s. com*/
 * @param context
 * @param type
 * @param service
 * @return looked-up EJB instance
 * @throws NamingException
 */
public static <T> T lookupBy(Context context, Class<T> type, Class<?> service) throws NamingException {
    try {
        return (T) context.lookup("java:module/" + service.getSimpleName() + "!" + type.getName());
    } catch (NamingException ex) {
        try {
            return (T) context.lookup("java:global/classes/" + service.getSimpleName() + "!" + type.getName());
        } catch (NamingException ex3) {
            try {
                return (T) context
                        .lookup("java:global/embed-classes/" + service.getSimpleName() + "!" + type.getName());
            } catch (NamingException ex1) {
                try {
                    return (T) context
                            .lookup("java:global/cobertura/" + service.getSimpleName() + "!" + type.getName());
                } catch (NamingException ex2) {
                    logger.error("Unable to retrieve to do jndi lookup on class: {}", type.getSimpleName());
                    throw ex2;
                }
            }
        }
    }
}