How to create an object of a given class NAME?

M

Miguel Farah

I got the following piece of code:

static private Idiom determineIdiom(Locale region) {
if (region==null) region=DEFAULT_REGION;
String language=region.getLanguage();
if (language==null) language=DEFAULT_LENGUAJE;

Idiom myIdiom=null;
if (language.equals("es")) { myIdiom=(Idiom)new
Idiom_es();
} else if (language.equals("en")) { myIdiom=(Idiom)new
Idiom_en();
} else if (language.equals("ca")) { myIdiom=(Idiom)new
Idiom_ca();
} else if (language.equals("fr")) { myIdiom=(Idiom)new
Idiom_fr();
} else {
myIdiom=(Idiom)new Idiom_es(); //default
}
return myIdiom;
}

All the Idiom_* classes extend Idiom, which in turn implements an
interface. If I want to add support for a new language (say, finnish),
I
have to write a Idiom_fi class AND I have to put a new if sentence in
this method:

} else if (language.equals("fi")) { myIdiom=(Idiom)new
Idiom_fi();


I'd like to get rid of the second requirement, and for that I'd need to
be able to
compose the name of the class I want to instance, sorta like this:

myIdiom=(Idiom)new {{"Idiom_"+language}}();

I'm not sure if this can be done in Java. Can it?

Thanks in advance.
 
J

Joe Attardi

myIdiom=(Idiom)new {{"Idiom_"+language}}();

I'm not sure if this can be done in Java. Can it?

You can use reflection. For example:

String idiomClassName = "Idiom_" + language;
Class idiomClass = Class.forName(idiomClassName);
Idiom idiom = idiomClass.newInstance();

Note that Class.forName() will throw ClassNotFoundException if an
invalid class name is given.
Hope this helps!
 
K

Karl Uppiano

Miguel Farah said:
I got the following piece of code:

static private Idiom determineIdiom(Locale region) {
if (region==null) region=DEFAULT_REGION;
String language=region.getLanguage();
if (language==null) language=DEFAULT_LENGUAJE;

Idiom myIdiom=null;
if (language.equals("es")) { myIdiom=(Idiom)new
Idiom_es();
} else if (language.equals("en")) { myIdiom=(Idiom)new
Idiom_en();
} else if (language.equals("ca")) { myIdiom=(Idiom)new
Idiom_ca();
} else if (language.equals("fr")) { myIdiom=(Idiom)new
Idiom_fr();
} else {
myIdiom=(Idiom)new Idiom_es(); //default
}
return myIdiom;
}

All the Idiom_* classes extend Idiom, which in turn implements an
interface. If I want to add support for a new language (say, finnish),
I
have to write a Idiom_fi class AND I have to put a new if sentence in
this method:

} else if (language.equals("fi")) { myIdiom=(Idiom)new
Idiom_fi();


I'd like to get rid of the second requirement, and for that I'd need to
be able to
compose the name of the class I want to instance, sorta like this:

myIdiom=(Idiom)new {{"Idiom_"+language}}();

I'm not sure if this can be done in Java. Can it?

Thanks in advance.

You could use reflection to avoid using hard-coded class names. Then to
support a new language, you would need to write a new class, but plug it in
using a resource bundle to map the language to the class name, e.g.,

in a resource properties file (e.g.,
..../my/idiom/package/Resources.properties),

en_us=my.idiom.package.Idiom_en
de_de=my.idiom.package.Idiom_de

etc. Obviously, there are details, such as variants (e.g., en_us_cajun) that
you might have to deal with.

In your code, instead of a bunch of if statements, you would do something
like this (some details, such as exception handlers, omitted - treat this as
pseudo code):

ResourceBundle resources =
ResourceBundle.getBundle("my.idiom.package.Resources");
String idiomClassName = resources.getString(region.getLanguage());
Class idiomClass = Class.forName(idiomClassName);
Idiom myIdiom = (Idiom)idiomClass.newInstance();

return(myIdiom);

Reflection is a very good way to accomplish plug-ins.
 
M

Miguel Farah

Joe Attardi wrote::
You can use reflection. For example:

String idiomClassName = "Idiom_" + language;
Class idiomClass = Class.forName(idiomClassName);
Idiom idiom = idiomClass.newInstance();

Note that Class.forName() will throw ClassNotFoundException if an
invalid class name is given.
Hope this helps!

Yes it does. This is exactly what I need. Thanks, Joe, Karl.
 
S

Simon Brooke

Karl Uppiano said:
You could use reflection to avoid using hard-coded class names. Then to
support a new language, you would need to write a new class, but plug it
in using a resource bundle to map the language to the class name, e.g.,

Reflection is a very good way to accomplish plug-ins.

Having said that, a separate class for each language is a really bad way to
do internationalisation. Internationalisation should be done through
resource bundles.

--
(e-mail address removed) (Simon Brooke) http://www.jasmine.org.uk/~simon/

;; Human history becomes more and more a race between
;; education and catastrophe.
H.G. Wells, "The Outline of History"
 
C

Chris Uppal

Simon said:
Having said that, a separate class for each language is a really bad way
to do internationalisation.

Unless you want different behaviour depending on national context -- in which
case it's a very good way to do it ;-)

-- chris
 
K

Karl Uppiano

Simon Brooke said:
Having said that, a separate class for each language is a really bad way
to
do internationalisation. Internationalisation should be done through
resource bundles.

If the OP's "Idiom" embodies behavior, my design would probably implement it
as an inner class of ListResourceBundle. If Idiom is basically data, then
simple resource bundles are the way to go.
 
M

Miguel Farah

Karl said:
[...]
Having said that, a separate class for each language is a really bad way
to do internationalisation. Internationalisation should be done through
resource bundles.

If the OP's "Idiom" embodies behavior, my design would probably implement it
as an inner class of ListResourceBundle. If Idiom is basically data, then
simple resource bundles are the way to go.

What I'm doing is a bunch of methods that convert a number ("42") to
the
phrase that represents it ("forty two"), plus the ordinal ("forty
second"), and a couple more constructs in the same vein. Resource
bundles can't handle infinite lines of data, AFAIK. ;-)
 
V

voorth

Have you considered using a ListResourceBundle? This gives you the same
language/locale resolution as a PropertyResourceBundle, but you get to
define the appropriate behaviour yourself.
 
M

Miguel Farah

voorth said:
Have you considered using a ListResourceBundle? This gives you the same
language/locale resolution as a PropertyResourceBundle, but you get to
define the appropriate behaviour yourself.

Unless I give ListResourceBundle less credit than it deserves, it won't
do things as complex as I want to - for example, I'll quote one of the
methods I have - the spanish version of "get a number and return it as
a
phrase" (I want each Idiom_XX class, where XX={es, en, fr, etc.}, to
have its own version of "String convierteEntero(String, int)"):


public String convierteEntero(String numero, int genero) {

if (!(esEnteroValido(numero))) return "número inválido";

String frase="";
boolean negativo=false;

if (numero.charAt(0)=='+') {
numero=numero.substring(1);
}

if (numero.charAt(0)=='-') {
negativo=true;
numero=numero.substring(1);
}

if (numero.equals(ceros(numero.length()))) {
numero="0";
negativo=false; // No queremos entregar "menos cero".
}

if (!(numero.equals("0"))) {
numero=quitaCerosALaIzquierda(numero);
}

if (numero.length()>6) {
// Números mayores que 1.000.000.

for (int i=0;i<RAICES.length;i++) {
int largo=6*(RAICES.length-i);
if (numero.length()>=(largo+1)) {

if
((numero.length()==largo+1)&&(numero.charAt(0)=='1')) {
frase+="un "+RAICES+"ón"
+((numero.equals("1"+ceros(largo)))?"":("
"+convierteEntero(numero.substring(1), genero)));
} else {

frase+=convierteEntero(numero.substring(0,numero.length()-largo),
GENERO_MASCULINO) // Sí, masculino. Ver la nota en la
javadocumentación.
+" "+RAICES+"ones";
String
resto=numero.substring(numero.length()-largo);
if (!(resto.equals(ceros(resto.length())))) {
frase+=" "+convierteEntero(resto, genero);
}
}
break;
}
}

} else {
// Números menores que 1.000.000 - es más fácil
manejarlos como enteros...
int esteNumero=(new Integer(numero).intValue());

if (esteNumero== 0) { frase+="cero";
} else if (esteNumero== 1) {
frase+="un"+(genero==GENERO_FEMENINO?"_":"");
} else if (esteNumero== 2) { frase+="dos";
} else if (esteNumero== 3) { frase+="tres";
} else if (esteNumero== 4) { frase+="cuatro";
} else if (esteNumero== 5) { frase+="cinco";
} else if (esteNumero== 6) { frase+="seis";
} else if (esteNumero== 7) { frase+="siete";
} else if (esteNumero== 8) { frase+="ocho";
} else if (esteNumero== 9) { frase+="nueve";
} else if (esteNumero== 10) { frase+="diez";
} else if (esteNumero== 11) { frase+="once";
} else if (esteNumero== 12) { frase+="doce";
} else if (esteNumero== 13) { frase+="trece";
} else if (esteNumero== 14) { frase+="catorce";
} else if (esteNumero== 15) { frase+="quince";
} else if (esteNumero== 16) { frase+="dieciséis";
} else if ( (esteNumero>=17)
&&(esteNumero<=19)
) {
frase+="dieci"+convierteEntero(numero.substring(1), genero);
} else if (esteNumero== 20) { frase+="veinte";
} else if (esteNumero== 21) {
frase+="veinti"+(genero==GENERO_FEMENINO?"una":"ún");
} else if (esteNumero== 22) { frase+="veintidós";
} else if (esteNumero== 23) { frase+="veintitrés";
} else if ( (esteNumero==24)
||(esteNumero==25)
) {
frase+="veinti"+convierteEntero(numero.substring(1), genero);
} else if (esteNumero== 26) { frase+="veintiséis";
} else if ( (esteNumero>=27)
&&(esteNumero<=29)
) {
frase+="veinti"+convierteEntero(numero.substring(1), genero);
} else if (esteNumero== 30) { frase+="treinta";
} else if (esteNumero== 40) { frase+="cuarenta";
} else if (esteNumero== 50) { frase+="cincuenta";
} else if (esteNumero== 60) { frase+="sesenta";
} else if (esteNumero== 70) { frase+="setenta";
} else if (esteNumero== 80) { frase+="ochenta";
} else if (esteNumero== 90) { frase+="noventa";
} else if (esteNumero<= 99) {
frase+=convierteEntero(numero.substring(0,1)+"0", genero)+" y
"+convierteEntero(numero.substring(1), genero);
} else if (esteNumero==100) { frase+="cien";
} else if (esteNumero<=199) { frase+="ciento
"+convierteEntero(numero.substring(1), genero);
} else if (esteNumero==500) { frase+="quinient_s";
} else if (esteNumero==700) { frase+="setecient_s";
} else if (esteNumero==900) { frase+="novecient_s";
} else if ( (esteNumero==200)
||(esteNumero==300)
||(esteNumero==400)
||(esteNumero==600)
||(esteNumero==800)
) {
frase+=convierteEntero(numero.substring(0,1), genero)+"cient_s";
} else if (esteNumero<=999) {
frase+=convierteEntero(numero.substring(0,1)+"00", genero)+"
"+convierteEntero(numero.substring(1), genero);
} else if (esteNumero==1000) { frase+="mil";
} else if (esteNumero<=1999) { frase+="mil
"+convierteEntero(numero.substring(1), genero);
} else if (esteNumero<=999999) {
frase+=convierteEntero(numero.substring(0,numero.length()-3), genero)+"
mil"

+(numero.substring(numero.length()-3).equals("000")?"":"
"+convierteEntero(numero.substring(numero.length()-3), genero));

} else {
// Esto es imposible por construcción.
frase="número desconocido";
}

}

frase=reemplazaTodo(frase, "_",
(genero==GENERO_FEMENINO?"a":"o"));

return (negativo?"menos ":"")+frase;
}

I don't think ListResourceBundle is up to the task. Of course, I could
be wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top