Call thinking caused by callback mechanism

Source: Internet
Author: User
Tags abstract arrays assert bind

When the interface is called, his implementation class is automatically called (Callback mechanism)

As for the parent subclass, the subclass makes a copy of the method of the parent class and can call the method of the parent class separately, and this method of the subclass is called separately.

The parent class has one copy of the method parent (super), the subclass has a copy (this), and the child class calls the parent class method, which is the subclass itself.

Call the parent class alone with super, then wrap the method in the parent class directly with this. The call can then which sub-class call is Who

Father:

Package com.esteel.settlement.pa.packet;

Import java.io.Serializable;

Import Java.lang.reflect.Field;

Import java.lang.reflect.InvocationTargetException;

Import Java.lang.reflect.Method;

Import Java.lang.reflect.ParameterizedType;

Import Java.lang.reflect.Type;

Import Java.nio.charset.Charset;

Import Java.text.DecimalFormat;

Import java.text.ParseException;

Import Java.text.SimpleDateFormat;

Import java.util.ArrayList;

Import Java.util.Arrays;

Import Java.util.Date;

Import java.util.LinkedList;

Import java.util.List;

Import Javax.validation.constraints.NotNull;

Import Javax.validation.constraints.Pattern;

Import Javax.xml.bind.annotation.XmlAccessType;

Import Javax.xml.bind.annotation.XmlAccessorType;

Import javax.xml.bind.annotation.XmlElement;

Import javax.xml.bind.annotation.XmlTransient;

Import Org.hibernate.validator.constraints.Length;

Import Org.hibernate.validator.constraints.NotEmpty;

Import Org.springframework.format.annotation.DateTimeFormat;

Import Org.springframework.util.Assert;

Import Org.springframework.util.StringUtils;

/**

* The base class of the safe docking interface is used to convert the data format

*

* @author Zhangxiuzhi

*

*/

@XmlAccessorType (Xmlaccesstype.field)

Public abstract class Papacket implements Serializable {

public static String RMB = "RMB";

public static String SUCCESS = "000000";

public static SimpleDateFormat sfdate = new SimpleDateFormat ("YyyyMMdd");

DecimalFormat df = new DecimalFormat ("##### #0.00");

Charset Charset = Charset.forname ("GBK");

Message request Type Request

public static String REQUEST = "01";

Message request Type Request

public static String RESPONSE = "02";

Reserved fields

@XmlElement (name = "Reserve")

Private String Reserve;

@XmlTransient

protected Packetheader Packetheader = new Packetheader ();

@XmlTransient

protected Businessheader Buisinessheader = new Businessheader ();

Trading Network Code

@XmlTransient

@NotEmpty

Private String QYDM;

Trading Network Serial number

@XmlElement (name = "Thirdlogno")

@Length (max = 20)

@NotEmpty

Private String Thirdlogno;

Service type

@XmlTransient

@NotEmpty

@Pattern (regexp = "01|02")

Private String Servtype;

Trading hours

@XmlTransient

@NotNull

@DateTimeFormat (pattern = "YYYYMMDD")

Private Date transdate;

Operator number

@XmlTransient

Private String Counterid;

Transaction code

@XmlTransient

@NotNull

@Length (min = 1, max = 4)

Private String Tranfunc;

Public Papacket () {

This.settranfunc (Tranfunc ());

}

Protected abstract String tranfunc ();

Public String Getreserve () {

return reserve;

}

public void Setreserve (String reserve) {

This.reserve = reserve;

}

Public String GETQYDM () {

return QYDM;

}

public void Setqydm (String qydm) {

THIS.QYDM = QYDM;

Trading Network Code

THIS.PACKETHEADER.SETQYDM (THIS.GETQYDM ());

THIS.BUISINESSHEADER.SETQYDM (THIS.QYDM);

}

Public String Getthirdlogno () {

return Thirdlogno;

}

public void Setthirdlogno (String thirdlogno) {

This.thirdlogno = Thirdlogno;

Request System serial Number

This.packetHeader.setThirdLogNo (This.getthirdlogno ());

This.buisinessHeader.setThirdLogNo (This.getthirdlogno ());

}

Public String Getservtype () {

return servtype;

}

public void Setservtype (String servtype) {

This.servtype = Servtype;

Service Type 01 Request 02 Answer

This.packetHeader.setServType (This.getservtype ());

This.buisinessHeader.setServType (This.getservtype ());

}

Public Date gettransdate () {

return transdate;

}

public void Settransdate (Date transdate) {

This.transdate = TransDate;

Trading hours

This.packetHeader.setTranDate (This.gettransdate ());

This.buisinessHeader.setTranDate (This.gettransdate ());

}

Public String Getcounterid () {

return Counterid;

}

public void Setcounterid (String counterid) {

This.counterid = Counterid;

Operator number

This.packetHeader.setCounterId (Counterid);

This.buisinessHeader.setCounterId (Counterid);

}

Public String Gettranfunc () {

return tranfunc;

}

public void Settranfunc (String tranfunc) {

This.tranfunc = Tranfunc;

This.buisinessHeader.setTranFunc (Tranfunc);

}

public void Setrspcode (String rspcode) {

This.buisinessHeader.setRspCode (Rspcode);

This.packetHeader.setRspCode (Rspcode);

}

Public String Getrspcode () {

return This.buisinessHeader.getRspCode ();

}

public void Setrspmsg (String rspmsg) {

THIS.BUISINESSHEADER.SETRSPMSG (RSPMSG);

THIS.PACKETHEADER.SETRSPMSG (RSPMSG);

}

Public String getrspmsg () {

return THIS.BUISINESSHEADER.GETRSPMSG ();

}

/**

* Parsing message headers

*

* @param packetheader

*/

private void Analyzepacketheader (byte[] packetheader) {

This.packetHeader.analyze (Packetheader);

}

/**

* Parsing Service headers

*

* @param businesserheader

*/

private void Analyzebusinsserheader (byte[] businesserheader) {

This.buisinessHeader.analyze (Businesserheader);

}

/**

* Composing the message into a string message that can be sent

*

* @return

*/

Public String Marshal () {

Assert.notnull (This.packetheader);

Assert.notnull (This.buisinessheader);

Business Report Issues

String data = this.getstring ();

Set the public value in the message

Trading Network Code

THIS.PACKETHEADER.SETQYDM (THIS.GETQYDM ());

THIS.BUISINESSHEADER.SETQYDM (THIS.QYDM);

Request System serial Number

This.packetHeader.setThirdLogNo (This.getthirdlogno ());

This.buisinessHeader.setThirdLogNo (This.getthirdlogno ());

Service Type 01 Request 02 Answer

This.packetHeader.setServType (This.getservtype ());

This.buisinessHeader.setServType (This.getservtype ());

Trading hours

This.packetHeader.setTranDate (This.gettransdate ());

This.buisinessHeader.setTranDate (This.gettransdate ());

Set the message length

This.packetHeader.setPacketLength (Businessheader.headerlen + data.getbytes (charset). length);

This.buisinessHeader.setLength (Data.getbytes (charset). length);

Operator number

This.packetHeader.setCounterId (This.getcounterid ());

This.buisinessHeader.setCounterId (This.getcounterid ());

Set Operation code

This.buisinessHeader.setTranFunc (This.gettranfunc ());

Byte[] cs = This.packetHeader.getPacketHeader ();

StringBuilder sb = new StringBuilder ();

Sb.append (New String (CS, CharSet));

Sb.append (New String (This.buisinessHeader.getBusinessHeader (), CharSet));

Sb.append (data);

return sb.tostring ();

}

/**

* Generate the required message string

*

* @return

*/

Public String getString () {

StringBuilder sb = new StringBuilder ();

try {

For (String FName:this.order ()) {

Object obj = GetValue (fName);

if (obj = = null) {

Sb.append ("");

}

if (obj instanceof String) {

Sb.append ((String) obj);

}

if (obj instanceof Integer) {

Sb.append ((Integer) obj). Intvalue ());

}

if (obj instanceof Long) {

Sb.append (((Long) obj). Intvalue ());

}

if (obj instanceof Double) {

Sb.append (Df.format (Double) obj);

}

if (obj instanceof Date) {

Sb.append (Sfdate.format (obj));

}

Sb.append ("&");

}

} catch (Nosuchfieldexception | SecurityException | nosuchmethodexception | illegalaccessexception | IllegalArgumentException | InvocationTargetException e) {

E.printstacktrace ();

}

Sb.deletecharat (Sb.length ()-1);

return sb.tostring ();

}

/**

* Used to get attribute values when obtaining attribute names

*

* @param fName

* @return

* @throws nosuchfieldexception

* @throws SecurityException

* @throws nosuchmethodexception

* @throws illegalaccessexception

* @throws IllegalArgumentException

* @throws InvocationTargetException

*/

Private Object GetValue (String fName) throws Nosuchfieldexception, SecurityException, Nosuchmethodexception, Illegalaccessexception, IllegalArgumentException, invocationtargetexception {

FName = fname.substring (0, 1). toUpperCase () + fname.substring (1);

method = This.getclass (). GetMethod ("get" + fName);

Assert.notnull (method);

Return Method.invoke (this);

}

/**

* Set the value of the object

*

* @param fName

* @param value

* @throws nosuchmethodexception

* @throws SecurityException

* @throws nosuchfieldexception

* @throws IllegalArgumentException

* @throws illegalaccessexception

* @throws InvocationTargetException

* @throws ParseException

*/

private void SetValue (Object obj,string fieldName, list<string> values)

Throws Nosuchmethodexception, SecurityException, Nosuchfieldexception, IllegalArgumentException, Illegalaccessexception, InvocationTargetException, parseexception {

if (values = = null| | Values.isempty ()) {

Return

}

Used to determine if the first element in the values is removed, by default, but not deleted when the recursive call is processed

Boolean ismove=true;

String value = values.get (0);

String fName = fieldname.substring (0, 1). toUpperCase () + fieldname.substring (1);

Method get = Obj.getclass (). GetMethod ("get" + fName);

method = Obj.getclass (). GetMethod ("set" + FName, Get.getreturntype ());

Assert.notnull (method);

Switch (Get.getreturntype (). Getsimplename ()) {

Case "String": {

Method.invoke (obj, value);

Break

}

Case "int":

Case "Integer": {

if (Value.indexof (".") >-1) {

Value = value.substring (0, Value.indexof ("."));

}

Method.invoke (obj, new Integer (value));

Break

}

Case "Double":

Case "Double": {

Method.invoke (obj, Df.parse (value));

Break

}

Case "Long":

Case "Long": {

if (Value.indexof (".") >-1) {

Value = value.substring (0, Value.indexof ("."));

}

Method.invoke (obj, new Long (value));

Break

}

Case "Date": {

try{

Date date = Sfdate.parse (value);

Method.invoke (obj, date);

}catch (Exception ex) {

}

Break

}

Case "List": {

Field field = Obj.getclass (). Getdeclaredfield (FieldName);

Arraysizemethod Arraysizemethod = field.getannotation (Arraysizemethod.class);

Number of loops recorded by default 1

int numsize = 1;

if (arraysizemethod!=null) {

String sizemethodname = Arraysizemethod.value ();

if (! Stringutils.isempty (Sizemethodname)) {

Method Sizemethod = Obj.getclass (). GetMethod (Sizemethodname);

Numsize = ((int) sizemethod.invoke (obj));

}

}

list<resultarray> list = new arraylist<resultarray> ();

Type FC = Field.getgenerictype ();

if (FC instanceof Parameterizedtype) {

Parameterizedtype pt = (parameterizedtype) FC;

Class<?> Genericclazz = (class<?>) pt.getactualtypearguments () [0];

try {

Resultarray result = (resultarray) genericclazz.newinstance ();

string[] orders = Result.order ();

for (int i=0;i<numsize;i++) {

for (String str:orders) {

SetValue (result,str,values);

}

List.add (result);

result = (resultarray) genericclazz.newinstance ();

}

} catch (Instantiationexception e) {

E.printstacktrace ();

}

}

Method.invoke (obj, list);

Ismove = false;

Break

}

default:{

System.out.println (Get.getreturntype (). Getsimplename ());

}

}

Remove the first element in a queue

if (ismove) {

Values.remove (0);

}

}

/**

* Depending on the

*

* @param str

*/

public void analyze (String packetstr) {

byte[] src = packetstr.getbytes (charset);

Assert.istrue (Src.length > 344);

byte[] ph = new byte[222];

byte[] bh = new byte[122];

System.arraycopy (SRC, 0, ph, 0, ph.length);

System.arraycopy (SRC, 222, BH, 0, bh.length);

This.analyzepacketheader (ph);

This.analyzebusinsserheader (BH);

byte[] data = new byte[src.length-344];

System.arraycopy (SRC, 344, data, 0, data.length);

String str = new string (data, CharSet);

Set the value of the attribute in the header file to the public message

This.setcounterid (This.buisinessHeader.getCounterId ());

THIS.SETQYDM (THIS.BUISINESSHEADER.GETQYDM ());

This.settransdate (This.buisinessHeader.getTranDate ());

This.setservtype (This.buisinessHeader.getServType ());

This.setthirdlogno (This.buisinessHeader.getThirdLogNo ());

This.settranfunc (This.buisinessHeader.getTranFunc ());

string[] STRs = Str.split ("&");

list<string> list = new linkedlist<string> ();

List.addall (Arrays.aslist (STRs));

string[] Fnames = This.order ();//////////////////////////////////////who calls

String[] values = new String[fnames.length];

System.arraycopy (STRs, 0, values, 0, values.length > Strs.length strs.length:values.length);

Assert.istrue (fnames.length >= strs.length);

int index = 0;

try {

for (String fname:fnames) {

SetValue (fname, Values[index]);

SetValue (This,fname, list);

index++;

}

} catch (Nosuchmethodexception | SecurityException | nosuchfieldexception | IllegalArgumentException | illegalaccessexception | InvocationTargetException | ParseException e) {

E.printstacktrace ();

}

}

/**

* Return message generation sequence

*

* @return

*/

protected abstract string[] order ();

}

Sub-class:

@Test

public void Testansyze () {

String str = "1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&";

BP1303 bp = new BP1303 ();

Bp.analyze (str);

System.out.println (Bp.getstring ());

}

Subclasses Overwrite parent classes:

@Override

Protected string[] Order () {

String[] Objs = {"Funcflag", "Supacctid", "Custacctid", "CustName", "Thirdcustid", "Idtype", "Idcode", "Relatedacctid", " Acctflag "," Trantype "," Acctname "," Bankcode "," Bankname "," Oldrelatedacctid "," reserve "};

return OBJS;

}

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.