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:koper.util.ReflectUtil.java

/**
 * Get method arg names./*from  w  ww .jav  a2 s .c  o  m*/
 *
 * @param clazz
 * @param methodName
 * @return
 */
public static <T> String[] getMethodArgNames(Class<T> clazz, String methodName) {
    try {
        ClassPool pool = ClassPool.getDefault();
        CtClass cc = pool.get(clazz.getName());

        CtMethod cm = cc.getDeclaredMethod(methodName);

        // javaassist??????
        MethodInfo methodInfo = cm.getMethodInfo();
        CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
        LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute
                .getAttribute(LocalVariableAttribute.tag);
        if (attr == null) {
            throw new RuntimeException("LocalVariableAttribute of method is null! Class " + clazz.getName()
                    + ",method name is " + methodName);
        }
        String[] paramNames = new String[cm.getParameterTypes().length];
        int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
        for (int i = 0; i < paramNames.length; i++)
            paramNames[i] = attr.variableName(i + pos);

        return paramNames;
    } catch (NotFoundException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:ja.centre.gui.resources.Resources.java

public static String asString(Class aClass, String suffix) {
    String name = aClass.getName();
    name = "/" + name.replace(".", "/") + suffix;
    InputStream is = aClass.getResourceAsStream(name);

    if (is == null) {
        Arguments.doThrow("Resource file file for class \"" + aClass.getName() + "\" does not exist");
    }//from w ww.java 2s .c o  m

    StringBuilder builder = new StringBuilder();
    try {
        byte[] buffer = new byte[32768];
        int read;
        while ((read = is.read(buffer)) != -1) {
            builder.append(new String(buffer, 0, read, "UTF-8"));
        }
    } catch (IOException e) {
        States.shouldNeverReachHere(e);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            LOG.error("Exception occured when tried to close html resource input stream", e);
        }
    }
    return builder.toString();
}

From source file:Main.java

/**
 * Swing menus are looking pretty bad on Linux when the GTK LaF is used (See
 * bug #6925412). It will most likely never be fixed anytime soon so this
 * method provides a workaround for it. It uses reflection to change the GTK
 * style objects of Swing so popup menu borders have a minimum thickness of
 * 1 and menu separators have a minimum vertical thickness of 1.
 *//*from  w w  w  . ja  v  a 2s  .  c o m*/
public static void installGtkPopupBugWorkaround() {
    // Get current look-and-feel implementation class
    LookAndFeel laf = UIManager.getLookAndFeel();
    Class<?> lafClass = laf.getClass();

    // Do nothing when not using the problematic LaF
    if (!lafClass.getName().equals("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"))
        return;

    // We do reflection from here on. Failure is silently ignored. The
    // workaround is simply not installed when something goes wrong here
    try {
        // Access the GTK style factory
        Field field = lafClass.getDeclaredField("styleFactory");
        boolean accessible = field.isAccessible();
        field.setAccessible(true);
        Object styleFactory = field.get(laf);
        field.setAccessible(accessible);

        // Fix the horizontal and vertical thickness of popup menu style
        Object style = getGtkStyle(styleFactory, new JPopupMenu(), "POPUP_MENU");
        fixGtkThickness(style, "yThickness");
        fixGtkThickness(style, "xThickness");

        // Fix the vertical thickness of the popup menu separator style
        style = getGtkStyle(styleFactory, new JSeparator(), "POPUP_MENU_SEPARATOR");
        fixGtkThickness(style, "yThickness");
    } catch (Exception e) {
        // Silently ignored. Workaround can't be applied.
    }
}

From source file:com.CodeSeance.JSeance.CodeGenXML.Runtime.java

public static Log CreateLogger(Class classObj) {
    return LogFactory.getLog(classObj.getName().replace("com.CodeSeance.JSeance.CodeGenXML.", ""));
}

From source file:com.github.thesmartenergy.sparql.generate.jena.engine.TestBase.java

static void setUpClass(Class clazz) throws Exception {
    LOG = Logger.getLogger(clazz);
    LOG.debug(clazz.getName());
    String dir = clazz.getSimpleName();
    dir = Character.toLowerCase(dir.charAt(0)) + (dir.length() > 1 ? dir.substring(1) : "");
    examplePath = clazz.getResource("/" + dir);

    exampleDir = new File(examplePath.toURI());

    // read location-mapping
    URI confUri = exampleDir.toURI().resolve("configuration.ttl");
    Model conf = RDFDataMgr.loadModel(confUri.toString());

    // initialize file manager
    fileManager = FileManager.makeGlobal();
    Locator loc = new LocatorFile(exampleDir.toURI().getPath());
    LocationMapper mapper = new LocationMapper(conf);
    fileManager.addLocator(loc);// w  w  w .j  a  v  a  2 s  .  c  om
    fileManager.setLocationMapper(mapper);
}

From source file:Main.java

private static void registerFieldSetter(final Class<?> iClass, String fieldName, Field f) {
    // TRY TO GET THE VALUE BY ACCESSING DIRECTLY TO THE PROPERTY
    if (!f.isAccessible())
        f.setAccessible(true);//from   w  ww. j  a v a  2  s  .  co  m

    setters.put(iClass.getName() + "." + fieldName, f);
}

From source file:com.cloudera.sqoop.util.Jars.java

/**
 * Return the jar file path that contains a particular class.
 * Method mostly cloned from o.a.h.mapred.JobConf.findContainingJar().
 */// w  w w . j a  v a  2 s  .  c om
public static String getJarPathForClass(Class<? extends Object> classObj) {
    ClassLoader loader = classObj.getClassLoader();
    String classFile = classObj.getName().replaceAll("\\.", "/") + ".class";
    try {
        for (Enumeration<URL> itr = loader.getResources(classFile); itr.hasMoreElements();) {
            URL url = (URL) itr.nextElement();
            if ("jar".equals(url.getProtocol())) {
                String toReturn = url.getPath();
                if (toReturn.startsWith("file:")) {
                    toReturn = toReturn.substring("file:".length());
                }
                // URLDecoder is a misnamed class, since it actually decodes
                // x-www-form-urlencoded MIME type rather than actual
                // URL encoding (which the file path has). Therefore it would
                // decode +s to ' 's which is incorrect (spaces are actually
                // either unencoded or encoded as "%20"). Replace +s first, so
                // that they are kept sacred during the decoding process.
                toReturn = toReturn.replaceAll("\\+", "%2B");
                toReturn = URLDecoder.decode(toReturn, "UTF-8");
                return toReturn.replaceAll("!.*$", "");
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return null;
}

From source file:com.stratuscom.harvester.Utils.java

public static void logClassLoaderHierarchy(Logger log, Level level, Class cls) {
    log.log(level, MessageNames.CLASSLOADER_IS, new Object[] { cls.getName(), cls.getClassLoader() });
    try {/*from w  w w.  j  a  v  a2 s . c o m*/
        ClassLoader parent = cls.getClassLoader().getParent();
        while (parent != null) {
            log.log(level, MessageNames.PARENT_CLASS_LOADER_IS, new Object[] { parent });
            parent = parent.getParent();
        }
    } catch (Throwable t) {
        log.log(level, Strings.NEWLINE);
    }

}

From source file:com.wavemaker.tools.apidocs.tools.parser.util.DataTypeUtil.java

public static String getName(Class<?> type, int attempt) {
    if (attempt > 0) {
        String fullName = type.getName();
        String[] tokens = fullName.split("\\.");
        return StringUtils.join(tokens, '.', tokens.length - attempt, tokens.length);
    } else {/*  w w w. j  a  va2s .  co  m*/
        throw new IllegalArgumentException("Attempt should be more than or equal 1");
    }
}

From source file:com.squarespace.gibson.GibsonUtils.java

private static void append(MessageDigest md, Class<?> value) {
    append(md, value.getName());
}