CannotCompileExceptio-[source error]: no such class
leooon Jan 21, 2009 7:23 AMHi, I am getting 'CannotCompileExceptio-[source error]: no such class: MyAlert' error while making a reference to an static Inner class name 'MyAlert'.
*********below is my code***********
public void addCodeToStartup(String strClass, String classpath){
System.out.println("\tupdating '"+strClass.trim()+"' located at '"+classpath+"' ...");
String cp2 = "";
try{
cp2 = classpath.substring(0, classpath.lastIndexOf('\\'));
System.out.println("\tcp2 '"+cp2);
//String pkg = "";
//pkg = strClass.substring(0, strClass.lastIndexOf('.'));
}catch(StringIndexOutOfBoundsException siobe){
System.out.println("StringIndexOutOfBoundsException: "+siobe.getMessage());
}
//System.out.println("\tpkg '"+pkg+"'");
try {
ClassPool cp = ClassPool.getDefault();
cp.insertClassPath(".");
cp.insertClassPath(cp2+'\\');
cp.insertClassPath(classpath+'\\');
cp.importPackage("javax.microedition.lcdui");
cp.importPackage("javax.microedition.midlet");
cp.importPackage("javax.microedition.rms");
////////////////////////////////////////////////////////////////////////
CtClass cc = cp.get(strClass.trim());
CtClass anInterface = cp.get("javax.microedition.lcdui.CommandListener");
CtClass aSuperClass = cp.get("javax.microedition.lcdui.Alert");
CtClass aUiClass = cc.makeNestedClass("MyAlert", true);
System.out.println("\t1");
aUiClass.setSuperclass(aSuperClass);
aUiClass.addInterface(anInterface);
System.out.println("\t2");
CtField f = CtField.make("Command cmdOk;", aUiClass);
aUiClass.addField(f);
System.out.println("\t3");
f = CtField.make("static MIDlet midlet1;", aUiClass);
aUiClass.addField(f);
System.out.println("\t4");
CtConstructor c = CtNewConstructor.make(
"MyAlert(MIDlet midlet, String info) {"
+"\n super(\"Warning\", info, null, AlertType.WARNING);"
+"\n midlet1 = midlet;"
+"\n cmdOk = new Command(\"OK\", Command.EXIT, 1);"
+"\n addCommand(cmdOk);"
+"\n setCommandListener(this);"
+"\n }",
aUiClass);
aUiClass.addConstructor(c);
System.out.println("\t5");
CtMethod m = CtMethod.make(
"public void commandAction( Command cmd, Displayable displayable){"
+"\n\nif (cmd == cmdOk){"
+"\n midlet1.notifyDestroyed();"
+"\n}"
+"\n}",
aUiClass);
aUiClass.addMethod(m);
System.out.println("\t6");
m = CtMethod.make(
"static MyAlert getMyAlert(MIDlet newMIDlet, String s){"
+"\nreturn new MyAlert(newMIDlet, s);"
+"\n}",
aUiClass
);
aUiClass.addMethod(m);
System.out.println("\t7");
aUiClass.writeFile();
System.out.println("\tMyAlert class created!");
String dest = classpath +'\\'+ aUiClass.getName();
dest = dest.replace('.', '\\');
dest += ".class";
String newClass = aUiClass.getName();
newClass = newClass.replace('.', '\\')+".class";
System.out.println("\tnewClass: "+newClass);
System.out.println("\tdest: "+dest);
File file = null, destFile = null;
file = new File(newClass);
//destFile = new File(dest);
//file.renameTo(destFile);
//if (file.delete())
//System.out.println("\tfile succefull renames and copy is deleted!");
////////////////////////////////////////////////////////////////////////
// MIDlet TO BE MODIFIED //
////////////////////////////////////////////////////////////////////////
m = cc.getDeclaredMethod("startApp");
System.out.println("\t1");
//ADD YOUR startUp code here
m.insertBefore(
"{"
+"\n\n boolean flag=true;"
+"\n byte[] kb=null;"
+"\n\n try {"
+"\n RecordStore settings = RecordStore.openRecordStore(\"Demo\", true);"
+"\n if (settings.getNumRecords() <= 0) {"
+"\n //startTime = System.currentTimeMillis();"
+"\n kb=Long.toString(System.currentTimeMillis()).getBytes();"
+"\n settings.addRecord(kb,0,kb.length);// time in millis"
+"\n byte[] counter={0};"
+"\n settings.addRecord(counter,0,1); // counter"
+"\n flag= true;"
+"\n }"
+"\n\n kb=settings.getRecord(2);"
+"\n kb[0]=(byte)(kb[0]+1);"
+"\n System.out.println(\"value of counter \" + kb[0]);"
+"\n settings.setRecord(2,kb,0,1);"
+"\n\n if(settings.getRecord(2)[0]>2) {"
+"\n flag=false;"
+"\n }"
+"\n\n settings.closeRecordStore();"
+"\n }catch(Exception e) {"
+"\n e.printStackTrace();"
+"\n flag=false;"
+"\n }"
+"\n if(!flag) "
+"\n Display.getDisplay(this).setCurrent(MyAlert.getMyAlert(this, \"Your MIDlet expired, download again\"));"
+"\n else"
+"\n Display.getDisplay(this).setCurrent(MyAlert.getMyAlert(this, \"Still Valid!\"));"
+"\n "
+"}"
);
System.out.println("\tmethod created");
cc.writeFile();
System.out.println("\tYessssss");
}catch (NotFoundException nfe) {
System.out.println("\tNotFoundException: "+nfe.getMessage());
}catch (CannotCompileException cce) {
System.out.println("\tCannotCompileException: "+cce.getMessage());
System.out.println(cce.getReason());
System.out.println(cce.getCause());
}catch (IOException ioe) {
System.out.println("\tIOException: "+ioe.getMessage());
}
}
**********************************
Any sample example how to inject a static InnerClass is appreciated.
Best Regards
Mukesh