package org.opentrafficsim.xml.bindings;
import javax.management.modelmbean.XMLParseException;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.djutils.logger.CategoryLogger;
import org.djutils.reflection.ClassUtil;
/**
* StaticFieldNameAdapter converts between the XML String for a class name and the Class object.
*
* Copyright (c) 2013-2021 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* @version $Revision$, $LastChangedDate$, by $Author$, initial version Apr 5, 2019
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
*/
public class StaticFieldNameAdapter extends XmlAdapter
{
/** {@inheritDoc} */
@Override
public Object unmarshal(final String field) throws Exception
{
try
{
int dot = field.lastIndexOf(".");
String className = field.substring(0, dot);
String fieldName = field.substring(dot + 1);
return ClassUtil.resolveField(Class.forName(className), fieldName).get(null);
}
catch (Exception exception)
{
CategoryLogger.always().error(exception, "Problem parsing Static Field '" + field + "'");
throw exception;
}
}
/** {@inheritDoc} */
@Override
public String marshal(final Object v) throws Exception
{
throw new XMLParseException("Unable to marshal an object to it's original static field location.");
}
}