🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #14765

Re: Loading classes / casting problem

#14765
From: Alex
Date: Sat, 07 Aug 1999 00:00
88 lines
3157 bytes
Hello Casper, THANK YOU VERY MUCH FOR YOUR ANSWER !

Fist I have to tell everyone who is interested in this problem that I made a
mistake writing my last post:

Of course I didn�t try to cast a Class into another but I tried it with an
Object:

            Class myClass = myClassLoader.load
("mypackage.befehle.myCommand");

            Object object = myClass.newInstance();
// I forgot this line in my last post

            Befehl b =  (Befehl) object;        // throws a
ClassCastException although object implements Befehl (it�s runtime class)



Marcel van den Dungen <info@c-content.nl> wrote in message
news:37AACC80.85E24B37@c-content.nl...
> maybe you have to cast your classes to implemented versions of befehl,
> like for instance myCommand.
>
> greetings
> casper
>

I tried it, but it ended with a ClassCastException, too.
In this case I can at least imagine why the exception is thrown:

If I have two classes implementing Befehl like these ...

class FirstClass implements Befehl {
   public void execute();

  public void a_totally_different_Function (){}
}

class SecondClass implements Befehl {
   public void execute();

  public void the_problem_Function()  {}
}

... and I load one of them ...

Class class = myClassLoader.load ("FirstClass");
Object object = class.newInstance();

... now "object" is (at runtime) a FirstClass and includes the method
"a_totally_different_Function ()" but NOT "the_problem_Function()"

Casting "object" into SecondClass ("SecondClass s = (SecondClass) object")
which also implements Befehl would mean that "object" must have
"the_problem_Function()" but it doesn�t.


For everyone out there who has a similar problem, here is my workaround.
Even though this works, it is not the most elegant code I ever wrote. So if
anyone knows where the ClassCastException that I described before comes
from, please let us know.

I want to load some class files and resolve them knowing that they all have
at least one method in common (public void execute () {}).

So I load the class:
Class class = myClassLoader.load ("className");

then I get the method:
Method ex = class.getMethod ("execute", new Class[0]);

and I create an instance of the loaded class:
Object o = class.newInstance(); // this implements interface Befehl but I
can�t cast it ! (see above)

Now the method can be executed:
ex.invoke (o,new Object[0]);




That looks crazy to me but it works ! If someone knows a better way, please
tell us about it,

Yours,

Alex.
Technical Details
Message-ID: <7oi92f$38f3@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <7ocqkj$rm717@forums.borland.com> <37AACC80.85E24B37@c-content.nl>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f74b3,52cfadaf87143629
X-Google-Attributes: gidf74b3,public
From: "Alex" <alexbged@linux.zrz.tu-berlin.de>
Subject: Re: Loading classes / casting problem
Date: 1999/08/07
Message-ID: <7oi92f$38f3@forums.borland.com>
X-Deja-AN: 510051052
References: <7ocqkj$rm717@forums.borland.com> <37AACC80.85E24B37@c-content.nl>
X-Priority: 3
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211
Organization: Another Netscape Collabra Server User
X-MSMail-Priority: Normal
Newsgroups: borland.public.jbuilder.java.language