KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > util > ClassName


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.util;
21
22 /**
23  * Utility methods for working with class names.
24  *
25  * @author David Hovemeyer
26  */

27 public abstract class ClassName {
28     /**
29      * Convert class name to slashed format.
30      * If the class name is already in slashed format,
31      * it is returned unmodified.
32      *
33      * @param className a class name
34      * @return the same class name in slashed format
35      */

36     public static String JavaDoc toSlashedClassName(String JavaDoc className) {
37         if (className.indexOf('.') >= 0) {
38             className = className.replace('.', '/');
39         }
40         return className;
41     }
42
43     /**
44      * Convert class name to dotted format.
45      * If the class name is already in dotted format,
46      * it is returned unmodified.
47      *
48      * @param className a class name
49      * @return the same class name in dotted format
50      */

51     public static String JavaDoc toDottedClassName(String JavaDoc className) {
52         if (className.indexOf('/') >= 0) {
53             className = className.replace('/', '.');
54         }
55         return className;
56     }
57
58     /**
59      * Return whether or not the given class name is valid.
60      *
61      * @param className a possible class name
62      * @return true if it's a valid class name, false otherwise
63      */

64     public static boolean isValidClassName(String JavaDoc className) {
65         // FIXME: should use a regex
66

67         if (className.indexOf('[') >= 0
68                 || className.indexOf(';') >= 0
69                 || className.indexOf('\\') >= 0
70                 || className.indexOf('(') >= 0) {
71             return false;
72         }
73         return true;
74     }
75
76     /**
77      * Does a class name appear to designate an anonymous class?
78      * Only the name is analyzed. No classes are loaded or looked up.
79      *
80      * @param className class name, slashed or dotted, fully qualified or unqualified
81      * @return true if className is the name of an anonymous class
82      */

83     public static boolean isAnonymous(String JavaDoc className) {
84         int i = className.lastIndexOf('$');
85         if (i >= 0 && i + 1 < className.length()) {
86             return Character.isDigit(className.charAt(i + 1));
87         }
88         return false;
89     }
90
91 }
92
Popular Tags