PageRenderTime 210ms CodeModel.GetById 124ms app.highlight 14ms RepoModel.GetById 2ms app.codeStats 0ms

/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
Possible License(s): MPL-2.0-no-copyleft-exception
  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 */
 23package com.bluemarsh.jswat.core.util;
 24
 25import com.bluemarsh.jswat.core.CoreSettings;
 26import java.io.File;
 27import java.util.StringTokenizer;
 28
 29/**
 30 * Class defining utility methods for class names and element names.
 31 *
 32 * @author  Nathan Fiedler
 33 */
 34public class Names {
 35
 36    /**
 37     * None shall instantiate us.
 38     */
 39    private Names() {
 40    }
 41
 42    /**
 43     * Turn the package name into a file path using simple character
 44     * substitution. Strips off any inner-class names before making the
 45     * conversion. The source file extension from CoreSettings will be
 46     * added to the end of the return value.
 47     *
 48     * <p>Note that this class may have come from a source file that
 49     * had some other name, in which case the return value will be
 50     * meaningless. The ideal solution is to use the appropriate methods
 51     * of Location and ReferenceType to get the true source name and
 52     * then call the two-argument form of this method.</p>
 53     *
 54     * @param  clsname  fully-qualified name of the class, possibly
 55     *                  including an inner-class specification.
 56     * @return  path and filename of a source file.
 57     */
 58    public static String classnameToFilename(String clsname) {
 59        String cname = clsname;
 60        int idx = cname.indexOf('$');
 61        if (idx > 0) {
 62            cname = cname.substring(0, idx);
 63        }
 64        cname = cname.replace('.', File.separatorChar);
 65        CoreSettings cs = CoreSettings.getDefault();
 66        cname += cs.getSourceExtension();
 67        return cname;
 68    }
 69
 70    /**
 71     * Converts a class name, with the given source file name, into a path
 72     * and filename of the source file for the class.
 73     *
 74     * @param  clsname  fully-qualified name of class.
 75     * @param  srcname  name of source file containing class.
 76     * @return  path and filename of source file.
 77     */
 78    public static String classnameToFilename(String clsname, String srcname) {
 79        String filename = classnameToFilename(clsname);
 80        int lastbit = filename.lastIndexOf(File.separatorChar);
 81        if (lastbit > -1) {
 82            filename = filename.substring(0, lastbit);
 83            filename = filename + File.separator + srcname;
 84        } else {
 85            // Class without a path, just use the source name.
 86            filename = srcname;
 87        }
 88        return filename;
 89    }
 90
 91    /**
 92     * Returns just the package name of the class.
 93     *
 94     * @param  name  fully-qualified class name.
 95     * @return  package name (may be empty), or null if name is null.
 96     */
 97    public static String getPackageName(String name) {
 98        if (name == null) {
 99            return null;
100        }
101        int lastdot = name.lastIndexOf('.');
102        if (lastdot > 0) {
103            return name.substring(0, lastdot);
104        } else {
105            return "";
106        }
107    }
108
109    /**
110     * Returns just the name of the class, without the package name.
111     *
112     * @param  name  fully-qualified class name.
113     * @return  just the class name, or null if name is null.
114     */
115    public static String getShortClassName(String name) {
116        if (name == null) {
117            return null;
118        }
119        int lastdot = name.lastIndexOf('.');
120        if (lastdot > 0) {
121            return name.substring(lastdot + 1);
122        } else {
123            return name;
124        }
125    }
126
127    /**
128     * Test whether a given string is a valid Java identifier. Unlike
129     * org.openide.util.Utilities, this does not consider keywords to
130     * be valid identifiers (because they are not identifiers).
131     *
132     * @param  id  String which should be checked.
133     * @return  true if a valid identifier, false otherwise.
134     */
135    public static boolean isJavaIdentifier(String id) {
136        if (id == null) {
137            return false;
138        }
139        if (id.trim().isEmpty()) {
140            return false;
141        }
142        if (!Character.isJavaIdentifierStart(id.charAt(0))) {
143            return false;
144        }
145        for (int ii = 1; ii < id.length(); ii++) {
146            if (!Character.isJavaIdentifierPart(id.charAt(ii))) {
147                return false;
148            }
149        }
150        return true;
151    }
152
153    /**
154     * Determine if the given string is a valid method identifier.
155     *
156     * @param  s  string to validate.
157     * @return  true if string is a valid method identifier.
158     */
159    public static boolean isMethodIdentifier(String s) {
160        if (s == null || s.trim().isEmpty()) {
161            return false;
162        }
163        return isJavaIdentifier(s) || s.equals("<init>") || s.equals("<clinit>");
164    }
165
166    /**
167     * Determines if the given String represents a valid class name.
168     * The string is split into tokens delimited by a period (.). The
169     * first or last token may be an asterisk (*) to represent a
170     * wild-card for the purpose of matching multiple classes (for use
171     * in setting breakpoints), if allowWild is true.
172     *
173     * @param  s          String to validate.
174     * @param  allowWild  true to allow wildcards, false to treat as invalid.
175     * @return  true if name is valid, false otherwise.
176     */
177    public static boolean isValidClassname(String s, boolean allowWild) {
178        if (s == null || s.trim().isEmpty()) {
179            return false;
180        }
181        boolean valid = true;
182        StringTokenizer tokenizer = new StringTokenizer(s, ".");
183        int numTokens = tokenizer.countTokens();
184        int curToken = 0;
185        while (tokenizer.hasMoreTokens()) {
186            String token = tokenizer.nextToken();
187            curToken++;
188            if (allowWild && token.equals("*")) {
189                if (curToken == 1) {
190                    continue;
191                } else if (curToken == numTokens) {
192                    break;
193                }
194            }
195            if (!isJavaIdentifier(token)) {
196                valid = false;
197                break;
198            }
199        }
200        return valid;
201    }
202}