|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jul 2000
Città: Vignola (MO)
Messaggi: 316
|
[JAVA] Caricare classi dinamicamente
Con il seguente codice riesco a caricare una classe
Codice PHP:
Grazie ciao
__________________
<Asus A8N-E Deluxe> <Amd64 3000+> <Ram 2.5 Gb> <Geffo3> <2 Maxtor 160 Gb RAID 1> <Adsl ZyXEL 645r> <ESS Maestro> <GNU/Linux Debian Etch> Wii 1037 4238 6261 1967 |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Jul 1999
Città: Torino
Messaggi: 2221
|
Devi costruirti un metodo apposta... non so se nelle ultime versioni di Java è stato implementato un metodo che accetta parametri del costruttore... Io usavo questo:
Codice:
protected Object newInstance(Object[] _params)
throws FactoryException
{
Object ret = null;
try
{
Class clInst = Class.forName(className);
Class[] params = new Class[_params.length];
Object[] objects = new Object[_params.length];
for(int i = 0; i < _params.length; i++)
{
params[i] = _params[i].getClass();
//System.out.println(i + " classe: " + _params[i].getClass().getName());
objects[i] = _params[i];
}
Constructor constructor = clInst.getConstructor(params);
//System.out.println("Constructor: " + constructor.getName());
//Object[] objects = new Object[]{_params};
ret = constructor.newInstance(objects);
}catch(ClassNotFoundException cnfe){
StringBuffer sb = new StringBuffer("ClassNotFoundException: ");
sb.append(cnfe.getMessage());
cnfe.printStackTrace(System.out);
throw new FactoryException(sb.toString());
}catch(NoSuchMethodException nsme){
StringBuffer sb = new StringBuffer("NoSuchMethodException: ");
sb.append(nsme.getMessage());
nsme.printStackTrace(System.err);
throw new FactoryException(sb.toString());
}catch(InvocationTargetException ite){
StringBuffer sb = new StringBuffer("InvocationTargetException: ");
sb.append(ite.getMessage());
ite.printStackTrace(System.out);
throw new FactoryException(sb.toString());
}catch(InstantiationException ie){
StringBuffer sb = new StringBuffer("InstantiationException: ");
sb.append(ie.getMessage());
ie.printStackTrace(System.out);
throw new FactoryException(sb.toString());
}catch(IllegalAccessException iae){
StringBuffer sb = new StringBuffer("IllegalAccessException: ");
sb.append(iae.getMessage());
iae.printStackTrace(System.out);
throw new FactoryException(sb.toString());
}
return ret;
}
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Jul 2000
Città: Vignola (MO)
Messaggi: 316
|
Fichissimo grazie... adesso lo provo poi ti so dire
__________________
<Asus A8N-E Deluxe> <Amd64 3000+> <Ram 2.5 Gb> <Geffo3> <2 Maxtor 160 Gb RAID 1> <Adsl ZyXEL 645r> <ESS Maestro> <GNU/Linux Debian Etch> Wii 1037 4238 6261 1967 |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 21:08.



















