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

com.alibaba.nacos.common.packagescan.DefaultPackageScan Maven / Gradle / Ivy

There is a newer version: 2.4.2
Show newest version
/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * 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 com.alibaba.nacos.common.packagescan;

import com.alibaba.nacos.common.packagescan.classreading.ClassReader;
import com.alibaba.nacos.common.packagescan.resource.PathMatchingResourcePatternResolver;
import com.alibaba.nacos.common.packagescan.resource.Resource;
import com.alibaba.nacos.common.packagescan.resource.ResourcePatternResolver;
import com.alibaba.nacos.common.packagescan.util.NestedIoException;
import com.alibaba.nacos.common.utils.ClassUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;

/**
 * Scan all appropriate Class object through the package name.
 *
 * @author hujun
 */
public class DefaultPackageScan implements PackageScan {
    
    protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultPackageScan.class);
    
    private final PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
    
    public DefaultPackageScan() {
    }
    
    /**
     * Scan all appropriate Class object through the package name and Class object.
     *
     * @param pkg          package name,for example, com.alibaba.nacos.common
     * @param requestClass super class
     * @param           Class type
     * @return a set contains Class
     */
    @Override
    public  Set> getSubTypesOf(String pkg, Class requestClass) {
        Set> set = new HashSet<>(16);
        String packageSearchPath =
                ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(pkg) + '/'
                        + "**/*.class";
        try {
            Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
            for (Resource resource : resources) {
                Class scanClass = getClassByResource(resource);
                if (requestClass.isAssignableFrom(scanClass)) {
                    set.add((Class) scanClass);
                }
            }
        } catch (IOException | ClassNotFoundException e) {
            LOGGER.error("scan path: {} failed", packageSearchPath, e);
        }
        return set;
    }
    
    /**
     * Scan all appropriate Class object through the package name and annotation.
     *
     * @param pkg        package name,for example, com.alibaba.nacos.common
     * @param annotation annotation
     * @param         Class type
     * @return a set contains Class object
     */
    @Override
    public  Set> getTypesAnnotatedWith(String pkg, Class annotation) {
        Set> set = new HashSet<>(16);
        String packageSearchPath =
                ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(pkg) + '/'
                        + "**/*.class";
        try {
            Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
            for (Resource resource : resources) {
                Class scanClass = getClassByResource(resource);
                if (scanClass.isAnnotationPresent(annotation)) {
                    set.add((Class) scanClass);
                }
            }
        } catch (IOException | ClassNotFoundException e) {
            LOGGER.error("scan path: {} failed", packageSearchPath, e);
        }
        return set;
    }
    
    private Class getClassByResource(Resource resource) throws IOException, ClassNotFoundException {
        String className = getClassReader(resource).getClassName();
        return Class.forName(ClassUtils.resourcePathToConvertClassName(className));
    }
    
    private static ClassReader getClassReader(Resource resource) throws IOException {
        try (InputStream is = resource.getInputStream()) {
            try {
                return new ClassReader(is);
            } catch (IllegalArgumentException ex) {
                throw new NestedIoException("ASM ClassReader failed to parse class file - "
                        + "probably due to a new Java class file version that isn't supported yet: " + resource, ex);
            }
        }
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy