question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ClassCastException: MissingProperty cannot be cast to java.lang.Double

See original GitHub issue

Hello, we have a use case that needs a custom comparator for Double values. However in some cases we get the following exception:

java.lang.ClassCastException: org.javers.core.metamodel.property.MissingProperty cannot be cast to java.lang.Double
	at org.javers.core.metamodel.type.CustomValueComparatorNullSafe.equals(CustomValueComparatorNullSafe.java:29)
	at org.javers.core.metamodel.type.PrimitiveOrValueType.equals(PrimitiveOrValueType.java:32)
	at org.javers.core.diff.appenders.ValueChangeAppender.calculateChanges(ValueChangeAppender.java:34)
	at org.javers.core.diff.appenders.ValueChangeAppender.calculateChanges(ValueChangeAppender.java:10)
	at org.javers.core.diff.DiffFactory.appendChanges(DiffFactory.java:153)
	at org.javers.core.diff.DiffFactory.appendPropertyChanges(DiffFactory.java:143)
....

I managed to reproduce it in the following test case.

class CustomValueComparatorCase extends Specification {
    class Parent {}

    class Child1 extends Parent {
        String prop1
    }

    class Child2 extends Parent {
        Double prop2
    }

    class Container {
        Map<String, Parent> map = new HashMap<>()
    }

    class CustomDoubleComparator implements CustomValueComparator<Double> {
        @Override
        boolean equals(Double a, Double b) {
            return round(a) == round(b)
        }

        @Override
        String toString(Double value) {
            return round(value).toString()
        }

        private BigDecimal round(Double val) {
            return NumberUtils.toScaledBigDecimal(val, 4, RoundingMode.HALF_UP)
        }
    }

    def "should compare map values using their concrete type"() {
        given:
        def javers = JaversBuilder.javers()
                .registerValue(Double.class, new CustomDoubleComparator())
                .build()

        when:
        Parent c1 = new Child1(prop1: "Hi")
        Parent c2 = new Child2(prop2: 1.2)


        Container container1 = new Container()
        container1.map.put("key", c1)

        Container container2 = new Container()
        container2.map.put("key", c2)

        def diff = javers.compare(container1, container2)

        then:
        diff.changes.size() > 0

        println(diff.changes)
    }
}

It seems that Javers sees the values in the map as Parent type not the concrete type. If I change Map<String, Parent> map to Map<String, Object> map then the test passes. If I keep Map<String, Parent> map and compare the maps directly like javers.compare(container1.map, container2.map) then the test passes.

Would it be possible to avoid this exception?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
bartoszwalacikcommented, Feb 7, 2020

Fixed in 5.8.9, try now (map was not the cause) but handling MISSING_PROPERTY in CustomValueComparator

0reactions
mdiicommented, Feb 7, 2020

Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

java.lang.Long cannot be cast to java.lang.Double what am I ...
I am attempting to pull session data (numbers) from Firestore and I get a java.lang.Long cannot be cast to java.lang.Double error.
Read more >
How to fix java.lang.classcastexception cannot be cast to in Java
As name suggests ClassCastException in Java comes when we try to type cast an object and object is not of the type we...
Read more >
RuntimeException "java.lang.Double cannot be cast to java ...
Spectrum issue with ConstructField option in a Transformer stage "RuntimeException "java.lang.Double cannot be cast to java.lang.
Read more >
java.lang.Integer cannot be cast to java.lang.Double" error
I can't figure out why I am getting a "java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double" error on this ...
Read more >
How to Fix java.lang.classcastexception in Java?
ClassCastException in Java occurs when we try to convert the data type of ... BigDecimal cannot be cast to class java.lang. ... double...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found