【问题标题】:Orika - Not working in eclipseOrika - 不在日食中工作
【发布时间】:2015-10-14 20:27:43
【问题描述】:

我无法让 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) 在 ma.glasnost.orika.impl.DefaultMapperFactory.buildMapper(DefaultMapperFactory.java:1372) 在 ma.glasnost.orika.impl.DefaultMapperFactory.build(DefaultMapperFactory.java:1199) 在 ma.glasnost.orika.impl.DefaultMapperFactory.getMapperFacade(DefaultMapperFactory.java:859) 在 OrikaTest.test(OrikaTest.java:37) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 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/ eclipse/jdt/internal/compiler/impl/CompilerOptions;Lorg/eclipse/jdt/internal/compiler/ICompilerRequestor;Lorg/eclipse/jdt/internal/compiler/IProblemFactory;)V 在 ma.glasnost.orika.impl.generator.EclipseJdtCompiler.(EclipseJdtCompiler.java:87) 在 ma.glasnost.orika.impl.generator.EclipseJdtCompiler.(EclipseJdtCompiler.java:77) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:526) 在 java.lang.Class.newInstance(Class.java:379) 在 ma.glasnost.orika.impl.generator.EclipseJdtCompilerStrategy.(EclipseJdtCompilerStrategy.java:57) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:526) 在 java.lang.Class.newInstance(Class.java:379) 在 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) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 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)

任何帮助将不胜感激

【问题讨论】:

  • 显示PersonDTO的代码
  • 添加了完整的类,包括 PersonDTO 和导入...谢谢您的帮助

标签: java eclipse orika


【解决方案1】:

关于

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“启用步进调试”部分。

【讨论】:

  • 你太棒了!!感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2017-04-12
  • 1970-01-01
  • 2012-01-26
  • 1970-01-01
  • 2015-09-12
  • 1970-01-01
  • 2011-07-03
  • 2015-02-15
相关资源
最近更新 更多