All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tinygroup.annotation.impl.JavaPathManage Maven / Gradle / Ivy

The newest version!
/**
 *  Copyright (c) 1997-2013, www.tinygroup.org ([email protected]).
 *
 *  Licensed under the GPL, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.gnu.org/licenses/gpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.tinygroup.annotation.impl;

import org.tinygroup.beancontainer.BeanContainerFactory;
import org.tinygroup.fileresolver.FileResolver;

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

/**
 * 类路径管理
 *
 * @author renhui
 */
public class JavaPathManage {

    private static final String FILE_RESOLVER = "fileResolver";
    private static final String BACK_SLASH = "\\";
    private static final String POINT = ".";
    // Jar包分格符
    private static final String EXCLAMATION_MARK = "!";
    // Jar包后缀名
    private static final String JAR_SUFFIX = ".jar";
    // !/符号
    private static final String EXCLAMATION_AND_SLASH_MARK = "!/";
    //  /符号
    private static final String SLASH_MARK = "/";

    // 保存的类根路径列表
    private List javaPaths = new ArrayList();
    // 文件扫描器对象
    private FileResolver fileResolver;

    public void init() {
		fileResolver = BeanContainerFactory
		.getBeanContainer(this.getClass().getClassLoader()).getBean(FILE_RESOLVER);
		List paths=new ArrayList();
		paths.addAll(fileResolver.getScanningPaths());
		for (String path : paths) {
			if(path.indexOf(JAR_SUFFIX)==-1){
				if(!path.endsWith(SLASH_MARK)){
					path=path.concat(SLASH_MARK);
				}
			javaPaths.add(path.replace(BACK_SLASH, SLASH_MARK));
			}
		}
	}
//    public void init() {
//        fileResolver = SpringUtil.getBean(FILE_RESOLVER);
//        Collection fileObjects = fileResolver.getResolveFileObjectSet();
//        for (FileObject fileObject : fileObjects) {
//            String path = fileObject.getAbsolutePath();
//            if (path.indexOf(JAR_SUFFIX) == -1) {
//                if (!path.endsWith(SLASH_MARK)) {
//                    path = path.concat(SLASH_MARK);
//                }
//                javaPaths.add(path.replace(BACK_SLASH, SLASH_MARK));
//            }
//        }
//    }

    /**
     * 根据路径获取类全路径
     *
     * @param fileName
     * @return
     */
    public String obtainClassName(String fileName) {
        String className = null;
        int jarIndex = fileName.indexOf(EXCLAMATION_MARK);
        if (jarIndex != -1) {
            className = fileName.substring(jarIndex + EXCLAMATION_AND_SLASH_MARK.length(), fileName.length());
        } else {
            for (String class_path : javaPaths) {
                int index = fileName.indexOf(class_path);
                if (index != -1) {
                    className = fileName.substring(
                            index + class_path.length(), fileName.length());
                    break;
                }
            }
        }
        if(className==null){
        	throw new RuntimeException("未找到对应的className,文件名:"+fileName);
        }
        return className.replace(SLASH_MARK, POINT);

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy