-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consolidate class scanning logic (#1088)
* Ported everything from Elide 5.x * More cleanup * Fixed checkstyle error * Cleanup * Added test with no @entity on Elide model * Merged in changes from Elide 5.x * Added package includes back * Class searching ignores inheritance * Added test based on inspection feedback * Fixed inspection comment * Merged in elide 5 changes * Added class scanner tests * Inspection rework * Turned back on OWASP scanning * More rework * Made changes to Include preserves inheritance property
- Loading branch information
Showing
29 changed files
with
442 additions
and
185 deletions.
There are no files selected for viewing
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
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
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
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
67 changes: 67 additions & 0 deletions
67
elide-core/src/main/java/com/yahoo/elide/utils/ClassScanner.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2015, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
package com.yahoo.elide.utils; | ||
|
||
import org.reflections.Reflections; | ||
import org.reflections.scanners.SubTypesScanner; | ||
import org.reflections.scanners.TypeAnnotationsScanner; | ||
import org.reflections.util.ClasspathHelper; | ||
import org.reflections.util.ConfigurationBuilder; | ||
import org.reflections.util.FilterBuilder; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.Set; | ||
|
||
/** | ||
* Scans a package for classes by looking at files in the classpath. | ||
*/ | ||
public class ClassScanner { | ||
/** | ||
* Scans all classes accessible from the context class loader which belong to the given package and subpackages. | ||
* | ||
* @param toScan package to scan | ||
* @param annotation Annotation to search | ||
* @return The classes | ||
*/ | ||
static public Set<Class<?>> getAnnotatedClasses(Package toScan, Class<? extends Annotation> annotation) { | ||
return getAnnotatedClasses(toScan.getName(), annotation); | ||
} | ||
|
||
/** | ||
* Scans all classes accessible from the context class loader which belong to the given package and subpackages. | ||
* | ||
* @param packageName package name to scan. | ||
* @param annotation Annotation to search | ||
* @return The classes | ||
*/ | ||
static public Set<Class<?>> getAnnotatedClasses(String packageName, Class<? extends Annotation> annotation) { | ||
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); | ||
|
||
configurationBuilder.addUrls(ClasspathHelper.forPackage(packageName)); | ||
configurationBuilder.setScanners(new SubTypesScanner(), new TypeAnnotationsScanner()); | ||
configurationBuilder.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(packageName))); | ||
|
||
Reflections reflections = new Reflections(configurationBuilder); | ||
|
||
return reflections.getTypesAnnotatedWith(annotation, true); | ||
} | ||
|
||
/** | ||
* Returns all classes within a package. | ||
* @param packageName The root package to search. | ||
* @return All the classes within a package. | ||
*/ | ||
static public Set<Class<?>> getAllClasses(String packageName) { | ||
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); | ||
|
||
configurationBuilder.addUrls(ClasspathHelper.forPackage(packageName)); | ||
configurationBuilder.setScanners(new SubTypesScanner(false)); | ||
configurationBuilder.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(packageName))); | ||
|
||
Reflections reflections = new Reflections(configurationBuilder); | ||
return reflections.getSubTypesOf(Object.class); | ||
} | ||
} |
Oops, something went wrong.