2014-09-01 111 views
6

我认为这会比较容易,但唉,它似乎不是。双括号初始化类型混淆

我目前正在使用Java EE 6在我的项目中为Facade-like结构编写单元测试。
对于测试,我使用Junit 4.11和Eclipse Kepler作为IDE。

从我所看到的情况来看,似乎有双重大括号初始化的“错误”,但我不够知识足以将我的手指放在为什么它不工作,因为我认为它应该。

要切入正题,我使用下面的类进行转换在集中的地方:

package com.example-company.util.converters; 

import java.util.HashMap; 
import java.util.Map; 

import com.example-company.model.Location; 
import com.example-company.model.Right; 

public final class ModelConverters { 

    private static final Map<Class<?>, ModelConverter<?, String>> modelConverterBacking = new HashMap<Class<?>, ModelConverter<?, String>>(); 
    static { 
     modelConverterBacking.put(Right.class, new RightConverter()); 
     modelConverterBacking.put(Location.class, new LocationConverter()); 
    }; 

    public static <T> String convert(final T input) 
      throws IllegalStateException { 
     @SuppressWarnings("unchecked") 
     ModelConverter<T, String> modelConverter = (ModelConverter<T, String>) modelConverterBacking 
       .get(input.getClass()); 
     if (modelConverter == null) { 
      throw new IllegalStateException("No mapping found for " 
        + input.getClass()); 
     } 
     return modelConverter.convertToView(input); 
    } 
} 

至于这正好这主要是与泛型和静态地图打。现在我决定我应该为此写几个单元测试。下面的课程稍微缩短了,所有不重现问题的测试用例都被删除了。

package com.example-company.test.unit.util.converters; 

import static org.junit.Assert.assertEquals; 
import com.example-company.model.Location; 
import com.example-company.util.converters.ModelConverters; 

import org.junit.Test; 

public class ModelConvertersFacadeTests { 

    @Test 
    public void test_MappingForLocationExists() { 
     final Location stub = new Location() { 
      { 
       setLocationName(""); 
      } 
     }; 

     String actual = ModelConverters.convert(stub); 
     assertEquals("", actual); 
    } 
} 

到目前为止好,真的没有什么事情发生,至少不是我现在得到的。那就是:一个花哨IllegalStateException与以下堆栈跟踪:

java.lang.IllegalStateException: No mapping found for class com.example-company.test.unit.util.converters.ModelConvertersFacadeTests$1 
    at com.example-company.util.converters.ModelConverters.convert(ModelConverters.java:23) 
    at com.example-company.test.unit.util.converters.ModelConvertersFacadeTests.test_MappingForLocationExists(ModelConvertersFacadeTests.java:24) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    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:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

我做的第一件事情,再次运行它,然后设置一个断点检查ModelConverters#convert()

里面发生了什么我得到了什么略有flabberghasted我:

Debug Perspective: Expressions

似乎input.getClass()重轮到ModelConvertersFacadeTests。但为什么它不返回com.example-company.model.Location

Full Debug Perspective, names censored

+0

[你一般应该小心使用双括号初始化](http://stackoverflow.com/q/924285/521799) – 2014-12-17 09:02:29

回答

13

似乎input.getClass()返回ModelConvertersFacadeTests

这是不正确的。您的堆栈跟踪说,这是类:

com。示例,company.test.unit.util.converters.ModelConvertersFacadeTests $ 1

注意$1底。这意味着你的类是一个匿名的(它没有自己的名字)内部类。

我们在截图中看到的this$0只是参考到外部类。

每次你做new SomeClass() { ... }你正在创建一个匿名的内部类。

双括号初始化本身与此无关。每次你使用双括号初始化时,你也在创建一个匿名的内部类。


解决通过地图查找不同

MapRight.classLocation.class的映射,但它不具备子类这两类的映射。

static { 
    modelConverterBacking.put(Right.class, new RightConverter()); 
    modelConverterBacking.put(Location.class, new LocationConverter()); 
}; 

什么你可以 DO(不是说这是最好的方法),通过地图的钥匙,并且是循环检查:

mapKey.isAssignableFrom(input.getClass()) 

当返回true,你知道,你要么有一个mapKey的类,要么你有它的一个子类。

除了循环遍历映射键之外,还可以遍历所传递对象的超类和已实现接口,并为每个对象执行modelConverterBacking.get查找。效果将是相同的。


不使用匿名内部类解决

您当前的代码是:

final Location stub = new Location() { 
    { 
     setLocationName(""); 
    } 
}; 

如果你不是会做:

final Location stub = new Location(); 
stub.setLocationName(""); 

那么你没有创建任何匿名的内部类,因此不会有这个问题。

但是,即使你只是这样做:

final Location stub = new Location() {}; 
stub.setLocationName(""); 

然后你有一个匿名内部类,这将导致问题为您服务。


这是非常重要不要混淆了两类ModelConvertersFacadeTests$1ModelConvertersFacadeTests