View Full Version : Reflection??
2uantuM
12-31-03, 01:31 AM
Hey,
I'm starting to learn Java and I have a question concerning reflection.
I have the following class:
import java.util.regex.*;
public class parsePacket extends Thread {
private String strPacket;
private String strGame;
public parsePacket(String myPacket) {
Pattern p = Pattern.compile("[^A-Za-z0-9|@|=|.|&]");
Matcher m = p.matcher(myPacket);
myPacket=m.replaceAll("");
strPacket= myPacket;
}
public void run() {
strPacket=strPacket.substring(strPacket.indexOf("&")+1);
strGame=strPacket.substring(strPacket.indexOf("game")+5,strPacket.indexOf("&"));
logger.printAction("Loading class for "+strGame);
try{
Class plugin = Class.forName("games."+strGame+"."+strGame);
//What goes here???
}
catch(ClassNotFoundException e){
logger.printError("The plugin for that game was not found on the server", false);
}
}
}
And I am wondering how I can initialize a constructor of the class determined and pass a String to that constructor?
parsePacket myPacket = new parsePacket("blah");
myPacket.run();
2uantuM
12-31-03, 12:41 PM
That wasn't the question, I'm talking about reflection, not multithreading. The answer you gave me isn't correct for multithreading either; it would be parsePacketobject.start() (run is used for multithreading, read about it here: http://web.cefriel.it/~alfonso/SEBook/Part2/Multithreading.pdf
Anyway, The problem is I dont know how to use the object "plugin"
Class plugin = Class.forName("games."+strGame+"."+strGame);
You can't just do Plugin c = (Plugin)c.newInstance(); It doesn't work.
2uantuM
12-31-03, 01:21 PM
I seem to have gotten a little farther:
import java.util.regex.*;
import java.lang.reflect.*;
public class parsePacket extends Thread {
private String strPacket;
private String strGame;
public parsePacket(String myPacket) {
Pattern p = Pattern.compile("[^A-Za-z0-9|@|=|.|&]");
Matcher m = p.matcher(myPacket);
myPacket=m.replaceAll("");
strPacket= myPacket;
}
public void run() {
strPacket=strPacket.substring(strPacket.indexOf("&")+1);
strGame=strPacket.substring(strPacket.indexOf("game")+5,strPacket.indexOf("&"));
logger.printAction("Loading class for "+strGame);
try{
Class[] strArgsClass = new Class[] {String.class};
Object[] strArgs = new Object[] {strPacket};
Constructor strArgsConstructor;
Class plugin = Class.forName("games."+strGame+"."+strGame);
strArgsConstructor=plugin.getConstructor(strArgsCl ***);
}
catch(ClassNotFoundException e){
logger.printError("The plugin for that game was not found on the server", false);
}
catch(InstantiationException e){
logger.printError("There seems to be a bug with the plugin " + strGame+". ("+e+")",false);
}
catch(IllegalAccessException e){
logger.printError("There seems to be a bug with the plugin " + strGame+". ("+e+")",false);
}
catch(NoSuchMethodException e){
logger.printError("There seems to be a bug with the plugin " + strGame+". ("+e+")",false);
}
}
}
Any ideas on that last line?
2uantuM
12-31-03, 07:21 PM
Figured it out:
import java.util.regex.*;
import java.lang.reflect.*;
public class parsePacket extends Thread {
private String strPacket;
private String strGame;
public parsePacket(String myPacket) {
Pattern p = Pattern.compile("[^A-Za-z0-9|@|=|.|&]");
Matcher m = p.matcher(myPacket);
myPacket=m.replaceAll("");
strPacket= myPacket;
}
public void run() {
strPacket=strPacket.substring(strPacket.indexOf("&")+1);
strGame=strPacket.substring(strPacket.indexOf("game")+5,strPacket.indexOf("&"));
logger.printAction("Loading class for "+strGame);
try{
Class[] strArgsClass = new Class[] {String.class};
Object[] strArgs = new Object[] {strPacket};
Constructor strArgsConstructor;
Class plugin = Class.forName("games."+strGame+"."+strGame);
strArgsConstructor=plugin.getConstructor(strArgsCl ***);
strArgsConstructor.newInstance(strArgs);
}
catch(ClassNotFoundException e){
logger.printError("The plugin for that game was not found on the server", false);
}
catch(InstantiationException e){
logger.printError("There seems to be a bug with the plugin " + strGame+". ("+e+")",false);
}
catch(IllegalAccessException e){
logger.printError("There seems to be a bug with the plugin " + strGame+". ("+e+")",false);
}
catch(NoSuchMethodException e){
logger.printError("There seems to be a bug with the plugin " + strGame+". ("+e+")",false);
}
catch(InvocationTargetException e){
logger.printError("There seems to be a bug with the plugin " + strGame+". ("+e+")",false);
}
}
}
vBulletin® v3.6.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.