When the system is integrated, the system sends information whether it is through the network or case, we often encounter the fixed length of each digit of the parameter string. How can this program be easier to understand than the limit? Here is a method provided by me. Using annotation, the setting value of the parameter string is directly stored in the program, rather than in the setting parameter, this should be more intuitive.
There is a program to show the Term of office and history of the central Democratic Republic of China. The statement format is as follows:
First, we define an annotation, which can set the starting position and length of a bit, as follows:
1 package idv.steven; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 8 @Retention(RetentionPolicy.RUNTIME) 9 @Target(ElementType.FIELD)10 public @interface MsgField {11 public int start();12 public int length();13 }
Then define a pojo, and set the starting position and length of each digit in a similar sense, because there are four operators in interest, of course, this category also has four corresponding positions.
1 package IDV. steven. pojo; 2 3 Import IDV. steven. msgfield; 4 5 public class message {6 @ msgfield (START = 0, length = 4) 7 private string startyear; 8 @ msgfield (START = 4, length = 4) 9 private string endyear; 10 @ msgfield (START = 8, length = 10) 11 private string name; 12 @ msgfield (START = 18, length = 30) 13 private string evaluate; 14 15/** 16 * @ return start year 17 */18 Public String getstartyear () {19 Return startyear; 20} 21 public void setstartyear (string startyear) {22 This. startyear = startyear; 23} 24 25/** 26 * @ return year 27 */28 Public String getendyear () {29 return endyear; 30} 31 public void setendyear (string endyear) {32 this. endyear = endyear; 33} 34 35/** 36 * @ return name 37 */38 public string getname () {39 return name; 40} 41 public void setname (string name) {42 this. name = Name; 43} 44 45/** 46 * @ return response 47 */48 Public String getevaluate () {49 return evaluate; 50} 51 public void setevaluate (string evaluate) {52 this. evaluate = evaluate; 53} 54}
The following is a program for analyzing interest...
1 package IDV. steven; 2 3 Import Java. lang. reflect. field; 4 Import Java. lang. reflect. invocationtargetexception; 5 import Java. lang. reflect. method; 6 Import Java. util. arrays; 7 8 Import IDV. steven. pojo. message; 9 10/** 11 * analyze the messages with Fixed Length of padding 12 * @ author Steven 13 */14 public class parser {15 private message parsing (byte [] MSG) 16 throws illegalargumentexception, illegalaccessexception, 17 nosuchmethodexcep Tion, securityexception, 18 invocationtargetexception, classnotfoundexception {19 20 message = new message (); 21 22 class <?> Aclass = Class. forname (message. class. getname (); 23 field [] fields = Aclass. getdeclaredfields (); 24 for (field: fields) {25 msgfield = field. getannotation (msgfield. class); 26 if (msgfield! = NULL) {27 byte [] submsg = arrays. copyofrange (MSG, msgfield. start (), msgfield. start () + msgfield. length (); 28 29 method = Aclass. getmethod (getsettername (field. getname (), String. class); 30 method. invoke (message, new string (submsg); 31 // field. set (message, new string (submsg); 32} 33} 34 35 return message; 36} 37 38 private string getsettername (string fieldname) {39 return string. format ("set % S % S ", fieldname. substring (0, 1 ). touppercase (), fieldname. substring (1); 40} 41 42 Public void run (byte [] MSG) {43 try {44 message = parsing (MSG); 45 46 system. out. println ("term of service:" + message. getstartyear () + "~ "+ Message. getendyear (); 47 system. out. println ("Total name:" + message. getname (); 48 system. out. println ("History history failed:" + message. getevaluate (); 49} catch (exception e) {50 e. printstacktrace (); 51} 52} 53 54 public static void main (string [] ARGs) {55 56 new Parser (). run ("20082016 Ma yingjiu, a selfish idiot, hit the masses! ". Getbytes (); 57} 58}
The results are as follows:
Term of office: 2008 ~ 2016 President's name: Ma yingjiu's historical Director: A selfish idiot who has been infected by the Chinese people!