gpt4 book ai didi

java - 类型参数不在其范围内

转载 作者:搜寻专家 更新时间:2023-11-01 03:36:07 28 4
gpt4 key购买 nike

在此类上运行 maven compile 时出现错误,但在执行时似乎工作正常。我已经看过很多关于同一主题的其他帖子,但我自己却无法做到这一点。

编辑 - 删除原始代码片段并替换

编辑 - 提供 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 和子类:

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>>();
}
}

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project Test: Compilation failure: Compilation failure: [ERROR]...\src\main\java\common\main\MainTester.java:[14,18] type parameter common.mapping.rule.MappingRule is not within its bound [ERROR]...\src\main\java\common\handler\MappingProtocolHandler.java:[10,60] type parameter common.mapping.rule.MappingRule is not within its bound

最佳答案

我认为你不应该使用原始类型的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

编辑2:运行这段代码时我总是出错。你如何编译这段代码?也许 eclipse 编译器(?)允许您运行这段代码?

编辑3:根据问题作者的说法,该代码仅在 Eclipse 中运行。有时 eclipse 编译器的行为似乎有所不同,但在那种情况下,您应该将 JDK 视为命令式的(因为您的构建不是在 eclipse 中完成的,而是在带有 javac 的 maven 中完成的)。我不确定原因(当然可以在 JLS 中搜索),但为什么要混合类型边界和原始类型,这些类型只是为了与 1.5 之前的 Java 版本向后兼容而引入的?

eclipse 和 javac 之间的泛型似乎还有其他差异:Maven Compiler vs Eclipse Compiler Generics Difference?

关于java - 类型参数不在其范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31030814/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com