【问题标题】:Why does Java complain that it can't find my local class?为什么 Java 抱怨找不到我的本地类?
【发布时间】:2011-11-25 22:16:54
【问题描述】:

我正在尝试设置 Dozer 以在我的两个实体之间执行复杂的映射。本质上,我希望它根据值是否为 1 (100%) 将我的 percentCompleted double 转换为布尔值。

为此,我创建了以下方法:

private void initEntityMappings()
{
    BeanMappingBuilder builder = new BeanMappingBuilder() {
        @Override
        protected void configure() {
            class isCompletedConverter implements CustomConverter {
                @Override
                public Object convert(Object destination, Object source, Class destClass, Class sourceClass) {
                    if (source == null) { return null; }

                    // Make sure the source is a double and the destination is a boolean
                    if (!(source instanceof Double) || !(destination instanceof Boolean))
                        throw new MappingException("Source was not a double or destination was not a boolean");

                    Boolean castedDest = (Boolean)destination;
                    Double castedSrc = (Double)source;
                    castedDest = castedSrc == 1;

                    return castedDest;
                }
            };

            mapping(TaskDetailsViewModel.class, TaskSummaryViewModel.class)
                .fields("percentCompleted", "isCompleted", customConverter(isCompletedConverter));
        }
    };  
}

问题在于.fields() 调用会抱怨,因为它说找不到isCompletedConverter 的符号。由于这是我第一次参加本地课程,我确信我做错了什么,但我无法弄清楚到底是什么。

【问题讨论】:

    标签: java local-class


    【解决方案1】:

    您正在使用 token isCompletedConverter(与 isCompletedConverter 的实例相反,或者它是 .class 对象)在您使用它的特定点是无效的。您包含它的方式有点像在演员表或instanceof 检查中,但这是与customConverter 看起来的方法调用不同的语法。

    根据customConverter() 的操作,尝试isCompletedConverter.classnew isCompletedConverter()(我无法从给定的代码中看出)。如果您将本地类从 isCompletedConverter 重命名为 IsCompletedConverter 以匹配常规 Java 约定,也可能会变得更清楚。

    【讨论】:

    • 啊,isCompletedConverter.class 工作了,现在我明白为什么了。也很好地调用了 java 约定。
    猜你喜欢
    • 2011-01-31
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 2021-06-27
    相关资源
    最近更新 更多