Skip to content

Commit

Permalink
added annotation converters #21
Browse files Browse the repository at this point in the history
needs unit tests
  • Loading branch information
Faithcaio committed Dec 27, 2014
1 parent 76ea07f commit 5173757
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface Converter
public @interface Converter // TODO Unit tests!
{
/**
* The class of the Converter to use for the annotated field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public final void registerConverter(Class clazz, Converter converter)
return;
}
converters.put(clazz, converter);
annotationConverters.put(converter.getClass(), converter);
}

/**
Expand Down Expand Up @@ -231,6 +232,10 @@ private Converter getConverter(Class<?> clazz)
}
return converter;
}
public Converter getAnnotationConverter(Class<? extends Converter> clazz)
{
return this.annotationConverters.get(clazz);
}

private Converter findConverter(Class clazz)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import de.cubeisland.engine.reflect.Section;
import de.cubeisland.engine.reflect.annotations.Comment;
import de.cubeisland.engine.reflect.annotations.Converter;
import de.cubeisland.engine.reflect.annotations.Name;
import de.cubeisland.engine.reflect.codec.ConverterManager;
import de.cubeisland.engine.reflect.exception.ConversionException;
Expand Down Expand Up @@ -94,7 +95,7 @@ protected final ReflectedPath getPathFor(Field field)
}
else
{
path = ReflectedPath.forName(StringUtils.fieldNameToPath(field.getName()));
path = ReflectedPath.forName(StringUtils.fieldNameToPath(field.getName())); // TODO configurable Naming convention #20
}
this.paths.put(field, path);
}
Expand Down Expand Up @@ -122,7 +123,7 @@ public Node toNode(Section section, ConverterManager manager) throws ConversionE
}
try
{
Node newNode = manager.convertToNode(field.get(section));
Node newNode = toNode(section, manager, field);
addComment(newNode, field);

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

@SuppressWarnings("unchecked")
private Node toNode(Section section, ConverterManager manager, Field field) throws ConversionException, IllegalAccessException
{
Node newNode;
if (field.isAnnotationPresent(Converter.class))
{
newNode = manager.getAnnotationConverter(field.getAnnotation(Converter.class).value()).toNode(field.get(section), manager);
}
else
{
newNode = manager.convertToNode(field.get(section));
}
return newNode;
}

/**
* Adds a comment to the given Node
*
Expand Down Expand Up @@ -212,8 +228,7 @@ else if (e instanceof ConversionException)
* @param manager the manager
*/
@SuppressWarnings("unchecked")
public void fromNode(Section section, MapNode node, MapNode defaultNode,
ConverterManager manager) throws ConversionException
public void fromNode(Section section, MapNode node, MapNode defaultNode, ConverterManager manager) throws ConversionException
{
for (Field field : this.getReflectedFields(section.getClass()))
{
Expand All @@ -240,10 +255,13 @@ public void fromNode(Section section, MapNode node, MapNode defaultNode,
}
}

if (Section.class.isAssignableFrom(field.getType()))
if (field.isAnnotationPresent(Converter.class))
{
value = manager.getAnnotationConverter(field.getAnnotation(Converter.class).value()).fromNode(fieldNode, manager);
}
else if (Section.class.isAssignableFrom(field.getType()))
{
Section fillSection = SectionFactory.newSectionInstance((Class<? extends Section>)field.getType(),
section);
Section fillSection = SectionFactory.newSectionInstance((Class<? extends Section>)field.getType(), section);
manager.convertFromNode((MapNode)fieldNode, (MapNode)defaultNode.getNodeAt(fieldPath), fillSection);
value = fillSection;
}
Expand Down

0 comments on commit 5173757

Please sign in to comment.