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.github.pjungermann.config.utils.NameUtils.java

/**
 * Returns the natural name representation for the given class by splitting
 * the camel case name into multiple words, i.e.
 * {@code my.package.MyClass} gets {@code "My Class"}.
 *
 * @param clazz           the class to create the natural name for.
 * @param trailingName    the trailing name to be removed
 * @return the natural name./*from  w  ww. j av  a 2  s .  com*/
 */
@NotNull
public static String getNaturalName(@NotNull final Class clazz, @Nullable final String trailingName) {
    final String simpleName = clazz.getSimpleName();
    if (trailingName != null && !trailingName.isEmpty() && simpleName.endsWith(trailingName)) {
        return getNaturalName(simpleName.substring(0, simpleName.length() - trailingName.length()));
    }

    return getNaturalName(simpleName);
}

From source file:com.github.pjungermann.config.utils.NameUtils.java

/**
 * Returns the property name representation for the given class
 * without the trailing name / suffix, i.e. {@code "myClass"}
 * for {@code my.package.MyClassDummy} and {@code "Dummy"}.
 *
 * @param clazz           the class for which you need the property name for.
 * @param trailingName    the trailing name to be remove from the property name.
 * @return the property name representation.
 *///w  w w  .  j a  va  2 s  .co m
@NotNull
public static String getPropertyName(@NotNull final Class clazz, @Nullable final String trailingName) {
    final String simpleName = clazz.getSimpleName();
    if (trailingName != null && !trailingName.isEmpty() && simpleName.endsWith(trailingName)) {
        return getPropertyName(simpleName.substring(0, simpleName.length() - trailingName.length()));
    }

    return getPropertyName(simpleName);
}

From source file:com.glaf.core.context.ContextFactory.java

@SuppressWarnings("unchecked")
public static <T> T getBean(Class<?> clazz) {
    if (ctx == null) {
        throw new RuntimeException(" Spring context is null, please check your spring config.");
    }/*from   w  w  w  .  j a  v  a2  s  .co m*/
    String name = clazz.getSimpleName();
    name = name.substring(0, 1).toLowerCase() + name.substring(1);
    return (T) ctx.getBean(name);
}

From source file:Main.java

public static boolean creteEntity(Object entity, String fileName) {
    boolean flag = false;
    try {//from  w  ww . j av  a2 s .c  o m
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(fileName);

        Class clazz = entity.getClass();
        Field[] fields = clazz.getDeclaredFields();
        Node EntityElement = document.getElementsByTagName(clazz.getSimpleName() + "s").item(0);

        Element newEntity = document.createElement(clazz.getSimpleName());
        EntityElement.appendChild(newEntity);

        for (Field field : fields) {
            field.setAccessible(true);
            Element element = document.createElement(field.getName());
            element.appendChild(document.createTextNode(field.get(entity).toString()));
            newEntity.appendChild(element);
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(document);
        StreamResult streamResult = new StreamResult(new File(fileName));
        transformer.transform(domSource, streamResult);
        flag = true;
    } catch (ParserConfigurationException pce) {
        System.out.println(pce.getLocalizedMessage());
        pce.printStackTrace();
    } catch (TransformerException te) {
        System.out.println(te.getLocalizedMessage());
        te.printStackTrace();
    } catch (IOException ioe) {
        System.out.println(ioe.getLocalizedMessage());
        ioe.printStackTrace();
    } catch (SAXException sae) {
        System.out.println(sae.getLocalizedMessage());
        sae.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return flag;
}

From source file:org.sakuli.loader.BeanLoader.java

/**
 * @param classDef class definition of the expected bean
 * @param <T>      generic type of the returned bean.
 * @return an singleton bean of type {@code <T>} or null
 *//*from ww w .j  av a 2 s.  c o m*/
public static <T> T loadBean(Class<T> classDef) {
    try {
        logger.debug("load bean '{}' from application context", classDef.getSimpleName());
        return getBeanFactory().getBean(classDef);
    } catch (Throwable e) {
        logger.error("error in BeanLoader", e);
        throw e;
    }
}

From source file:cloudnet.util.Dumper.java

private static String getSimpleNameWithoutArrayQualifier(Class clazz) {
    String simpleName = clazz.getSimpleName();
    int indexOfBracket = simpleName.indexOf('[');
    if (indexOfBracket != -1) {
        return simpleName.substring(0, indexOfBracket);
    }//from   w ww  .j av  a  2  s. co m
    return simpleName;
}

From source file:Main.java

private static void putKeyValue(Class<?> clazz, String buildField, SortedMap<String, String> keysToValues) {
    try {/*from  w  w  w . j a va  2s . c  o m*/
        Field field = clazz.getField(buildField);
        Object value = field.get(null);
        String key = clazz.getSimpleName().toLowerCase() + "." + buildField.toLowerCase();
        keysToValues.put(key, String.valueOf(value));
    } catch (SecurityException | NoSuchFieldException | IllegalAccessException e) {
        // ignore
    }
}

From source file:be.i8c.sag.util.FileUtils.java

/**
 * Get the File that represents the location of this jar that contains the clazz.
 *
 * Note: The file may be a directory when you are running this
 * code from your IDE or without packing it into a jar.
 *
 * @param clazz A class that is located in the jar file.
 * @return A File that represents the location of the jar that contains the clazz.
 *///  w  w w.  j  a  v a2  s .com
public static File getJarFile(Class<?> clazz) {
    String className = clazz.getSimpleName() + ".class";
    String classPath = "/" + clazz.getName().replace('.', '/') + ".class";

    URL generatorFileUrl = clazz.getResource(className);

    // We want to remove the last instance of the internal class path
    String jarPath = new StringBuilder(new StringBuilder(generatorFileUrl.getPath()).reverse().toString()
            .replaceFirst(new StringBuilder(classPath).reverse().toString(), "")).reverse().toString();

    // Remove the file:/ and the '!' at the end of the path.
    // This only needs to be done if it's a file
    jarPath = jarPath.replaceAll("^file:/", "");
    jarPath = jarPath.replaceAll("!$", "");

    return new File(jarPath);
}

From source file:com.autentia.wuija.trace.persistence.OperationalTraceBuilder.java

public static OperationalTrace generateOperationalTrace(String userName, OperationalTraceTypeEnum type,
        AdvancedQuery query, Class<?> classOfProperty) {
    return generateOperationalTrace(userName, type, getGriterionsInJSon(query, classOfProperty.getSimpleName()),
            null);// w  w w .j  av  a  2  s . co m
}

From source file:Main.java

/**
 * getChildClass/*from  ww w  .j  a  v  a2  s . c  om*/
 *
 * @param field List<Child> children type
 * @return thie child class
 */
public static Class getChildClass(Field field) {
    Type t = field.getGenericType();
    Type actualType = ((ParameterizedType) t).getActualTypeArguments()[0];
    Class subclass = null;
    try {
        subclass = Class.forName(actualType.getClass().getName());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    System.out.println(subclass.getSimpleName());
    return subclass;
}