in jaxb-api-2.2/src/main/java/javax/xml/bind/ContextFinder.java [117:191]
static JAXBContext newInstance( String contextPath,
String className,
ClassLoader classLoader,
Map properties )
throws JAXBException
{
try {
Class spiClass = safeLoadClass(className,classLoader);
/*
* javax.xml.bind.context.factory points to a class which has a
* static method called 'createContext' that
* returns a javax.xml.JAXBContext.
*/
Object context = null;
// first check the method that takes Map as the third parameter.
// this is added in 2.0.
try {
Method m = spiClass.getMethod("createContext",String.class,ClassLoader.class,Map.class);
// Throw an early exception instead of having an exception thrown in the createContext method
if (m.getReturnType() != JAXBContext.class) {
throw handleClassCastException(m.getReturnType(), JAXBContext.class);
}
// any failure in invoking this method would be considered fatal
context = m.invoke(null,contextPath,classLoader,properties);
} catch (NoSuchMethodException e) {
// it's not an error for the provider not to have this method.
}
if(context==null) {
// try the old method that doesn't take properties. compatible with 1.0.
// it is an error for an implementation not to have both forms of the createContext method.
Method m = spiClass.getMethod("createContext",String.class,ClassLoader.class);
// Throw an early exception instead of having an exception thrown in the createContext method
if (m.getReturnType() != JAXBContext.class) {
throw handleClassCastException(m.getReturnType(), JAXBContext.class);
}
// any failure in invoking this method would be considered fatal
context = m.invoke(null,contextPath,classLoader);
}
if(!(context instanceof JAXBContext)) {
// the cast would fail, so generate an exception with a nice message
throw handleClassCastException(context.getClass(), JAXBContext.class);
}
return (JAXBContext)context;
} catch (ClassNotFoundException x) {
throw new JAXBException(
Messages.format( Messages.PROVIDER_NOT_FOUND, className ),
x);
} catch (InvocationTargetException x) {
handleInvocationTargetException(x);
// for other exceptions, wrap the internal target exception
// with a JAXBException
Throwable e = x;
if(x.getTargetException()!=null)
e = x.getTargetException();
throw new JAXBException( Messages.format( Messages.COULD_NOT_INSTANTIATE, className, e ), e );
} catch (RuntimeException x) {
// avoid wrapping RuntimeException to JAXBException,
// because it indicates a bug in this code.
throw x;
} catch (Exception x) {
// can't catch JAXBException because the method is hidden behind
// reflection. Root element collisions detected in the call to
// createContext() are reported as JAXBExceptions - just re-throw it
// some other type of exception - just wrap it
throw new JAXBException(
Messages.format( Messages.COULD_NOT_INSTANTIATE, className, x ),
x);
}
}