英文:
Call AWS Batch meter Generic 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;
}
Calling Above API
batchMeterUsageResponse.getValueForField(RESULT, (Class<List<UsageRecordResult>>) new ArrayList<>().getClass());
Error I am getting:
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>>
英文:
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;
}
Calling Above API
batchMeterUsageResponse.getValueForField(RESULT, (Class<List<UsageRecordResult>>) new ArrayList<>().getClass());
Error i am getting :
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>>
答案1
得分: 1
TL;DR — 如果我对你的代码意图的假设正确,那么最简单的解决方案在这里演示 …
...
Optional< List > wtTF = batchMeterUsageResponse.getValueForField( RESULTS, List.class );
...
要执行上面显示的代码,请单击顶部的绿色Start按钮在此IDE的顶部。
详细回答
BatchMeterUsageResult
具有getResults()
方法和getUnprocessedRecords()
方法,看起来与你问题中的方法相同。因此,我上面提出的修复方案假定你示例代码的意图是根据fieldName.hashCode
的值返回List<UsageRecordResult>
或List<UsageRecord>
之一。
> „我遇到的错误:“
> 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>>
时失败了。
编译器知道new ArrayList<>().getClass()
的Class
类型是Class<capture#1 of ? extends java.util.ArrayList>
。你试图将其转换为Class<List<UsageRecordResult>>
。
编译器无法进行转换,因为这两个Class
类型是不兼容的。它们不兼容的根本原因是List<UsageRecordResult>
不符合extends java.util.ArrayList
的条件。请记住:接口List<E>
是ArrayList<E>
的超类。而且如你所知,不可能将超类分配给子类。
但这只是你的代码问题的第一部分。其他问题的细节可能对你来说不像上面的解决方案那么有趣。
如果我对你的代码意图的假设不正确,请编辑你的问题以更清晰地解释你的代码的实际意图。谢谢。
英文:
TL;DR — If my assumption about the intention of your code is correct, then the simplest solution is demonstrated here…
...
Optional< List > wtTF = batchMeterUsageResponse.getValueForField( RESULTS, List.class );
...
To execute the code shown above, click the green Start button at the top of this IDE.
The long answer
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 i am getting :“
> 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>>
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.
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>>
.
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.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论