KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > javaToJimple > JavaToJimple


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2004 Jennifer 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.javaToJimple;
21 import polyglot.main.*;
22 import polyglot.frontend.*;
23 import polyglot.util.*;
24 import polyglot.visit.*;
25 import polyglot.ast.*;
26
27 import java.util.*;
28 import java.io.*;
29
30 public class JavaToJimple {
31     
32     public static final polyglot.frontend.Pass.ID CAST_INSERTION = new polyglot.frontend.Pass.ID("cast-insertion");
33     public static final polyglot.frontend.Pass.ID STRICTFP_PROP = new polyglot.frontend.Pass.ID("strictfp-prop");
34     public static final polyglot.frontend.Pass.ID ANON_CONSTR_FINDER = new polyglot.frontend.Pass.ID("anon-constr-finder");
35     public static final polyglot.frontend.Pass.ID SAVE_AST = new polyglot.frontend.Pass.ID("save-ast");
36     
37     /**
38      * sets up the info needed to invoke polyglot
39      */

40     public polyglot.frontend.ExtensionInfo initExtInfo(String JavaDoc fileName, List sourceLocations){
41         
42         Set source = new HashSet();
43         ExtensionInfo extInfo = new soot.javaToJimple.jj.ExtensionInfo() {
44             public List passes(Job job) {
45                 List passes = super.passes(job);
46                 //beforePass(passes, Pass.EXIT_CHECK, new VisitorPass(polyglot.frontend.Pass.FOLD, job, new polyglot.visit.ConstantFolder(ts, nf)));
47
beforePass(passes, Pass.EXIT_CHECK, new VisitorPass(CAST_INSERTION, job, new CastInsertionVisitor(job, ts, nf)));
48                 beforePass(passes, Pass.EXIT_CHECK, new VisitorPass(STRICTFP_PROP, job, new StrictFPPropagator(false)));
49                 beforePass(passes, Pass.EXIT_CHECK, new VisitorPass(ANON_CONSTR_FINDER, job, new AnonConstructorFinder(job, ts, nf)));
50                 afterPass(passes, Pass.PRE_OUTPUT_ALL, new SaveASTVisitor(SAVE_AST, job, this));
51                 removePass(passes, Pass.OUTPUT);
52                 return passes;
53             }
54             
55         };
56         polyglot.main.Options options = extInfo.getOptions();
57
58         options.assertions = true;
59         options.source_path = new LinkedList();
60         Iterator it = sourceLocations.iterator();
61         while (it.hasNext()){
62             Object JavaDoc next = it.next();
63             //System.out.println("adding src loc: "+next.toString());
64
options.source_path.add(new File(next.toString()));
65         }
66
67         options.source_ext = new String JavaDoc []{"java"};
68         options.serialize_type_info = false;
69         
70         source.add(fileName);
71         
72         options.source_path.add(new File(fileName).getParentFile());
73         
74         polyglot.main.Options.global = options;
75
76         return extInfo;
77     }
78     
79     /**
80      * uses polyglot to compile source and build AST
81      */

82     public polyglot.ast.Node compile(polyglot.frontend.Compiler compiler, String JavaDoc fileName, polyglot.frontend.ExtensionInfo extInfo){
83         SourceLoader source_loader = compiler.sourceExtension().sourceLoader();
84
85         try {
86             FileSource source = new FileSource(new File(fileName));
87             // This hack is to stop the catch block at the bottom causing an error
88
// with versions of Polyglot where the constructor above can't throw IOException
89
// It should be removed as soon as Polyglot 1.3 is no longer supported.
90
if(false) throw new IOException("Bogus exception");
91
92             SourceJob job = null;
93
94             if (compiler.sourceExtension() instanceof soot.javaToJimple.jj.ExtensionInfo){
95                 soot.javaToJimple.jj.ExtensionInfo jjInfo = (soot.javaToJimple.jj.ExtensionInfo)compiler.sourceExtension();
96                 if (jjInfo.sourceJobMap() != null){
97                     job = (SourceJob)jjInfo.sourceJobMap().get(source);
98                 }
99             }
100             if (job == null){
101                 job = compiler.sourceExtension().addJob(source);
102             }
103    
104             boolean result = false;
105             result = compiler.sourceExtension().runToCompletion();
106         
107             if (!result) {
108             
109                 throw new soot.CompilationDeathException(0, "Could not compile");
110             }
111
112         
113             
114             polyglot.ast.Node node = job.ast();
115
116             return node;
117
118         }
119         catch (IOException e){
120             return null;
121         }
122
123     }
124
125 }
126
Popular Tags