Top Description Constructors Methods
org.glassfish.jaxb.runtime.api

public abstract Class ClassResolver

extends Object
Class Inheritance
Imports
com.sun.istack.NotNull, .Nullable, jakarta.xml.bind.JAXBContext, .Unmarshaller, .ValidationEventHandler, jakarta.xml.bind.annotation.XmlAnyElement

Dynamically locates classes to represent elements discovered during the unmarshalling.

THIS INTERFACE IS SUBJECT TO CHANGE WITHOUT NOTICE.

Background

JAXBContext#newInstance(Class...) requires that application informs JAXB about all the classes that it may see in the instance document. While this allows JAXB to take time to optimize the unmarshalling, it is sometimes inconvenient for applications.

This is where ClassResolver comes to resucue.

A ClassResolver instance can be specified on Unmarshaller via Unmarshaller#setProperty(String, Object) as follows:

unmarshaller.setProperty( ClassResolver.class.getName(), new MyClassResolverImpl() );

When an Unmarshaller encounters (i) an unknown root element or (ii) unknown elements where unmarshaller is trying to unmarshal into XmlAnyElement with lax=true, unmarshaller calls resolveElementName(String, String) method to see if the application may be able to supply a class that corresponds to that class.

When a Class is returned, a new JAXBContext is created with all the classes known to it so far, plus a new class returned. This operation may fail (for example because of some conflicting annotations.) This failure is handled just like Exceptions thrown from ClassResolver#resolveElementName(String, String).

Author
Kohsuke Kawaguchi
Since
2.1

Constructor Summary

AccessConstructor and Description
protected
ClassResolver()

Default constructor.

Method Summary

Modifier and TypeMethod and Description
public abstract Class<?>

Returns:

If a non-null class is returned, it will be used to unmarshal this element. If null is returned, the resolution is assumed to be failed, and the unmarshaller will behave as if there was no to begin with (that is, to report it to ValidationEventHandler, then move on.)
resolveElementName
(String
Namespace URI of the unknown element. Can be empty but never null.
nsUri
,
String
Local name of the unknown element. Never be empty nor null.
localName
)

JAXB calls this method when it sees an unknown element.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ClassResolverback to summary
protected ClassResolver()

Default constructor.

Method Detail

resolveElementNameback to summary
public abstract Class<?> resolveElementName(String nsUri, String localName) throws Exception

JAXB calls this method when it sees an unknown element.

See the class javadoc for details.

Parameters
nsUri:String

Namespace URI of the unknown element. Can be empty but never null.

localName:String

Local name of the unknown element. Never be empty nor null.

Returns:Class<?>

If a non-null class is returned, it will be used to unmarshal this element. If null is returned, the resolution is assumed to be failed, and the unmarshaller will behave as if there was no to begin with (that is, to report it to ValidationEventHandler, then move on.)

Annotations
@Nullable
Exceptions
Exception:
Throwing any RuntimeException causes the unmarshaller to stop immediately. The exception will be propagated up the call stack. Throwing any other checked Exception results in the error reproted to ValidationEventHandler (just like any other error during the unmarshalling.)