Question

I'm trying to implement a somewhat general converter, which transforms the data based on a given annotation. Say I want to transform these annotated strings in any matter.

All is well, until the code hits my converter's "matches" method. The "sourceType" I'm getting is always stripped out of all of the useful information. Has anyone had any success with such a setup, or am I missing something?

public class TestStringWriteConverter implements ConditionalGenericConverter {

@Override
    public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
        if (sourceType.hasAnnotation(GivenAnnotation.class)) {
            //->never gets executed, because sourceType is being stripped out of it's useful infos
        }

I followed the problem to MappingMongoConverter from this package org.springframework.data.mongodb.core.convert

    protected void writeInternal(Object obj, final DBObject dbo, MongoPersistentEntity<?> entity) {
       //...
                if (null != propertyObj) {
                    if (!conversions.isSimpleType(propertyObj.getClass())) {
                        writePropertyInternal(propertyObj, dbo, prop);
                    } else {
                       // I always end up here, which is correct but the whole prop object is being omitted in favor of the getFieldName() property
                        writeSimpleInternal(propertyObj, dbo, prop.getFieldName());
                    }
                }

}

The spring versions I'm using:

  <spring.version>3.2.5.RELEASE</spring.version>
  <spring.data.version>1.3.2.RELEASE</spring.data.version>

Any help is much appreciated.

Was it helpful?

Solution

I think you misunderstand what sourceType.hasAnnotation(…) actually returns. As the name suggests, it inspects the type for annotations. So for a given type like this:

@MyAnnotation
class Foo { }

it would allow you to find @MyAnnotation. However you are writing about "annotated strings". I assume you mean something like:

class Bar {

  @MyAnnotation
  String property;
}

This is not a type annotation and the Converter API is not meant to cover such cases. If you think supporting such scenarios would be worthfile, please file a JIRA ticket.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top