Add getAnnotations() and getAnnotation(...) methods to Property

#27 Merged at 9b7aafe
Repository
RoKish
Branch
default
Repository
asomov
Branch
default
Author
  1. Roman Kishchenko
Reviewers
Description

Description

As a client of SnakeYaml I want to have an access to the annotations of properties so that I could add my custom annotations like Jackson and JAXB have. Especially my use case is to use annotations to:

  • override property name (JsonProperty#value)
  • implement something like @JsonUnwrapped
  • ignore properties (like @JsonIgnore)

Please note that I'm not going to add these annotations as a part of SnakeYaml, it's just to be used in my project that uses SnakeYaml for YAML serialization. Without this change I can't do it (only by means of reflection).

Let's say I want to ignore all the properties that are annotated with @Transient annotation. I can override Representer#representJavaBeanProperty, however currently there's no way to access annotations. With my changes it would be possible to do something like this:

@Override
protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue, Tag customTag) {
    NodeTuple tuple = null;
    if (property.getAnnotation(Transient.class) == null) { // I need the getter to do this. Without the getter I can't access the annotation
        tuple = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
    }
    return tuple;
}

Actually, the commit solves the issue #307.

Notes

I added PropertyTest as my previous pull request was declined because of bad coverage. I still think, that PropertyTest more about testing PropertyUtils and BeanAccess because depending on the PropertyUtils logic, FieldProperty/MethodProperty/MissingProperty is returned. Provided that PropertyUtils works as expected, there's nothing to test because it's really just a getter for annotations.

Comments (3)