Example usage for java.lang Class forName

List of usage examples for java.lang Class forName

Introduction

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

Prototype

@CallerSensitive
public static Class<?> forName(String className) throws ClassNotFoundException 

Source Link

Document

Returns the Class object associated with the class or interface with the given string name.

Usage

From source file:ConstructorAccess.java

public static void main(String... args) {
    try {/* ww  w.  jav  a  2  s.  c  om*/
        Class<?> c = Class.forName(args[0]);
        Constructor[] allConstructors = c.getDeclaredConstructors();
        for (Constructor ctor : allConstructors) {
            int searchMod = modifierFromString(args[1]);
            int mods = accessModifiers(ctor.getModifiers());
            if (searchMod == mods) {
                out.format("%s%n", ctor.toGenericString());
                out.format("  [ synthetic=%-5b var_args=%-5b ]%n", ctor.isSynthetic(), ctor.isVarArgs());
            }
        }

        // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:example.HelloIvy.java

public static void main(String[] args) throws Exception {
    String message = "Hello Ivy!";
    System.out.println("standard message : " + message);
    System.out.println(//ww  w .j a v  a 2s . co m
            "capitalized by " + WordUtils.class.getName() + " : " + WordUtils.capitalizeFully(message));

    HttpClient client = new HttpClient();
    HeadMethod head = new HeadMethod("http://www.ibiblio.org/");
    client.executeMethod(head);

    int status = head.getStatusCode();
    System.out.println("head status code with httpclient: " + status);
    head.releaseConnection();

    System.out.println("now check if httpclient dependency on commons-logging has been realized");
    Class<?> clss = Class.forName("org.apache.commons.logging.Log");
    System.out.println("found logging class in classpath: " + clss);
}

From source file:Eon.java

public static void main(String... args) {
    try {/*from   ww w  .  ja  v  a 2 s.c  o m*/
        Class<?> c = (args.length == 0 ? Eon.class : Class.forName(args[0]));
        out.format("Enum name:  %s%nEnum constants:  %s%n", c.getName(), Arrays.asList(c.getEnumConstants()));
        if (c == Eon.class)
            out.format("  Eon.values():  %s%n", Arrays.asList(Eon.values()));

        // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:MethodTrouble.java

public static void main(String... args) {
    try {/*from  w  w  w  . j  a v  a2 s .  com*/
        String mName = args[0];
        Class cArg = Class.forName(args[1]);
        Class<?> c = (new MethodTrouble<Integer>()).getClass();
        Method m = c.getMethod(mName, cArg);
        System.out.format("Found:%n  %s%n", m.toGenericString());

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

From source file:org.jasig.portlet.data.Importer.java

public static void main(String[] args) throws Exception {
    String dir = args[0];//from   w  w w.j a v a2  s . c o  m
    String importExportContext = args[1];
    String sessionFactoryBeanName = args[2];
    String modelClassName = args[3];
    String serviceBeanName = args[4];
    String serviceBeanMethodName = args[5];

    ApplicationContext context = PortletApplicationContextLocator.getApplicationContext(importExportContext);
    SessionFactory sessionFactory = context.getBean(sessionFactoryBeanName, SessionFactory.class);
    Class<?> modelClass = Class.forName(modelClassName);
    Object service = context.getBean(serviceBeanName);

    JAXBContext jc = JAXBContext.newInstance(modelClass);

    File folder = new File(dir);
    File[] files = folder.listFiles(new ImportFileFilter());

    for (File f : files) {
        StreamSource xml = new StreamSource(f.getAbsoluteFile());
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement je1 = unmarshaller.unmarshal(xml, modelClass);
        Object object = je1.getValue();
        Session session = sessionFactory.getCurrentSession();
        Transaction transaction = session.beginTransaction();

        Method method = service.getClass().getMethod(serviceBeanMethodName, modelClass);
        method.invoke(service, object);
        transaction.commit();
    }
}

From source file:FieldSpy.java

public static void main(String... args) {
    try {/* ww  w .ja  v a 2  s  .c o m*/
        Class<?> c = Class.forName("FieldSpy");
        Field f = c.getField("name");
        System.out.format("Type: %s%n", f.getType());
        System.out.format("GenericType: %s%n", f.getGenericType());

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

From source file:ConstructorTroubleToo.java

public static void main(String... args) {
    try {// w  ww  . j a v  a2s.  c  o m
        Class<?> c = Class.forName("ConstructorTroubleToo");
        // Method propagetes any exception thrown by the constructor (including checked exceptions).
        if (args.length > 0 && args[0].equals("class")) {
            Object o = c.newInstance();
        } else {
            Object o = c.getConstructor().newInstance();
        }

        // production code should handle these exceptions more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    } catch (InstantiationException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    } catch (NoSuchMethodException x) {
        x.printStackTrace();
    } catch (InvocationTargetException x) {
        x.printStackTrace();
        err.format("%n%nCaught exception: %s%n", x.getCause());
    }
}

From source file:gov.nih.nci.cabig.caaers.tools.ObjectDump.java

public static void main(String[] args) {
    ObjectDump target = new ObjectDump();

    Class cls = null;/*w w  w  . j a v  a  2s. co  m*/
    Field[] fields;

    try {
        cls = Class.forName(target.getClass().getName());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    write("Class: " + target.getClass().getName() + "\n");
    fields = cls.getFields();

    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        write("\tfield[" + (i + 1) + "]: " + field.getName() + "\r\n");

        Type type = field.getType();
        write(type.toString());
    }

}

From source file:Spy.java

public static void main(String... args) {
    try {//  w w  w .j  av  a2 s.  co  m
        Class<?> c = Class.forName(args[0]);
        int searchMods = 0x0;
        for (int i = 1; i < args.length; i++) {
            searchMods |= modifierFromString(args[i]);
        }

        Field[] flds = c.getDeclaredFields();
        out.format("Fields in Class '%s' containing modifiers:  %s%n", c.getName(),
                Modifier.toString(searchMods));
        boolean found = false;
        for (Field f : flds) {
            int foundMods = f.getModifiers();
            // Require all of the requested modifiers to be present
            if ((foundMods & searchMods) == searchMods) {
                out.format("%-8s [ synthetic=%-5b enum_constant=%-5b ]%n", f.getName(), f.isSynthetic(),
                        f.isEnumConstant());
                found = true;
            }
        }

        if (!found) {
            out.format("No matching fields%n");
        }

        // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:MethodSpy.java

public static void main(String... args) {
    try {/* w ww  .  j a  v a  2 s.c o  m*/
        Class<?> c = Class.forName(args[0]);
        Method[] allMethods = c.getDeclaredMethods();
        for (Method m : allMethods) {
            if (!m.getName().equals(args[1])) {
                continue;
            }
            out.format("%s%n", m.toGenericString());

            out.format(fmt, "ReturnType", m.getReturnType());
            out.format(fmt, "GenericReturnType", m.getGenericReturnType());

        }

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