我的 java 应用程序遇到一个非常奇怪的问题。我正在使用 Maven 构建它并在 Eclipse IDE 中开发它。这可能是一个有点冗长的解释,但请坚持到底,因为这个问题真的很奇怪,我不知道是什么原因造成的。
这是我正在编写的代码示例:
假设我们有一个“Handler”接口(interface)。它可以处理特定的对象类型:
public interface Handler<T> {
public void handle(T obj);
}
现在假设我们想要处理程序链接。我们可以这样做:
public class HandlerChain<T> implements Handler<T> {
private Handler<? super T> h;
@Override
public void handle(T obj) {
//h can handle T objects
h.handle(obj);
}
private HandlerChain(Handler<? super T> h) {
super();
this.h = h;
}
//syntax sugar to start the chain
public static <T> HandlerChain<T> start(Handler<? super T> h){
return new HandlerChain<T>(h);
}
//add another handler to the chain
public HandlerChain<T> next(final Handler<? super T> handler){
return new HandlerChain<T>(new Handler<T>() {
@Override
public void handle(T obj) {
h.handle(obj);
handler.handle(obj);
}
});
}
}
现在让我们为字符串处理程序创建一些处理程序工厂:
public class Handlers {
public static Handler<String> h1(){
return new Handler<String>(){
@Override
public void handle(String obj) {
// do something
}
};
}
public static Handler<String> h2(){
return new Handler<String>(){
@Override
public void handle(String obj) {
// do something
}
};
}
}
最后我们创建一个类,使用链中的两个处理程序来处理一些字符串:
public class Test {
public void doHandle(String obj){
HandlerChain.start(Handlers.h1()).next(Handlers.h2()).handle(obj);
}
}
所以,对我来说这段代码似乎没有任何问题。 Eclipse IDE 也不介意。它甚至正确地运行了它。但是当我尝试从 cli 使用 maven 编译此代码时,出现错误:
Test.java:[7,50] next(Handler<? super java.lang.Object>) in HandlerChain<java.lang.Object> cannot be applied to (Handler<java.lang.String>)
有人遇到过类似的问题吗?我真的很想知道这种语法在java中是否有效,或者这是由于错误的设置或其他原因导致的一些奇怪的编译器错误?重复 Eclipse 编译并正确运行此代码,但 maven cli 无法编译它。
最后,这是我在 pom.xml 中的 maven-compiler-plugin 设置。它可能与整个问题相关。
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
</executions>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
提前致谢!
最佳答案
Had anyone stumbled upon similar problems?
是的,当使用泛型类型做奇怪和不寻常(可能不正确)的事情时也是如此。
问题是 Eclipse 编译器不会报告这些奇怪构造的编译错误,而 Sun JDK 中的标准 javac
则提示类型删除。 (它是使用 JDK 1.6 的)。 (如果我没记错的话:eclipse只报告一个警告)
我的解决方案是设置maven来使用eclipse编译器。另一个(更好的)选项是修复代码,但由于这是一项相当复杂的任务,而且我在运行时没有遇到任何问题,所以我选择了“快速而肮脏”的第一个选项。
以下是如何设置编译器:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${compiler.source}</source>
<target>${compiler.target}</target>
<encoding>${source.encoding}</encoding>
<fork>false</fork>
<compilerId>jdt</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-jdt</artifactId>
<version>0.13.0</version>
</dependency>
</dependencies>
</plugin>
关于java - Maven cli 给出语法错误,但在 eclipse 中没问题。 java语法正确吗?真奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15819898/