클래스 경로에서 Java 패키지의 모든 클래스를 어떻게 읽습니까?
Java 패키지에 포함 된 클래스를 읽어야합니다. 이러한 클래스는 클래스 경로에 있습니다. 이 작업을 Java 프로그램에서 직접 수행해야합니다. 간단한 방법을 알고 계십니까?
List<Class> classes = readClassesFrom("my.package")
당신이있는 경우 봄을 당신이 다음 클래스 경로에 다음이 그것을 할 것입니다.
XmlRootElement로 주석이 달린 패키지에서 모든 클래스를 찾습니다.
private List<Class> findMyTypes(String basePackage) throws IOException, ClassNotFoundException
{
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
List<Class> candidates = new ArrayList<Class>();
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
resolveBasePackage(basePackage) + "/" + "**/*.class";
Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
for (Resource resource : resources) {
if (resource.isReadable()) {
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
if (isCandidate(metadataReader)) {
candidates.add(Class.forName(metadataReader.getClassMetadata().getClassName()));
}
}
}
return candidates;
}
private String resolveBasePackage(String basePackage) {
return ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(basePackage));
}
private boolean isCandidate(MetadataReader metadataReader) throws ClassNotFoundException
{
try {
Class c = Class.forName(metadataReader.getClassMetadata().getClassName());
if (c.getAnnotation(XmlRootElement.class) != null) {
return true;
}
}
catch(Throwable e){
}
return false;
}
여기에 설명 된 Reflections Project를 사용할 수 있습니다.
매우 완전하고 사용하기 쉽습니다.
위 웹 사이트의 간략한 설명 :
Reflections는 클래스 경로를 스캔하고 메타 데이터의 색인을 생성하며 런타임시 쿼리 할 수 있으며 프로젝트 내의 많은 모듈에 대해 해당 정보를 저장하고 수집 할 수 있습니다.
예:
Reflections reflections = new Reflections(
new ConfigurationBuilder()
.setUrls(ClasspathHelper.forJavaClassPath())
);
Set<Class<?>> types = reflections.getTypesAnnotatedWith(Scannable.class);
나는 이것을 사용하며 파일 또는 jar 아카이브와 함께 작동합니다.
public static ArrayList<String>getClassNamesFromPackage(String packageName) throws IOException{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL packageURL;
ArrayList<String> names = new ArrayList<String>();;
packageName = packageName.replace(".", "/");
packageURL = classLoader.getResource(packageName);
if(packageURL.getProtocol().equals("jar")){
String jarFileName;
JarFile jf ;
Enumeration<JarEntry> jarEntries;
String entryName;
// build jar file name, then loop through zipped entries
jarFileName = URLDecoder.decode(packageURL.getFile(), "UTF-8");
jarFileName = jarFileName.substring(5,jarFileName.indexOf("!"));
System.out.println(">"+jarFileName);
jf = new JarFile(jarFileName);
jarEntries = jf.entries();
while(jarEntries.hasMoreElements()){
entryName = jarEntries.nextElement().getName();
if(entryName.startsWith(packageName) && entryName.length()>packageName.length()+5){
entryName = entryName.substring(packageName.length(),entryName.lastIndexOf('.'));
names.add(entryName);
}
}
// loop through files in classpath
}else{
URI uri = new URI(packageURL.toString());
File folder = new File(uri.getPath());
// won't work with path which contains blank (%20)
// File folder = new File(packageURL.getFile());
File[] contenuti = folder.listFiles();
String entryName;
for(File actual: contenuti){
entryName = actual.getName();
entryName = entryName.substring(0, entryName.lastIndexOf('.'));
names.add(entryName);
}
}
return names;
}
Spring has implemented an excellent classpath search function in the PathMatchingResourcePatternResolver
. If you use the classpath*
: prefix, you can find all the resources, including classes in a given hierarchy, and even filter them if you want. Then you can use the children of AbstractTypeHierarchyTraversingFilter
, AnnotationTypeFilter
and AssignableTypeFilter
to filter those resources either on class level annotations or on interfaces they implement.
Java 1.6.0_24:
public static File[] getPackageContent(String packageName) throws IOException{
ArrayList<File> list = new ArrayList<File>();
Enumeration<URL> urls = Thread.currentThread().getContextClassLoader()
.getResources(packageName);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
File dir = new File(url.getFile());
for (File f : dir.listFiles()) {
list.add(f);
}
}
return list.toArray(new File[]{});
}
This solution was tested within the EJB environment.
Scannotation and Reflections use class path scanning approach:
Reflections reflections = new Reflections("my.package");
Set<Class<? extends Object>> classes = reflections.getSubTypesOf(Object.class);
Another approach is to use Java Pluggable Annotation Processing API to write annotation processor which will collect all annotated classes at compile time and build the index file for runtime use. This mechanism is implemented in ClassIndex library:
Iterable<Class> classes = ClassIndex.getPackageClasses("my.package");
That functionality is still suspiciously missing from the Java reflection API as far as I know. You can get a package object by just doing this:
Package packageObj = Package.getPackage("my.package");
But as you probably noticed, that won't let you list the classes in that package. As of right now, you have to take sort of a more filesystem-oriented approach.
I found some sample implementations in this post
I'm not 100% sure these methods will work when your classes are buried in JAR files, but I hope one of those does it for you.
I agree with @skaffman...if you have another way of going about this, I'd recommend doing that instead.
The most robust mechanism for listing all classes in a given package is currently ClassGraph, because it handles the widest possible array of classpath specification mechanisms, including the new JPMS module system. (I am the author.)
List<String> classNames;
try (ScanResult scanResult = new ClassGraph().whitelistPackages("my.package")
.enableClassInfo().scan()) {
classNames = scanResult.getAllClasses().getNames();
}
eXtcos looks promising. Imagine you want to find all the classes that:
- Extend from class "Component", and store them
- Are annotated with "MyComponent", and
- Are in the “common” package.
With eXtcos this is as simple as
ClasspathScanner scanner = new ClasspathScanner();
final Set<Class> classStore = new ArraySet<Class>();
Set<Class> classes = scanner.getClasses(new ClassQuery() {
protected void query() {
select().
from(“common”).
andStore(thoseExtending(Component.class).into(classStore)).
returning(allAnnotatedWith(MyComponent.class));
}
});
Bill Burke has written a (nice article about class scanning] and then he wrote Scannotation.
Hibernate has this already written:
- org.hibernate.ejb.packaging.Scanner
- org.hibernate.ejb.packaging.NativeScanner
CDI might solve this, but don't know - haven't investigated fully yet
.
@Inject Instance< MyClass> x;
...
x.iterator()
Also for annotations:
abstract class MyAnnotationQualifier
extends AnnotationLiteral<Entity> implements Entity {}
I happen to have implemented it, and it works in most cases. Since it is long, I put it in a file here.
The idea is to find the location of the class source file which is available in most cases (a known exception are JVM class files -- as far as I've tested). If the code is in a directory, scan through all files and only spot class files. If the code is in a JAR file, scan all entries.
This method can only be used when:
You have a class that is in the same package you want to discover, This class is called a SeedClass. For example, if you want to list all classes in 'java.io', the seed class may be
java.io.File
.Your classes are in a directory or in a JAR file it has source file information (not source code file, but just source file). As far as I've tried, it work almost 100% except the JVM class (those classes come with the JVM).
Your program must have permission to access ProtectionDomain of those classes. If your program is loaded locally, there should be no problem.
I've tested the program only for my regular usage, so it may still have problem.
I hope this helps.
Here is another option, slight modification to another answer in above/below:
Reflections reflections = new Reflections("com.example.project.package",
new SubTypesScanner(false));
Set<Class<? extends Object>> allClasses =
reflections.getSubTypesOf(Object.class);
Back when applets were common place, one might have a URL on the classpath. When the classloader required a class, it would search all the locations on the classpath, including http resources. Because you can have things like URLs and directories on the classpath, there is no easy way to get a definitive list of the classes.
However, you can get pretty close. Some of the Spring libraries are doing this now. You can get all the jar's on the classpath, and open them up like files. You can then take this list of files, and create a data structure containing your classes.
The project ldapbeans provides a class Scanner that do this.
use dependency maven:
groupId: net.sf.extcos
artifactId: extcos
version: 0.4b
then use this code :
ComponentScanner scanner = new ComponentScanner();
Set classes = scanner.getClasses(new ComponentQuery() {
@Override
protected void query() {
select().from("com.leyton").returning(allExtending(DynamicForm.class));
}
});
If you're already using Guava, you can use ClassPath. Check out the docs here: https://google.github.io/guava/releases/17.0/api/docs/com/google/common/reflect/ClassPath.html
Brent - the reason the association is one way has to do with the fact that any class on any component of your CLASSPATH can declare itself in any package (except for java/javax). Thus there just is no mapping of ALL the classes in a given "package" because nobody knows nor can know. You could update a jar file tomorrow and remove or add classes. It's like trying to get a list of all people named John/Jon/Johan in all the countries of the world - none of us is omniscient therefore none of us will ever have the correct answer.
'IT story' 카테고리의 다른 글
사용자가 SSH 터널을 설정하도록 허용하지만 다른 것은 설정하지 않습니다. (0) | 2020.09.03 |
---|---|
여러 이미지의 유사성을 비교하기위한 이미지 지문 (0) | 2020.09.03 |
git은 xcodeproject / project.pbxproj 파일을 무시해야합니까? (0) | 2020.09.03 |
C, Clojure, Python, Ruby, Scala 등의 벤치 마크 해석 (0) | 2020.09.03 |
Xcode의 Prefix.pch 파일은 무엇입니까? (0) | 2020.09.03 |