KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > detect > BuildUnconditionalParamDerefDatabase


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2005, 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.detect;
21
22 import java.util.BitSet JavaDoc;
23
24 import org.apache.bcel.classfile.Method;
25 import org.apache.bcel.generic.ReferenceType;
26 import org.apache.bcel.generic.Type;
27
28 import edu.umd.cs.findbugs.FindBugsAnalysisFeatures;
29 import edu.umd.cs.findbugs.SystemProperties;
30 import edu.umd.cs.findbugs.ba.AnalysisContext;
31 import edu.umd.cs.findbugs.ba.CFG;
32 import edu.umd.cs.findbugs.ba.CFGBuilderException;
33 import edu.umd.cs.findbugs.ba.ClassContext;
34 import edu.umd.cs.findbugs.ba.DataflowAnalysisException;
35 import edu.umd.cs.findbugs.ba.SignatureParser;
36 import edu.umd.cs.findbugs.ba.XFactory;
37 import edu.umd.cs.findbugs.ba.XMethod;
38 import edu.umd.cs.findbugs.ba.deref.UnconditionalValueDerefDataflow;
39 import edu.umd.cs.findbugs.ba.deref.UnconditionalValueDerefSet;
40 import edu.umd.cs.findbugs.ba.npe.ParameterNullnessProperty;
41 import edu.umd.cs.findbugs.ba.npe.ParameterNullnessPropertyDatabase;
42 import edu.umd.cs.findbugs.ba.vna.ValueNumber;
43 import edu.umd.cs.findbugs.ba.vna.ValueNumberDataflow;
44
45 /**
46  * Build database of unconditionally dereferenced parameters.
47  *
48  * @author David Hovemeyer
49  */

50 public class BuildUnconditionalParamDerefDatabase {
51     public static final boolean VERBOSE_DEBUG = SystemProperties.getBoolean("fnd.debug.nullarg.verbose");
52     private static final boolean DEBUG = SystemProperties.getBoolean("fnd.debug.nullarg") || VERBOSE_DEBUG;
53     
54     public void visitClassContext(ClassContext classContext) {
55         boolean fullAnalysis = AnalysisContext.currentAnalysisContext().getBoolProperty(FindBugsAnalysisFeatures.INTERPROCEDURAL_ANALYSIS_OF_REFERENCED_CLASSES);
56         if (!fullAnalysis && !AnalysisContext.currentAnalysisContext().getSubtypes().isApplicationClass(classContext.getJavaClass()))
57                 return;
58         if (VERBOSE_DEBUG) System.out.println("Visiting class " + classContext.getJavaClass().getClassName());
59         Method[] methodList = classContext.getJavaClass().getMethods();
60         for (Method method : methodList) {
61             boolean hasReferenceParameters = false;
62             for (Type argument : method.getArgumentTypes())
63                 if (argument instanceof ReferenceType)
64                     hasReferenceParameters = true;
65
66             if (!hasReferenceParameters) continue;
67
68             if (classContext.getMethodGen(method) == null)
69                 continue; // no code
70

71             if (VERBOSE_DEBUG) System.out.println("Check " + method);
72             analyzeMethod(classContext, method);
73         }
74     }
75
76     private void analyzeMethod(ClassContext classContext, Method method) {
77         try {
78             CFG cfg = classContext.getCFG(method);
79
80             ValueNumberDataflow vnaDataflow = classContext.getValueNumberDataflow(method);
81             UnconditionalValueDerefDataflow dataflow =
82                 classContext.getUnconditionalValueDerefDataflow(method);
83     
84             int numParams = new SignatureParser(method.getSignature()).getNumParameters();
85             int paramLocalOffset = method.isStatic() ? 0 : 1;
86
87             // Build BitSet of params that are unconditionally dereferenced
88
BitSet JavaDoc unconditionalDerefSet = new BitSet JavaDoc();
89             UnconditionalValueDerefSet entryFact = dataflow.getResultFact(cfg.getEntry());
90             for (int i = 0; i < numParams; i++) {
91                 ValueNumber paramVN = vnaDataflow.getAnalysis().getEntryValue(i + paramLocalOffset);
92                 
93                 if (entryFact.isUnconditionallyDereferenced(paramVN)) {
94                     unconditionalDerefSet.set(i);
95                 }
96             }
97             
98             // No need to add properties if there are no unconditionally dereferenced params
99
if (unconditionalDerefSet.isEmpty()) {
100                 if (VERBOSE_DEBUG) {
101                     System.out.println("\tResult is empty");
102                 }
103                 return;
104             }
105
106             if (VERBOSE_DEBUG) {
107                 ClassContext.dumpUnconditionalValueDerefDataflow(method, cfg, vnaDataflow, classContext.getIsNullValueDataflow(method), dataflow);
108             }
109             ParameterNullnessProperty property = new ParameterNullnessProperty();
110             property.setNonNullParamSet(unconditionalDerefSet);
111             
112             XMethod xmethod = XFactory.createXMethod(classContext.getJavaClass(), method);
113             AnalysisContext.currentAnalysisContext().getUnconditionalDerefParamDatabase().setProperty(xmethod, property);
114             if (DEBUG) {
115                 System.out.println("Unconditional deref: " + xmethod + "=" + property);
116             }
117         } catch (CFGBuilderException e) {
118             AnalysisContext.currentAnalysisContext().getLookupFailureCallback().logError(
119                     "Error analyzing " + method + " for unconditional deref training", e);
120         } catch (DataflowAnalysisException e) {
121             AnalysisContext.currentAnalysisContext().getLookupFailureCallback().logError(
122                     "Error analyzing " + method + " for unconditional deref training", e);
123         }
124     }
125
126 }
127
Popular Tags