about how to invoke a method using reflection.

35 views
Skip to first unread message

aro...@naevatec.com

unread,
Nov 4, 2013, 4:21:22 AM11/4/13
to google-code...@googlegroups.com
hi, i need that a method from a third party been executed, the metod is org.mobicents.as7.deployment.SIPWebContext.setApplicationName(String).

I have this code, newAppNameFromCallServiceString is a String:

        @SuppressWarnings("unchecked")
        Set<Method> setters = getAllMethods(org.mobicents.as7.deployment.SIPWebContext.class,
                                            withModifier(Modifier.PUBLIC),
                                            withPrefix("setApplicationName"),
                                            withParametersAssignableTo(String.class),
                                            withParametersCount(1));
        Object ctx = servletConfig;
        Object ctx3 =null;
        try {
            Field fWrapper = ctx.getClass().getDeclaredField("wrapper");
            fWrapper.setAccessible(true);
            Object ctx2 = fWrapper.get(ctx);
            Field fParent = Class.forName("org.apache.catalina.core.ContainerBase").getDeclaredField("parent");
            fParent.setAccessible(true);
            ctx3 = fParent.get(ctx2);
            if (ctx3.getClass().getName().equals("org.mobicents.as7.deployment.SIPWebContext")) {
               
                for (Method aMethod : setters) {
                    System.out.println("METODO SET: " + aMethod.getName());
                    if ("setApplicationName".equals(aMethod.getName())) {
                        System.out.println("ENCONTRADO setApplicationName!!!");
                        //invoke(ctx3, new Object[] {});
                        //necesitas que ctx3 sea una instancia del objeto!!!, si no da un  java.lang.IllegalArgumentException: object is not an instance of declaring class
//i need that ctx3 be an instance from
org.mobicents.as7.deployment.SIPWebContext.class, but how do i get an instance from this? i cannot do a new Object() If i dont get an instance, i get a IllegalArgumentException
//                        aMethod.invoke(ctx3, new Object[] {newAppNameFromCallServiceString});
                        break;
                    }
                }
               
            }
        } catch (java.lang.Throwable xcp) {
            xcp.printStackTrace();
        }


Could anybody help me with my issue? it is my first time using Reflection, and probably the subject of the question is out of the limits, but if someone could point me in the right direction?

thanks


aro...@naevatec.com

unread,
Nov 4, 2013, 7:01:47 AM11/4/13
to google-code...@googlegroups.com
hi, i have tried with this code and still doesn t work, it gives me a java.lang.InstantiationException: org.mobicents.as7.deployment.SIPWebContext.

I have reviewed the code from Class class and i can see that it crashes when tries to get the default constructor.

<my-code>

try {
            Class<?> clazzClass = Class.forName("org.mobicents.as7.deployment.SIPWebContext");
            //here happens the exception, when tries to create an instance
            org.mobicents.as7.deployment.SIPWebContext instanceSipWebContext = (org.mobicents.as7.deployment.SIPWebContext) clazzClass.newInstance();

            for (Method aMethod : setters) {
                System.out.println("METODO SET: " + aMethod.getName());
                if ("setApplicationName".equals(aMethod.getName())) {
                    System.out.println("ENCONTRADO setApplicationName!!!");
                    // invoke(ctx3, new Object[] {});
                    // necesitas que ctx3 sea una instancia del objeto!!!, si no
                    // da un java.lang.IllegalArgumentException: object is not
                    // an instance of declaring class
                    aMethod.invoke(instanceSipWebContext, new Object[] { newAppNameFromCallServiceString });
                    break;
                }
            }
           
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Throwable th) {
            th.printStackTrace();
        }
</my-code>

Thanks in advance, i keep doing my research

Reply all
Reply to author
Forward
0 new messages