KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > EntryPoints


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Ondrej Lhotak
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 package soot;
21 import soot.jimple.*;
22 import soot.util.*;
23 import java.util.*;
24
25
26 /** Returns the various potential entry points of a Java program.
27  * @author Ondrej Lhotak
28  */

29 public class EntryPoints
30 {
31     public EntryPoints( Singletons.Global g ) {}
32     public static EntryPoints v() { return G.v().soot_EntryPoints(); }
33
34     final NumberedString sigMain = Scene.v().getSubSigNumberer().
35         findOrAdd( "void main(java.lang.String[])" );
36     final NumberedString sigFinalize = Scene.v().getSubSigNumberer().
37         findOrAdd( "void finalize()" );
38     final NumberedString sigExit = Scene.v().getSubSigNumberer().
39         findOrAdd( "void exit()" );
40     final NumberedString sigClinit = Scene.v().getSubSigNumberer().
41         findOrAdd( "void <clinit>()" );
42     final NumberedString sigInit = Scene.v().getSubSigNumberer().
43         findOrAdd( "void <init>()" );
44     final NumberedString sigStart = Scene.v().getSubSigNumberer().
45         findOrAdd( "void start()" );
46     final NumberedString sigRun = Scene.v().getSubSigNumberer().
47         findOrAdd( "void run()" );
48     final NumberedString sigObjRun = Scene.v().getSubSigNumberer().
49         findOrAdd( "java.lang.Object run()" );
50     final NumberedString sigForName = Scene.v().getSubSigNumberer().
51         findOrAdd( "java.lang.Class forName(java.lang.String)" );
52     private final void addMethod( List set, SootClass cls, NumberedString methodSubSig ) {
53         if( cls.declaresMethod( methodSubSig ) ) {
54             set.add( cls.getMethod( methodSubSig ) );
55         }
56     }
57     private final void addMethod( List set, String JavaDoc methodSig ) {
58         if( Scene.v().containsMethod( methodSig ) ) {
59             set.add( Scene.v().getMethod( methodSig ) );
60         }
61     }
62     /** Returns only the application entry points, not including entry points
63      * invoked implicitly by the VM. */

64     public List application() {
65         List ret = new ArrayList();
66         addMethod( ret, Scene.v().getMainClass(), sigMain );
67         for( Iterator clinitIt = clinitsOf(Scene.v().getMainClass() ).iterator(); clinitIt.hasNext(); ) {
68             final SootMethod clinit = (SootMethod) clinitIt.next();
69             ret.add(clinit);
70         }
71         return ret;
72     }
73     /** Returns only the entry points invoked implicitly by the VM. */
74     public List implicit() {
75         List ret = new ArrayList();
76         addMethod( ret, "<java.lang.System: void initializeSystemClass()>" );
77         addMethod( ret, "<java.lang.ThreadGroup: void <init>()>");
78         //addMethod( ret, "<java.lang.ThreadGroup: void remove(java.lang.Thread)>");
79
addMethod( ret, "<java.lang.Thread: void exit()>");
80         addMethod( ret, "<java.lang.ThreadGroup: void uncaughtException(java.lang.Thread,java.lang.Throwable)>");
81         //addMethod( ret, "<java.lang.System: void loadLibrary(java.lang.String)>");
82
addMethod( ret, "<java.lang.ClassLoader: void <init>()>");
83         addMethod( ret, "<java.lang.ClassLoader: java.lang.Class loadClassInternal(java.lang.String)>");
84         addMethod( ret, "<java.lang.ClassLoader: void checkPackageAccess(java.lang.Class,java.security.ProtectionDomain)>");
85         addMethod( ret, "<java.lang.ClassLoader: void addClass(java.lang.Class)>");
86         addMethod( ret, "<java.lang.ClassLoader: long findNative(java.lang.ClassLoader,java.lang.String)>");
87         addMethod( ret, "<java.security.PrivilegedActionException: void <init>(java.lang.Exception)>");
88         //addMethod( ret, "<java.lang.ref.Finalizer: void register(java.lang.Object)>");
89
addMethod( ret, "<java.lang.ref.Finalizer: void runFinalizer()>");
90         addMethod( ret, "<java.lang.Thread: void <init>(java.lang.ThreadGroup,java.lang.Runnable)>");
91         addMethod( ret, "<java.lang.Thread: void <init>(java.lang.ThreadGroup,java.lang.String)>");
92         return ret;
93     }
94     /** Returns all the entry points. */
95     public List all() {
96         List ret = new ArrayList();
97         ret.addAll( application() );
98         ret.addAll( implicit() );
99         return ret;
100     }
101     /** Returns a list of all static initializers. */
102     public List clinits() {
103         List ret = new ArrayList();
104         for( Iterator clIt = Scene.v().getClasses().iterator(); clIt.hasNext(); ) {
105             final SootClass cl = (SootClass) clIt.next();
106             addMethod( ret, cl, sigClinit );
107         }
108         return ret;
109     }
110     /** Returns a list of all constructors taking no arguments. */
111     public List inits() {
112         List ret = new ArrayList();
113         for( Iterator clIt = Scene.v().getClasses().iterator(); clIt.hasNext(); ) {
114             final SootClass cl = (SootClass) clIt.next();
115             addMethod( ret, cl, sigInit );
116         }
117         return ret;
118     }
119
120     /** Returns a list of all concrete methods of all application classes. */
121     public List methodsOfApplicationClasses() {
122         List ret = new ArrayList();
123         for( Iterator clIt = Scene.v().getApplicationClasses().iterator(); clIt.hasNext(); ) {
124             final SootClass cl = (SootClass) clIt.next();
125             for( Iterator mIt = cl.getMethods().iterator(); mIt.hasNext(); ) {
126                 final SootMethod m = (SootMethod) mIt.next();
127                 if( m.isConcrete() ) ret.add( m );
128             }
129         }
130         return ret;
131     }
132
133     /** Returns a list of all concrete main(String[]) methods of all
134      * application classes. */

135     public List mainsOfApplicationClasses() {
136         List ret = new ArrayList();
137         for( Iterator clIt = Scene.v().getApplicationClasses().iterator(); clIt.hasNext(); ) {
138             final SootClass cl = (SootClass) clIt.next();
139             if( cl.declaresMethod( "void main(java.lang.String[])" ) ) {
140                 SootMethod m = cl.getMethod("void main(java.lang.String[])" );
141                 if( m.isConcrete() ) ret.add( m );
142             }
143         }
144         return ret;
145     }
146
147     /** Returns a list of all clinits of class cl and its superclasses. */
148     public List clinitsOf( SootClass cl ) {
149         List ret = new ArrayList();
150         while(true) {
151             addMethod( ret, cl, sigClinit );
152             if( !cl.hasSuperclass() ) break;
153             cl = cl.getSuperclass();
154         }
155         return ret;
156     }
157 }
158
159
160
Popular Tags