我是 Java EE 的新手,当我查看编译后的代码(我找不到 javax:javaee-api-6.0 的源代码)时,我注意到了这个类。
package javax.servlet;
import java.util.EventObject;
public class ServletContextEvent extends EventObject
{
public ServletContextEvent(ServletContext paramServletContext);
public ServletContext getServletContext();
}
但是javax:javaee-api-7.0中的同一个类就是这个
package javax.servlet;
import java.util.EventObject;
public class ServletContextEvent extends EventObject
{
private static final long serialVersionUID = -7501701636134222423L;
public ServletContextEvent(ServletContext source)
{
super(source);
}
public ServletContext getServletContext()
{
return (ServletContext)super.getSource();
}
}
在同一个包中也发生了 ServletException(可能还有更多,因为我没有逐一检查它们)。
假设 Java Decompiler 给了我源代码的样子,从纯 java 语法的角度来看,我无法理解为什么 6.0 类不是抽象的(或者,不是接口(interface))。
问题1.为什么6.0中的类不是抽象的也不是接口(interface)的?
问题 2. 为什么 7.0 中的实现发生了变化?人们是否意识到 6.0 版本在使用 javaee-api 编译代码时会造成麻烦?
我问的原因是因为在 Intellij IDEA (12.1.4) 中使用 javaee-web-api(它具有与 javaee-api 类似的类,请参阅 this)时实际上遇到了编译错误。错误看起来像这样:
Internal error: (java.lang.ClassFormatError) Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletContextEvent
那么问题3.有没有办法在Intellij IDEA中避免这种情况?
最佳答案
Did people realize the 6.0 version would cause trouble when you compile code with javaee-api?
是的,其中一个问题是您不能在运行测试时使用这些类。参见 this Arquillian FAQ和 Adam Bien's blog post .
Is there a way to avoid this in Intellij IDEA?
请参阅上面的链接。该解决方案并非特定于 Intellij IDEA。
关于javax :javaee-api-6. 0 包含没有方法体的非抽象类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20125115/