我已经切换到最新的JDK 7,我遇到了在emma覆盖工具打乱的字节代码上运行testng单元测试的问题.我的测试用例都没有正确运行,对于大多数测试用例我都收到了这样的错误.
java.lang.ClassFormatError: Illegal local variable table length 10 in method measurement.meter.AbstractSerialPortMeter.<init>(Lmeasurement/meter/SerialPort;)V at measurement.meter.Elc3133aTest.setUp(Elc3133aTest.java:42)
Run Code Online (Sandbox Code Playgroud)
我在这里找到了一篇文章JSR 292 Goodness Fast Code Coverage Tool Less 10k,它说"JSR 292引入了一个新的字节码指令invokedynamic,但也有几种新的常量池常量.这意味着大多数解析字节码的工具都像ASM,BCEL,findbugs或EMMA需要更新为兼容java 7."
检查了艾玛的主页,但看起来它已经很久没有更新了.
有人解决了类似的问题吗?
我也曾尝试过Cobertura.它看起来工作得更好但我得到了很多类型的例外VerifyError.
java.lang.VerifyError: Expecting a stackmap frame at branch target 85 in method measurement.meter.AbstractSerialPortMeter.close()V at offset 26
at measurement.meter.AbstractSerialPortMeterTest.setUp(AbstractSerialPortMeterTest.java:27)
Run Code Online (Sandbox Code Playgroud) class Test {
public static void main(String...args) {
String s1 = "Good";
s1 = s1 + "morning";
System.out.println(s1.intern());
String s2 = "Goodmorning";
if (s1 == s2) {
System.out.println("both are equal");
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码在Java 6和Java 7中生成不同的输出.在Java 6中,s1==s2条件返回false并在Java 7中s1==s2返回true.为什么?
为什么这个程序在Java 6和Java 7中产生不同的输出?
好的,所以在Java 7中我们有
o.hashCode();
Objects.hashCode(o);
Objects.hash(o);
Run Code Online (Sandbox Code Playgroud)
前两个与零点检查大致相同,但最后一个是什么?
提供单个对象引用时,返回的值不等于该对象引用的哈希代码.
这是为什么?我的意思是,我们不需要3种做同样事情的方法,我理解......但为什么我们需要Objects.hash()呢?你什么时候选择使用一个与另一个?
有人可以解释一下,为什么在JDK 7中添加了这个功能以及它是如何工作的?
在浏览JDK 7新功能时,我发现了以下代码.
int i;
//Java 7 allows underscore in integer
i=3455_11_11;
Run Code Online (Sandbox Code Playgroud) 我安装了JDK 7和Eclipse 3.6M6.然后,我在Eclipse中添加了JRE 7作为新的JRE执行环境,并将编译器合规性级别设置为Java 7.我可以使用javacJDK 7附带的命令行编译以下代码.
import java.util.HashMap;
import java.util.Map;
public class Try {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,Eclipse提供了以下错误消息.
类型HashMap的参数数量不正确; 它不能参数化参数Try.java/TryJava7/src第7行Java问题
令牌"<"上的语法错误,?预计在此令牌后Try.java/TryJava7/src第7行Java问题
尽管我已将编译器的合规性级别设置为Java 7,但看起来Eclipse还不了解Java7语法.是否有可能在Eclipse中使用Java 7?
以下是内容.classpath.
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Run Code Online (Sandbox Code Playgroud)
而且,以下是内容.settings/org.eclipse.jdt.core.prefs.
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
Run Code Online (Sandbox Code Playgroud) 我正在寻找Java7的新功能.我发现一个是 try-with-resources Statement.任何人都可以告诉我它究竟意味着什么?我们应该使用它的原因和位置,以及我们可以利用此功能的地方?即使是try声明也没有catch阻止让我感到困惑.
Java 7中的JCombobox已经更新为使用泛型 - 我一直认为它有点疏忽,它还没有,所以我很高兴看到这种变化.
但是,当试图以这种方式使用JCombobox时,我意识到我期望使用这些泛型类型的方法仍然只是返回Object.
这究竟是为什么?对我来说,这似乎是一个愚蠢的设计决定.我意识到底层的ListModel有一个通用的getElementAt()方法,所以我会用它来代替 - 但它有点迂回的做法,看起来它可能在JComboBox本身上被改变了.
我想打包maven-(多)模块,父POM包括:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
我正在使用Java 1.7,属性指定如下:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<slf4j.version>1.6.1</slf4j.version>
</properties>
Run Code Online (Sandbox Code Playgroud)
Maven版本是2.2.1:
johannes@luna:~/workspace/treetank/bundles/treetank-core$ mvn -version
Apache Maven 2.2.1 (rdebian-6)
Java version: 1.7.0
Java home: /usr/lib/jvm/jdk1.7.0/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux" version: "3.0.0-14-generic" arch: "amd64" Family: "unix"
Run Code Online (Sandbox Code Playgroud)
我不知道为什么它不使用Java版本1.7.在调用时,mvn package我得到错误(例如,使用-source 7或更高版本来启用菱形运算符).你知道它为什么尝试使用1.6吗?
有效的POM是:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</execution>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding> …Run Code Online (Sandbox Code Playgroud) 使用检测的JDK7编译代码存在已知的兼容性问题.至于http://www.oracle.com/technetwork/java/javase/compatibility-417013.html
版本号为51的类文件使用类型检查验证程序进行独占验证,因此这些方法在适当时必须具有StackMapTable属性.对于版本为50的类文件,如果文件中的堆栈映射丢失或不正确,Hotspot JVM将(并继续)故障转移到类型推断验证程序.对于版本为51的类文件(Java SE 7的默认版本),不会发生此故障转移行为.修改版本51类文件中的字节码的任何工具必须确保更新stackmap信息以与字节码一致以通过验证.
解决方案是使用-XX:-UseSplitVerifier如下所述:https:
//community.oracle.com/blogs/fabriziogiudici/2012/05/07/understanding-subtle-new-behaviours-jdk-7
它有多安全?我想Oracle已将此检查置于原因之中.如果我不使用它,我可能会冒一些其他问题.
使用后果可能是什么-XX:-UseSplitVerifier?
谢谢,
彼得.