jeudi 1 septembre 2016

Dynamically loading method from external class

I am trying to load methods Customer.cypher and Customer.cypherCBC method from my class Configuration. Customer class is rendering from different environments so few environmets are having cypherCBC() and cypher() method and few are having only cypher() method.

Now i want to check if cypherCBC if not there in Customer class then load cypher() method. My function is so far;

   try {
        Class<?> customerClass = Class.forName("com.myapp.impl.service.Customer");

        Object  obj = customerClass.newInstance();

        //here getting "NoSuchMethodException" exception
        Method methodCBC = client.getDeclaredMethod("cypherCBC", String.class); //line - 7


         if(methodCBC.getName().equals("cypherCBC")){
            methodCBC.invoke(obj, new String(dbshPass));
            System.out.println("CYPHER_CBC: "
               + methodCBC.invoke(obj, new String(dbshPass)));
        }else{

            Method method = customerClass.getDeclaredMethod("cypher", String.class);
            method.invoke(obj, new String(dbshPass));
            System.out.println("CYPHER: " + method.invoke(obj, new String(dbshPass)));
        }

    }catch (Exception e){
        e.printStackTrace();
    }

Getting an error at line 7.

NoSuchMethodException: com.myapp.impl.service.Customer.cypherCBC(java.lang.String)

that means for particular environment class Customer doesn't having cypherCBC() method, but ideally it should come in else part and execute cypher() method.





Aucun commentaire:

Enregistrer un commentaire