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:com.makariev.dynamodb.config.base.BaseRepository.java

private static <T> T newInstance(Class<T> currentClass) {
    try {//from   w  w w.j av  a2  s.  com
        final T newEntity = currentClass.newInstance();
        return newEntity;
    } catch (InstantiationException ex) {
        throw new RuntimeException("could not create " + currentClass.getName(), ex);
    } catch (IllegalAccessException ex) {
        throw new RuntimeException("could not create " + currentClass.getName(), ex);
    }
}

From source file:com.netflix.paas.dao.astyanax.AstyanaxDao.java

private static String entityNameFromClass(Class<?> entityType) {
    return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE,
            StringUtils.removeEnd(StringUtils.substringAfterLast(entityType.getName(), "."), "Entity"));
}

From source file:jp.terasoluna.fw.util.ExceptionUtil.java

/**
 * ?????/*from w ww .j av  a 2 s .c o m*/
 * 
 * <p>
 *  ??????????????
 *  ????????
 *  ???getRootCause()????????ServletException??
 * </p>
 *
 * @param throwable 
 * @return ???
 */
public static String getStackTrace(Throwable throwable) {
    StringBuilder sb = new StringBuilder();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while (throwable != null) {
        baos.reset();
        throwable.printStackTrace(new PrintStream(baos));
        sb.append(baos.toString());

        //throwable?Class??
        Class<? extends Throwable> throwableClass = throwable.getClass();

        // ServletException ?? getRootCause ?
        if (SERVLET_EXCEPTION_NAME.equals(throwableClass.getName())) {
            try {
                //throwable = ((ServletException) throwable).getRootCause()
                //Class??????
                Method method = throwableClass.getMethod(GET_ROOT_CAUSE);
                throwable = (Throwable) method.invoke(throwable);
            } catch (NoSuchMethodException e) {
                //???????
                log.error(e.getMessage());
                throwable = null;
            } catch (IllegalAccessException e) {
                //?????????
                log.error(e.getMessage());
                throwable = null;
            } catch (InvocationTargetException e) {
                //?????
                log.error(e.getMessage());
                throwable = null;
            }

        } else {
            throwable = throwable.getCause();
        }
    }
    return sb.toString();
}

From source file:Main.java

public static void printMemberClasses(final Class dataType) {
    final Class[] nestedClasses = dataType.getClasses();
    final Class[] declaredNestedClasses = dataType.getDeclaredClasses();
    final Class[] nestedInterfaces = dataType.getInterfaces();
    final Class declaringClass = dataType.getDeclaringClass();

    System.out.println("Member Class infor for: " + dataType.getName());
    System.out.println("Nested Classes: " + Arrays.asList(nestedClasses));
    System.out.println("Declared Nested Classes: " + Arrays.asList(declaredNestedClasses));
    System.out.println("Interfaces: " + Arrays.asList(nestedInterfaces));
    System.out.println("Declaring Class: " + declaringClass);
}

From source file:gemlite.core.util.Util.java

/***
 * Lgemlite/core/util/Util;//from   w ww . j  ava2  s  .  com
 * @param cls
 * @return
 */
public final static String getInternalDesc(Class<?> cls) {
    String name = cls.getName();
    StringBuilder builder = new StringBuilder();
    builder.append("L");
    builder.append(name.replaceAll("\\.", "\\/"));
    builder.append(";");
    return builder.toString();
}

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  .  ja  v a 2  s  .c  om
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:info.magnolia.freemarker.FreemarkerUtil.java

/**
 * Same as {@link #createTemplateName(Class, String)} but adds the classifier between
 * the template name and the extension./*from w  ww.j av  a  2  s . co  m*/
 */
public static String createTemplateName(Class<?> klass, String classifier, String ext) {
    classifier = (classifier != null) ? StringUtils.capitalize(classifier) : "";
    return "/" + StringUtils.replace(klass.getName(), ".", "/") + classifier + "." + ext;
}

From source file:com.arpnetworking.jackson.BuilderDeserializer.java

@SuppressWarnings("unchecked")
static <T> Class<? extends Builder<? extends T>> getBuilderForClass(final Class<? extends T> clazz)
        throws ClassNotFoundException {
    return (Class<? extends Builder<? extends T>>) (Class.forName(clazz.getName() + "$Builder"));
}

From source file:com.netflix.nicobar.core.module.ScriptModuleUtils.java

/**
 * Convert a ScriptModule to its compiled equivalent ScriptArchive.
 * <p>//  w  w  w.  ja  v  a 2  s  . co m
 * A jar script archive is created containing compiled bytecode
 * from a script module, as well as resources and other metadata from
 * the source script archive.
 * <p>
 * This involves serializing the class bytes of all the loaded classes in
 * the script module, as well as copying over all entries in the original
 * script archive, minus any that have excluded extensions. The module spec
 * of the source script archive is transferred as is to the target bytecode
 * archive.
 *
 * @param module the input script module containing loaded classes
 * @param jarPath the path to a destination JarScriptArchive.
 * @param excludeExtensions a set of extensions with which
 *        source script archive entries can be excluded.
 *
 * @throws Exception
 */
public static void toCompiledScriptArchive(ScriptModule module, Path jarPath, Set<String> excludeExtensions)
        throws Exception {
    ScriptArchive sourceArchive = module.getSourceArchive();
    JarOutputStream jarStream = new JarOutputStream(new FileOutputStream(jarPath.toFile()));
    try {
        // First copy all resources (excluding those with excluded extensions)
        // from the source script archive, into the target script archive
        for (String archiveEntry : sourceArchive.getArchiveEntryNames()) {
            URL entryUrl = sourceArchive.getEntry(archiveEntry);
            boolean skip = false;
            for (String extension : excludeExtensions) {
                if (entryUrl.toString().endsWith(extension)) {
                    skip = true;
                    break;
                }
            }

            if (skip)
                continue;

            InputStream entryStream = entryUrl.openStream();
            byte[] entryBytes = IOUtils.toByteArray(entryStream);
            entryStream.close();

            jarStream.putNextEntry(new ZipEntry(archiveEntry));
            jarStream.write(entryBytes);
            jarStream.closeEntry();
        }

        // Now copy all compiled / loaded classes from the script module.
        Set<Class<?>> loadedClasses = module.getModuleClassLoader().getLoadedClasses();
        Iterator<Class<?>> iterator = loadedClasses.iterator();
        while (iterator.hasNext()) {
            Class<?> clazz = iterator.next();
            String classPath = clazz.getName().replace(".", "/") + ".class";
            URL resourceURL = module.getModuleClassLoader().getResource(classPath);
            if (resourceURL == null) {
                throw new Exception("Unable to find class resource for: " + clazz.getName());
            }

            InputStream resourceStream = resourceURL.openStream();
            jarStream.putNextEntry(new ZipEntry(classPath));
            byte[] classBytes = IOUtils.toByteArray(resourceStream);
            resourceStream.close();
            jarStream.write(classBytes);
            jarStream.closeEntry();
        }

        // Copy the source moduleSpec, but tweak it to specify the bytecode compiler in the
        // compiler plugin IDs list.
        ScriptModuleSpec moduleSpec = sourceArchive.getModuleSpec();
        ScriptModuleSpec.Builder newModuleSpecBuilder = new ScriptModuleSpec.Builder(moduleSpec.getModuleId());
        newModuleSpecBuilder.addCompilerPluginIds(moduleSpec.getCompilerPluginIds());
        newModuleSpecBuilder.addCompilerPluginId(BytecodeLoadingPlugin.PLUGIN_ID);
        newModuleSpecBuilder.addMetadata(moduleSpec.getMetadata());
        newModuleSpecBuilder.addModuleDependencies(moduleSpec.getModuleDependencies());

        // Serialize the modulespec with GSON and its default spec file name
        ScriptModuleSpecSerializer specSerializer = new GsonScriptModuleSpecSerializer();
        String json = specSerializer.serialize(newModuleSpecBuilder.build());
        jarStream.putNextEntry(new ZipEntry(specSerializer.getModuleSpecFileName()));
        jarStream.write(json.getBytes(Charsets.UTF_8));
        jarStream.closeEntry();
    } finally {
        if (jarStream != null) {
            jarStream.close();
        }
    }
}

From source file:gaffer.example.gettingstarted.walkthrough.WalkthroughStrSubstitutor.java

private static String getJavaDocLink(final Class<?> clazz) {
    return "[" + clazz.getSimpleName() + "](" + JAVA_DOC_URL_PREFIX + clazz.getName().replaceAll("\\.", "/")
            + ".html)";
}