0

I've read several questions that are similar to mine, but I still don't undestand what's happening, or why it only happens for the (int) and not for the other more complex types:

    HashMap<?, ?> returncp = startAndWaitForJob("DSP_WF_" + operation, cp);

    DSPResponseDTO dto = (DSPResponseDTO) returncp.get("RESPONSE_OBJECT");
    String respDesc = (String) returncp.get("statusInfoResponseDescription");
    int respCode = (int) returncp.get("statusInfoResponseCode");

The compilation error:

[javac]         int respCode = (int) returncp.get("statusInfoResponseCode");
[javac]                                             ^
[javac]   required: int
[javac]   found:    CAP#1
[javac]   where CAP#1 is a fresh type-variable:
[javac]     CAP#1 extends Object from capture of ?

The questions already perused:

incompatible types and fresh type-variable

Bounded-wildcard related compiler error

Java: Wildcard Types Mismatch Results in Compilation Error

Community
  • 1
  • 1
WurmD
  • 1,231
  • 5
  • 21
  • 42
  • can you give jsfiddle or any other link for your program code? – legend Oct 13 '16 at 18:01
  • You're trying to assign an `int` to a `String`? – Kayaman Oct 13 '16 at 18:12
  • @Kayaman sigh.. sorry it was late and I copy errored here. It's int respCode = (int) returncp.get("statusInfoResponseCode"); and the error is exactly the same – WurmD Oct 14 '16 at 10:39
  • I think `HashMap, ?>` does not work as you expect. What happends when you write `HashMap` or just `HashMap` instead? – Ben Oct 14 '16 at 11:37

2 Answers2

0

I do not know what types are in your map, but here

String respCode = (int) returncp.get("statusInfoResponseCode");

you cast the result of the getcall to an intand assign it to a String.

If it is an Integer, use

Integer respCode = (Integer) returncp.get("statusInfoResponseCode");
P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66
  • sigh.. sorry it was late and I copy errored here. It's int respCode = (int) returncp.get("statusInfoResponseCode"); and the error is exactly the same. If you'd delete this answer it would be appreaciated – WurmD Oct 14 '16 at 10:40
  • @WurmD Did you at least try casting to `Integer`? – Kayaman Oct 14 '16 at 10:40
0

First what is CAP#1

I found this quote:

What on earth does "capture#337 of ?" mean? When the compiler encounters a variable with a wildcard in its type, such as the box parameter of rebox(), it knows that there must have been some T for which box is a Box. It does not know what type T represents, but it can create a placeholder for that type to refer to the type that T must be. That placeholder is called the capture of that particular wildcard. In this case, the compiler has assigned the name "capture#337 of ?" to the wildcard in the type of box. Each occurrence of a wildcard in each variable declaration gets a different capture, so in the generic declaration foo(Pair x, Pair y), the compiler would assign a different name to the capture of each of the four wildcards because there is no relationship between any of the unknown type parameters.

Now let's go back to the message CAP#1 extends Object from capture of ?

It considers the CAP#1 as an instance of Object, more like an instance of a class extending the class Object.

Now one need to imagine that the generic are transform into cast at compile time. Which means that:

HashMap<?, ?> map = new HashMap();
int test = (int) map.get("");

is more or less the equivalent of:

HashMap<?, ?> map = new HashMap();
int test = (int) (Object) map.get("");

Which is not possible (or allowed by the compiler) to cast an Object into an 'int'. Of course, this Object is used to make it simple to represent.

But it is totally possible to cast an Object into a 'String' or an 'Integer'.

So what you could do is:

HashMap<?, Integer> returncp = startAndWaitForJob("DSP_WF_" + operation, cp);

In that case you will not even need to cast anything (see).

But if that is not possible for any reason:

int respCode = ((Integer) returncp.get("statusInfoResponseCode")).intValue();
Loïc
  • 581
  • 3
  • 8