KickJava   Java API By Example, From Geeks To Geeks.

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


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.System
22  *
23  * @author Feng Qian
24  */

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

40   public void simulateMethod(SootMethod method,
41                  ReferenceVariable thisVar,
42                  ReferenceVariable returnVar,
43                  ReferenceVariable params[]){
44
45     String JavaDoc subSignature = method.getSubSignature();
46
47     if (subSignature.equals("void arraycopy(java.lang.Object,int,java.lang.Object,int,int)")) {
48       java_lang_System_arraycopy(method, thisVar, returnVar, params);
49       return;
50     
51     } else if (subSignature.equals("void setIn0(java.io.InputStream)")) {
52       java_lang_System_setIn0(method, thisVar, returnVar, params);
53       return;
54       
55     } else if (subSignature.equals("void setOut0(java.io.PrintStream)")) {
56       java_lang_System_setOut0(method, thisVar, returnVar, params);
57       return;
58
59     } else if (subSignature.equals("void setErr0(java.io.PrintStream)")) {
60       java_lang_System_setErr0(method, thisVar, returnVar, params);
61       return;
62
63     } else if (subSignature.equals("java.util.Properties initProperties(java.util.Properties)")) {
64       java_lang_System_initProperties(method, thisVar, returnVar, params);
65       return;
66
67     } else if (subSignature.equals("java.lang.String mapLibraryName(java.lang.String)")) {
68       java_lang_System_mapLibraryName(method, thisVar, returnVar, params);
69       return;
70
71     } else if (subSignature.equals("java.lang.Class getCallerClass()")) {
72       java_lang_System_getCallerClass(method, thisVar, returnVar, params);
73       return;
74
75     } else {
76       defaultMethod(method, thisVar, returnVar, params);
77       return;
78     }
79   }
80
81   /********************* java.lang.System *********************/
82   /**
83    * Copies an array from the specified source array, beginning at the
84    * specified position, to the specified position of the destination
85    * array.
86    *
87    * NOTE: If the content of array is reference type, then it is
88    * necessary to build a connection between elements of
89    * two arrays
90    *
91    * dst[] = src[]
92    *
93    * public static native void arraycopy(java.lang.Object,
94    * int,
95    * java.lang.Object,
96    * int,
97    * int);
98    */

99   public void java_lang_System_arraycopy(SootMethod method,
100                         ReferenceVariable thisVar,
101                         ReferenceVariable returnVar,
102                         ReferenceVariable params[]){
103     ReferenceVariable srcElm = helper.arrayElementOf(params[0]);
104     ReferenceVariable dstElm = helper.arrayElementOf(params[2]);
105     // never make a[] = b[], it violates the principle of jimple statement.
106
// make a temporary variable.
107
ReferenceVariable tmpVar = helper.tempLocalVariable(method);
108     helper.assign(tmpVar, srcElm);
109     helper.assign(dstElm, tmpVar);
110   }
111
112   /**
113    * NOTE: this native method is not documented in JDK API.
114    * It should have the side effect:
115    * System.in = parameter
116    *
117    * private static native void setIn0(java.io.InputStream);
118    */

119   public void java_lang_System_setIn0(SootMethod method,
120                          ReferenceVariable thisVar,
121                          ReferenceVariable returnVar,
122                          ReferenceVariable params[]) {
123     ReferenceVariable sysIn =
124       helper.staticField("java.lang.System", "in");
125     helper.assign(sysIn, params[0]);
126   }
127
128   /**
129    * NOTE: the same explanation as setIn0:
130    * G.v().out = parameter
131    *
132    * private static native void setOut0(java.io.PrintStream);
133    */

134   public void java_lang_System_setOut0(SootMethod method,
135                           ReferenceVariable thisVar,
136                           ReferenceVariable returnVar,
137                           ReferenceVariable params[]) {
138     ReferenceVariable sysOut =
139       helper.staticField("java.lang.System", "out");
140     helper.assign(sysOut, params[0]);
141   }
142
143   /**
144    * NOTE: the same explanation as setIn0:
145    * System.err = parameter
146    *
147    * private static native void setErr0(java.io.PrintStream);
148    */

149   public void java_lang_System_setErr0(SootMethod method,
150                           ReferenceVariable thisVar,
151                           ReferenceVariable returnVar,
152                           ReferenceVariable params[]) {
153     ReferenceVariable sysErr =
154       helper.staticField("java.lang.System", "err");
155     helper.assign(sysErr, params[0]);
156   }
157
158   /**
159    * NOTE: this method is not documented, it should do following:
160    * @return = System.props;
161    * System.props = parameter;
162    *
163    * private static native
164    * java.util.Properties initProperties(java.util.Properties);
165    */

166   public
167     void java_lang_System_initProperties(SootMethod method,
168                      ReferenceVariable thisVar,
169                      ReferenceVariable returnVar,
170                      ReferenceVariable params[]) {
171     ReferenceVariable sysProps =
172       helper.staticField("java.lang.System", "props");
173     helper.assign(returnVar, sysProps);
174     helper.assign(sysProps, params[0]);
175   }
176
177   /**
178    * NOTE: it is platform-dependent, create a new string, needs to be verified.
179    *
180    * public static native java.lang.String mapLibraryName(java.lang.String);
181    */

182   public
183     void java_lang_System_mapLibraryName(SootMethod method,
184                      ReferenceVariable thisVar,
185                      ReferenceVariable returnVar,
186                      ReferenceVariable params[]) {
187     helper.assignObjectTo(returnVar, Environment.v().getStringObject());
188   }
189
190   /**
191    * Undocumented, used by class loading.
192    *
193    * static native java.lang.Class getCallerClass();
194    */

195   public
196     void java_lang_System_getCallerClass(SootMethod method,
197                      ReferenceVariable thisVar,
198                      ReferenceVariable returnVar,
199                      ReferenceVariable params[]) {
200     helper.assignObjectTo(returnVar, Environment.v().getClassObject());
201   }
202
203   /**
204    * Following methods have NO side effects.
205    *
206    * private static native void registerNatives();
207    * public static native long currentTimeMillis();
208    * public static native int identityHashCode(java.lang.Object);
209    */

210
211
212 }
213
Popular Tags