KickJava   Java API By Example, From Geeks To Geeks.

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


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 package edu.umd.cs.findbugs.ba;
20
21 import org.apache.bcel.Constants;
22
23 public abstract class AbstractClassMember implements ClassMember {
24     private final String JavaDoc className;
25     private final String JavaDoc name;
26     private final String JavaDoc signature;
27     private final int accessFlags;
28     private boolean resolved;
29     private int cachedHashCode = 0;
30     static int slashCountClass = 0;
31     static int dottedCountClass = 0;
32     static int slashCountSignature= 0;
33     static int dottedCountSignature = 0;
34     
35
36     protected AbstractClassMember(String JavaDoc className, String JavaDoc name, String JavaDoc signature, int accessFlags) {
37         if (className.indexOf('.') >= 0) {
38             // className = className.replace('.','/');
39
dottedCountClass++;
40         } else if (className.indexOf('/') >= 0) {
41             assert false;
42             slashCountClass++;
43             className = className.replace('/','.');
44         }
45         if (signature.indexOf('.') >= 0) {
46             assert false;
47             signature = signature.replace('.','/');
48             dottedCountSignature++;
49         } else if (signature.indexOf('/') >= 0) slashCountSignature++;
50         this.className = className;
51         this.name = name;
52         this.signature = signature;
53         this.accessFlags = accessFlags;
54     }
55
56     public String JavaDoc getClassName() {
57         return className;
58     }
59
60     public String JavaDoc getName() {
61         return name;
62     }
63
64     public String JavaDoc getPackageName() {
65         int lastDot = className.lastIndexOf('.');
66         if (lastDot == -1) return className;
67         return className.substring(0,lastDot);
68     }
69     public String JavaDoc getSignature() {
70         return signature;
71     }
72
73     public boolean isReferenceType() {
74         return signature.startsWith("L") || signature.startsWith("[");
75     }
76
77     public int getAccessFlags() {
78         return accessFlags;
79     }
80
81     public boolean isFinal() {
82         return (accessFlags & Constants.ACC_FINAL) != 0;
83     }
84
85     public boolean isPublic() {
86         return (accessFlags & Constants.ACC_PUBLIC) != 0;
87     }
88     
89     public boolean isProtected() {
90         return (accessFlags & Constants.ACC_PROTECTED) != 0;
91     }
92     
93     public boolean isPrivate() {
94         return (accessFlags & Constants.ACC_PRIVATE) != 0;
95     }
96
97     public int compareTo(ClassMember other) {
98         // This may be compared to any kind of PackageMember object.
99
// If the other object is a different kind of field,
100
// just compare class names.
101
if (this.getClass() != other.getClass())
102             return this.getClass().getName().compareTo(other.getClass().getName());
103
104         int cmp;
105         cmp = className.compareTo(other.getClassName());
106         if (cmp != 0)
107             return cmp;
108         cmp = name.compareTo(other.getName());
109         if (cmp != 0)
110             return cmp;
111         return signature.compareTo(other.getSignature());
112     }
113
114     public boolean isResolved() {
115         return resolved;
116     }
117       void markAsResolved() {
118          resolved = true;
119      }
120     @Override JavaDoc
121          public int hashCode() {
122         if (cachedHashCode == 0) {
123             cachedHashCode = className.hashCode() ^ name.hashCode() ^ signature.hashCode();
124         }
125         return cachedHashCode;
126     }
127
128     @Override JavaDoc
129          public boolean equals(Object JavaDoc o) {
130         if (o == null || this.getClass() != o.getClass())
131             return false;
132         AbstractClassMember other = (AbstractClassMember) o;
133         return className.equals(other.className)
134                 && name.equals(other.name)
135                 && signature.equals(other.signature);
136     }
137
138     @Override JavaDoc
139          public String JavaDoc toString() {
140         return className + "." + name;
141     }
142
143 }
144
Popular Tags