New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException at ReflectionUtil.getAllFields() when using custom interface as a variable type #78

Closed
tomazic89 opened this Issue Jan 8, 2015 · 3 comments

Comments

Projects
None yet
2 participants
@tomazic89

tomazic89 commented Jan 8, 2015

Version used: 1.0.1
Source: maven repo
The bug is in line 97 of ReflectionUtils.java
if (methodSource.getSuperclass() != Object.class) { //recursion stop condition
There should also be null check
if (methodSource.getSuperclass() != Object.class && methodSource.getSuperclass() != null) {

I was trying to play around with PropertyMapping but I think that this is not the case.

I'm adding simple unit test:

package com.xyz.test.entityaudit;

import static org.junit.Assert.assertTrue;

import java.util.List;

import org.javers.core.Javers;
import org.javers.core.JaversBuilder;
import org.javers.core.diff.Diff;
import org.javers.core.diff.changetype.ValueChange;
import org.javers.core.metamodel.annotation.Entity;
import org.javers.core.metamodel.annotation.Id;
import org.junit.Test;

/**
 * @author nejct
 */
public class JaversTest {

    @Test
    public void simpleClassTest() {
        // given
        SimpleTestClass foo = new SimpleTestClass(1l, "Foo", new TestInterfaceImpl("Bar"));
        SimpleTestClass bar = new SimpleTestClass(1l, "Bar", new TestInterfaceImpl("Foo"));

        Javers javers = JaversBuilder.javers().build();

        // when
        Diff diff = javers.compare(foo, bar);

        // then
        assertTrue(diff.getChanges().size() == 2);
        for(ValueChange change : diff.getChangesByType(ValueChange.class)) {
            if (change.getProperty().getName().equals("testInterfaceProperty")) {
                assertTrue(change.getLeft().equals("Bar"));
                assertTrue(change.getRight().equals("Foo"));
            } else if (change.getProperty().getName().equals("stringProperty")) {
                assertTrue(change.getLeft().equals("Foo"));
                assertTrue(change.getRight().equals("Bar"));
            } else {

            }
        }

        System.out.println(diff);
    }

    @Test
    public void classWithInterfaceTest() {
        // given
        TestClassWithInterfaceProperty foo = new TestClassWithInterfaceProperty(1l, "Foo", new TestInterfaceImpl("Bar"));
        TestClassWithInterfaceProperty bar = new TestClassWithInterfaceProperty(1l, "Bar", new TestInterfaceImpl("Foo"));

        Javers javers = JaversBuilder.javers().build();

        // when
        Diff diff = javers.compare(foo, bar);

        // then
        assertTrue(diff.getChanges().size() == 2);
        for(ValueChange change : diff.getChangesByType(ValueChange.class)) {
            if (change.getProperty().getName().equals("testInterfaceProperty")) {
                assertTrue(change.getLeft().equals("Bar"));
                assertTrue(change.getRight().equals("Foo"));
            } else if (change.getProperty().getName().equals("stringProperty")) {
                assertTrue(change.getLeft().equals("Foo"));
                assertTrue(change.getRight().equals("Bar"));
            } else {

            }
        }

        System.out.println(diff);
    }

    @Entity
    public class SimpleTestClass {
        @Id
        private Long id;
        private List<String> list = java.util.Arrays.asList("Foo", "Bar");
        private String stringProperty;
        private TestInterfaceImpl testInterface;

        public SimpleTestClass(Long id, String stringProperty, TestInterfaceImpl testInterface) {
            this.id = id;
            this.stringProperty = stringProperty;
            this.testInterface = testInterface;
        }
    }

    @Entity
    public class TestClassWithInterfaceProperty {
        @Id
        private Long id;
        private List<String> list = java.util.Arrays.asList("Foo", "Bar");
        private String string;
        private TestInterface testInterface;

        public TestClassWithInterfaceProperty(Long id, String string, TestInterfaceImpl testInterface) {
            this.id = id;
            this.string = string;
            this.testInterface = testInterface;
        }
    }

    interface TestInterface {
        String getProperty();
    }

    public class TestInterfaceImpl implements TestInterface {

        private String testInterfaceProperty;

        public TestInterfaceImpl(String testInterfaceProperty) {
            this.testInterfaceProperty = testInterfaceProperty;
        }

        @Override
        public String getProperty() {
            return "Foo";
        }
    }
}

EDIT: added updated test if you want to use it later.

@bartoszwalacik bartoszwalacik self-assigned this Jan 8, 2015

@bartoszwalacik

This comment has been minimized.

Member

bartoszwalacik commented Jan 8, 2015

thanks for detailed report, i am looking at this right now

bartoszwalacik added a commit that referenced this issue Jan 8, 2015

#78
NPE fix

@bartoszwalacik bartoszwalacik referenced this issue Jan 8, 2015

Merged

NPE fix #79

@bartoszwalacik

This comment has been minimized.

Member

bartoszwalacik commented Jan 9, 2015

done & released, v.1.0.2
see http://javers.org/documentation/features/#release-notes

give us a start if yuo like JaVers https://github.com/javers/javers/stargazers

@tomazic89

This comment has been minimized.

tomazic89 commented Jan 9, 2015

It works as expected now. Can't thank you enough for this quick response and fix.
The star is a sure thing now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment