KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > classfile > ClassDescriptor


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, 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.classfile;
21
22 import java.io.Serializable JavaDoc;
23
24 import edu.umd.cs.findbugs.util.ClassName;
25
26 /**
27  * Descriptor identifying a class.
28  *
29  * @author David Hovemeyer
30  */

31 public class ClassDescriptor implements Comparable JavaDoc<ClassDescriptor>, Serializable JavaDoc {
32     private static final long serialVersionUID = 1L;
33     private final String JavaDoc className;
34     
35     /**
36      * Constructor.
37      *
38      * @param className class name in VM format, e.g. "java/lang/String"
39      */

40     public ClassDescriptor(String JavaDoc className) {
41         if (className.indexOf('.') >= 0) {
42             throw new IllegalArgumentException JavaDoc("Class name " + className + " not in VM format");
43         }
44         if (!ClassName.isValidClassName(className)) {
45             throw new IllegalArgumentException JavaDoc("Invalid class name " + className);
46         }
47         this.className = className;
48     }
49     
50     /**
51      * @return Returns the class name in VM format, e.g. "java/lang/String"
52      */

53     public String JavaDoc getClassName() {
54         return className;
55     }
56     
57     /* (non-Javadoc)
58      * @see java.lang.Comparable#compareTo(java.lang.Object)
59      */

60     public int compareTo(ClassDescriptor o) {
61         return className.compareTo(o.className);
62     }
63
64     /**
65      * Get the resource name of this class as it would appear in the classpath.
66      * E.g., "java/lang/String.class"
67      *
68      * @return the resource name
69      */

70     public String JavaDoc toResourceName() {
71         return className + ".class";
72     }
73
74     /**
75      * Get the name of the class in dotted format.
76      *
77      * @return the name of the class in dotted format
78      */

79     public String JavaDoc toDottedClassName() {
80         return ClassName.toDottedClassName(className);
81     }
82
83     /**
84      * Create a class descriptor from a resource name.
85      *
86      * @param resourceName the resource name
87      * @return the class descriptor
88      */

89     public static ClassDescriptor fromResourceName(String JavaDoc resourceName) {
90         if (!isClassResource(resourceName)) {
91             throw new IllegalArgumentException JavaDoc("Resource " + resourceName + " is not a class");
92         }
93         return new ClassDescriptor(resourceName.substring(0, resourceName.length() - 6));
94     }
95
96     /**
97      * Determine whether or not the given resource name refers to a class.
98      *
99      * @param resourceName the resource name
100      * @return true if the resource is a class, false otherwise
101      */

102     public static boolean isClassResource(String JavaDoc resourceName) {
103         // This could be more sophisticated.
104
return resourceName.endsWith(".class");
105     }
106     
107     /* (non-Javadoc)
108      * @see java.lang.Object#toString()
109      */

110     @Override JavaDoc
111     public String JavaDoc toString() {
112         return className;
113     }
114     
115     /* (non-Javadoc)
116      * @see java.lang.Object#equals(java.lang.Object)
117      */

118     @Override JavaDoc
119     public boolean equals(Object JavaDoc obj) {
120         if (obj == null || obj.getClass() != this.getClass()) {
121             return false;
122         }
123         ClassDescriptor other = (ClassDescriptor) obj;
124         return this.className.equals(other.className);
125     }
126     
127     /* (non-Javadoc)
128      * @see java.lang.Object#hashCode()
129      */

130     @Override JavaDoc
131     public int hashCode() {
132         return className.hashCode();
133     }
134 }
135
Popular Tags