Bookmark Topic Watch Topic
  • New Topic

How to use SecurityManager?  RSS feed

 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi All,

Please look at the following code. This program is mutating the String class's fields which is a contradiction specified that String is immutable class. I came to this conclusion since in both the occasions of calling the hashCode() method it returns the same value.

import java.lang.reflect.*;
public class StringHacker {
public static void main(String[] args) {
try {
String s = new String("hi");
Thread t = new Thread();
System.out.println(s.hashCode());
Class stringClass = Class.forName("java.lang.String");
Field value = stringClass.getDeclaredField("value");
value.setAccessible(true);
Object arrayOfChars = new char[]{'h','e'};
value.set(s,arrayOfChars);
System.out.println(s);
System.out.println(s.hashCode());
}catch(Exception e) {
e.printStackTrace();
}
}
}

Please clarify me if this kind of code can be denied by enforcing a SecurityManager. If it is possible, can anybody tell me how to make use of it?

Thanks in advance!
dinesh.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Report post to moderator
In this duplicate thread that you started -where there is a lengthy discussion already-, Ilja answered that exact question. Let's continue the discussion in that tread.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!