我在这个类上运行maven编译时遇到错误,但在执行时似乎工作得很好。我在同一主题上看了很多其他帖子,但却无法为自己工作。
编辑 - 删除原始代码段并替换
编辑 - 提供MCVE - 非常精简
顺便说一下,这是在Java 6上运行
public class MainTester {
@SuppressWarnings("rawtypes")
public static void main(String[] args) {
ProtocolHandler<MappingRule> mappingProtocolHandler = new MappingProtocolHandler();
Map<String, List<MappingRule>> protocols = mappingProtocolHandler.getProtocols();
System.out.println(protocols);
}
}
ProtocolHandler和Child类:
public abstract class ProtocolHandler<E extends Rule<?, ?>> {
public Map<String, List<E>> getProtocols() {
return getProtocolsForRequest();
}
protected abstract Map<String, List<E>> getProtocolsForRequest();
}
@SuppressWarnings("rawtypes")
public class MappingProtocolHandler extends ProtocolHandler<MappingRule>{
@Override
protected Map<String, List<MappingRule>> getProtocolsForRequest() {
return new HashMap<String, List<MappingRule>>();
}
}
规则类:
public interface Rule<F, T> {
public void execute(F object, T object2);
}
public abstract class BaseRule<F, T> implements Rule<F, T>, Comparable<BaseRule<F, T>> {
}
public abstract class MappingRule<F,T> extends BaseRule<F, T> implements CustomAttributes{
}
public abstract class InputRule extends MappingRule<Object, Map<String,Object>> {
}
public abstract class OutputRule extends MappingRule<Map<String, Object>, Object> {
}
public interface CustomAttributes {
}
处理程序类:
public abstract class ProtocolHandler<E extends Rule<?, ?>> {
public Map<String, List<E>> getProtocols() {
return getProtocolsForRequest();
}
protected abstract Map<String, List<E>> getProtocolsForRequest();
}
@SuppressWarnings("rawtypes")
public class MappingProtocolHandler extends ProtocolHandler<MappingRule>{
@Override
protected Map<String, List<MappingRule>> getProtocolsForRequest() {
return new HashMap<String, List<MappingRule>>();
}
}
[错误]无法执行目标 org.apache.maven.plugins:Maven的编译器插件:2.5.1:编译 项目上的(默认编译)测试:编译失败:编译 失败: [错误] ... \ SRC \主\ java的\ COMMON \主\ MainTester.java:[14,18] 类型参数common.mapping.rule.MappingRule不在其范围内 [错误] ... \ SRC \主\ java的\ COMMON \处理器\ MappingProtocolHandler.java:[10,60] 类型参数common.mapping.rule.MappingRule不在其绑定范围内
答案 0 :(得分:1)
我认为你不应该使用原始类型的MappingRule
private ProtocolHandler<MappingRule> mappingProtocolHandler;
public void execute(){
Map<String, List<MappingRule>> protocols = mappingProtocolHandler.getProtocols(protocolExecutionRequest);
}
但是
private ProtocolHandler<MappingRule<String, String>> mappingProtocolHandler;
public void execute(){
Map<String, List<MappingRule<String, String>>> protocols = mappingProtocolHandler.getProtocols(/**/);
}
编辑: 如果只有maven给你一个错误,也许它被配置为将警告视为错误?见Javac: Treat warnings as errors
EDIT2: 运行此代码时总是会出错。你如何编译这段代码?也许eclipse编译器(?)允许你运行这个代码?
EDIT3: 根据问题的作者,代码只在eclipse中运行。有时eclipse编译器的行为似乎有所不同,但在这种情况下你应该将JDK视为必要(因为你的构建不是在eclipse中完成的,而是在使用javac的maven中)。我不确定原因(当然可以搜索JLS),但是为什么混合类型边界和原始类型只是为了在1.5之前的Java版本向后兼容而引入?
eclipse和javac之间的泛型似乎存在其他差异:Maven Compiler vs Eclipse Compiler Generics Difference?