问题描述
我必须调用通用的AWS Batch Meter API.当我调用此函数时,出现错误.请帮助如何调用通用API
I have to call AWS batch meter API which is generic. When I am calling this function I am getting an error. Please help how to call a generic API
public <T> Optional<T> getValueForField(String fieldName, Class<T> clazz) {
byte var4 = -1;
switch(fieldName.hashCode()) {
case -1532767274:
if (fieldName.equals("Results")) {
var4 = 0;
}
break;
case 743387085:
if (fieldName.equals("UnprocessedRecords")) {
var4 = 1;
}
}
switch(var4) {
case 0:
return Optional.ofNullable(clazz.cast(this.results()));
case 1:
return Optional.ofNullable(clazz.cast(this.unprocessedRecords()));
default:
return Optional.empty();
}
}
public List<UsageRecordResult> results() {
return this.results;
}
通过API调用
batchMeterUsageResponse.getValueForField(RESULT, (Class<List<UsageRecordResult>>) new ArrayList<>().getClass());
我遇到错误:
Error:(98, 175) java: incompatible types: java.lang.Class<capture#1 of ? extends java.util.ArrayList> cannot be converted to java.lang.Class<java.util.List<software.amazon.awssdk.services.marketplacemetering.model.UsageRecordResult>>
推荐答案
TL; DR —如果我对您的代码意图的假设是正确的,则 此处展示了最简单的解决方案 …
...
Optional< List > wtTF = batchMeterUsageResponse.getValueForField( RESULTS, List.class );
...
要执行上面显示的代码,请单击绿色的 开始 按钮 此IDE顶部 .
To execute the code shown above, click the green Start button at the top of this IDE.
详细答案
BatchMeterUsageResult
具有 getResults()
方法和 getUnprocessedRecords()
方法看起来像您在其中使用的相同方法你的问题.因此,我上面建议的修复程序假设您的示例代码旨在返回 List<UsageRecordResult>
或 List<UsageRecord>
;取决于 fieldName.hashCode
的值.
BatchMeterUsageResult
has a getResults()
method and a getUnprocessedRecords()
method that look like the same methods you have in your question. So my proposed fix above assumes that the intention of your sample code, is to return either a List<UsageRecordResult>
or a List<UsageRecord>
; depending on the value of fieldName.hashCode
.
Error:(98, 175) java: incompatible types: java.lang.Class<capture#1 of ? extends java.util.ArrayList> cannot be converted to java.lang.Class<java.util.List<software.amazon.awssdk.services.marketplacemetering.model.UsageRecordResult>>
错误消息告诉您,您尝试从一种类型的 Class<List<E>>
转换为另一种类型的 Class<List<E>>
失败.
The error message is telling you that your attempt to cast from one type of Class<List<E>>
to a different type of Class<List<E>>
, failed.
编译器知道 new ArrayList<>().getClass()
的 Class
类型为 Class<capture#1 of ? extends java.util.ArrayList>
.您正在尝试将其投射到 Class<List<UsageRecordResult>>
.
The compiler knows the Class
type of new ArrayList<>().getClass()
to be Class<capture#1 of ? extends java.util.ArrayList>
. You are trying to cast that to Class<List<UsageRecordResult>>
.
由于两种 Class
类型不兼容,因此编译器无法进行转换.它们不兼容的根本原因是因为 List<UsageRecordResult>
不符合以下条件: extends java.util.ArrayList
.请记住:接口 List<E>
是 ArrayList<E>
的超类.如您所知,不可能将超类分配给子类.
The compiler cannot do the conversion because the two Class
types are incompatible. The underlying reason they are incompatible is because List<UsageRecordResult>
does not meet the criteria of: extends java.util.ArrayList
. Remember: The interface List<E>
is the superclass of ArrayList<E>
. And as you know, it's impossible to assign a superclass to a subclass.
但这只是代码问题的第一部分.其他问题的细节可能不如上述使代码编译的解决方案那么有趣.
But that's only the first part of the problem with your code. The details of the other problems probably won't be as interesting to you as the above solution to make your code compile.
如果我对您的代码意图的假设不正确,那么请编辑您的问题,并提供对代码实际意图的更清晰的解释? TIA.
If my assumption about your code's intention is incorrect, then please edit your question with a clearer explanation of the actual intention of your code? TIA.
这篇关于调用AWS Batch Meter通用API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!