Use reflect to modify the string value abnormally.

Source: Internet
Author: User

Import java. Lang. Reflect. field;

Public class modifystr {

Public static void modifystring (string s ){
String TMP = "Modify it if you want to modify it. What should I do ";
Class CLS = S. getclass ();

Try {
Field offset = Cls. getdeclaredfield ("offset ");
Offset. setaccessible (true );
Offset. setint (S, 0 );

Field COUNT = Cls. getdeclaredfield ("count ");
Count. setaccessible (true );
Count. setint (S, TMP. Length ());

Field Value = Cls. getdeclaredfield ("value ");
Value. setaccessible (true );
Value. Set (S, TMP. tochararray ());
} Catch (securityexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (nosuchfieldexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (illegalargumentexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (illegalaccessexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

}

Public static void main (string [] ARGs ){
// Todo auto-generated method stub
String S = "Everything works ";
Modifystring (s );
System. Out. println (s );
}

}
 

Import java. Lang. Reflect. field;
Public class swapstring {
Public void swap (string str1, string str2 ){
String tmpstr = new string (str1 );
Class CLS = string. Class;
 
Try {
Field offset = Cls. getdeclaredfield ("offset ");
Field COUNT = Cls. getdeclaredfield ("count ");
Field Value = Cls. getdeclaredfield ("value ");

Offset. setaccessible (true );
Count. setaccessible (true );
Value. setaccessible (true );

Offset. setint (str1, 0 );
Count. setint (str1, str2.length ());
Value. Set (str1, str2.tochararray ());

Offset. setint (str2, 0 );
Count. setint (str2, tmpstr. Length ());
Value. Set (str2, tmpstr. tochararray ());
} Catch (securityexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (nosuchfieldexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (illegalargumentexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (illegalaccessexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
Public static void main (string [] ARGs ){
Swapstring Ss = new swapstring ();
String S1 = "string1 ";
String S2 = "string2 ";
System. Out. println ("before swap:/n S1 =" + S1 + ", S2 =" + S2 );
SS. Swap (S1, S2 );
System. Out. println ("after swap:/n S1 =" + S1 + ", S2 =" + S2 );
}

}

Import java. Lang. Reflect. field;
Public class swapstring ...{
Public void swap (string str1, string str2 )...{
Class CLS = string. Class;
Try ...{
Field offset = Cls. getdeclaredfield ("offset ");
Field COUNT = Cls. getdeclaredfield ("count ");
Field Value = Cls. getdeclaredfield ("value ");

Offset. setaccessible (true );
Count. setaccessible (true );
Value. setaccessible (true );

Int offset1 = offset. getint (str1 );
Int count1 = count. getint (str1 );
Object value1 = value. Get (str1 );

Int offset2 = offset. getint (str2 );
Int count2 = count. getint (str2 );
Object value2 = value. Get (str2 );

Offset. setint (str1, offset2 );
Count. setint (str1, count2 );
Value. Set (str1, value2 );

Offset. setint (str2, offset1 );
Count. setint (str2, count1 );
Value. Set (str2, value1 );
} Catch (securityexception e )...{
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (nosuchfieldexception e )...{
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (illegalargumentexception e )...{
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (illegalaccessexception e )...{
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
Public static void main (string [] ARGs )...{
Swapstring Ss = new swapstring ();
String S1 = "string1 ";
String S2 = "string2 ";
System. Out. println ("before swap: S1 =" + S1 + ", S2 =" + S2 );
SS. Swap (S1, S2 );
System. Out. println ("after swap: S1 =" + S1 + ", S2 =" + S2 );
}

}

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.