KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > pointer > util > NativeMethodDriver


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  * A wrapper for native method side-effect simulation.
22  * The caller passes in a native method with parameters,
23  * the corresponding native simulator gets called.
24  *
25  * @author Feng Qian
26  */

27 package soot.jimple.toolkits.pointer.util;
28
29 import soot.*;
30 import soot.jimple.toolkits.pointer.representations.*;
31 import soot.jimple.toolkits.pointer.nativemethods.*;
32 import java.util.*;
33
34 public class NativeMethodDriver {
35     private NativeHelper helper;
36     public NativeMethodDriver( NativeHelper helper ) {
37         this.helper = helper;
38         cnameToSim.put("java.lang.Object", new JavaLangObjectNative(helper));
39         cnameToSim.put("java.lang.System", new JavaLangSystemNative(helper));
40         cnameToSim.put("java.lang.Runtime", new JavaLangRuntimeNative(helper));
41         cnameToSim.put("java.lang.Shutdown", new JavaLangShutdownNative(helper));
42         cnameToSim.put("java.lang.String", new JavaLangStringNative(helper));
43         cnameToSim.put("java.lang.Float", new JavaLangFloatNative(helper));
44         cnameToSim.put("java.lang.Double", new JavaLangDoubleNative(helper));
45         cnameToSim.put("java.lang.StrictMath", new JavaLangStrictMathNative(helper));
46         cnameToSim.put("java.lang.Throwable", new JavaLangThrowableNative(helper));
47         cnameToSim.put("java.lang.Class", new JavaLangClassNative(helper));
48         cnameToSim.put("java.lang.Package", new JavaLangPackageNative(helper));
49         cnameToSim.put("java.lang.Thread", new JavaLangThreadNative(helper));
50         cnameToSim.put("java.lang.ClassLoader", new JavaLangClassLoaderNative(helper));
51         cnameToSim.put("java.lang.ClassLoader$NativeLibrary",
52                        new JavaLangClassLoaderNativeLibraryNative(helper));
53         cnameToSim.put("java.lang.SecurityManager",
54                        new JavaLangSecurityManagerNative(helper));
55
56
57         cnameToSim.put("java.lang.reflect.Field",
58                        new JavaLangReflectFieldNative(helper));
59         cnameToSim.put("java.lang.reflect.Array",
60                        new JavaLangReflectArrayNative(helper));
61         cnameToSim.put("java.lang.reflect.Method",
62                        new JavaLangReflectMethodNative(helper));
63         cnameToSim.put("java.lang.reflect.Constructor",
64                        new JavaLangReflectConstructorNative(helper));
65         cnameToSim.put("java.lang.reflect.Proxy",
66                        new JavaLangReflectProxyNative(helper));
67
68
69         cnameToSim.put("java.io.FileInputStream",
70                        new JavaIoFileInputStreamNative(helper));
71         cnameToSim.put("java.io.FileOutputStream",
72                        new JavaIoFileOutputStreamNative(helper));
73         cnameToSim.put("java.io.ObjectInputStream",
74                        new JavaIoObjectInputStreamNative(helper));
75         cnameToSim.put("java.io.ObjectOutputStream",
76                        new JavaIoObjectOutputStreamNative(helper));
77         cnameToSim.put("java.io.ObjectStreamClass",
78                        new JavaIoObjectStreamClassNative(helper));
79         cnameToSim.put("java.io.FileSystem", new JavaIoFileSystemNative(helper));
80         cnameToSim.put("java.io.FileDescriptor", new JavaIoFileDescriptorNative(helper));
81
82
83         cnameToSim.put("java.util.ResourceBundle",
84                        new JavaUtilResourceBundleNative(helper));
85         cnameToSim.put("java.util.TimeZone", new JavaUtilTimeZoneNative(helper));
86         
87
88         cnameToSim.put("java.util.jar.JarFile",
89                        new JavaUtilJarJarFileNative(helper));
90         
91         cnameToSim.put("java.util.zip.CRC32",
92                        new JavaUtilZipCRC32Native(helper));
93         cnameToSim.put("java.util.zip.Inflater",
94                        new JavaUtilZipInflaterNative(helper));
95         cnameToSim.put("java.util.zip.ZipFile",
96                        new JavaUtilZipZipFileNative(helper));
97         cnameToSim.put("java.util.zip.ZipEntry",
98                        new JavaUtilZipZipEntryNative(helper));
99         
100
101         cnameToSim.put("java.security.AccessController",
102                        new JavaSecurityAccessControllerNative(helper));
103         
104
105         cnameToSim.put("java.net.InetAddress",
106                        new JavaNetInetAddressNative(helper));
107         cnameToSim.put("java.net.InetAddressImpl",
108                        new JavaNetInetAddressImplNative(helper));
109
110
111         cnameToSim.put("sun.misc.Signal",
112                        new SunMiscSignalNative(helper));
113         cnameToSim.put("sun.misc.NativeSignalHandler",
114                        new SunMiscSignalHandlerNative(helper));
115     }
116
117   private HashMap cnameToSim = new HashMap(100);
118   private boolean DEBUG = false;
119
120   /**
121    * The entry point of native method simulation.
122    * @param method, must be a native method
123    * @param thisVar, the variable represent @this,
124    * it can be null if the method is static
125    * @param returnVar, the variable represent @return
126    * it is null if the method has no return
127    * @param params, array of parameters.
128    */

129   public boolean process(SootMethod method,
130                 ReferenceVariable thisVar,
131                 ReferenceVariable returnVar,
132                 ReferenceVariable params[]) {
133
134     String JavaDoc cname = method.getDeclaringClass().getName();
135     NativeMethodClass clsSim = (NativeMethodClass)cnameToSim.get(cname);
136
137 // G.v().out.println(method.toString());
138
if (clsSim == null) {
139       //G.v().out.println("WARNING: it is unsafe to simulate the method ");
140
//G.v().out.println(" "+method.toString());
141
//throw new NativeMethodNotSupportedException(method);
142
return true;
143     } else {
144
145       try {
146     clsSim.simulateMethod(method,
147                   thisVar,
148                   returnVar,
149                   params);
150       } catch (NativeMethodNotSupportedException e) {
151           if(DEBUG) {
152               G.v().out.println("WARNING: it is unsafe to simulate the method ");
153               G.v().out.println(" "+method.toString());
154           }
155       }
156       return true;
157     }
158   }
159 }
160
Popular Tags