Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

Java reflection not agreeing with method declaration -


sorry length of question. i'm new java , i've come across stumping me. i'm new java don't know terminology yet, please bear me; have 3 years of php experience (mostly procedural, not oo), little java. i'm aware debugging system.out.println wrong way it, works , it's i'm used (insert joke php programmers here if must). i'm still trying figure out how use netbeans debugger.

i'm working on adding feature web application uses struts (1.x). problem i'm having seems method declared want string passed it, doing reflection on method says wants string[] (a string array). i'm constrained in can't make major structural changes app, , of course have make sure don't break in app working, i'm trying make changes in context of what's there. so, problem...

here method declared (many many lines cut these show hope relevant bits):

aereportbean.java:

public class aereportbean {     private string selecteddownloadfields = null;      public string getselecteddownloadfields() {         return selecteddownloadfields;     }      // note there no overloading of function anywhere, declaration.     public void setselecteddownloadfields(string selecteddownloadfields) {         this.selecteddownloadfields = selecteddownloadfields;     } } 

when user clicks submit on form, gets handled aereportsubmitaction.java:

public class aereportsubmitaction extends baseaction {     public actionforward doexecute(             actionmapping mapping,             actionform form,             httpservletrequest request,             httpservletresponse response         ) throws exception {         // works fine, paramater getting passed in request:         system.out.println("url parameter: " + request.getparameter("selecteddownloadfields");          aereportbean bean = new aereportbean(request.getlocale(), 0);          propertyutil.setallfromrequest(request, bean);         // prints "null", meaning setallfromrequest line above failing set property.         system.out.println("aereportsubmitaction.java - bean.getselecteddownloadfields() after setallfromrequest: " + bean.getselecteddownloadfields());     } } 

propertyutil.setallfromrequest() magic, , real problem, happens:

public class propertyutil {     /**      * takes parameters request object , if there's matching      * mutator method in bean, sets      */     static public void setallfromrequest(servletrequest request, object out) {         // iterate through request parameter names , try set each one.         (enumeration parameternames = request.getparameternames(); parameternames.hasmoreelements();) {             string name = (string) parameternames.nextelement();             try {                 propertyutil.setsimpleproperty(out, name, request.getparameter(name));             }             catch (exception e) {                 log.info("exception while setting properties request. parametername=" + name, e);             }         }     }      /**      * sets property object using object's mutator method.      * assumes naming conventions accessor methods      * @param bean object property      * @param property name of property obtain      * @param newproperty object set      */     // note: seems wrapper method below it...     static public void setsimpleproperty(object bean, string property, object newproperty) throws exception {         propertyutil.setsimpleproperty(bean, property, newproperty, null);     }      /**      * sets property object using object's mutator method.      * assumes naming conventions accessor methods      * @param bean object property      * @param property name of property obtain      * @param newproperty object set      */     static public void setsimpleproperty(object bean, string property, object newproperty, class type) throws exception {         // capitalize first letter in property , append "set" front         string methodname = "set" + property.substring(0, 1).touppercase() + property.substring(1);         method method;          class[] parameters;          // if type passed in when method called, add class array.         if (type != null) {             parameters = new class[]{type};         }         // if type not specified, determine type's class calling getclass() on it; class used below call appropriate setter method.         else {             parameters = new class[]{newproperty.getclass()};         }          // here's reflection problem...         // iterate through methods in bean.  if method named "setselecteddownloadfields", print out info it.         (method m : bean.getclass().getmethods()) {             if (m.getname().equals("setselecteddownloadfields")) {                 // newproperty incoming data comes html form field.                 system.out.println("newproperty.getclass(): " + newproperty.getclass()); // prints "class java.lang.string"                      // added cameron skinner in comments.                     system.out.println("m.togenericstring: " + m.togenericstring()); // prints "public void com.[company deleted].bean.aereportbean.setselecteddownloadfields(java.lang.string[])"                  system.out.println("m.getname(): " + m.getname()); // prints "setselecteddownloadfields"                 system.out.println("parameters:");                 (class c : m.getparametertypes()) {                     system.out.println("--c.getcanonicalname(): " + c.getcanonicalname()); // prints "java.lang.string[]"                     system.out.println("--c.getname(): " + c.getname()); // prints "[ljava.lang.string;"                 }             }         }           // , here's fails...         try {             system.out.println("bean.getclass(): " + bean.getclass());  // prints "class com.[company deleted].bean.aereportbean"             system.out.println("methodname: " + methodname); // prints "setselecteddownloadfields"             system.out.println("for (class p : parameters):");             (class p : parameters) {                 system.out.println("--p.getcanonicalname(): " + p.getcanonicalname()); // prints "java.lang.string"             }              // here looks method called, effectively, aereportbean.setselecteddownloadfields(string s), above see reflection showing aereportbean.setselecteddownloadfields(string[] s), try block fails.             method = bean.getclass().getmethod(methodname, parameters);         }         catch (nosuchmethodexception e) {             // lines below here fail until bombs out exception @ bottom...              // if no method can found, see if it's primitive type             // has been wrapped             class valueclass = newproperty.getclass();             //system.out.println("valueclass.tostring() = " + valueclass.tostring());             try {                 if (valueclass.equals(integer.class)) {                     method = bean.getclass().getmethod(methodname, new class[]{int.class});                 }                 else if (valueclass.equals(double.class)) {                     method = bean.getclass().getmethod(methodname, new class[]{double.class});                 }                 else if (valueclass.equals(long.class)) {                     method = bean.getclass().getmethod(methodname, new class[]{long.class});                 }                 else if (valueclass.equals(float.class)) {                     method = bean.getclass().getmethod(methodname, new class[]{float.class});                 }                 else {                     throw new exception(e.getmessage());                 }             }             catch (nosuchmethodexception ex) {                 throw new exception(ex.getmessage());             }         }          // if had gotten point, call method appropriate parameters, , property set.         try {             // execute method             method.invoke(bean, new object[]{newproperty});         }         catch (exception ex) {             throw new exception(ex.getmessage());         }     } } 

i don't know i'm missing here, there must something. other html form elements on same page work perfectly. please let me know if more information needed. thanks!

the code results doesn't lie. it's telling class isn't expect be. you've multiple aereportbean classes of different versions in classpath of project, possibly in different packages, , wrong 1 has been imported or got precedence in classloading. type/class search in netbeans find classes given name in classpath (i don't netbeans, in eclispe it's ctrl+shift+t, netbeans equivalent alt+shift+o).

update: possible cause netbeans didn't build project automatically on save of source file (an ide should create/refresh .class files during build). somewhere in settings.


Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -