English 中文(简体)
如何消除这种非专利的警告?
原标题:How to get rid of this generics warning?

我试图篡改一个通用的接口,每当我模拟时,我就得到这一警告:

通用外语的表达需要不受限制的转换,以适应通用外语和外语;

我的接口

interface GenericInterface<T>{
    public T get();
}

我的考验

@Test
public void testGenericMethod(){
    GenericInterface<String> mockedInterface = EasyMock.createMock(GenericInterface.class);
}

我在试验案件中的第一线得到警告。

我如何消除这一一般性警告?

问题回答

发出警告的正确步骤是:

  • First and foremost, prove that the unchecked cast is safe, and document why
  • Only then perform the unchecked cast, and annotate @SuppressWarnings("unchecked") on the variable declaration (not on the whole method)

类似情况:

// this cast is correct because...
@SuppressWarnings("unchecked")
GenericInterface<String> mockedInterface =
    (GenericInterface<String>) EasyMock.createMock(GenericInterface.class);

Guidelines

摘录如下: 有效 Java 2nd Edition: 项目24: 消除未经检查的警告:

  • Eliminate every unchecked warning that you can.
  • If you can t eliminate a warning, and you can prove that the code that provoked the warning is typesafe, then (and only then) suppress the warning with @SuppressWarning("unchecked") annotation.
  • Always use the SuppressWarning annotation on the smallest scope possible.
  • Every time you use an @SuppressWarning("unchecked") annotation, add a comment saying why it s safe to do so.

Related questions


Refactoring the cast

在大多数情况下,还有可能在一种发电机中进行未加检查的投放。 它看着这样的情况:

static <E> Set<E> newSet(Class<? extends Set> klazz) {
    try {
        // cast is safe because newly instantiated set is empty
        @SuppressWarnings("unchecked")
        Set<E> set = (Set<E>) klazz.newInstance();
        return set;
    } catch (InstantiationException e) {
        throw new IllegalArgumentException(e);
    } catch (IllegalAccessException e) {
        throw new IllegalArgumentException(e);          
    }
}

然后,在别处,你只能做:

// compiles fine with no unchecked cast warnings!
Set<String> names = newSet(HashSet.class);
Set<Integer> nums = newSet(TreeSet.class);

See also

这里的问题是,方便的磁克(createMock)()将退回一个类型的物体Generic Interface,而不是Genericface Inter<String>。 您可使用<条码>@Supresswarnings的说明,以忽略警告,或可尝试和明确投到<条码>。 GenericInterface<String>(我认为这只是提供了不同的警告)。

似乎讨论了同样的警告......

i guess @Suppress Warnings可能是该情况下幸福的关键

如果你真心要避免汇编者发出警告,你就能够宣布你为避免这种警告而进行的试验的接口。

 interface MockGenericInterface extends GenericInterface<String> {}

然后,你可以做到:

 GenericInterface<String> mockedInterface = EasyMock.createMock(MockGenericInterface.class);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签