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 );
}
}