get Class Name By File - Java Reflection

Java examples for Reflection:Class Name

Description

get Class Name By File

Demo Code


//package com.java2s;
import java.io.File;

import java.util.ArrayList;
import java.util.List;

public class Main {

    private static List<String> getClassNameByFile(String filePath,
            List<String> className, boolean childPackage,
            String originalPackage) {
        List<String> myClassName = new ArrayList<String>();
        File file = new File(filePath);

        File[] childFiles = file.listFiles();
        for (File childFile : childFiles) {
            if (childFile.isDirectory()) {
                if (childPackage) {
                    myClassName.addAll(getClassNameByFile(
                            childFile.getPath(), myClassName, childPackage,
                            originalPackage));
                }/*  w w  w. j  av  a  2  s.  c om*/
            } else {
                String childFilePath = childFile.getPath();
                if (childFilePath.endsWith(".class")) {
                    childFilePath = childFilePath.substring(
                            childFilePath.indexOf(originalPackage),
                            childFilePath.lastIndexOf("."));
                    childFilePath = childFilePath.replace("\\", ".");
                    myClassName.add(childFilePath);
                }
            }
        }

        return myClassName;
    }
}

Related Tutorials