Mega Code Archive

 
Categories / Java Tutorial / Reflection
 

Determine whether the supplied string represents a well-formed fully-qualified Java classname

/*  * JBoss DNA (http://www.jboss.org/dna)  * See the COPYRIGHT.txt file distributed with this work for information  * regarding copyright ownership.  Some portions may be licensed  * to Red Hat, Inc. under one or more contributor license agreements.  * See the AUTHORS.txt file in the distribution for a full listing of   * individual contributors.   *  * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA  * is licensed to you under the terms of the GNU Lesser General Public License as  * published by the Free Software Foundation; either version 2.1 of  * the License, or (at your option) any later version.  *  * JBoss DNA is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  * Lesser General Public License for more details.  *  * You should have received a copy of the GNU Lesser General Public  * License along with this software; if not, write to the Free  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.  */ import java.text.CharacterIterator; import java.text.StringCharacterIterator; public class Utils {   /**    * Determine whether the supplied string represents a well-formed fully-qualified Java classname. This utility method enforces    * no conventions (e.g., packages are all lowercase) nor checks whether the class is available on the classpath.    *     * @param classname    * @return true if the string is a fully-qualified class name    */   public static boolean isFullyQualifiedClassname( String classname ) {       if (classname == null) return false;       String[] parts = classname.split("[\\.]");       if (parts.length == 0) return false;       for (String part : parts) {           CharacterIterator iter = new StringCharacterIterator(part);           // Check first character (there should at least be one character for each part) ...           char c = iter.first();           if (c == CharacterIterator.DONE) return false;           if (!Character.isJavaIdentifierStart(c) && !Character.isIdentifierIgnorable(c)) return false;           c = iter.next();           // Check the remaining characters, if there are any ...           while (c != CharacterIterator.DONE) {               if (!Character.isJavaIdentifierPart(c) && !Character.isIdentifierIgnorable(c)) return false;               c = iter.next();           }       }       return true;   } }