atos1990 / orika

Automatically exported from code.google.com/p/orika
0 stars 0 forks source link

Property with no getter (null readMethod) is not being excluded/ignored #67

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Perform auto-mapping where at least one of the properties has a setter but no 
getter. See example code below. I would have expected Orika to ignore the 
property.

import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;

public class OrikaTest
{
   public static class Bean {

      private String name;
      private int size;

      /*public int getSize() {
         return size;
      }*/
      public void setSize(int size) {
         this.size = size;
      }
      public String getName() {
         return name;
      }
      public void setName(String value) {
         this.name = value;
      }
   }

   public static void main(String[] args)
   {
      MapperFactory factory = new DefaultMapperFactory.Builder().build();
      factory.registerClassMap(factory
            .classMap(Bean.class, Bean.class).byDefault()
            .toClassMap());
      MapperFacade mapper = factory.getMapperFacade();
      Bean bean = new Bean();
      bean.setSize(20);
      bean.setName("Kidney");
      Bean bean2 = mapper.map(bean, Bean.class);
   }

}

What is the expected output? What do you see instead?
I expected the mapping to ignore the 'size' property. Instead an exception is 
thrown because the generated code won't compile.
Exception in thread "main" ma.glasnost.orika.MappingException: 
ma.glasnost.orika.impl.generator.CompilerStrategy$SourceCodeGenerationException:
 An exception occured while compiling the following method:

    public void mapAtoB(java.lang.Object a, java.lang.Object b, ma.glasnost.orika.MappingContext mappingContext) {

super.mapAtoB(a, b, mappingContext);

OrikaTest.Bean source = ((OrikaTest.Bean)a);

OrikaTest.Bean destination = ((OrikaTest.Bean)b);
destination.setName(((java.lang.String)source.getName()));
destination.setSize(((int)source.null));          <----- 'source.null' is not 
valid code
        if(customMapper != null) { 
             customMapper.mapAtoB(source, destination, mappingContext);
        }
    }

 for ma.glasnost.orika.generated.Orika_Bean_Bean_Mapper1517791225

    at ma.glasnost.orika.impl.generator.MapperGenerator.build(MapperGenerator.java:116)
...etc...

What version of the product are you using? On what operating system?
Orika 1.3.5 on Windows 7 64bit

Please provide any additional information below.
Basically MapperGenerator.generateFieldMapCode is getting a false positive for 
the sourceProperty.isReadable() call..

....
    private FieldMap generateFieldMapCode(CodeSourceBuilder code, FieldMap fieldMap, ClassMap<?, ?> classMap, Type<?> destinationType, StringBuilder logDetails) throws Exception {

        final VariableRef sourceProperty = new VariableRef(fieldMap.getSource(), "source");
        final VariableRef destinationProperty = new VariableRef(fieldMap.getDestination(), "destination");

        if (!sourceProperty.isReadable() || ((!destinationProperty.isAssignable()) && !destinationProperty.isCollection() && !destinationProperty.isArray() && !destinationProperty.isMap())) {
            if (logDetails != null) {

                logDetails.append("excluding because ");
....etc...
The stacktrace from the isReadable() call looks like this..
VariableRef.getGetter(Property, String) line: 431   
VariableRef.getter() line: 70   
VariableRef.isReadable() line: 78   
..with getGetter looking like this..
    protected static String getGetter(final Property property, String variableExpression) {
        String var = variableExpression;
        if (property.hasPath()) {
            for (final Property p : property.getPath()) {
                var = getGetter(p, var);
            }
        }
        return "((" + property.getType().getCanonicalName() + ")" + var + 
                ( property.isArrayElement() ? "" : ".") + property.getGetter() + ")";
    }

So getGetter ends up returning a non-null string, even though property.getter 
is null and the property doesn't get excluded as it should.

Original issue reported on code.google.com by drenna...@gmail.com on 20 Nov 2012 at 5:01

GoogleCodeExporter commented 9 years ago

Original comment by elaat...@gmail.com on 20 Nov 2012 at 11:31

GoogleCodeExporter commented 9 years ago
I just commited a fix for the issue, available on master/github
ASAP we can also ship a bugfix/features release.

Thanks a lot, 

Kindest regards

Original comment by elaat...@gmail.com on 20 Nov 2012 at 11:33

GoogleCodeExporter commented 9 years ago

Original comment by elaat...@gmail.com on 20 Nov 2012 at 11:35

GoogleCodeExporter commented 9 years ago
Thanks that was fast :) I gave the snapshot build, 
https://repository-orika.forge.cloudbees.com/snapshot/ma/glasnost/orika/orika-co
re/1.3.6-SNAPSHOT/orika-core-1.3.6-20121120.114714-36-deps-included.jar and it 
works.

Original comment by drenna...@gmail.com on 20 Nov 2012 at 1:57