forked from elaatifi/orika
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enable mapping of package private classes
- Loading branch information
Showing
12 changed files
with
183 additions
and
37 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
34 changes: 34 additions & 0 deletions
34
core/src/main/java/ma/glasnost/orika/impl/generator/Analysis.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,34 @@ | ||
package ma.glasnost.orika.impl.generator; | ||
|
||
import java.lang.reflect.Modifier; | ||
|
||
class Analysis { | ||
|
||
static Visibility getMostRestrictiveVisibility(Class<?> classToCheck) { | ||
Visibility visibility = Visibility.PUBLIC; | ||
Class<?> currentClass = classToCheck; | ||
while (currentClass != null) { | ||
int modifiers = currentClass.getModifiers(); | ||
if (Modifier.isPrivate(modifiers)) { | ||
visibility = Visibility.PRIVATE; | ||
} else if (Modifier.isProtected(modifiers) && visibility != Visibility.PRIVATE) { | ||
visibility = Visibility.PROTECTED; | ||
} else if (Modifier.isPublic(modifiers)) { | ||
// visibility = Visibility.PUBLIC not needed because if visibiliy were anything | ||
// else than PUBLIC we wouldn't set it anyways | ||
} else if (visibility != Visibility.PRIVATE && visibility != Visibility.PROTECTED) { | ||
visibility = Visibility.PACKAGE; | ||
} | ||
currentClass = currentClass.getEnclosingClass(); | ||
} | ||
return visibility; | ||
} | ||
|
||
enum Visibility { | ||
PRIVATE, PACKAGE, PROTECTED, PUBLIC | ||
} | ||
|
||
private Analysis() { | ||
throw new UnsupportedOperationException("not instantiable"); | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
tests/src/main/java/ma/glasnost/orika/test/packageprivate/PackagePrivateTestCase.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,49 @@ | ||
package ma.glasnost.orika.test.packageprivate; | ||
|
||
import ma.glasnost.orika.MapperFacade; | ||
import ma.glasnost.orika.MapperFactory; | ||
import ma.glasnost.orika.test.MappingUtil; | ||
import ma.glasnost.orika.test.packageprivate.otherpackage.SomePublicDto; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class PackagePrivateTestCase { | ||
|
||
@Test | ||
public void testMappingPackagePrivateToPublic() { | ||
SomePrivateEntity source = new SomePrivateEntity(); | ||
source.setField("test value"); | ||
|
||
final SomePublicDto actual = getMapperFacade().map(source, SomePublicDto.class); | ||
|
||
assertEquals(source.getField(), actual.getField()); | ||
} | ||
|
||
@Test | ||
public void testMappingPublicToPackagePrivate() { | ||
SomePublicDto source = new SomePublicDto(); | ||
source.setField("test value"); | ||
|
||
final SomePrivateEntity actual = getMapperFacade().map(source, SomePrivateEntity.class); | ||
|
||
assertEquals(source.getField(), actual.getField()); | ||
} | ||
|
||
@Test | ||
public void testMappingPackagePrivateToPackagePrivate() { | ||
SomePrivateEntity source = new SomePrivateEntity(); | ||
source.setField("test value"); | ||
|
||
final SimilarEntity actual = getMapperFacade().map(source, SimilarEntity.class); | ||
|
||
assertEquals(source.getField(), actual.getField()); | ||
} | ||
|
||
private MapperFacade getMapperFacade() { | ||
final MapperFactory mapperFactory = MappingUtil.getMapperFactory(true); | ||
mapperFactory.classMap(SomePrivateEntity.class, SomePublicDto.class); | ||
mapperFactory.classMap(SomePrivateEntity.class, SimilarEntity.class); | ||
return mapperFactory.getMapperFacade(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/src/main/java/ma/glasnost/orika/test/packageprivate/SimilarEntity.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 ma.glasnost.orika.test.packageprivate; | ||
|
||
class SimilarEntity { | ||
private String field; | ||
|
||
public String getField() { | ||
return field; | ||
} | ||
|
||
public void setField(String field) { | ||
this.field = field; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/src/main/java/ma/glasnost/orika/test/packageprivate/SomePrivateEntity.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 ma.glasnost.orika.test.packageprivate; | ||
|
||
class SomePrivateEntity { | ||
private String field; | ||
|
||
public String getField() { | ||
return field; | ||
} | ||
|
||
public void setField(String field) { | ||
this.field = field; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/src/main/java/ma/glasnost/orika/test/packageprivate/otherpackage/SomePublicDto.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 ma.glasnost.orika.test.packageprivate.otherpackage; | ||
|
||
public class SomePublicDto { | ||
private String field; | ||
|
||
public String getField() { | ||
return field; | ||
} | ||
|
||
public void setField(String field) { | ||
this.field = field; | ||
} | ||
} |