Skip to content

Commit 5173757

Browse files
committed
added annotation converters #21
needs unit tests
1 parent 76ea07f commit 5173757

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

core/src/main/java/de/cubeisland/engine/reflect/annotations/Converter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
@Retention(RetentionPolicy.RUNTIME)
3535
@Target(ElementType.FIELD)
3636
@Documented
37-
public @interface Converter
37+
public @interface Converter // TODO Unit tests!
3838
{
3939
/**
4040
* The class of the Converter to use for the annotated field

core/src/main/java/de/cubeisland/engine/reflect/codec/ConverterManager.java

+5
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ public final void registerConverter(Class clazz, Converter converter)
167167
return;
168168
}
169169
converters.put(clazz, converter);
170+
annotationConverters.put(converter.getClass(), converter);
170171
}
171172

172173
/**
@@ -231,6 +232,10 @@ private Converter getConverter(Class<?> clazz)
231232
}
232233
return converter;
233234
}
235+
public Converter getAnnotationConverter(Class<? extends Converter> clazz)
236+
{
237+
return this.annotationConverters.get(clazz);
238+
}
234239

235240
private Converter findConverter(Class clazz)
236241
{

core/src/main/java/de/cubeisland/engine/reflect/codec/converter/SectionConverter.java

+25-7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import de.cubeisland.engine.reflect.Section;
3636
import de.cubeisland.engine.reflect.annotations.Comment;
37+
import de.cubeisland.engine.reflect.annotations.Converter;
3738
import de.cubeisland.engine.reflect.annotations.Name;
3839
import de.cubeisland.engine.reflect.codec.ConverterManager;
3940
import de.cubeisland.engine.reflect.exception.ConversionException;
@@ -94,7 +95,7 @@ protected final ReflectedPath getPathFor(Field field)
9495
}
9596
else
9697
{
97-
path = ReflectedPath.forName(StringUtils.fieldNameToPath(field.getName()));
98+
path = ReflectedPath.forName(StringUtils.fieldNameToPath(field.getName())); // TODO configurable Naming convention #20
9899
}
99100
this.paths.put(field, path);
100101
}
@@ -122,7 +123,7 @@ public Node toNode(Section section, ConverterManager manager) throws ConversionE
122123
}
123124
try
124125
{
125-
Node newNode = manager.convertToNode(field.get(section));
126+
Node newNode = toNode(section, manager, field);
126127
addComment(newNode, field);
127128

128129
Node prevNode = baseNode.getNodeAt(getPathFor(field));
@@ -154,6 +155,21 @@ public Node toNode(Section section, ConverterManager manager) throws ConversionE
154155
return baseNode;
155156
}
156157

158+
@SuppressWarnings("unchecked")
159+
private Node toNode(Section section, ConverterManager manager, Field field) throws ConversionException, IllegalAccessException
160+
{
161+
Node newNode;
162+
if (field.isAnnotationPresent(Converter.class))
163+
{
164+
newNode = manager.getAnnotationConverter(field.getAnnotation(Converter.class).value()).toNode(field.get(section), manager);
165+
}
166+
else
167+
{
168+
newNode = manager.convertToNode(field.get(section));
169+
}
170+
return newNode;
171+
}
172+
157173
/**
158174
* Adds a comment to the given Node
159175
*
@@ -212,8 +228,7 @@ else if (e instanceof ConversionException)
212228
* @param manager the manager
213229
*/
214230
@SuppressWarnings("unchecked")
215-
public void fromNode(Section section, MapNode node, MapNode defaultNode,
216-
ConverterManager manager) throws ConversionException
231+
public void fromNode(Section section, MapNode node, MapNode defaultNode, ConverterManager manager) throws ConversionException
217232
{
218233
for (Field field : this.getReflectedFields(section.getClass()))
219234
{
@@ -240,10 +255,13 @@ public void fromNode(Section section, MapNode node, MapNode defaultNode,
240255
}
241256
}
242257

243-
if (Section.class.isAssignableFrom(field.getType()))
258+
if (field.isAnnotationPresent(Converter.class))
259+
{
260+
value = manager.getAnnotationConverter(field.getAnnotation(Converter.class).value()).fromNode(fieldNode, manager);
261+
}
262+
else if (Section.class.isAssignableFrom(field.getType()))
244263
{
245-
Section fillSection = SectionFactory.newSectionInstance((Class<? extends Section>)field.getType(),
246-
section);
264+
Section fillSection = SectionFactory.newSectionInstance((Class<? extends Section>)field.getType(), section);
247265
manager.convertFromNode((MapNode)fieldNode, (MapNode)defaultNode.getNodeAt(fieldPath), fillSection);
248266
value = fillSection;
249267
}

0 commit comments

Comments
 (0)