Weird class cast behavior

So I have this code:
class SearcherAllocator extends SearcherBase.Pointer_Alloc{
@Override
public Pointer call(){

class JSearcher extends Searcher{

}

return new JSearcher();

}
}
Searcher.alloc(new SearcherAllocator().retainReference());

Searcher searcher = (Searcher) Searcher.alloc().call();
class SearcherAllocator extends SearcherBase.Pointer_Alloc{
@Override
public Pointer call(){

class JSearcher extends Searcher{

}

return new JSearcher();

}
}
Searcher.alloc(new SearcherAllocator().retainReference());

Searcher searcher = (Searcher) Searcher.alloc().call();
May look a bit messy but essentially the class hierarchy of Searcher is Searcher extends SearcherBase extends Pointer. However, even though Searcher extends Pointer, when the line
Searcher searcher = (Searcher) Searcher.alloc().call();
Searcher searcher = (Searcher) Searcher.alloc().call();
runs, it gives me this: Exception in thread "main" java.lang.ClassCastException: class org.bytedeco.javacpp.Pointer cannot be cast to class com.niki.nativeplayer.Bridge.Searcher (org.bytedeco.javacpp.Pointer and com.niki.nativeplayer.Bridge.Searcher are in unnamed module of loader 'app') there are two Searcher.alloc() functions, one is a setter and one is a getter. I think that part is self explanatory. I tried to replicate this error with this:
public static void main(String[] args) {
class Pointer{


}
class SearcherBase extends Pointer{


}
class Searcher extends SearcherBase{

}

Supplier<Pointer> allocator =()->{
class JSearcher extends Searcher{

}
return new JSearcher();
};
Searcher searcher= (Searcher) allocator.get();
}
public static void main(String[] args) {
class Pointer{


}
class SearcherBase extends Pointer{


}
class Searcher extends SearcherBase{

}

Supplier<Pointer> allocator =()->{
class JSearcher extends Searcher{

}
return new JSearcher();
};
Searcher searcher= (Searcher) allocator.get();
}
But this code ,which should be pretty much equivalent, doesn't throw that exception.
32 Replies
JavaBot
JavaBot4mo ago
This post has been reserved for your question.
Hey @nikcho-kouhai! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Kyo-chan
Kyo-chan4mo ago
No, nothing is self explanatory The error says you have an object that's a Pointer and you're trying to cast it to Searcher. That's not going to work The second example you show doesn't do that at all
nikcho-kouhai
nikcho-kouhaiOP4mo ago
well alright though how doesn't it do that? I htink I do pretty much the exact same in the two examples you can pretty much ignore Searcher.alloc to be honest. It's literally just a function call that's exactly what I do in the short example code I feel like I haven't sent the entire Searcher, SearcherBase and Pointer sources because they are somewhat long in the original post but the point is Searcher extends Pointer The method itself even returns fine. The problem comes when I try to cast it and I think I forgot to mention that this isn't a compile time error, but a runtime exception. So I know for a fact Searcher extends Pointer, because return new JSearcher(); doesn't cause issues.
Kyo-chan
Kyo-chan4mo ago
Look, you just did not show any "first examples". As you said, what you showed doesn't exactly matter when you investigate a class cast exception
nikcho-kouhai
nikcho-kouhaiOP4mo ago
alright, what do you want to see?
Kyo-chan
Kyo-chan4mo ago
The error tells you you have an object that's a Pointer and you try to cast it to Searcher. Your second example does not do that.
nikcho-kouhai
nikcho-kouhaiOP4mo ago
well yeah but the initial code doesn't do that either. I have an object of type JSearcher that inherits Searcher that gets returned as a Pointer and then gets casted to a Searcher
Kyo-chan
Kyo-chan4mo ago
Ideally, the relevant code, but considering your structure and various confusion I'm guessing you don't have a clue what's relevant. So the next best move would be to make your program as tiny as possible so that it has the problem and nothing but that, and you show the entire program. Maybe, maybe not. How would we know whether you have that?
nikcho-kouhai
nikcho-kouhaiOP4mo ago
you can literally see Searcher.alloc().call() which invokes the call() method that's standing literally 10 lines above it one sec*
Kyo-chan
Kyo-chan4mo ago
Why would I think I have that? It doesn't look so.
nikcho-kouhai
nikcho-kouhaiOP4mo ago
@Override
public Pointer call(){

class JSearcher extends Searcher{

}

return new JSearcher();

}
@Override
public Pointer call(){

class JSearcher extends Searcher{

}

return new JSearcher();

}
this is literally it
Kyo-chan
Kyo-chan4mo ago
And what do you do with it?
nikcho-kouhai
nikcho-kouhaiOP4mo ago
this is the method that's invoked in the
Searcher searcher = (Searcher) Searcher.alloc().call();
Searcher searcher = (Searcher) Searcher.alloc().call();
line in short- i invoke it
Kyo-chan
Kyo-chan4mo ago
Maybe, maybe not Also, how do you know that Searcher is a subtype of Pointer?
nikcho-kouhai
nikcho-kouhaiOP4mo ago
I ran it through the debugger I know I invoke it I am literally looking at it
Kyo-chan
Kyo-chan4mo ago
I did not run it through the debugger
nikcho-kouhai
nikcho-kouhaiOP4mo ago
I am not getting a compile time error at the very least
Kyo-chan
Kyo-chan4mo ago
good point
nikcho-kouhai
nikcho-kouhaiOP4mo ago
I am literally telling you I ran it through the debugger line by line I can send you a video if you want
Kyo-chan
Kyo-chan4mo ago
And I am literally telling you that I, on the other hand, have not done that
nikcho-kouhai
nikcho-kouhaiOP4mo ago
I would create a minimalistic example of this code and send it to you but I tried and the example works
Kyo-chan
Kyo-chan4mo ago
Videos aren't interactive. While it is not impossible that it would prove your point, I'd rather not risk the frustration
nikcho-kouhai
nikcho-kouhaiOP4mo ago
well what can I do then
Kyo-chan
Kyo-chan4mo ago
Most likely, the method calls aren't wired as you expect them
nikcho-kouhai
nikcho-kouhaiOP4mo ago
they literally are one step over and I am in the call function
Kyo-chan
Kyo-chan4mo ago
Rebuild the program from minimalistic towards its real version, until it breaks again
nikcho-kouhai
nikcho-kouhaiOP4mo ago
that would be stupidly difficult to be honest something to note though one sec
Kyo-chan
Kyo-chan4mo ago
I suppose you could try giving the entire program, but if it's too big only people who aren't me are going to look at it
nikcho-kouhai
nikcho-kouhaiOP4mo ago
there's nothing to actually look at in the program. The program itself is the example I gave.. it just involves native code
Kyo-chan
Kyo-chan4mo ago
You seem to understand what's going on. I'll stop bothering you about it
nikcho-kouhai
nikcho-kouhaiOP4mo ago
look this is the most minimalistic variant of the program there is even if I made an example it would be the exact same I can send you the other classes but I doubt that would help something to note though
final AtomicReference<Searcher> ref= new AtomicReference<>();
class SearcherAllocator extends SearcherBase.Pointer_Alloc{
@Override
public Pointer
call(){
System.out.println("allocator called");
class JSearcher extends Searcher{

}
ref.set(new JSearcher().retainReference());

return new JSearcher().retainReference();

}
}

Searcher.alloc(new SearcherAllocator().retainReference());

Searcher.alloc().call();
Searcher searcher = ref.get() ;
final AtomicReference<Searcher> ref= new AtomicReference<>();
class SearcherAllocator extends SearcherBase.Pointer_Alloc{
@Override
public Pointer
call(){
System.out.println("allocator called");
class JSearcher extends Searcher{

}
ref.set(new JSearcher().retainReference());

return new JSearcher().retainReference();

}
}

Searcher.alloc(new SearcherAllocator().retainReference());

Searcher.alloc().call();
Searcher searcher = ref.get() ;
this here works perfectly fine. (ignore that I am doing new JSearcher twice. it's just for the sake of the example)
JavaBot
JavaBot4mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server