Java File Name to Class Name filenameToClassname(String fileName)

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

Description

This method checks and transforms the filename of a potential Class given by fileName.

License

Open Source License

Parameter

Parameter Description
fileName is the filename.

Return

the according Java class-name for the given fileName if it is a class-file that is no anonymous , else null.

Declaration

public static String filenameToClassname(String fileName) 

Method Source Code

//package com.java2s;
/*/*from ww w  .j av a 2  s  .  c om*/
 * Copyright 2010, 2011 Institut Pasteur.
 * 
 * This file is part of ICY.
 * 
 * ICY is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ICY 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ICY. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * This method checks and transforms the filename of a potential {@link Class} given by
     * <code>fileName</code>.
     * 
     * @param fileName
     *        is the filename.
     * @return the according Java {@link Class#getName() class-name} for the given
     *         <code>fileName</code> if it is a class-file that is no anonymous {@link Class}, else
     *         <code>null</code>.
     */
    public static String filenameToClassname(String fileName) {
        // class file ?
        if (fileName.toLowerCase().endsWith(".class"))
            // remove ".class" extension and fix classname
            return fixClassName(fileName.substring(0, fileName.length() - 6));

        return null;
    }

    /**
     * This method checks and transforms the filename of a potential {@link Class} given by
     * <code>fileName</code>.<br>
     * Code written by Jorg Hohwiller for the m-m-m project (http://m-m-m.sf.net)
     * 
     * @param fileName
     *        is the filename.
     * @return the according Java {@link Class#getName() class-name} for the given
     *         <code>fileName</code> if it is a class-file that is no anonymous {@link Class}, else
     *         <code>null</code>.
     */
    public static String fixClassName(String fileName) {
        // replace path separator by package separator
        String result = fileName.replace('/', '.');

        // handle inner classes...
        final int lastDollar = result.lastIndexOf('$');
        if (lastDollar > 0) {
            char innerChar = result.charAt(lastDollar + 1);
            // ignore anonymous inner class
            if ((innerChar >= '0') && (innerChar <= '9'))
                return null;

            // TODO: check we really don't need to replace '$' by '.'
            // return result.replace('$', '.');
        }

        return result;
    }
}

Related

  1. fileNameToClass(String fileName)
  2. fileNameToClassName(String f)
  3. filenameToClassname(String filename)