• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Casting

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, here is a line of code which simply cannot be compiled using jdk 1.6.0_56.

List localList = this.o.a(adw.class, aQ().b(1.0D, 0.0D, 1.0D));
for (adw localadw : (List)localList) { /*blah*/}

An error message prints out:

xn.java:288: incompatible types
found : java.lang.Object
required: adw
for (adw localadw : (List)localList)
^

I have no idea how this is incorrect - maybe the casting of the object/variable type?
BTW This is in minecraft source code - it runs normally.
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the class (or interface) "adw" avallable to the compiler?
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use something like following:

List<adw> localList = this.o.a(adw.class, aQ().b(1.0D, 0.0D, 1.0D)); // If you know that your method always returns list of objects of class adw.

for(adw localadw : localList) {...}

Error comes because List localList is a list of Objects and you try to iterate over localList and want to get adw objects; which is not their.

You can also do following:

List localList = this.o.a(adw.class, aQ().b(1.0D, 0.0D, 1.0D));
for (Object o : localList) {
adw localadw = (adw) o;
...
}
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gaurangkumar is right. localList isn't a generic List (even after the cast, which is unnecessary because you're just casting a List to a List). So the compiler assumes the contents are Objects, and you're trying to assign them to an adw variable.

By the way, adw is a terrible name for a class or interface!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're working with decompiled, obfuscated code. One of the things that the Java compiler does when compiling code is throw away all the generics (type erasure).

In one of your previous questions I explained that decompilers do not (always) produce source code that can be compiled back into a working program. This is an example.
 
Ash Whin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you: this worked!

List<adw> localList = this.o.a(adw.class, aQ().b(1.0D, 0.0D, 1.0D));
for (adw localadw : localList) {}

I learnt something: Decompilers sometimes produce bugs in the code, and it is YOUR job to fix it (or ask it here).

 
Ash Whin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another question. Another error.

Object localObject2;
localObject2 = this.o.a(xm.class, aQ().b(10.0D, 10.0D, 10.0D));
for (xm localxm : (List)localObject2) {}

]xn.java:816: incompatible types
found : java.lang.Object
required: xm
/* 893 */ for (xm localxm : (List)localObject2)
^
Note: xn.java uses unchecked or unsafe operations.

What unchecked or unsafe operations? Also, I am not sure how to tell jdk that localObject2 is a part of xm. Is this how I do it:
Object xm.localObject2;

instead of simply
Object localObject2;

Right? Also, how do I prevent jdk from complaining about unchecked operations? I tried using javac -X -Xlint:{all} but nothing came out...
*The command*
 
Ash Whin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for spamming.

Then these came out:
xn.java:812: ';' expected
/* */ Object xm.localObject2;
^
xn.java:812: not a statement
/* */ Object xm.localObject2;

And the part about the security was gone.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ash Whin wrote:I learnt something: Decompilers sometimes produce bugs in the code, and it is YOUR job to fix it (or ask it here).


I wouldn't call it like that. Decompilers don't (deliberately) produce bugs in the code. When you compile your Java source code to a *.class file, not 100% of the information in the source code is retained. Some information is thrown away, for example the use of generics. It's not possible for a decompiler to get the exact original source code from the *.class file, because some information is simply not in the *.class file.

Ash Whin wrote:
xn.java:816: incompatible types
found : java.lang.Object
required: xm
/* 893 */ for (xm localxm : (List)localObject2)
^


This is exactly the same as the first error you had.

Ash Whin wrote:
Then these came out:


I have no idea what those lines are supposed to mean. It's not valid Java source code.
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Decompilers don't (deliberately) produce bugs in the code. When you compile your Java source code to a *.class file, not 100% of the information in the source code is retained.


Also, most freely available decompilers can't deal with the latest JVM features. Most of those you'll find on the web have not even been updated to handle the Java 5 additions (so they disregard even the limited generics information that is in the class files), and even fewer have been updated to handle the features added in Java 7 and Java 8.
 
Ash Whin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyway, how to cast an Object to a class type, when there is already a definition?
 
Ash Whin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did it.




It got compiled and ran, but there was this "unchecked" security error. Much wow.
~Thread Closed~
 
Marshal
Posts: 75663
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason for the unchecked warning is given above. The decompiler cannot reconstruct the generics, so you get the code minus generics.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The message about an unchecked cast is a warning, not an error, and it doesn't have anything to do with security.
 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic