Java executes string methods with method

Source: Internet
Author: User
Java executes string methods with method Reference test assembly method public static void main (string[] args) {samplingformdto samplingform=new samplingformdto (); try{   String top = "settop (23.1)";   dotmethod (Top,samplingform); catch (Exception e) {    e.printstacktrace ();}} /** * Due to the number of coordinate points field, using reflection analysis method to solve this problem * @param methodstr method name * @param obj parameter * @throws nosuchmethodexception * @throws Securitye Xception * @throws illegalaccessexception * @throws illegalargumentexception * @throws invocationtargetexception/&nbsp ;   private void Dotmethod (String methodstr,object obj) throws Nosuchmethodexception, SecurityException, Illegalaccessexception, IllegalArgumentException, invocationtargetexception{       //Disassembly method String, Find method name         String methodname = methodstr.substring (0, Methodstr.indexof ('));        //Find parameters         float[] args = null;         String paramstr = methodstr.substring (Methodstr.indexof (') +1,methodstr.indexof (') '));         if (!paramstr.isempty ()) {            string[] tmp = PARAMSTR.SPL It (",");             args = new Float[tmp.length];             Args[0] = Float.parsefloat (tmp[0]);        }         Class C = Obj.getclass ();         Method method = C.getmethod (methodname, Float.class); Note that invoke parameters do not write wrong, your method is what type, invoke on what type parameters         Method.invoke (obj, args[0]);  
} }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.