Example usage for java.lang InstantiationException printStackTrace

List of usage examples for java.lang InstantiationException printStackTrace

Introduction

In this page you can find the example usage for java.lang InstantiationException printStackTrace.

Prototype

public void printStackTrace(PrintStream s) 

Source Link

Document

Prints this throwable and its backtrace to the specified print stream.

Usage

From source file:uk.ac.kcl.texthunter.utils.Utils.java

public static Connection loadAndConnectDerby(Connection con) {
    /*//from w  w w .  j av a  2 s.  c  om
     *  The JDBC driver is loaded by loading its class.
     *  If you are using JDBC 4.0 (Java SE 6) or newer, JDBC drivers may
     *  be automatically loaded, making this code optional.
     *
     *  In an embedded environment, this will also start up the Derby
     *  engine (though not any databases), since it is not already
     *  running. In a client environment, the Derby engine is being run
     *  by the network server framework.
     *
     *  In an embedded environment, any static Derby system properties
     *  must be set before loading the driver to take effect.
     */

    String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    String protocol = "jdbc:derby:";
    try {
        Class.forName(driver).newInstance();
        //    System.out.println("Loaded the appropriate driver");
    } catch (ClassNotFoundException cnfe) {
        System.err.println("\nUnable to load the JDBC driver " + driver);
        System.err.println("Please check your CLASSPATH.");
        cnfe.printStackTrace(System.err);
    } catch (InstantiationException ie) {
        System.err.println("\nUnable to instantiate the JDBC driver " + driver);
        ie.printStackTrace(System.err);
    } catch (IllegalAccessException iae) {
        System.err.println("\nNot allowed to access the JDBC driver " + driver);
        iae.printStackTrace(System.err);
    }

    /* By default, the schema APP will be used when no username is
     * provided.
     * Otherwise, the schema name is the same as the user name (in this
     * case "user1" or USER1.)
     *
     * Note that user authentication is off by default, meaning that any
     * user can connect to your database using any password. To enable
     * authentication, see the Derby Developer's Guide.
     */
    String dbName = "annotationDB"; // the name of the database
    try {
        /*
         * This connection specifies create=true in the connection URL to
         * cause the database to be created when connecting for the first
         * time. To remove the database, remove the directory derbyDB (the
         * same as the database name) and its contents.
         *
         * The directory derbyDB will be created under the directory that
         * the system property derby.system.home points to, or the current
         * directory (user.dir) if derby.system.home is not set.
         */
        con = DriverManager.getConnection(protocol + dbName + ";create=true");
    } catch (SQLException ex) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
    }

    return con;

}

From source file:org.apache.jmeter.junit.JMeterTest.java

public static Collection<Object> getObjects(Class<?> extendsClass) throws Exception {
    String exName = extendsClass.getName();
    Object myThis = "";
    Iterator<String> classes = ClassFinder
            .findClassesThatExtend(JMeterUtils.getSearchPaths(), new Class[] { extendsClass }).iterator();
    List<Object> objects = new LinkedList<>();
    String n = "";
    boolean caughtError = true;
    Throwable caught = null;/*  www  .  java 2s  .  co m*/
    try {
        while (classes.hasNext()) {
            n = classes.next();
            // TODO - improve this check
            if (n.endsWith("RemoteJMeterEngineImpl")) {
                continue; // Don't try to instantiate remote server
            }
            Class<?> c = null;
            try {
                c = Class.forName(n);
                try {
                    // Try with a parameter-less constructor first
                    objects.add(c.newInstance());
                } catch (InstantiationException e) {
                    caught = e;
                    try {
                        // Events often have this constructor
                        objects.add(c.getConstructor(new Class[] { Object.class })
                                .newInstance(new Object[] { myThis }));
                    } catch (NoSuchMethodException f) {
                        // no luck. Ignore this class
                        System.out.println(
                                "o.a.j.junit.JMeterTest WARN: " + exName + ": NoSuchMethodException  " + n
                                        + ", missing empty Constructor or Constructor with Object parameter");
                    }
                }
            } catch (NoClassDefFoundError e) {
                // no luck. Ignore this class
                System.out.println("o.a.j.junit.JMeterTest WARN: " + exName + ": NoClassDefFoundError " + n
                        + ":" + e.getMessage());
                e.printStackTrace(System.out);
            } catch (IllegalAccessException e) {
                caught = e;
                System.out.println("o.a.j.junit.JMeterTest WARN: " + exName + ": IllegalAccessException " + n
                        + ":" + e.getMessage());
                e.printStackTrace(System.out);
                // We won't test restricted-access classes.
            } catch (HeadlessException | ExceptionInInitializerError e) {// EIIE can be caused by Headless
                caught = e;
                System.out.println("o.a.j.junit.JMeterTest Error creating " + n + " " + e.toString());
            } catch (Exception e) {
                caught = e;
                if (e instanceof RemoteException) { // not thrown, so need to check here
                    System.out.println(
                            "o.a.j.junit.JMeterTest WARN: " + "Error creating " + n + " " + e.toString());
                } else {
                    throw new Exception("Error creating " + n, e);
                }
            }
        }
        caughtError = false;
    } finally {
        if (caughtError) {
            System.out.println("Last class=" + n);
            System.out.println("objects.size=" + objects.size());
            System.out.println("Last error=" + caught);
        }
    }

    if (objects.isEmpty()) {
        System.out.println("No classes found that extend " + exName + ". Check the following:");
        System.out.println("Search paths are:");
        String ss[] = JMeterUtils.getSearchPaths();
        for (String s : ss) {
            System.out.println(s);
        }
        if (!classPathShown) {// Only dump it once
            System.out.println("Class path is:");
            String cp = System.getProperty("java.class.path");
            String classPathElements[] = JOrphanUtils.split(cp, java.io.File.pathSeparator);
            for (String classPathElement : classPathElements) {
                System.out.println(classPathElement);
            }
            classPathShown = true;
        }
    }
    return objects;
}

From source file:org.jdal.ui.bind.ConfigurableControlAccessorFactory.java

/**
 * {@inheritDoc}//w  ww.  j av  a 2s.c  om
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public ControlAccessor getControlAccessor(Object control) {
    Class<? extends ControlAccessor> accessorClass = null;
    ControlAccessor accessor = null;
    Class<? extends Object> clazz = control.getClass();

    accessorClass = accessors.get(clazz);

    if (accessorClass == null) { // try with superclasses

        List superclasses = ClassUtils.getAllSuperclasses(clazz);
        superclasses.addAll(ClassUtils.getAllInterfaces(clazz));
        Iterator iter = superclasses.iterator();

        while (iter.hasNext() && accessorClass == null) {
            accessorClass = accessors.get(iter.next());
        }
    }

    if (accessorClass != null) {
        try {
            Constructor<? extends ControlAccessor> ctor = accessorClass.getConstructor(Object.class);
            accessor = ctor.newInstance(control);
        } catch (InstantiationException e) {
            log.error(e);
        } catch (IllegalAccessException e) {
            log.error(e);
        } catch (SecurityException e) {
            log.error(e);
        } catch (NoSuchMethodException e) {
            log.error(e);
        } catch (IllegalArgumentException e) {
            log.error(e);
        } catch (InvocationTargetException e) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            e.printStackTrace(new PrintStream(baos));
            log.error(baos);
        }
    } else {
        log.warn("Can't find a accessor for class: " + clazz.getName());
    }

    return accessor;
}