Coverage Report - org.apache.commons.classscan.builtin.ClassNameHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassNameHelper
0%
0/18
0%
0/4
1.8
 
 1  
 /*
 2  
  * Licensed under the Apache License, Version 2.0 (the "License");
 3  
  * you may not use this file except in compliance with the License.
 4  
  * You may obtain a copy of the License at
 5  
  *
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  *
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package org.apache.commons.classscan.builtin;
 15  
 
 16  
 import java.util.regex.Matcher;
 17  
 import java.util.regex.Pattern;
 18  
 
 19  
 /**
 20  
  * Classname helper methods
 21  
  */
 22  
 public final class ClassNameHelper {
 23  0
         private ClassNameHelper() {
 24  0
         }
 25  
 
 26  
     private static final String IDENTIFIER_REGEX = "\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*";
 27  
     private static final String FILE_REGEX = "(" + IDENTIFIER_REGEX + ")\\.class";
 28  
 
 29  
     private static final String PATH_REGEX = "((" + IDENTIFIER_REGEX + "/)*)" + FILE_REGEX;
 30  0
     private static Pattern pathPattern = Pattern.compile(PATH_REGEX);
 31  
 
 32  
     /**
 33  
      * Check if supplied path name is a valid pathed file name. The path must
 34  
      * contain valid package names, the file name must be a valid Class name,
 35  
      * and must be followed by ".class"
 36  
      * 
 37  
      * @param path
 38  
      *            A file name with path
 39  
      * @return The class name without the trailing ".class", with the dotted
 40  
      *         package prefix, or null if the supplied filename is invalid
 41  
      */
 42  
     public static String getCannonicalClassNameFromPathName(String path) {
 43  0
         Matcher m = pathPattern.matcher(path);
 44  0
         if (m.matches()) {
 45  0
             String packagePrefix = m.group(1).replace('/', '.');
 46  0
             String className = m.group(3);
 47  0
             return packagePrefix + className;
 48  
         }
 49  
         else {
 50  0
             return null;
 51  
         }
 52  
     }
 53  
 
 54  0
     private static Pattern IDENTIFIER_PACKAGE = Pattern.compile(IDENTIFIER_REGEX);
 55  
 
 56  
     /**
 57  
      * Check if supplied name is a valid java package name portion or class
 58  
      * identifier.
 59  
      * 
 60  
      * @param identifier
 61  
      *            The name of a package file
 62  
      * @return true if the identifier is a valid part of a package or class name
 63  
      */
 64  
     public static boolean isValidIdentifier(String identifier) {
 65  0
         Matcher ipm = IDENTIFIER_PACKAGE.matcher(identifier);
 66  0
         return ipm.matches();
 67  
     }
 68  
 
 69  0
     private static Pattern filePattern = Pattern.compile(FILE_REGEX);
 70  
 
 71  
     /**
 72  
      * Check if supplied file name is a valid class file name. The name must be
 73  
      * a valid Class name followed by ".class"
 74  
      * 
 75  
      * @param fileName
 76  
      *            The name of a file (without path)
 77  
      * @return The class name without the trailing ".class", or null if the
 78  
      *         supplied filename is invalid
 79  
      */
 80  
     public static String getClassNameFromFileName(String fileName) {
 81  0
         Matcher fpm = filePattern.matcher(fileName);
 82  0
         if (fpm.matches()) {
 83  0
             return fpm.group(1);
 84  
         }
 85  
         else {
 86  0
             return null;
 87  
         }
 88  
     }
 89  
 
 90  
     /**
 91  
      * Get the canonical name from the internal name
 92  
      * 
 93  
      * @param internalName
 94  
      *            The internal name in the form Ljava/lang/Object;
 95  
      * @return The canonical name in the form java.lang.Object
 96  
      */
 97  
     public static String internalToCanonicalName(String internalName) {
 98  0
         return internalName.substring(1, internalName.length() - 1).replace('/', '.');
 99  
     }
 100  
 }