Example usage for java.lang NoSuchMethodException initCause

List of usage examples for java.lang NoSuchMethodException initCause

Introduction

In this page you can find the example usage for java.lang NoSuchMethodException initCause.

Prototype

public synchronized Throwable initCause(Throwable cause) 

Source Link

Document

Initializes the cause of this throwable to the specified value.

Usage

From source file:nl.talsmasoftware.enumerables.support.json.jackson2.Compatibility.java

@SuppressWarnings("unchecked")
private static <T> T call(Object target, String method) throws NoSuchMethodException {
    try {/*from  w w  w . j av a 2 s  .c  om*/

        return (T) method(target.getClass(), method).invoke(target);

    } catch (IllegalAccessException iae) {
        NoSuchMethodException nsme = new NoSuchMethodException(
                String.format("Not allowed to call method \"%s\": %s", method, iae.getMessage()));
        nsme.initCause(iae);
        throw nsme;
    } catch (InvocationTargetException ite) {
        Throwable cause = ite.getCause();
        if (cause == null)
            cause = ite; // shouldn't happen!
        throw cause instanceof RuntimeException ? (RuntimeException) cause
                : new RuntimeException(cause.getMessage(), cause);
    }
}

From source file:android.support.v71.widget.RecyclerView.java

/**
 * Instantiate and set a LayoutManager, if specified in the attributes.
 *  LayotManager/*from   w  w  w. ja  v  a  2 s .c o  m*/
 */
private void createLayoutManager(Context context, String className, AttributeSet attrs, int defStyleAttr,
        int defStyleRes) {
    if (className != null) {
        className = className.trim();
        if (className.length() != 0) { // Can't use isEmpty since it was added in API 9.
            className = getFullClassName(context, className);
            try {
                ClassLoader classLoader;
                if (isInEditMode()) {
                    // Stupid layoutlib cannot handle simple class loaders.
                    classLoader = this.getClass().getClassLoader();
                } else {
                    classLoader = context.getClassLoader();
                }
                Class<? extends LayoutManager> layoutManagerClass = classLoader.loadClass(className)
                        .asSubclass(LayoutManager.class);
                Constructor<? extends LayoutManager> constructor;
                Object[] constructorArgs = null;
                try {
                    constructor = layoutManagerClass.getConstructor(LAYOUT_MANAGER_CONSTRUCTOR_SIGNATURE);
                    constructorArgs = new Object[] { context, attrs, defStyleAttr, defStyleRes };
                } catch (NoSuchMethodException e) {
                    try {
                        constructor = layoutManagerClass.getConstructor();
                    } catch (NoSuchMethodException e1) {
                        e1.initCause(e);
                        throw new IllegalStateException(
                                attrs.getPositionDescription() + ": Error creating LayoutManager " + className,
                                e1);
                    }
                }
                constructor.setAccessible(true);
                setLayoutManager(constructor.newInstance(constructorArgs));
            } catch (ClassNotFoundException e) {
                throw new IllegalStateException(
                        attrs.getPositionDescription() + ": Unable to find LayoutManager " + className, e);
            } catch (InvocationTargetException e) {
                throw new IllegalStateException(attrs.getPositionDescription()
                        + ": Could not instantiate the LayoutManager: " + className, e);
            } catch (InstantiationException e) {
                throw new IllegalStateException(attrs.getPositionDescription()
                        + ": Could not instantiate the LayoutManager: " + className, e);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException(
                        attrs.getPositionDescription() + ": Cannot access non-public constructor " + className,
                        e);
            } catch (ClassCastException e) {
                throw new IllegalStateException(
                        attrs.getPositionDescription() + ": Class is not a LayoutManager " + className, e);
            }
        }
    }
}