如果您愿意使用其他库,则可以使用 Reflections Project,它允许您搜索包中列出的类。
Reflections reflections = new Reflections("my.package.prefix");
//or
Reflections reflections = new Reflections(ClasspathHelper.forPackage("my.package.prefix"),
new SubTypesScanner(), new TypesAnnotationScanner(), new FilterBuilder().includePackage(...), ...);
//or using the ConfigurationBuilder
new Reflections(new ConfigurationBuilder()
.filterInputsBy(new FilterBuilder().includePackage("my.project.prefix"))
.setUrls(ClasspathHelper.forPackage("my.project.prefix"))
.setScanners(new SubTypesScanner(), new TypeAnnotationsScanner().filterResultsBy(optionalFilter), ...));
//then query, for example:
Set<Class<? extends Module>> modules = reflections.getSubTypesOf(com.google.inject.Module.class);
Set<Class<?>> singletons = reflections.getTypesAnnotatedWith(javax.inject.Singleton.class);
Set<String> properties = reflections.getResources(Pattern.compile(".*\\.properties"));
Set<Constructor> injectables = reflections.getConstructorsAnnotatedWith(javax.inject.Inject.class);
Set<Method> deprecateds = reflections.getMethodsAnnotatedWith(javax.ws.rs.Path.class);
Set<Field> ids = reflections.getFieldsAnnotatedWith(javax.persistence.Id.class);
Set<Method> someMethods = reflections.getMethodsMatchParams(long.class, int.class);
Set<Method> voidMethods = reflections.getMethodsReturn(void.class);
Set<Method> pathParamMethods = reflections.getMethodsWithAnyParamAnnotated(PathParam.class);
Set<Method> floatToString = reflections.getConverters(Float.class, String.class);
如您所见,您可以使用不同的过滤器进行搜索。我不认为你不能为 java 文件做,但你可以在所有类中搜索包名。