JBoss Community

ClassCastException when modifiying JOptionPane behaviour

created by Telcontar Thorongil in Javassist - View the full discussion

I'm triying to modify the JOptionPane behaviour to log all the user responses. If do it when the class is loaded with HotsSapper is OK, but i can't do it whith the HotSwapper because the application it's launched by Java Web Start and i haven't be able to open the needed socket.

 

If i try to modify the class before it's loaded i have an ClassCastException throwed by BasicOptionPaneUI. there is the code i use:

 

 

public class Test2 {

    public static void main(String[] args) throws Exception {

        ClassPool cp = ClassPool.getDefault();

        CtClass cc = cp.get("javax.swing.JOptionPane");

        CtMethod[] methods = cc.getMethods();

        for (CtMethod method : methods) {

            if (method.getName().equals("showConfirmDialog")) {

                if(method.getLongName().equals("javax.swing.JOptionPane.showConfirmDialog(java.awt.Component,java.lang.Object,                               java.lang.String,int,int,javax.swing.Icon)")){

                    String after = "System.out.println(\"User answered: \" + $_ );";

                    method.insertAfter(after);

                }

            }

        }

        try{

            Class c=cc.toClass();

            c.newInstance();

        }catch(CannotCompileException ex){

            ex.printStackTrace();

        }

        JOptionPane.showConfirmDialog(null,"Yer or Not?");

    }

}

 

Why is this happening? how can i fix it?

Reply to this message by going to Community

Start a new discussion in Javassist at Community