NodeSortRecordGenerator sortRecord,
ConstantPoolGen cpg,
String className)
{
final InstructionList il = new InstructionList();
final MethodGenerator init =
new MethodGenerator(ACC_PUBLIC,
org.apache.bcel.generic.Type.VOID,
null, null, "<init>", className,
il, cpg);
// Call the constructor in the NodeSortRecord superclass
il.append(ALOAD_0);
il.append(new INVOKESPECIAL(cpg.addMethodref(NODE_SORT_RECORD,
"<init>", "()V")));
final int initLocale = cpg.addMethodref("java/util/Locale",
"<init>",
"(Ljava/lang/String;"+
"Ljava/lang/String;)V");
final int getCollator = cpg.addMethodref(COLLATOR_CLASS,
"getInstance",
"(Ljava/util/Locale;)"+
COLLATOR_SIG);
final int setStrength = cpg.addMethodref(COLLATOR_CLASS,
"setStrength", "(I)V");
final int levels = sortObjects.size();
/*
final int levelsField = cpg.addFieldref(className, "_levels", "I");
il.append(new PUSH(cpg, levels));
il.append(new PUTSTATIC(levelsField));
*/
// Compile code that initializes the locale
String language = null;
String country = null;
Sort sort = (Sort)sortObjects.elementAt(0);
for (int level = 0; level < levels; level++) {
if (language == null && sort._lang != null) {
language = sort._lang;
}
if (country == null && sort._country != null) {
country = sort._country;
}
}
final int collator =
cpg.addFieldref(className, "_collator", COLLATOR_SIG);
final int locale =
cpg.addFieldref(className, "_locale", LOCALE_SIG);
if (language != null) {
// Create new Locale object on stack
il.append(new NEW(cpg.addClass("java/util/Locale")));
il.append(DUP);
il.append(DUP);
il.append(new PUSH(cpg, language));
il.append(new PUSH(cpg, (country != null ? country : EMPTYSTRING)));
il.append(new INVOKESPECIAL(initLocale));
il.append(ALOAD_0);
il.append(SWAP);
il.append(new PUTFIELD(locale));
// Use that Locale object to get the required Collator object
il.append(new INVOKESTATIC(getCollator));
il.append(ALOAD_0);
il.append(SWAP);
il.append(new PUTFIELD(collator));
}
il.append(ALOAD_0);
il.append(new GETFIELD(collator));
il.append(new ICONST(Collator.TERTIARY));
il.append(new INVOKEVIRTUAL(setStrength));
il.append(RETURN);
init.stripAttributes(true);
init.setMaxLocals();
init.setMaxStack();
return init.getMethod();
}