java - How to solve IllegalArgumentException? -
i have application uses classloader , reflection load , invoke methods. @ runtime receive exception : java.lang.illegalargumentexception : object not instance of declaring class
. here code:
file operatorfile = new file(operatorspath); url operatorfilepath = operatorfile.tourl(); url[] operatorfilepaths = new url[]{operatorfilepath}; classloader operatorsloader = new urlclassloader(operatorfilepaths); class operatorinterface = operatorsloader.loadclass("operator"); filenamefilter filterdotclass = new filenamefilter() { public boolean accept(file dir, string name) { return name.endswith(".class"); } }; file[] operatorfiles = new file(operatorspath).listfiles(filterdotclass); class[] operatorclass = new class[operatorfiles.length-1]; int index = 0; method methodsinoperator; object instance; string operatorsign; map<string , object> operatormap = new hashmap<string , object>(); (file oprfile : operatorfiles) { string filename = oprfile.getname(); //string filename = "operators."+oprfile.getname(); filename = filename.substring(0, filename.length() - 6); class currentclass = operatorsloader.loadclass(filename); if (!filename.equalsignorecase("operator") && operatorinterface.isassignablefrom(currentclass)) { operatorclass[index] = currentclass; instance = currentclass.newinstance(); methodsinoperator = currentclass.getmethod("getsign", null); operatorsign = (string)methodsinoperator.invoke(instance, null); operatormap.put(operatorsign,instance); ++index; } } while(expression != null) { string[] elementinexpression = expression.split(","); if(elementinexpression.length != 3) { system.out.println("expression should have 2 operand , 1 operator "); throw new validationexception("validation on number of element failed1"); } validation.validateoperand(elementinexpression[0],elementinexpression[1]); validation.validateoperator(elementinexpression[2]); double firstnumber = double.parsedouble(elementinexpression[0]); double secondnumber = double.parsedouble(elementinexpression[1]); double output = 0; object obj; for(class operatorcls : operatorclass) { obj = (object)operatormap.get(elementinexpression[2]); methodsinoperator = operatorcls.getmethod("calculate", new class[] { double.class, double.class } ); output =(double)methodsinoperator.invoke(obj, firstnumber, secondnumber); } processingresult.add(output); expression = mathexpreader.readline(); } readerwriter.writefile(path,processingresult);
can me?
Comments
Post a Comment