Can't Call Methods in Class Created W/ Reflection  
Author Message
Hal Vaughan





PostPosted: 2004-12-8 4:09:00 Top

java-programmer, Can't Call Methods in Class Created W/ Reflection I am testing a class that I'll need later. I need to be able to load one of
a number of classes that all implement a particular interface, then call 3
methods in them. When I run the test class, I get the same error
(indicated in comments below). I've gone over this several times and
compared it with tutorials online, but I can't find my error (which, the
way my brain works, means it's probably something simple and obvious and
I'm just to caught in it to see what's wrong).

The code for the calling class and the called class are below.

Thank you for any help!

Hal

-------------------------------------------------------
Calling class (ldt):


import java.io.*;
import java.lang.reflect.*;

public class ldt {

int iCount, iLimit;
int iSet = 0, iGet = 0, iProcess = 0;
String dirConf, dirProcess, sName, setFile, RDFile, MMFile;
String[] fList;

public static void main(String[] args) {
ldt l = new ldt();
l.dostuff();
}

public ldt() {

}

public void dostuff() {
int i;
String sTemp = "LDRDMod000";
Class cSearch = null;
Object oSearch = null;
Method[] oMethod = null;
Method mSet = null, mGet = null, mProcess = null;
try {
cSearch = Class.forName(sTemp);
} catch (Exception e) {
System.out.println("Error: Could not find class: "+sTemp+", Error: "+e);
return;
}
oMethod = cSearch.getMethods();
try {
mSet = cSearch.getMethod("set", new Class[] {String.class,
String.class});
mGet = cSearch.getMethod("get", new Class[] {String.class});
mProcess = cSearch.getMethod("process", new Class[] {});
} catch (Exception e) {
System.out.println("Error with getting method. Exception: "+e);
}
try {
oSearch = (Object) cSearch;
Object[] aInput = new Object[] {(String) "one", (String) "two"};
Object o = mSet.invoke(cSearch, aInput);
//Line above seems to be what always causes error, printed below. Error is
//always: "java.lang.IllegalArgumentException: object is not an instance of
//declaring class"
} catch (Exception e) {
System.out.println("Error: Set Value: "+e);
return;
}
return;
}
}


Here is the called class that I'm trying to instantiate through reflection
(LDRDMod000):


import java.io.*;
import java.util.HashMap;

public class LDRDMod000 implements LDRDMod {

String sName = "", sStreet = "", sCity = "", sState = "", sZip = "";
HashMap hData = new HashMap();
LDConfig sysConfig;

public LDRDMod000() {

}

public void set(String sKey, String sVal) {
hData.put(sKey.toLowerCase(), sVal);
return;
}

public String get(String sKey) {
String sVal = (String) hData.get(sKey.toLowerCase());
return sVal;
}

public void process() {
String sIn = get("filename");
String sOut = sIn.substring(0, sIn.indexOf(".RD")) + ".MM";
File outFile = new File(sOut);
if (!outFile.exists()) {
File inFile = new File(sIn);
// inFile.renameTo(outFile);
}
set("filename", sOut);
return;
}
}
 
Patricia Shanahan





PostPosted: 2004-12-8 14:18:00 Top

java-programmer >> Can't Call Methods in Class Created W/ Reflection Hal Vaughan wrote:

> I am testing a class that I'll need later. I need to be able to load one of
> a number of classes that all implement a particular interface, then call 3
> methods in them. When I run the test class, I get the same error
> (indicated in comments below). I've gone over this several times and
> compared it with tutorials online, but I can't find my error (which, the
> way my brain works, means it's probably something simple and obvious and
> I'm just to caught in it to see what's wrong).
...
> Class cSearch = null;
....
> oMethod = cSearch.getMethods();
> try {
> mSet = cSearch.getMethod("set", new Class[] {String.class,
> String.class});
> mGet = cSearch.getMethod("get", new Class[] {String.class});
> mProcess = cSearch.getMethod("process", new Class[] {});
> } catch (Exception e) {
> System.out.println("Error with getting method. Exception: "+e);
> }
> try {
> oSearch = (Object) cSearch;
> Object[] aInput = new Object[] {(String) "one", (String) "two"};
> Object o = mSet.invoke(cSearch, aInput);
> //Line above seems to be what always causes error, printed below. Error is
> //always: "java.lang.IllegalArgumentException: object is not an instance of
> //declaring class"
> } catch (Exception e) {
> System.out.println("Error: Set Value: "+e);
> return;
> }
> return;
> }
> }


cSearch and oSearch are both references to a java.lang.Class
object, and mSet is a method in the class it defines, not a
method in java.lang.Class. Shouldn't you do something like:

oSearch = cSearch.newInstance();

to create an instance of the class cSearch references?

Patricia



 
Hal Vaughan





PostPosted: 2004-12-8 23:03:00 Top

java-programmer >> Can't Call Methods in Class Created W/ Reflection Patricia Shanahan wrote:

> Hal Vaughan wrote:
>
>> I am testing a class that I'll need later. I need to be able to load one
>> of a number of classes that all implement a particular interface, then
>> call 3
>> methods in them. When I run the test class, I get the same error
>> (indicated in comments below). I've gone over this several times and
>> compared it with tutorials online, but I can't find my error (which, the
>> way my brain works, means it's probably something simple and obvious and
>> I'm just to caught in it to see what's wrong).
> ...
>> Class cSearch = null;
> ....
>> oMethod = cSearch.getMethods();
>> try {
>> mSet = cSearch.getMethod("set", new Class[] {String.class,
>> String.class});
>> mGet = cSearch.getMethod("get", new Class[] {String.class});
>> mProcess = cSearch.getMethod("process", new Class[] {});
>> } catch (Exception e) {
>> System.out.println("Error with getting method. Exception: "+e);
>> }
>> try {
>> oSearch = (Object) cSearch;
>> Object[] aInput = new Object[] {(String) "one", (String) "two"};
>> Object o = mSet.invoke(cSearch, aInput);
>> //Line above seems to be what always causes error, printed below. Error
>> is //always: "java.lang.IllegalArgumentException: object is not an
>> instance of //declaring class"
>> } catch (Exception e) {
>> System.out.println("Error: Set Value: "+e);
>> return;
>> }
>> return;
>> }
>> }
>
>
> cSearch and oSearch are both references to a java.lang.Class
> object, and mSet is a method in the class it defines, not a
> method in java.lang.Class. Shouldn't you do something like:
>
> oSearch = cSearch.newInstance();
>
> to create an instance of the class cSearch references?
>
> Patricia

Still gives the same error. I'll keep trying, though.

Thanks.

Hal
 
 
Hal Vaughan





PostPosted: 2004-12-9 0:43:00 Top

java-programmer >> Can't Call Methods in Class Created W/ Reflection Patricia Shanahan wrote:

> Hal Vaughan wrote:
>
>> I am testing a class that I'll need later. I need to be able to load one
>> of a number of classes that all implement a particular interface, then
>> call 3
>> methods in them. When I run the test class, I get the same error
>> (indicated in comments below). I've gone over this several times and
>> compared it with tutorials online, but I can't find my error (which, the
>> way my brain works, means it's probably something simple and obvious and
>> I'm just to caught in it to see what's wrong).
> ...
>> Class cSearch = null;
> ....
>> oMethod = cSearch.getMethods();
>> try {
>> mSet = cSearch.getMethod("set", new Class[] {String.class,
>> String.class});
>> mGet = cSearch.getMethod("get", new Class[] {String.class});
>> mProcess = cSearch.getMethod("process", new Class[] {});
>> } catch (Exception e) {
>> System.out.println("Error with getting method. Exception: "+e);
>> }
>> try {
>> oSearch = (Object) cSearch;
>> Object[] aInput = new Object[] {(String) "one", (String) "two"};
>> Object o = mSet.invoke(cSearch, aInput);
>> //Line above seems to be what always causes error, printed below. Error
>> is //always: "java.lang.IllegalArgumentException: object is not an
>> instance of //declaring class"
>> } catch (Exception e) {
>> System.out.println("Error: Set Value: "+e);
>> return;
>> }
>> return;
>> }
>> }
>
>
> cSearch and oSearch are both references to a java.lang.Class
> object, and mSet is a method in the class it defines, not a
> method in java.lang.Class. Shouldn't you do something like:
>
> oSearch = cSearch.newInstance();
>
> to create an instance of the class cSearch references?
>
> Patricia

Okay, here's what happened: I had seen different references, so I had
oSearch and cSearch, one a Class, one an Object.牋I爃ad爐ried燿ifferent
versions, swapping oSearch and cSearch in different places to see if it
made a difference.牋It燿idn't.牋BUT?-爓hen營爐ried爓hat爕ou爏uggested,營
had left the line

oSearch = (Object) cSearch

in place, so I was getting an error because of a line I had intended to
comment out.牋I爃ad燽asically爅ust燼dded爐he爈ine爕ou爏uggested爎ight燼fter
getting cSearch, which meant the usable object of oSearch was replaced by
the line above.牋Once營燿umped爐he爈ine營爉entioned,爄t爓orked爁ine.

The VERY frustrating thing about this is that I had found a number of
tutorials and examples on the web, and not ONE included this extra step.
Every one of them used the original class obtained from the Class.forName()
method.牋I'm爂oing爐o爏end爀-mail爐o爐he爋wners/writers爋f爐hose爐utorials
and point out this extra needed step.

Thanks for the help!

Hal