Last active
September 6, 2018 12:55
-
-
Save fairjm/e1666af349a0b5c565e6 to your computer and use it in GitHub Desktop.
used to scan RequestMapping(SpringMVC)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.PrintStream; | |
import java.lang.reflect.Method; | |
import java.util.LinkedList; | |
import java.util.List; | |
import org.apache.commons.lang3.ArrayUtils; | |
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 org.springframework.util.SystemPropertyUtils; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
/** | |
* | |
* @author fairjm | |
* | |
*/ | |
public class ScanRequestMappingTool { | |
public static final String CONTROLLER_CLASS_GLOB = "*Controller.class"; | |
public static final String INDENT = "\t"; | |
/** | |
* originally from | |
* http://stackoverflow.com/questions/1456930/how-do-i-read-all-classes-from-a-java-package-in- | |
* the-classpath | |
* by Shalom938 | |
*/ | |
public static List<Class> listClasses(final String basePackage) | |
throws IOException, ClassNotFoundException { | |
final ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); | |
final MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory( | |
resourcePatternResolver); | |
final List<Class> candidates = new LinkedList<Class>(); | |
final String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + | |
resolveBasePackage(basePackage) + "/" + "**/" + CONTROLLER_CLASS_GLOB; | |
final Resource[] resources = resourcePatternResolver.getResources(packageSearchPath); | |
for (final Resource resource : resources) { | |
if (resource.isReadable()) { | |
final MetadataReader metadataReader = metadataReaderFactory | |
.getMetadataReader(resource); | |
candidates.add(Class.forName(metadataReader.getClassMetadata().getClassName())); | |
} | |
} | |
return candidates; | |
} | |
public static String resolveBasePackage(final String basePackage) { | |
return ClassUtils.convertClassNameToResourcePath( | |
SystemPropertyUtils.resolvePlaceholders(basePackage)); | |
} | |
private static void printElementInArray(final Object[] array, final PrintStream out) { | |
if (ArrayUtils.isEmpty(array)) { | |
return; | |
} | |
for (final Object elem : array) { | |
out.print(elem); | |
} | |
} | |
public static void analysisRequestMappingAnnotation(final Class clazz, final PrintStream out) { | |
out.println(clazz.getName() + ":"); | |
final Method[] methods = clazz.getDeclaredMethods(); | |
for (final Method method : methods) { | |
final RequestMapping annotation = method.getAnnotation(RequestMapping.class); | |
if (annotation != null) { | |
out.print(INDENT); | |
out.println(method.getName()); | |
out.print(INDENT + INDENT); | |
printElementInArray(annotation.method(), out); | |
out.print(INDENT); | |
printElementInArray(annotation.value(), out); | |
out.println(); | |
} | |
} | |
} | |
public static void main(final String[] args) throws ClassNotFoundException, IOException { | |
final List<Class> classes = listClasses("the package to scan"); | |
final PrintStream out = System.out; | |
for (final Class clazz : classes) { | |
analysisRequestMappingAnnotation(clazz, out); | |
out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment