if (first != null && second != null && !first.equals(second)) {
// not null & not equal
} else if (first == null ^ second == null) {
// not both null and not both not null
// (first == null && second != null) || (first != null && second == null)
} else {
// both null or equal
}
Run Code Online (Sandbox Code Playgroud)
FindBugs抱怨else if(first == null ^ second == null){...}
我有以下课程:
public interface IDataSource<T> {
public List<T> getData(int numberOfEntries);
}
public class MyDataSource implements IDataSource<MyData> {
public List<MyData> getData(int numberOfEntries) {
...
}
}
public class MyOtherDataSource implements IDataSource<MyOtherData> {
public List<MyOtherData> getData(int numberOfEntries) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
我想使用一个工厂,根据数据类型返回正确的实现.我写了以下内容,但我得到了"未经检查的演员"警告:
public static <T> IDataSource<T> getDataSource(Class<T> dataType) {
if (dataType.equals(MyData.class)) {
return (IDataSource<T>) new MyDataSource();
} else if (dataType.equals(MyOtherData.class)) {
return (IDataSource<T>) new MyOtherDataSource();
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
我做错了吗?我该怎么做才能摆脱警告?
我有以下测试代码:
@RunWith(PowerMockRunner.class)
@PrepareForTest({Logger.class, Files.class})
public class TestClass {
private static final List<String> LIST = new ArrayList<String>() {{
add("some entry");
}};
private static final Path PATH = Paths.get("/tmp/foo");
@Before
public void setup() {
PowerMockito.spy(Files.class);
PowerMockito.doReturn(LIST).when(Files.class, "readAllLines", PATH, Charset.defaultCharset());
}
@Test
public void test() {}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码在最后一行抛出“NoSuchFileException”。这是堆栈跟踪:
java.nio.file.NoSuchFileException: /tmp/foo at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86) at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102) at sun.nio .fs.UnixException.rethrowAsIOException(UnixException.java:107) at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214) at java.nio.file.Files.newByteChannel(Files.java:317) at java。 nio.file.Files.newByteChannel(Files.java:363) 在 java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:380) 在 java.nio.file.Files.newInputStream(Files.java:108)在 java.nio.file.Files.newBufferedReader(Files.java:2677) 在 java.nio.file.Files.readAllLines(Files.java:3033) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect .NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.powermock.reflect.internal …