KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > pointer > nativemethods > JavaLangStringNative


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Feng Qian
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /**
21  * Simulates the native method side effects in class java.lang.String
22  *
23  * @author Feng Qian
24  * @author <XXX>
25  */

26
27 package soot.jimple.toolkits.pointer.nativemethods;
28
29 import soot.*;
30 import soot.jimple.toolkits.pointer.representations.*;
31 import soot.jimple.toolkits.pointer.util.*;
32
33 public class JavaLangStringNative extends NativeMethodClass {
34     public JavaLangStringNative( NativeHelper helper ) { super(helper); }
35
36   /**
37    * Implements the abstract method simulateMethod.
38    * It distributes the request to the corresponding methods
39    * by signatures.
40    */

41   public void simulateMethod(SootMethod method,
42                  ReferenceVariable thisVar,
43                  ReferenceVariable returnVar,
44                  ReferenceVariable params[]){
45
46     String JavaDoc subSignature = method.getSubSignature();
47
48     if (subSignature.equals("java.lang.String intern()")) {
49       java_lang_String_intern(method, thisVar, returnVar, params);
50       return;
51
52     } else {
53       defaultMethod(method, thisVar, returnVar, params);
54       return;
55
56     }
57   }
58   /************************** java.lang.String ***********************/
59   /**
60    * Returns a canonical representation for the string object. A pool
61    * of strings, initially empty, is maintained privately by the class
62    * String.
63    *
64    * When the intern method is invoked, if the pool already contains a
65    * * string equal to this String object as determined by the *
66    * equals(Object) method, then the string from the pool is *
67    * returned. Otherwise, this String object is added to the pool and
68    * a * reference to this String object is returned.
69    *
70    * It follows that for any two strings s and t,
71    * s.intern() == t.intern()
72    * is true if and only if s.equals(t) is true.
73    *
74    * All literal strings and string-valued constant expressions are *
75    * interned. String literals are defined in Section 3.10.5 of the Java *
76    * Language Specification Returns: a string that has the same
77    * contents * as this string, but is guaranteed to be from a pool of
78    * unique * strings.
79    *
80    * Side Effect: from the description, we can see, it is tricky to
81    * know the side effect of this native method.
82    * Take a conservative way to handle this.
83    *
84    * It may be @return = this;
85    * pool = this;
86    *
87    * why should we care about String in points-to analysis?
88    *
89    * public native java.lang.String intern();
90    */

91   public void java_lang_String_intern(SootMethod method,
92                          ReferenceVariable thisVar,
93                          ReferenceVariable returnVar,
94                          ReferenceVariable params[]) {
95     helper.assignObjectTo(returnVar, Environment.v().getStringObject());
96   }
97 }
98
Popular Tags