-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Since a later version of Java, when you call getCanonicalName on an enum with abstract methods, it will return a null value. This messes up several routines in BeanMapper. The method to get the canonical name has been abstracted into a utility class that can deal with the null value by calling a fallback function that looks up the canonical name of the superclass. This solves the problem at hand. All methods that directly call getCanonicalName now call the utility class instead.
- Loading branch information
1 parent
806b09d
commit 66672e2
Showing
15 changed files
with
95 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,5 +38,4 @@ public T get() { | |
} | ||
return value; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,4 +91,4 @@ public int getGenericParameterIndex() { | |
return 0; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,5 +54,4 @@ private Object[] mapParameterizedArguments(Type[] constructorParameterTypes, Obj | |
} | ||
return mappedArguments; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.beanmapper.utils; | ||
|
||
public class CanonicalClassName { | ||
public static String determineCanonicalClassName(Class<?> clazz) { | ||
return clazz.getCanonicalName() == null ? | ||
fallbackWhenCanonicalNameIsNull(clazz) : | ||
clazz.getCanonicalName(); | ||
} | ||
|
||
// See bug 190, https://github.com/42BV/beanmapper/issues/190 | ||
// When an enum has abstract methods, calling getCanonicalName() on its class will return a null value. | ||
// This fallback method will allow for the expected canonical name value to be returned. getCanonicalName | ||
// must no longer be called directly. | ||
private static String fallbackWhenCanonicalNameIsNull(Class<?> clazz) { | ||
return clazz.getSuperclass().getCanonicalName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/test/java/io/beanmapper/testmodel/enums/ComplexEnumResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.beanmapper.testmodel.enums; | ||
|
||
public class ComplexEnumResult { | ||
|
||
public String name; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/java/io/beanmapper/testmodel/enums/WithAbstractMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.beanmapper.testmodel.enums; | ||
|
||
public enum WithAbstractMethod { | ||
ONE_VALUE() { | ||
@Override | ||
public void someAbstractMethod() {} | ||
}; | ||
public abstract void someAbstractMethod(); | ||
|
||
public String getName() { | ||
return name(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/java/io/beanmapper/utils/CanonicalClassNameTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.beanmapper.utils; | ||
|
||
import io.beanmapper.testmodel.enums.Day; | ||
import io.beanmapper.testmodel.enums.WithAbstractMethod; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class CanonicalClassNameTest { | ||
@Test | ||
public void determineCanonicalClassNameOfOrdinaryEnum() { | ||
assertEquals("io.beanmapper.testmodel.enums.Day", CanonicalClassName.determineCanonicalClassName(Day.MONDAY.getClass())); | ||
} | ||
|
||
@Test | ||
public void determineCanonicalClassNameOfEnumWithAbstractMethod() { | ||
assertEquals("io.beanmapper.testmodel.enums.WithAbstractMethod", | ||
CanonicalClassName.determineCanonicalClassName(WithAbstractMethod.ONE_VALUE.getClass())); | ||
} | ||
} |