View Javadoc
1   package org.opentrafficsim.xml.bindings;
2   
3   import javax.management.modelmbean.XMLParseException;
4   import javax.xml.bind.annotation.adapters.XmlAdapter;
5   
6   import org.djutils.reflection.ClassUtil;
7   
8   /**
9    * StaticFieldNameAdapter converts between the XML String for a class name and the Class object. <br>
10   * <p>
11   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
13   * <p>
14   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Apr 5, 2019 <br>
15   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
17   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
18   */
19  public class StaticFieldNameAdapter extends XmlAdapter<String, Object>
20  {
21  
22      /** {@inheritDoc} */
23      @Override
24      public Object unmarshal(final String field) throws Exception
25      {
26          int dot = field.lastIndexOf(".");
27          String className = field.substring(0, dot);
28          String fieldName = field.substring(dot + 1);
29          return ClassUtil.resolveField(Class.forName(className), fieldName).get(null);
30      }
31  
32      /** {@inheritDoc} */
33      @Override
34      public String marshal(final Object v) throws Exception
35      {
36          throw new XMLParseException("Unable to marshal an object to it's original static field location.");
37      }
38  
39  }