vendredi 25 mai 2018

Method .invoke() Java Underlying Code Syntax Errors or Errors in general

I've been using reflection in Java to mark submitted Java code, and I've just noticed that when the the underlying code has an error(syntax etc), the return value will simply default to the last correct invocation of that method. I'm not really sure how to solve this. Is there any way to simply return null if the underlying method has an error?

public static Object runIt(Class[] class_holder, Object[] variable_holders, String methods) {
        try {
          Class thisClass = Class.forName("Test"); //creating a new instance of the class Test
          Object iClass = thisClass.newInstance();
          Method thisMethod = thisClass.getDeclaredMethod(methods, class_holder); //getting the specified method
          Object result = thisMethod.invoke(iClass, variable_holders); //invoking the method
          return result; 
          }
        catch (Exception e) {
          e.printStackTrace();
        }
        return null; 
      }

In this case, result just returns the previous error-free implementation of the underlying method.





Aucun commentaire:

Enregistrer un commentaire