I having trouble getting Orika working using eclipse.... I added the maven orika dependencies (1.4.6) and I created a sample junit test
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
public class OrikaTest {
@Test
public void test() {
//System.setProperty(OrikaSystemProperties.COMPILER_STRATEGY,EclipseJdtCompilerStrategy.class.getName());
MapperFactory mapperFactory = new DefaultMapperFactory.Builder()
.build();
mapperFactory
.classMap(PersonDTO.class, PersonEntity.class)
.field("cellPhone", "phone")
.byDefault()
.register();
PersonDTO dto = new PersonDTO();
dto.setFirstName("John");
dto.setLastName("Smith");
//dto.setRoles(Lists.newArrayList("Role1", "Role2"));
dto.setCellPhone("845-111-2222");
MapperFacade mapper = mapperFactory.getMapperFacade();
PersonEntity entity = mapper.map(dto, PersonEntity.class);
assertEquals(entity.getFirstName(), "Jeff");
assertNotNull("Roles not filled", entity);
}
}
class PersonDTO {
private String firstName;
private String lastName;
private String cellPhone;
private List<String> roles;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public List<String> getRoles() {
return roles;
}
public void setRoles(List<String> roles) {
this.roles = roles;
}
public String getCellPhone() {
return cellPhone;
}
public void setCellPhone(String cellPhone) {
this.cellPhone = cellPhone;
}
}
class PersonEntity {
private String firstName;
private String lastName;
private String phone;
private List<String> roles;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public List<String> getRoles() {
return roles;
}
public void setRoles(List<String> roles) {
this.roles = roles;
}
}
When I run the unit test, I get the following exception:
ma.glasnost.orika.MappingException: ma.glasnost.orika.impl.generator.CompilerStrategy$SourceCodeGenerationException: class PersonDTO is not accessible at ma.glasnost.orika.impl.generator.MapperGenerator.build(MapperGenerator.java:105) at ma.glasnost.orika.impl.DefaultMapperFactory.buildMapper(DefaultMapperFactory.java:1372) at ma.glasnost.orika.impl.DefaultMapperFactory.build(DefaultMapperFactory.java:1199) at ma.glasnost.orika.impl.DefaultMapperFactory.getMapperFacade(DefaultMapperFactory.java:859) at OrikaTest.test(OrikaTest.java:37) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) Caused by: ma.glasnost.orika.impl.generator.CompilerStrategy$SourceCodeGenerationException: class PersonDTO is not accessible at ma.glasnost.orika.impl.generator.JavassistCompilerStrategy.assureTypeIsAccessible(JavassistCompilerStrategy.java:155) at ma.glasnost.orika.impl.generator.MapperGenerator.build(MapperGenerator.java:58) ... 27 more
Note: I see in the console eclipse printing the defined mapping. However, I get the above error when executing:
MapperFacade mapper = mapperFactory.getMapperFacade();
If I attempt to use the EclipseJdtCompilerStrategy, by setting the system property, I get a different exception:
java.lang.NoSuchMethodError: org.eclipse.jdt.internal.compiler.Compiler.(Lorg/eclipse/jdt/internal/compiler/env/INameEnvironment;Lorg/eclipse/jdt/internal/compiler/IErrorHandlingPolicy;Lorg/eclipse/jdt/internal/compiler/impl/CompilerOptions;Lorg/eclipse/jdt/internal/compiler/ICompilerRequestor;Lorg/eclipse/jdt/internal/compiler/IProblemFactory;)V at ma.glasnost.orika.impl.generator.EclipseJdtCompiler.(EclipseJdtCompiler.java:87) at ma.glasnost.orika.impl.generator.EclipseJdtCompiler.(EclipseJdtCompiler.java:77) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at java.lang.Class.newInstance(Class.java:379) at ma.glasnost.orika.impl.generator.EclipseJdtCompilerStrategy.(EclipseJdtCompilerStrategy.java:57) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at java.lang.Class.newInstance(Class.java:379) at ma.glasnost.orika.impl.UtilityResolver.resolveUtility(UtilityResolver.java:123) at ma.glasnost.orika.impl.UtilityResolver.getDefaultCompilerStrategy(UtilityResolver.java:51) at ma.glasnost.orika.impl.DefaultMapperFactory$MapperFactoryBuilder.(DefaultMapperFactory.java:310) at ma.glasnost.orika.impl.DefaultMapperFactory$Builder.(DefaultMapperFactory.java:587) at OrikaTest.test(OrikaTest.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Any help would be greatly appreciated
Regarding the
class PersonDTO is not accessible at
ma.glasnost.orika.impl.generator.JavassistCompilerStrategy.assureTypeIsAccessible(JavassistCompilerStrategy.java:155)
Your PersonDTO
class is package-visible, so Orika code cannot access it from its package. Make it a public class
and that problem should go away. For reference, see specifically that line in Orika code.
That happens when you are using the default (Javassist) compiler.
For the Eclipse compiler, double-check that you added the relevant Maven dependency
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-eclipse-tools</artifactId>
<version>1.4.6</version><!-- should correspond to your core Orika version -->
</dependency>
and enable the compiler when creating the mapperFactory
MapperFactory factory =
new DefaultMapperFactory.Builder()
.compilerStrategy(new EclipseJdtCompilerStrategy())
.build();
The system property should be used as a fallback, so rather use the builder approach above.
For details see the Troubleshooting Guide "Enable step-debugging" section.