/core/src/com/bluemarsh/jswat/core/util/Names.java

http://jswat.googlecode.com/ · Java · 202 lines · 100 code · 10 blank · 92 comment · 34 complexity · c6272f65b38bafc8407640f557b71dcf MD5 · raw file

  1. /*
  2. * The contents of this file are subject to the terms of the Common Development
  3. * and Distribution License (the License). You may not use this file except in
  4. * compliance with the License.
  5. *
  6. * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
  7. * or http://www.netbeans.org/cddl.txt.
  8. *
  9. * When distributing Covered Code, include this CDDL Header Notice in each file
  10. * and include the License file at http://www.netbeans.org/cddl.txt.
  11. * If applicable, add the following below the CDDL Header, with the fields
  12. * enclosed by brackets [] replaced by your own identifying information:
  13. * "Portions Copyrighted [year] [name of copyright owner]"
  14. *
  15. * The Original Software is the JSwat Core Module. The Initial Developer of the
  16. * Software is Nathan L. Fiedler. Portions created by Nathan L. Fiedler
  17. * are Copyright (C) 2003-2010. All Rights Reserved.
  18. *
  19. * Contributor(s): Nathan L. Fiedler.
  20. *
  21. * $Id: Names.java 285 2010-11-20 23:56:08Z nathanfiedler $
  22. */
  23. package com.bluemarsh.jswat.core.util;
  24. import com.bluemarsh.jswat.core.CoreSettings;
  25. import java.io.File;
  26. import java.util.StringTokenizer;
  27. /**
  28. * Class defining utility methods for class names and element names.
  29. *
  30. * @author Nathan Fiedler
  31. */
  32. public class Names {
  33. /**
  34. * None shall instantiate us.
  35. */
  36. private Names() {
  37. }
  38. /**
  39. * Turn the package name into a file path using simple character
  40. * substitution. Strips off any inner-class names before making the
  41. * conversion. The source file extension from CoreSettings will be
  42. * added to the end of the return value.
  43. *
  44. * <p>Note that this class may have come from a source file that
  45. * had some other name, in which case the return value will be
  46. * meaningless. The ideal solution is to use the appropriate methods
  47. * of Location and ReferenceType to get the true source name and
  48. * then call the two-argument form of this method.</p>
  49. *
  50. * @param clsname fully-qualified name of the class, possibly
  51. * including an inner-class specification.
  52. * @return path and filename of a source file.
  53. */
  54. public static String classnameToFilename(String clsname) {
  55. String cname = clsname;
  56. int idx = cname.indexOf('$');
  57. if (idx > 0) {
  58. cname = cname.substring(0, idx);
  59. }
  60. cname = cname.replace('.', File.separatorChar);
  61. CoreSettings cs = CoreSettings.getDefault();
  62. cname += cs.getSourceExtension();
  63. return cname;
  64. }
  65. /**
  66. * Converts a class name, with the given source file name, into a path
  67. * and filename of the source file for the class.
  68. *
  69. * @param clsname fully-qualified name of class.
  70. * @param srcname name of source file containing class.
  71. * @return path and filename of source file.
  72. */
  73. public static String classnameToFilename(String clsname, String srcname) {
  74. String filename = classnameToFilename(clsname);
  75. int lastbit = filename.lastIndexOf(File.separatorChar);
  76. if (lastbit > -1) {
  77. filename = filename.substring(0, lastbit);
  78. filename = filename + File.separator + srcname;
  79. } else {
  80. // Class without a path, just use the source name.
  81. filename = srcname;
  82. }
  83. return filename;
  84. }
  85. /**
  86. * Returns just the package name of the class.
  87. *
  88. * @param name fully-qualified class name.
  89. * @return package name (may be empty), or null if name is null.
  90. */
  91. public static String getPackageName(String name) {
  92. if (name == null) {
  93. return null;
  94. }
  95. int lastdot = name.lastIndexOf('.');
  96. if (lastdot > 0) {
  97. return name.substring(0, lastdot);
  98. } else {
  99. return "";
  100. }
  101. }
  102. /**
  103. * Returns just the name of the class, without the package name.
  104. *
  105. * @param name fully-qualified class name.
  106. * @return just the class name, or null if name is null.
  107. */
  108. public static String getShortClassName(String name) {
  109. if (name == null) {
  110. return null;
  111. }
  112. int lastdot = name.lastIndexOf('.');
  113. if (lastdot > 0) {
  114. return name.substring(lastdot + 1);
  115. } else {
  116. return name;
  117. }
  118. }
  119. /**
  120. * Test whether a given string is a valid Java identifier. Unlike
  121. * org.openide.util.Utilities, this does not consider keywords to
  122. * be valid identifiers (because they are not identifiers).
  123. *
  124. * @param id String which should be checked.
  125. * @return true if a valid identifier, false otherwise.
  126. */
  127. public static boolean isJavaIdentifier(String id) {
  128. if (id == null) {
  129. return false;
  130. }
  131. if (id.trim().isEmpty()) {
  132. return false;
  133. }
  134. if (!Character.isJavaIdentifierStart(id.charAt(0))) {
  135. return false;
  136. }
  137. for (int ii = 1; ii < id.length(); ii++) {
  138. if (!Character.isJavaIdentifierPart(id.charAt(ii))) {
  139. return false;
  140. }
  141. }
  142. return true;
  143. }
  144. /**
  145. * Determine if the given string is a valid method identifier.
  146. *
  147. * @param s string to validate.
  148. * @return true if string is a valid method identifier.
  149. */
  150. public static boolean isMethodIdentifier(String s) {
  151. if (s == null || s.trim().isEmpty()) {
  152. return false;
  153. }
  154. return isJavaIdentifier(s) || s.equals("<init>") || s.equals("<clinit>");
  155. }
  156. /**
  157. * Determines if the given String represents a valid class name.
  158. * The string is split into tokens delimited by a period (.). The
  159. * first or last token may be an asterisk (*) to represent a
  160. * wild-card for the purpose of matching multiple classes (for use
  161. * in setting breakpoints), if allowWild is true.
  162. *
  163. * @param s String to validate.
  164. * @param allowWild true to allow wildcards, false to treat as invalid.
  165. * @return true if name is valid, false otherwise.
  166. */
  167. public static boolean isValidClassname(String s, boolean allowWild) {
  168. if (s == null || s.trim().isEmpty()) {
  169. return false;
  170. }
  171. boolean valid = true;
  172. StringTokenizer tokenizer = new StringTokenizer(s, ".");
  173. int numTokens = tokenizer.countTokens();
  174. int curToken = 0;
  175. while (tokenizer.hasMoreTokens()) {
  176. String token = tokenizer.nextToken();
  177. curToken++;
  178. if (allowWild && token.equals("*")) {
  179. if (curToken == 1) {
  180. continue;
  181. } else if (curToken == numTokens) {
  182. break;
  183. }
  184. }
  185. if (!isJavaIdentifier(token)) {
  186. valid = false;
  187. break;
  188. }
  189. }
  190. return valid;
  191. }
  192. }