KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > type > FieldStoreType


1 /*
2  * Bytecode analysis framework
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.ba.type;
21
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.bcel.classfile.ClassFormatException;
26 import org.apache.bcel.generic.ReferenceType;
27 import org.apache.bcel.generic.Type;
28
29 import edu.umd.cs.findbugs.ba.AnalysisContext;
30 import edu.umd.cs.findbugs.ba.Hierarchy;
31
32 /**
33  * Field property storing the types of values stored
34  * in a field. The idea is that we may be able to determine
35  * a more precise type for values loaded from the field
36  * than the field type alone would indicate.
37  *
38  * @author David Hovemeyer
39  */

40 public class FieldStoreType {
41     private HashSet JavaDoc<String JavaDoc> typeSignatureSet;
42     private ReferenceType loadType;
43     
44     public FieldStoreType() {
45         this.typeSignatureSet = new HashSet JavaDoc<String JavaDoc>();
46     }
47     
48     // TODO: type may be exact
49
public void addTypeSignature(String JavaDoc signature) {
50         loadType = null;
51         typeSignatureSet.add(signature);
52     }
53     
54     public Iterator JavaDoc<String JavaDoc> signatureIterator() {
55         return typeSignatureSet.iterator();
56     }
57     
58     public ReferenceType getLoadType(ReferenceType fieldType) {
59         if (loadType == null) {
60             computeLoadType(fieldType);
61         }
62         return loadType;
63     }
64
65     private void computeLoadType(ReferenceType fieldType) {
66         ReferenceType leastSupertype = null;
67         
68         for (Iterator JavaDoc<String JavaDoc> i = signatureIterator(); i.hasNext();) {
69             try {
70                 String JavaDoc signature = i.next();
71                 Type type = Type.getType(signature);
72                 if (!(type instanceof ReferenceType))
73                     continue;
74                 
75                 // FIXME: this will mangle interface types, since
76
// getFirstCommonSuperclass() ignores interfaces.
77
leastSupertype = (leastSupertype == null)
78                     ? (ReferenceType) type
79                     : leastSupertype.getFirstCommonSuperclass((ReferenceType) type);
80                     
81             } catch (ClassFormatException e) {
82                 // Bad signature: ignore
83
} catch (ClassNotFoundException JavaDoc e) {
84                 AnalysisContext.reportMissingClass(e);
85             }
86         }
87         
88         try {
89             if (leastSupertype != null && Hierarchy.isSubtype(leastSupertype, fieldType))
90                 loadType = leastSupertype;
91         } catch (ClassNotFoundException JavaDoc e) {
92             AnalysisContext.reportMissingClass(e);
93         }
94         
95         if (loadType == null)
96             loadType = fieldType;
97     }
98 }
99
Popular Tags