Java Class Name Create toClassName(String fileName)

Here you can find the source of toClassName(String fileName)

Description

Convert the Java-class-file-name to the equivalent Java-class-name (dot delimited package name).

License

Open Source License

Parameter

Parameter Description
fileName The file name String to be translated.

Return

Java Class runtime name representation of the supplied file name String.

Declaration

public static String toClassName(String fileName) 

Method Source Code

//package com.java2s;
/*//  w  w  w  . ja v  a 2s  .c o  m
   Milyn - Copyright (C) 2006 - 2010
    
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License (version 2.1) as published by the Free Software 
   Foundation.
    
   This library 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:    
   http://www.gnu.org/licenses/lgpl.txt
*/

public class Main {
    /**
     * Convert the Java-class-file-name to the equivalent Java-class-name (dot 
     * delimited package name).
     * <p/>
     * EG:<br/>
     * a/b/c/X.class converts to a.b.c.X<br/>
     * a/b/c/X converts to a.b.c.X<br/>
     * a.b.c.X converts to a.b.c.X<br/>
     * a.b.c.X.class converts to a.b.c.X<br/>
     * @param fileName The file name String to be translated.
     * @return Java Class runtime name representation of the supplied file name String.
     */
    public static String toClassName(String fileName) {
        StringBuffer className;

        if (fileName == null) {
            throw new IllegalArgumentException("null 'fileName' arg in method call.");
        }
        fileName = fileName.trim();
        if (fileName.equals("")) {
            throw new IllegalArgumentException("empty 'fileName' arg in method call.");
        }

        className = new StringBuffer(fileName);
        // Fixup the name - replace '/' with '.' and remove ".class" if
        // present.
        if (fileName.endsWith(".class") && fileName.length() > 6) {
            className.setLength(className.length() - 6);
        }
        for (int i = 0; i < className.length(); i++) {
            if (className.charAt(i) == '/') {
                className.setCharAt(i, '.');
            }
        }

        return className.toString();
    }
}

Related

  1. toClassName(Object obj)
  2. toClassname(String apparatClassname)
  3. toClassName(String className)
  4. toClassName(String className)
  5. toClassName(String className)
  6. toClassName(String fileName)
  7. toClassName(String internalName)
  8. toClassName(String resourceName)
  9. toClassName(String str)