Call AWS Batch meter Generic API.

huangapple go评论84阅读模式
英文:

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 &lt;T&gt; Optional&lt;T&gt; getValueForField(String fieldName, Class&lt;T&gt; clazz) {
        byte var4 = -1;
        switch(fieldName.hashCode()) {
        case -1532767274:
            if (fieldName.equals(&quot;Results&quot;)) {
                var4 = 0;
            }
            break;
        case 743387085:
            if (fieldName.equals(&quot;UnprocessedRecords&quot;)) {
                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&lt;UsageRecordResult&gt; results() {
        return this.results;
    }

Calling Above API

batchMeterUsageResponse.getValueForField(RESULT, (Class&lt;List&lt;UsageRecordResult&gt;&gt;) new ArrayList&lt;&gt;().getClass());

Error i am getting :

Error:(98, 175) java: incompatible types: java.lang.Class&lt;capture#1 of ? extends java.util.ArrayList&gt; cannot be converted to java.lang.Class&lt;java.util.List&lt;software.amazon.awssdk.services.marketplacemetering.model.UsageRecordResult&gt;&gt;

答案1

得分: 1

TL;DR — 如果我对你的代码意图的假设正确,那么最简单的解决方案在这里演示 &hellip;

...
Optional< List > wtTF = batchMeterUsageResponse.getValueForField( RESULTS, List.class );
...

要执行上面显示的代码,请单击顶部的绿色Start按钮在此IDE的顶部


详细回答

BatchMeterUsageResult具有getResults()方法和getUnprocessedRecords()方法,看起来与你问题中的方法相同。因此,我上面提出的修复方案假定你示例代码的意图是根据fieldName.hashCode的值返回List&lt;UsageRecordResult&gt;List&lt;UsageRecord&gt;之一。

> „我遇到的错误:

> 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&lt;List&lt;E&gt;&gt;的对象转换为不同类型的Class&lt;List&lt;E&gt;&gt;时失败了。

编译器知道new ArrayList&lt;&gt;().getClass()Class类型是Class&lt;capture#1 of ? extends java.util.ArrayList&gt;。你试图将其转换为Class&lt;List&lt;UsageRecordResult&gt;&gt;

编译器无法进行转换,因为这两个Class类型是不兼容的。它们不兼容的根本原因是List&lt;UsageRecordResult&gt;不符合extends java.util.ArrayList的条件。请记住:接口List&lt;E&gt;ArrayList&lt;E&gt;超类。而且如你所知,不可能将超类分配给子类。

但这只是你的代码问题的第一部分。其他问题的细节可能对你来说不像上面的解决方案那么有趣。

如果我对你的代码意图的假设不正确,请编辑你的问题以更清晰地解释你的代码的实际意图。谢谢。

英文:

TL;DR — If my assumption about the intention of your code is correct, then the simplest solution is demonstrated here&hellip;

...
Optional&lt; List &gt; 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&lt;UsageRecordResult&gt; or a List&lt;UsageRecord&gt;; 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&lt;List&lt;E&gt;&gt; to a different type of Class&lt;List&lt;E&gt;&gt;, failed.

The compiler knows the Class type of new ArrayList&lt;&gt;().getClass() to be Class&lt;capture#1 of ? extends java.util.ArrayList&gt;. You are trying to cast that to Class&lt;List&lt;UsageRecordResult&gt;&gt;.

The compiler cannot do the conversion because the two Class types are incompatible. The underlying reason they are incompatible is because List&lt;UsageRecordResult&gt; does not meet the criteria of: extends java.util.ArrayList. Remember: The interface List&lt;E&gt; is the superclass of ArrayList&lt;E&gt;. 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.

huangapple
  • 本文由 发表于 2020年8月14日 00:23:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63399325.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定