Example usage for java.lang.reflect InvocationTargetException printStackTrace

List of usage examples for java.lang.reflect InvocationTargetException printStackTrace

Introduction

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

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:Deny.java

public static void main(String... args) {
    try {//from   w  ww  .  j  a  v a 2  s  . com
        Constructor c = Deny.class.getDeclaredConstructor();
        // c.setAccessible(true); // solution
        c.newInstance();

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

From source file:MethodTroubleReturns.java

public static void main(String... args) {
    try {/*  w  w w  .j  av a 2s.  c o m*/
        MethodTroubleReturns mtr = new MethodTroubleReturns();
        Class<?> c = mtr.getClass();
        Method m = c.getDeclaredMethod("drinkMe", int.class);
        m.invoke(mtr, -1);

        // production code should handle these exceptions more gracefully
    } catch (InvocationTargetException x) {
        Throwable cause = x.getCause();
        System.err.format("drinkMe() failed: %s%n", cause.getMessage());
    } catch (Exception x) {
        x.printStackTrace();
    }
}

From source file:Deet.java

public static void main(String... args) {
    if (args.length != 4) {
        err.format("Usage: java Deet <classname> <langauge> <country> <variant>%n");
        return;/*  www  . j  a  v  a2  s .c  o  m*/
    }

    try {
        Class<?> c = Class.forName(args[0]);
        Object t = c.newInstance();

        Method[] allMethods = c.getDeclaredMethods();
        for (Method m : allMethods) {
            String mname = m.getName();
            if (!mname.startsWith("test") || (m.getGenericReturnType() != boolean.class)) {
                continue;
            }
            Type[] pType = m.getGenericParameterTypes();
            if ((pType.length != 1) || Locale.class.isAssignableFrom(pType[0].getClass())) {
                continue;
            }

            out.format("invoking %s()%n", mname);
            try {
                m.setAccessible(true);
                Object o = m.invoke(t, new Locale(args[1], args[2], args[3]));
                out.format("%s() returned %b%n", mname, (Boolean) o);

                // Handle any exceptions thrown by method to be invoked.
            } catch (InvocationTargetException x) {
                Throwable cause = x.getCause();
                err.format("invocation of %s failed: %s%n", mname, cause.getMessage());
            }
        }

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

From source file:ClassLoaderDemo0.java

public static void main(String[] argv) {
    System.out.println("ClassLoaderDemo starting");
    ClassLoaderDemo0 loader = new ClassLoaderDemo0();
    Class c = null;//from  www  .j a va2  s  .  c o  m
    Object demo;
    try {
        /* Load the "Demo" class from memory */

        System.out.println("About to load class  Demo");
        c = loader.loadClass("Demo", true);
        System.out.println("About to instantiate class Demo");
        demo = c.newInstance();
        System.out.println("Got Demo class loaded: " + demo);

        /* Now try to call a method */

        Method mi = c.getMethod("test", null);
        mi.invoke(demo, null);

    } catch (InvocationTargetException e) {
        // The invoked method threw an exception. We get it
        // wrapped up inside another exception, hence the
        // extra call here:
        e.getTargetException().printStackTrace();
        System.out.println("Could not run test method");
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Could not run test method");
    }
}

From source file:Main.java

static public void setFilterMatrix(ColorMatrixColorFilter filter, ColorMatrix colorMatrix) {
    try {/*from  w  w w  . ja va  2  s.  c  o  m*/
        sMethod.invoke(filter, colorMatrix);
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * A wrapper around SwingUtilities.invokeAndWait() that makes sure that 
 * SwingUtilities.invokeAndWait() is only called when the current thread is 
 * not the AWT event dispatching thread, as required by the documentation
 * of SwingUtilities.invokeAndWait(); plus catches exceptions thrown by 
 * SwingUtilities.invokeAndWait().//from  w w w . java 2  s.c  o m
 * @param runnable The Runnable to call in the event dispatch thread.
 */
public static void invokeAndWait(Runnable runnable) {
    try {
        if (SwingUtilities.isEventDispatchThread())
            runnable.run();
        else
            SwingUtilities.invokeAndWait(runnable);
    } catch (InvocationTargetException ex) {
        ex.printStackTrace();
    } catch (InterruptedException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

/**
 * Creates a new instance of xposed class.
 *
 * @param clzOurClass         the xposed class
 * @param objectOriginalClass   the instance of the original class
 * @return/*ww  w  .  j a v  a2 s .c o m*/
 */
private static Object createInstanceFromOurClass(Class clzOurClass, Object objectOriginalClass) {
    Object objectReturn = null;

    try {
        objectReturn = clzOurClass.getConstructors()[0].newInstance(objectOriginalClass);
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return objectReturn;
}

From source file:Main.java

public static boolean setCursorVisibility(Context context, boolean visible) {
    if (!nvExtensionSupported) {
        return false;
    }//from   w w  w  .j a va 2  s  .c  o m

    try {
        methodSetCursorVisibility.invoke(context.getSystemService(Context.INPUT_SERVICE), visible);
        return true;
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return false;
}

From source file:URLUtilities.java

/**
 * Calls <code>java.net.URLEncoder.encode(String, String)</code> via
 * reflection, if we are running on JRE 1.4 or later, otherwise reverts to
 * the deprecated <code>URLEncoder.encode(String)</code> method.
 *
 * @param s  the string to encode.// w  w  w . j av a  2  s .c  om
 * @param encoding  the encoding.
 *
 * @return The encoded string.
 *
 * @since 1.0.6
 */
public static String encode(String s, String encoding) {
    Class c = URLEncoder.class;
    String result = null;
    try {
        Method m = c.getDeclaredMethod("encode", STRING_ARGS_2);
        try {
            result = (String) m.invoke(null, new Object[] { s, encoding });
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    } catch (NoSuchMethodException e) {
        // we're running on JRE 1.3.1 so this is the best we have...
        result = URLEncoder.encode(s);
    }
    return result;
}

From source file:EventDispatcher.java

public static void fireEvent(final Dispatcher dispatcher, final List listeners, final Object evt,
        final boolean useEventQueue) {
    if (useEventQueue && !EventQueue.isDispatchThread()) {
        Runnable r = new Runnable() {
            public void run() {
                fireEvent(dispatcher, listeners, evt, useEventQueue);
            }//from www .ja va 2s  .  co  m
        };
        try {
            EventQueue.invokeAndWait(r);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            // Assume they will get delivered????
            // be nice to wait on List but how???
        } catch (ThreadDeath td) {
            throw td;
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return;
    }

    Object[] ll = null;
    Throwable err = null;
    int retryCount = 10;
    while (--retryCount != 0) {
        // If the thread has been interrupted this can 'mess up'
        // the class loader and cause this otherwise safe code to
        // throw errors.
        try {
            synchronized (listeners) {
                if (listeners.size() == 0)
                    return;
                ll = listeners.toArray();
                break;
            }
        } catch (Throwable t) {
            err = t;
        }
    }
    if (ll == null) {
        if (err != null)
            err.printStackTrace();
        return;
    }
    dispatchEvent(dispatcher, ll, evt);
}