KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > ResourceValueAnalysisTestDriver


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

19
20 package edu.umd.cs.findbugs.ba;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.bcel.classfile.ClassParser;
26 import org.apache.bcel.classfile.JavaClass;
27 import org.apache.bcel.classfile.Method;
28 import org.apache.bcel.generic.InstructionHandle;
29 import org.apache.bcel.generic.MethodGen;
30
31 import edu.umd.cs.findbugs.annotations.CheckForNull;
32
33 public abstract class ResourceValueAnalysisTestDriver <Resource, ResourceTrackerType extends ResourceTracker<Resource>> {
34
35     public abstract ResourceTrackerType createResourceTracker(ClassContext classContext, Method method)
36             throws CFGBuilderException, DataflowAnalysisException;
37
38     public void execute(String JavaDoc classFile, String JavaDoc methodName, int offset)
39             throws IOException JavaDoc, CFGBuilderException, DataflowAnalysisException {
40
41         final RepositoryLookupFailureCallback lookupFailureCallback = new DebugRepositoryLookupFailureCallback();
42
43         // Set the lookup failure callback in the Analysis Context
44
AnalysisContext analysisContext = AnalysisContext.create(lookupFailureCallback);
45
46         JavaClass jclass = new ClassParser(classFile).parse();
47         ClassContext classContext = analysisContext.getClassContext(jclass);
48
49         Method[] methodList = jclass.getMethods();
50         for (Method method : methodList) {
51             if (!method.getName().equals(methodName))
52                 continue;
53
54             CFG cfg = classContext.getCFG(method);
55
56             BasicBlock creationBlock = null;
57             InstructionHandle creationInstruction = null;
58
59             blockLoop:
60             for (Iterator JavaDoc<BasicBlock> ii = cfg.blockIterator(); ii.hasNext();) {
61                 BasicBlock basicBlock = ii.next();
62                 for (Iterator JavaDoc<InstructionHandle> j = basicBlock.instructionIterator(); j.hasNext();) {
63                     InstructionHandle handle = j.next();
64                     if (handle.getPosition() == offset) {
65                         creationBlock = basicBlock;
66                         creationInstruction = handle;
67                         break blockLoop;
68                     }
69                 }
70             }
71
72             if (creationInstruction == null || creationBlock == null) throw new IllegalArgumentException JavaDoc("No bytecode with offset " + offset);
73
74             final ResourceTrackerType resourceTracker = createResourceTracker(classContext, method);
75             final Resource resource =
76                     resourceTracker.isResourceCreation(creationBlock, creationInstruction, classContext.getConstantPoolGen());
77
78             if (resource == null)
79                 throw new IllegalArgumentException JavaDoc("offset " + offset + " is not a resource creation");
80
81             DataflowTestDriver<ResourceValueFrame, ResourceValueAnalysis<Resource>> driver =
82                     new DataflowTestDriver<ResourceValueFrame, ResourceValueAnalysis<Resource>>() {
83                         @Override JavaDoc @CheckForNull
84                         public Dataflow<ResourceValueFrame, ResourceValueAnalysis<Resource>> createDataflow(ClassContext classContext, Method method)
85                                 throws CFGBuilderException, DataflowAnalysisException {
86                             MethodGen methodGen = classContext.getMethodGen(method);
87                             if (methodGen == null) return null;
88                             CFG cfg = classContext.getCFG(method);
89                             DepthFirstSearch dfs = classContext.getDepthFirstSearch(method);
90
91                             ResourceValueAnalysis<Resource> analysis =
92                                     new ResourceValueAnalysis<Resource>(methodGen, cfg, dfs, resourceTracker, resource);
93                             Dataflow<ResourceValueFrame, ResourceValueAnalysis<Resource>> dataflow =
94                                     new Dataflow<ResourceValueFrame, ResourceValueAnalysis<Resource>>(cfg, analysis);
95                             dataflow.execute();
96
97                             return dataflow;
98                         }
99                     };
100
101             driver.execute(classContext, method);
102             break;
103         }
104     }
105 }
106
107 // vim:ts=4
108
Popular Tags