Orika - 不在eclipse中工作

时间:2015-10-14 20:27:43

标签: java eclipse orika

我无法让Orika使用eclipse工作....我添加了maven orika依赖项(1.4.6)并且我创建了一个样本junit测试

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;
    }
}

当我运行单元测试时,我得到以下异常:

  

ma.glasnost.orika.MappingException:ma.glasnost.orika.impl.generator.CompilerStrategy $ SourceCodeGenerationException:类PersonDTO无法访问       在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)       在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)       在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47)       在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)       在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)       在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)       在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)       在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)       在org.junit.runners.ParentRunner.run(ParentRunner.java:309)       在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)       在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)   引起:ma.glasnost.orika.impl.generator.CompilerStrategy $ SourceCodeGenerationException:类PersonDTO无法访问       在ma.glasnost.orika.impl.generator.JavassistCompilerStrategy.assureTypeIsAccessible(JavassistCompilerStrategy.java:155)       在ma.glasnost.orika.impl.generator.MapperGenerator.build(MapperGenerator.java:58)       ......还有27个

注意:我在控制台中看到eclipse打印定义的映射。但是,执行时出现上述错误:

MapperFacade mapper = mapperFactory.getMapperFacade();

如果我尝试使用EclipseJdtCompilerStrategy,通过设置系统属性,我得到一个不同的例外:

  

java.lang.NoSuchMethodError:org.eclipse.jdt.internal.compiler.Compiler。(Lorg / eclipse / jdt / internal / compiler / env / INameEnvironment; Lorg / eclipse / jdt / internal / compiler / IErrorHandlingPolicy; Lorg /蚀/ JDT /内部/编译器/ IMPL / CompilerOptions; Lorg /蚀/ JDT /内部/编译器/ ICompilerRequestor; Lorg /蚀/ JDT /内部/编译器/ IProblemFactory;)V       在ma.glasnost.orika.impl.generator.EclipseJdtCompiler。(EclipseJdtCompiler.java:87)       在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)       在java.lang.Class.newInstance(Class.java:379)       在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)       在java.lang.Class.newInstance(Class.java:379)       at ma.glasnost.orika.impl.UtilityResolver.resolveUtility(UtilityResolver.java:123)       在ma.glasnost.orika.impl.UtilityResolver.getDefaultCompilerStrategy(UtilityResolver.java:51)       在ma.glasnost.orika.impl.DefaultMapperFactory $ MapperFactoryBuilder。(DefaultMapperFactory.java:310)       在ma.glasnost.orika.impl.DefaultMapperFactory $ Builder。(DefaultMapperFactory.java:587)       在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)       在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47)       在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)       在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)       在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)       在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)       在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)       在org.junit.runners.ParentRunner.run(ParentRunner.java:309)       在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)       在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

非常感谢任何帮助

1 个答案:

答案 0 :(得分:3)

关于

class PersonDTO is not accessible at
    ma.glasnost.orika.impl.generator.JavassistCompilerStrategy.assureTypeIsAccessible(JavassistCompilerStrategy.java:155)

您的PersonDTO类是包可见的,因此Orika代码无法从其包中访问它。将其设为public class,该问题应该消失。供参考,请参阅Orika代码中的that line

当您使用默认(Javassist)编译器时会发生这种情况。

对于Eclipse编译器,请仔细检查您是否添加了相关的Maven依赖项

<dependency>
   <groupId>ma.glasnost.orika</groupId>
   <artifactId>orika-eclipse-tools</artifactId>
   <version>1.4.6</version><!-- should correspond to your core Orika version -->
</dependency> 

并在创建mapperFactory时启用编译器

MapperFactory factory = 
    new DefaultMapperFactory.Builder()
            .compilerStrategy(new EclipseJdtCompilerStrategy())
            .build();

系统属性应该用作后备,所以请使用上面的构建器方法。

有关详细信息,请参阅Troubleshooting Guide“启用步骤调试”部分。