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:MainClass.java

public static void main(String args[]) {
    Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
    System.out.println("Object Name: " + clip.getName());
    Transferable contents = clip.getContents(new MainClass().getClass());
    if (contents == null)
        System.out.println("\n\nThe clipboard is empty.");
    else {/*w w w .  ja v  a2 s . c  o m*/
        DataFlavor flavors[] = contents.getTransferDataFlavors();
        for (int i = 0; i < flavors.length; ++i) {
            System.out.println("\n\n Name: " + flavors[i].getHumanPresentableName());
            System.out.println("\n MIME Type: " + flavors[i].getMimeType());
            Class cl = flavors[i].getRepresentationClass();
            if (cl == null)
                System.out.println("null");
            else
                System.out.println(cl.getName());
        }
    }
}

From source file:ReflectionTest.java

public static void main(String[] args) {
    String name = "java.util.Date";
    try {/*from ww  w. j a  v  a2  s.co  m*/
        Class cl = Class.forName(name);
        Class supercl = cl.getSuperclass();
        System.out.print("class " + name);
        if (supercl != null && !supercl.equals(Object.class))
            System.out.println(" extends " + supercl.getName());
        System.out.print("Its constructors:");
        printConstructors(cl);
        System.out.println();
    } catch (ClassNotFoundException e) {
        System.out.println("Class not found.");
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];
    String name;/*from w  ww  .  j a  va2  s  . c  o m*/

    name = cls.getName();
    System.out.println(name);
    name = cls.getName() + "." + field.getName();
    System.out.println(name);
    name = constructor.getName();
    System.out.println(name);
    name = cls.getName() + "." + method.getName();
    System.out.println(name);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];
    String name;// w ww.j av  a 2s.  com

    name = cls.getName().substring(cls.getPackage().getName().length() + 1);
    System.out.println(name);
    name = field.getName();
    System.out.println(name);
    name = constructor.getName().substring(cls.getPackage().getName().length() + 1);
    System.out.println(name);
    name = method.getName();
    System.out.println(name);

}

From source file:Main.java

public static void main(String[] args) {
    Method[] methods = java.lang.Integer.class.getDeclaredMethods();

    for (int i = 0; i < methods.length; i++) {
        Method m = methods[i];/*  w w  w .jav  a 2  s . c  om*/
        Class retType = m.getReturnType();
        Class[] paramTypes = m.getParameterTypes();
        String name = m.getName();
        System.out.print(Modifier.toString(m.getModifiers()));
        System.out.print(" " + retType.getName() + " " + name + "(");
        for (int j = 0; j < paramTypes.length; j++) {
            if (j > 0)
                System.out.print(", ");
            System.out.print(paramTypes[j].getName());
        }
        System.out.println(");");
    }
}

From source file:MySuperClass.java

public static void main(String[] args) {
    Class<MyClass> c = MyClass.class;

    // Print declared fields
    ArrayList<String> fieldsDesciption = getDeclaredFieldsList(c);

    System.out.println("Declared Fields for " + c.getName());
    for (String desc : fieldsDesciption) {
        System.out.println(desc);
    }//from w ww.j  a v  a2s . co m
    fieldsDesciption = getFieldsList(c);

    System.out.println("\nAccessible Fields for " + c.getName());
    for (String desc : fieldsDesciption) {
        System.out.println(desc);
    }
}

From source file:TypeMapDemo.java

public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {

    Properties p = new Properties();
    p.load(new FileInputStream("db.properties"));
    Class c = Class.forName(p.getProperty("db.driver"));
    System.out.println("Loaded driverClass " + c.getName());

    Connection con = DriverManager.getConnection(p.getProperty("db.url"), "student", "student");
    System.out.println("Got Connection " + con);

    Statement s = con.createStatement();
    int ret;//from   www  . jav a2  s .  c  o m
    try {
        s.executeUpdate("drop table MR");
        s.executeUpdate("drop type MUSICRECORDING");
    } catch (SQLException andDoNothingWithIt) {
        // Should use "if defined" but not sure it works for UDTs...
    }
    ret = s.executeUpdate("create type MUSICRECORDING as object (" + "   id integer," + "   title varchar(20), "
            + "   artist varchar(20) " + ")");
    System.out.println("Created TYPE! Ret=" + ret);

    ret = s.executeUpdate("create table MR of MUSICRECORDING");
    System.out.println("Created TABLE! Ret=" + ret);

    int nRows = s.executeUpdate("insert into MR values(123, 'Greatest Hits', 'Ian')");
    System.out.println("inserted " + nRows + " rows");

    // Put the data class into the connection's Type Map
    // If the data class were not an inner class,
    // this would likely be done with Class.forName(...);
    Map map = con.getTypeMap();
    map.put("MUSICRECORDING", MusicRecording.class);
    con.setTypeMap(map);

    ResultSet rs = s.executeQuery("select * from MR where id = 123");
    //"select musicrecording(id,artist,title) from mr");
    rs.next();
    for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
        Object o = rs.getObject(i);
        System.out.print(o + "(Type " + o.getClass().getName() + ")\t");
    }
    System.out.println();
}

From source file:MyClass.java

public static void main(String[] args) {
    Class<MyClass> c = MyClass.class;

    ArrayList<String> methodsDesciption = getDeclaredMethodsList(c);
    System.out.println("Declared Methods  for " + c.getName());
    for (String desc : methodsDesciption) {
        System.out.println(desc);
    }/*from w w  w .ja  v a  2s .  c o m*/
    methodsDesciption = getMethodsList(c);
    System.out.println("\nMethods for  " + c.getName());
    for (String desc : methodsDesciption) {
        System.out.println(desc);
    }

}

From source file:grails.util.RunTests.java

public static void main(String[] args) {
    int exitCode = 0;
    try {//from  w  w w .j a  v a 2 s .co  m
        log.info("Bootstrapping Grails from classpath");
        ConfigurableApplicationContext appCtx = (ConfigurableApplicationContext) GrailsUtil
                .bootstrapGrailsFromClassPath();
        GrailsApplication application = (GrailsApplication) appCtx.getBean(GrailsApplication.APPLICATION_ID);

        Class[] allClasses = application.getAllClasses();
        log.debug("Going through [" + allClasses.length + "] classes");
        TestSuite s = new TestSuite();
        for (int i = 0; i < allClasses.length; i++) {
            Class clazz = allClasses[i];
            if (TestCase.class.isAssignableFrom(clazz) && !Modifier.isAbstract(clazz.getModifiers())) {
                log.debug("Adding test [" + clazz.getName() + "]");
                s.addTest(new GrailsTestSuite(appCtx, clazz));
            } else {
                log.debug("[" + clazz.getName() + "] is not a test case.");
            }
        }
        String[] beanNames = appCtx.getBeanNamesForType(PersistenceContextInterceptor.class);
        PersistenceContextInterceptor interceptor = null;
        if (beanNames.length > 0) {
            interceptor = (PersistenceContextInterceptor) appCtx.getBean(beanNames[0]);
        }

        try {
            if (interceptor != null) {
                interceptor.init();
            }
            TestResult r = TestRunner.run(s);
            exitCode = r.errorCount() + r.failureCount();
            if (exitCode > 0) {
                System.err.println("Tests failed!");
            }
            if (interceptor != null)
                interceptor.flush();
        } finally {
            if (interceptor != null)
                interceptor.destroy();
        }
    } catch (Exception e) {
        log.error("Error executing tests: " + e.getMessage(), e);
        exitCode = 1;
    } finally {
        System.exit(exitCode);
    }
}

From source file:Main.java

public static void main(String... args) {
    try {/* www  .ja v a2  s.c o  m*/
        Class<?> c = Class.forName(args[0]);
        Class[] argTypes = new Class[] { String[].class };
        Method main = c.getDeclaredMethod("main", argTypes);
        String[] mainArgs = Arrays.copyOfRange(args, 1, args.length);
        System.out.format("invoking %s.main()%n", c.getName());
        main.invoke(null, (Object) mainArgs);

        // production code should handle these exceptions more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    } catch (NoSuchMethodException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    } catch (InvocationTargetException x) {
        x.printStackTrace();
    }
}