我理解当使用具有泛型类型的varargs时,Java 7会发生这种情况;
但我的问题是......
当Eclipse说"它的使用可能会污染堆?"时,Eclipse到底意味着什么呢?
和
新@SafeVarargs注释如何阻止这种情况?
我意识到Java 8 lambda实现可能会发生变化,但在lambda build b39中,我发现只有当lambda表达式返回非void类型时才能省略大括号.例如,这编译:
public class Collections8 {
public static void main(String[] args) {
Iterable<String> names = Arrays.asList("Alice", "Bob", "Charlie");
names.filter(e -> e.length() > 4).forEach(e -> { System.out.println(e); });
}
}
Run Code Online (Sandbox Code Playgroud)
但删除这样的大括号:
names.filter(e -> e.length() > 4).forEach(e -> System.out.println(e));
Run Code Online (Sandbox Code Playgroud)
给出了错误
Collections8.java:6: error: method forEach in interface Iterable<T> cannot be applied to given types;
names.filter(e -> e.length() > 4).forEach(e -> System.out.println(e));
^
required: Block<? super String>
found: lambda
reason: incompatible return type void in lambda expression
where T is a …Run Code Online (Sandbox Code Playgroud) 为什么要尝试编译
public class GenericsFail {
public static void main(String[] args) {
accept(new HashMap<String, List<String>>());
}
public static void accept(Map<String, List<?>> multiMap) {}
}
Run Code Online (Sandbox Code Playgroud)
给出错误
GenericsFail.java:7: error: method accept in class GenericsFail cannot be applied to given types;
accept(new HashMap<String, List<String>>());
^
required: Map<String,List<?>>
found: HashMap<String,List<String>>
reason: actual argument HashMap<String,List<String>> cannot be converted to Map<String,List<?>> by method invocation conversion
Run Code Online (Sandbox Code Playgroud)
仅当通配符未嵌套在内时才允许使用通配符List.
我已经阅读了文档,但我仍然不明白我应该提供哪个类加载器作为参数.我尝试了一些选项,但这似乎对编译或代理行为没有影响.我可以将任何东西作为类加载器参数传递,包括null,并且代码仍然可以正常工作,这有点令人不安.任何人都可以解释这一点,并告诉我如果我为类加载器提供一个错误的参数会出现什么样的错误?我应该补充一点,我对Java或一般的类加载器没有一个非常直观的想法.
我正在寻找一种将环境变量传递到货物集装箱的方法.像这样的东西:
<plugin>
<groupId>org.codehaus.cargo>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<environmentVariables>
<myCustomVariable>value</myCustomVariable>
...
Run Code Online (Sandbox Code Playgroud) 我有一组运行的集成测试SpringJUnit4ClassRunner.我正在尝试使用maven surefire并行运行这些.但是,我注意到在进入synchronized块之前代码是阻塞的CacheAwareContextLoaderDelegate.loadContext().
有没有办法绕过这个缓存?我尝试这样做,但似乎有更多的共享状态而不仅仅是缓存本身,因为我的应用程序在Spring代码中死锁.或者可以通过以某种方式同步地图键而不是整个地图来使同步更细粒度?
我将测试并行化的动机是双重的:
@DirtiesContext.MergedContextConfiguration在上下文缓存中使用了密钥,因此这些测试将无法共享ApplicationContexts.如果我想使用Spring 3的基于Java的配置声明bean,我可以这样做:
@Configuration
public class MyConfiguration {
@Bean
public MyRepository myRepository() {
return new MyJpaRepository();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,由于我不能@Repository在此上下文中使用注释,如何让Spring执行异常转换?
例如Integer foo = 4,Long foo = 4L两者都编译,但Long foo = 4没有.这有什么理由吗?
我有一个Grizzly HttpServer,我想在整个测试组执行期间运行.另外,我想从@Rule测试本身内部与全局HttpServer实例进行交互.
由于我使用Maven Surefire而不是使用JUnit测试套件,因此我不能在测试套件上使用@BeforeClass/ @AfterClass.
现在,我能想到的只是懒洋洋地初始化静态字段并停止服务器Runtime.addShutdownHook()- 不好!
我知道可以使用直接序列化为String XmlMapper.writeValueAsString(),但我想序列化一个DOM树.要么是新文档,要么最好是直接序列化到现有DOM节点.这可以和杰克逊一起完成吗?
java ×8
generics ×2
spring ×2
autoboxing ×1
classloader ×1
dom ×1
eclipse ×1
grizzly ×1
jackson ×1
java-8 ×1
junit ×1
lambda ×1
maven ×1
maven-cargo ×1
reflection ×1
repository ×1
xml ×1