Java tutorial
/** * Copyright (c) 2011-2014, hubin (jobob@qq.com). * * 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.baomidou.mybatisplus.toolkit; import java.util.HashSet; import java.util.Set; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.type.classreading.CachingMetadataReaderFactory; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.util.ClassUtils; import com.baomidou.mybatisplus.exceptions.MybatisPlusException; /** * <p> * ?? * </p> * * @author hubin * @Date 2016-06-16 */ public class PackageHelper { /** * <p> * ??? * </p> * <p> * <property name="typeAliasesPackage" value="com.baomidou.*.entity"/> * </p> * * @param typeAliasesPackage * ?? * @return */ public static String[] convertTypeAliasesPackage(String typeAliasesPackage) { ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver(); MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver); String pkg = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(typeAliasesPackage) + "/*.class"; /* * ??Resource * ClassLoader.getResource("META-INF")????????? */ try { Set<String> set = new HashSet<String>(); Resource[] resources = resolver.getResources(pkg); if (resources != null && resources.length > 0) { MetadataReader metadataReader = null; for (Resource resource : resources) { if (resource.isReadable()) { metadataReader = metadataReaderFactory.getMetadataReader(resource); set.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage() .getName()); } } } if (!set.isEmpty()) { return set.toArray(new String[] {}); } else { throw new MybatisPlusException("not find typeAliasesPackage:" + pkg); } } catch (Exception e) { throw new MybatisPlusException("not find typeAliasesPackage:" + pkg, e); } } }