Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Probably you just need to map the primitives and for the rest of the classes perform the "forName" method:</p> <p>I would do something like:</p> <pre><code>void someWhere(){ String methodDescription = "doSomething int double java.lang.Integer java.lang.String" String [] parts = methodDescription.split(); String methodName= parts[0] Class [] paramsTypes = getParamTypes( parts ); // Well, not all the array, but a, sub array from 1 to arr.length.. Method m = someObject.class.getMethod( methodName, paramTypes ); etc. etc etc. } public Class[] paramTypes( String [] array ){ List&lt;Class&gt; list = new ArrayList&lt;Class&gt;(); for( String type : array ) { if( builtInMap.contains( type )) { list.add( builtInMap.get( type ) ); }else{ list.add( Class.forName( type ) ); } } return list.toArray(); } // That's right. Map&lt;String,Class&gt; builtInMap = new HashMap&lt;String,Class&gt;();{ builtInMap.put("int", Integer.TYPE ); builtInMap.put("long", Long.TYPE ); builtInMap.put("double", Double.TYPE ); builtInMap.put("float", Float.TYPE ); builtInMap.put("bool", Boolean.TYPE ); builtInMap.put("char", Character.TYPE ); builtInMap.put("byte", Byte.TYPE ); builtInMap.put("void", Void.TYPE ); builtInMap.put("short", Short.TYPE ); } </code></pre> <p>That is, create a map where the primitives types are stored and if the description belong to a primitive then use the mapped class. This map may also be loaded from an external configuration file, to add flexibility so you add String as a built in instead of java.lang.String or potentially have method like this.</p> <p>"doSomething string yes|no " </p> <p>There are lots of this kind of code in OS projects like Struts, Hibernate, Spring and Apache libs ( just to mention a few ) , so you don't need to start from zero.</p> <p>BTW. I did not compile the above code, but I'm pretty sure it works with little modifications don't down vote me for that.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload