1 package org.opentrafficsim.xml.bindings;
2
3 import java.lang.reflect.Field;
4
5 import org.djutils.logger.CategoryLogger;
6 import org.djutils.reflection.ClassUtil;
7 import org.opentrafficsim.xml.bindings.types.FieldType;
8
9
10
11
12
13
14
15
16
17
18
19 public class StaticFieldNameAdapter extends ExpressionAdapter<Field, FieldType>
20 {
21
22
23 @Override
24 public FieldType unmarshal(final String field)
25 {
26 if (isExpression(field))
27 {
28 return new FieldType(trimBrackets(field));
29 }
30 try
31 {
32 int dot = field.lastIndexOf(".");
33 String className = field.substring(0, dot);
34 String fieldName = field.substring(dot + 1);
35 return new FieldType(ClassUtil.resolveField(Class.forName(className), fieldName));
36 }
37 catch (Exception exception)
38 {
39 CategoryLogger.always().error(exception, "Problem parsing Static Field '" + field + "'");
40 throw new RuntimeException(exception);
41 }
42 }
43
44
45 @Override
46 public String marshal(final FieldType value)
47 {
48 return marshal(value, (v) -> (v.getDeclaringClass().getName() + "." + v.getName()));
49 }
50
51 }