Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This is what happens if you change a string using reflection.(Only for interned(hardcoded) strings)
- import java.lang.reflect.Field;
- public class MutateString {
- public static final String addr = "ADDR_PLACEH";
- //Results:
- //Initial: ADDR_PLACEH
- //From var: 192.168.1.1
- //Hardcoded: 192.168.1.1
- //Substring: ADDR_PLACEH
- //Equals test: true
- //Equals test with substring: false
- public static void main(String[] args) {
- System.out.print("Initial: "); System.out.println(addr);
- editIntStr("ADDR_PLACEH", "192.168.1.1");
- System.out.print("From var: "); System.out.println(addr);
- System.out.print("Hardcoded: "); System.out.println("ADDR_PLACEH");
- System.out.print("Substring: "); System.out.println("ADDR_PLACE" + "H".substring(0));
- System.out.print("Equals test: "); System.out.println("ADDR_PLACEH".equals("192.168.1.1"));
- System.out.print("Equals test with substring: "); System.out.println(("ADDR_PLACE" + "H".substring(0)).equals("192.168.1.1"));
- }
- public static void editIntStr(String from, String to) {
- Field f = null;
- try {
- f = String.class.getDeclaredField("value");
- } catch (NoSuchFieldException e) {
- e.printStackTrace();
- }
- f.setAccessible(true);
- try {
- f.set(from, to.toCharArray());
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement