Example usage for java.lang ClassNotFoundException initCause

List of usage examples for java.lang ClassNotFoundException initCause

Introduction

In this page you can find the example usage for java.lang ClassNotFoundException 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:Main.java

public static IIOMetadataFormat instantiateMetadataFormat(String formatName, boolean standardFormatSupported,
        String nativeMetadataFormatName, String nativeMetadataFormatClassName,
        String[] extraMetadataFormatNames, String[] extraMetadataFormatClassNames) {
    if (formatName == null) {
        throw new IllegalArgumentException("formatName == null!");
    }// w w  w. j  a  v  a 2  s.  c om
    if (formatName.equals(IIOMetadataFormatImpl.standardMetadataFormatName)) {
        if (standardFormatSupported) {
            return IIOMetadataFormatImpl.getStandardFormatInstance();
        }
    }

    String className = null;

    if (formatName.equals(nativeMetadataFormatName)) {
        className = nativeMetadataFormatClassName;
    } else if (extraMetadataFormatNames != null) {
        for (int i = 0; i < extraMetadataFormatNames.length; i++) {
            if (formatName.equals(extraMetadataFormatNames[i])) {
                className = extraMetadataFormatClassNames[i];
                break;
            }
        }
    }

    if (className == null) {
        throw new IllegalArgumentException("Unsupported format name");
    }

    // Get the context class loader and try to use it first
    ClassLoader contextClassloader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
        public ClassLoader run() {
            return Thread.currentThread().getContextClassLoader();
        }
    });

    Class cls;

    try {
        cls = Class.forName(className, true, contextClassloader);
    } catch (ClassNotFoundException e) {
        try {
            // Use current class loader
            cls = Class.forName(className);
        } catch (ClassNotFoundException e1) {
            throw new IllegalStateException("Can't obtain format");
        }
    }

    try {
        //???AWT:
        //Method getInstance = cls.getMethod("getInstance");
        //return (IIOMetadataFormat) getInstance.invoke(null);
        return null;
    } catch (Exception e) {
        IllegalStateException e1 = new IllegalStateException("Can't obtain format");
        e1.initCause(e); // Add some details to the message
        throw e1;
    }
}