Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

UNIX enhancements aren't.


devel / comp.lang.java.programmer / Why does Java complain if you cast a Map but not an Object?

SubjectAuthor
* Why does Java complain if you cast a Map but not an Object?e.d.pro...@gmail.com
`* Why does Java complain if you cast a Map but not an Object?Arne_Vajhøj
 `* Why does Java complain if you cast a Map but not an Object?e.d.pro...@gmail.com
  `* Why does Java complain if you cast a Map but not an Object?Arne_Vajhøj
   `- Why does Java complain if you cast a Map but not an Object?Michael Jung

1
Why does Java complain if you cast a Map but not an Object?

<632f38ec-674f-44ad-b8d4-53aa32534c09n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=235&group=comp.lang.java.programmer#235

  copy link   Newsgroups: comp.lang.java.programmer
X-Received: by 2002:a05:6214:1924:b0:5e6:4193:996f with SMTP id es4-20020a056214192400b005e64193996fmr2953540qvb.9.1681917415882;
Wed, 19 Apr 2023 08:16:55 -0700 (PDT)
X-Received: by 2002:a81:ae0b:0:b0:545:6106:5334 with SMTP id
m11-20020a81ae0b000000b0054561065334mr2187496ywh.8.1681917415615; Wed, 19 Apr
2023 08:16:55 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.java.programmer
Date: Wed, 19 Apr 2023 08:16:55 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=2601:980:8281:72b0:5959:28b2:47a5:9b6e;
posting-account=2czF5goAAAD4GBMPIGV4KcD2K4PhoB_H
NNTP-Posting-Host: 2601:980:8281:72b0:5959:28b2:47a5:9b6e
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <632f38ec-674f-44ad-b8d4-53aa32534c09n@googlegroups.com>
Subject: Why does Java complain if you cast a Map but not an Object?
From: e.d.programmer@gmail.com (e.d.pro...@gmail.com)
Injection-Date: Wed, 19 Apr 2023 15:16:55 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1719
 by: e.d.pro...@gmail.com - Wed, 19 Apr 2023 15:16 UTC

@SuppressWarnings("unchecked")
public static Map<MyObject,MyObject> testObjectUnchecked(Object in) {
return (Map<MyObject,MyObject>) in; // warning
} public static Map<MyObject,MyObject> testObjectChecked(Object in) {
return testMap(in); // no warning
} public static Map<MyObject,MyObject> testMap(Object in) {
if (in instanceof Map) {
Map<?,?> m1 = (Map<?,?>)in;
Map<MyObject,MyObject> m2 = new HashMap<>();
for (Entry<?,?> e : m1.entrySet()) {
m2.put((MyObject)e.getKey(),(MyObject)e.getValue()); // no warning
}
return m2;
}
return Collections.<MyObject,MyObject>emptyMap();
}

Re: Why does Java complain if you cast a Map but not an Object?

<u1p97t$4nqi$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=236&group=comp.lang.java.programmer#236

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: arne@vajhoej.dk (Arne Vajhøj)
Newsgroups: comp.lang.java.programmer
Subject: Re: Why does Java complain if you cast a Map but not an Object?
Date: Wed, 19 Apr 2023 13:43:26 -0400
Organization: A noiseless patient Spider
Lines: 94
Message-ID: <u1p97t$4nqi$1@dont-email.me>
References: <632f38ec-674f-44ad-b8d4-53aa32534c09n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 19 Apr 2023 17:43:26 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e68d6b2e7704051c3dc3f19203fb0784";
logging-data="155474"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+88QXymMx7RtB5OT8XGSTs1EDs1GcI/g="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.10.0
Cancel-Lock: sha1:AffVjUfpSAJWfBPA2VOAveGQy9A=
In-Reply-To: <632f38ec-674f-44ad-b8d4-53aa32534c09n@googlegroups.com>
Content-Language: en-US
 by: Arne Vajhøj - Wed, 19 Apr 2023 17:43 UTC

On 4/19/2023 11:16 AM, e.d.pro...@gmail.com wrote:
> @SuppressWarnings("unchecked")
> public static Map<MyObject,MyObject> testObjectUnchecked(Object in) {
> return (Map<MyObject,MyObject>) in; // warning
> }
> public static Map<MyObject,MyObject> testObjectChecked(Object in) {
> return testMap(in); // no warning
> }
> public static Map<MyObject,MyObject> testMap(Object in) {
> if (in instanceof Map) {
> Map<?,?> m1 = (Map<?,?>)in;
> Map<MyObject,MyObject> m2 = new HashMap<>();
> for (Entry<?,?> e : m1.entrySet()) {
> m2.put((MyObject)e.getKey(),(MyObject)e.getValue()); // no warning
> }
> return m2;
> }
> return Collections.<MyObject,MyObject>emptyMap();
> }

I think it makes sense.

(MyObject)e.getKey() and (MyObject)e.getValue() will work
fine if they are a MyObject and throw a ClassCastException
if they are not a MyObject.

(Map<MyObject,MyObject>)in is worse:
- it will work if it is a Map<MyObject,MyObject>
- it will throw a ClassCastException it is not a Map
- it will seemingly work but likely cause a ClassCastException
at some later point if it is a Map<X,Y> but not a
Map<MyObject,MyObject>

Try this example using List instead of Map:

import java.util.ArrayList;
import java.util.List;

public class ToWarnOrNotToWarn {
private static String test1(Object o) {
try {
return (String)o;
} catch(ClassCastException ex) {
System.out.println(o.getClass().getName() + " is not a
String");
return null;
}
}
private static List<String> test2(Object o) {
try {
return (List<String>)o;
} catch(ClassCastException ex) {
System.out.println(o.getClass().getName() + " is not a
List<String>");
return null;
}
}
public static void main(String[] args) {
Object o1 = "ABC";
System.out.println(test1(o1));
Object o2 = 123;
System.out.println(test1(o2));
System.out.println(test2(true));
List<String> a1 = new ArrayList<String>();
a1.add("ABC");
System.out.println(test2(a1));
System.out.println(test2(a1).get(0).substring(0, 1));
List<Integer> a2 = new ArrayList<Integer>();
a2.add(123);
System.out.println(test2(a2));
System.out.println(test2(a2).get(0).substring(0, 1));
}
}

result:

ABC
java.lang.Integer is not a String
null
java.lang.Boolean is not a List<String>
null
[ABC]
A [123]
Exception in thread "main" java.lang.ClassCastException:
java.lang.Integer cannot be cast to java.lang.String
at ToWarnOrNotToWarn.main(ToWarnOrNotToWarn.java:36)

the warning on test2 is justified because the cast in test2 cause
a ClassCastException in main.

Arne

Re: Why does Java complain if you cast a Map but not an Object?

<4ab3b92b-0570-439a-9022-5498dc95e9d3n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=237&group=comp.lang.java.programmer#237

  copy link   Newsgroups: comp.lang.java.programmer
X-Received: by 2002:ad4:4b64:0:b0:5ef:43ee:61fb with SMTP id m4-20020ad44b64000000b005ef43ee61fbmr3446626qvx.6.1681928396088;
Wed, 19 Apr 2023 11:19:56 -0700 (PDT)
X-Received: by 2002:a25:7453:0:b0:b92:282b:d557 with SMTP id
p80-20020a257453000000b00b92282bd557mr372110ybc.13.1681928395724; Wed, 19 Apr
2023 11:19:55 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.java.programmer
Date: Wed, 19 Apr 2023 11:19:55 -0700 (PDT)
In-Reply-To: <u1p97t$4nqi$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:980:8281:72b0:70ed:524b:537d:e322;
posting-account=2czF5goAAAD4GBMPIGV4KcD2K4PhoB_H
NNTP-Posting-Host: 2601:980:8281:72b0:70ed:524b:537d:e322
References: <632f38ec-674f-44ad-b8d4-53aa32534c09n@googlegroups.com> <u1p97t$4nqi$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4ab3b92b-0570-439a-9022-5498dc95e9d3n@googlegroups.com>
Subject: Re: Why does Java complain if you cast a Map but not an Object?
From: e.d.programmer@gmail.com (e.d.pro...@gmail.com)
Injection-Date: Wed, 19 Apr 2023 18:19:56 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: e.d.pro...@gmail.com - Wed, 19 Apr 2023 18:19 UTC

On Wednesday, April 19, 2023 at 1:43:39 PM UTC-4, Arne Vajhøj wrote:

> System.out.println(test2(a2));
> System.out.println(test2(a2).get(0).substring(0, 1));

So no warnings about casting one thing to another possibly compatible thing (Object to String) because the cast immediately throws exception if Object is not a String, but using containers (List, Map) it warns because the cast hides the error; it actually allows incompatible casting (List<Integer> to List<String>) and can print the resulting object, and crashes when you try to return an element into a typed variable. Of course we don't need to worry about immediate casting exceptions because it's Runtime.
If you take a List<Integer> and cast it to a List<String>, you can call get(idx) and return the value into a variable declared as String and crash (but it compiles). If you return the value into a variable declared as Object, it works, so when you called .get(0) it didn't care that it wasn't a String until it tried to process the .substring. As long as you treat that result as Object, it's fine.
System.out.println(test2(a2).get(0).getClass()); // List<String> doesn't care that element is Integer

Re: Why does Java complain if you cast a Map but not an Object?

<u1pf3r$5ksa$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=238&group=comp.lang.java.programmer#238

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: arne@vajhoej.dk (Arne Vajhøj)
Newsgroups: comp.lang.java.programmer
Subject: Re: Why does Java complain if you cast a Map but not an Object?
Date: Wed, 19 Apr 2023 15:23:41 -0400
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <u1pf3r$5ksa$1@dont-email.me>
References: <632f38ec-674f-44ad-b8d4-53aa32534c09n@googlegroups.com>
<u1p97t$4nqi$1@dont-email.me>
<4ab3b92b-0570-439a-9022-5498dc95e9d3n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 19 Apr 2023 19:23:39 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e68d6b2e7704051c3dc3f19203fb0784";
logging-data="185226"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19B/D4Woc1Gm47hEqk2BfTKPNaIgsVwohg="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.10.0
Cancel-Lock: sha1:Kq82N3AvRObh8Qr8zY5bpXgVg9Q=
In-Reply-To: <4ab3b92b-0570-439a-9022-5498dc95e9d3n@googlegroups.com>
Content-Language: en-US
 by: Arne Vajhøj - Wed, 19 Apr 2023 19:23 UTC

On 4/19/2023 2:19 PM, e.d.pro...@gmail.com wrote:
> On Wednesday, April 19, 2023 at 1:43:39 PM UTC-4, Arne Vajhøj wrote:
>
>> System.out.println(test2(a2));
>> System.out.println(test2(a2).get(0).substring(0, 1));
>
> So no warnings about casting one thing to another possibly compatible
> thing (Object to String) because the cast immediately throws
> exception if Object is not a String, but using containers (List, Map)
> it warns because the cast hides the error; it actually allows
> incompatible casting (List<Integer> to List<String>) and can print
> the resulting object, and crashes when you try to return an element
> into a typed variable. Of course we don't need to worry about
> immediate casting exceptions because it's Runtime. If you take a
> List<Integer> and cast it to a List<String>, you can call get(idx)
> and return the value into a variable declared as String and crash
> (but it compiles). If you return the value into a variable declared
> as Object, it works, so when you called .get(0) it didn't care that
> it wasn't a String until it tried to process the .substring. As long
> as you treat that result as Object, it's fine.
> System.out.println(test2(a2).get(0).getClass()); // List<String>
> doesn't care that element is Integer

Yes. That is how I see it all work.

Arne

Re: Why does Java complain if you cast a Map but not an Object?

<87r0saaihs.fsf@golem.phantasia.org>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=240&group=comp.lang.java.programmer#240

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!news.nntp4.net!.POSTED!not-for-mail
From: miju@golem.phantasia.org (Michael Jung)
Newsgroups: comp.lang.java.programmer
Subject: Re: Why does Java complain if you cast a Map but not an Object?
Date: Sun, 23 Apr 2023 14:30:23 +0200
Message-ID: <87r0saaihs.fsf@golem.phantasia.org>
References: <632f38ec-674f-44ad-b8d4-53aa32534c09n@googlegroups.com>
<u1p97t$4nqi$1@dont-email.me>
<4ab3b92b-0570-439a-9022-5498dc95e9d3n@googlegroups.com>
<u1pf3r$5ksa$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
X-Trace: eJwlzU0KAyEMQOF1ewrJXkthaBlEmKsER2yKJqFGev3+zP7xPi7vEZjVlsDFolMZRlw95iyTLUGn59z0gWw4CIO8KsTzqUmtv2xHwwS363JfV4iuIzWfpWtDYhveJMEc5Xve+IBM/xC4i/sAnaoqwQ==
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux)
Cancel-Lock: sha1:k9Mo5YhZ/zLzZYhRwyTPrrhBAXE= sha1:AKsnBxygH3ug8IqO5+LXgVTeNEM=
X-Abuse-Contact: "https://abuse.nntp4.net"
 by: Michael Jung - Sun, 23 Apr 2023 12:30 UTC

Arne Vajhøj <arne@vajhoej.dk> writes:
> On 4/19/2023 2:19 PM, e.d.pro...@gmail.com wrote:
>> On Wednesday, April 19, 2023 at 1:43:39 PM UTC-4, Arne Vajhøj wrote:
>> So no warnings about casting one thing to another possibly compatible
>> thing (Object to String) because the cast immediately throws
>> exception if Object is not a String, but using containers (List, Map)
>> it warns because the cast hides the error; it actually allows
>> incompatible casting (List<Integer> to List<String>) and can print
>> the resulting object, and crashes when you try to return an element
>> into a typed variable. Of course we don't need to worry about
>> immediate casting exceptions because it's Runtime. If you take a
>> List<Integer> and cast it to a List<String>, you can call get(idx)
>> and return the value into a variable declared as String and crash
>> (but it compiles). If you return the value into a variable declared
>> as Object, it works, so when you called .get(0) it didn't care that
>> it wasn't a String until it tried to process the .substring. As long
>> as you treat that result as Object, it's
>> fine. System.out.println(test2(a2).get(0).getClass()); //
>> List<String>
>> doesn't care that element is Integer
> Yes. That is how I see it all work.

Think of "Object" is the supertype of all objects and "?" as the
(generic) subtype of all objects. When you cast Object to Map that is
worth a warning because it is a downcast. Casting an arbitrary Map to
Map<?,?> is okay, because the Map *must* map some ? to (another) ?.
Later, when you cast ? to MyObject that is an upcast and that is okay.
The compiler apparently "loses" the reference that this specific
instance of ? could already have been bound to a superclass.

Maybe if you replace all "?" with "? extends Object" the compiler knows
that the subtype could be Object itself and it issues the warning?

It is not "because cast hides errors".

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor