问题标签 [reflections]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
1618 浏览

playframework - Play 框架 - 是否可以强制 Play 生成 .class 文件,即使在 PROD 模式下也是如此?

在我的 Play 应用程序中,我使用 Reflections ( http://code.google.com/p/reflections/ ) 来获取一些带有特定注释的字段。

Reflections 需要访问 .class 文件才能创建其索引。在 DEV 模式下,它工作得很好,因为 Play 在 tmp/classes 中生成 .class 文件。但在 PROD 模式下,Play 默认不会生成那些 .class 文件。因此我的应用程序不起作用!

有没有办法强制 Play 生成 .class 文件,即使在 PROD 模式下也是如此?

0 投票
1 回答
1327 浏览

java - Guice and Reflections inside Jar File

I am experimenting with Google Guice (3.0) and Google Reflections (0.9.6).

I have the following files:

Operation.java:

Addition.java:

Other various "operation" classes spread across multiple packages.

CalculatorModule.java:

Calculator.java:

And finally, App.java:

After executing App.java inside of IntelliJ I get the following output, as expected:

Calculator: 3 3
Modulus: 0
Division: 1
Multiplication: 9
Addition: 6

However, when I package this application as a jar I only get the following output:

Calculator: 3 3

Why is this and how can I fix it? The closest thing I found was Issue 48, but that says fixed as of May 2011. Surely that would have made it into Maven by now...

0 投票
1 回答
235 浏览

java - 查找调用库的类的类路径

我正在开发的 java 库中使用 google Reflections。我使用 Reflections 的原因是因为我想找到所有带有特定注释的类。简化事情,在我的库中,我有一个回答这些类的方法,它通过如下行调用:

在我的库的当前版本中,我要求用户在反射库需要查找带注释的类的位置显式添加包名称:

通过这种方式,反射将查找仅位于该包中的类。

如果我的库的用户没有添加此搜索路径,我将反射配置为在系统类加载器中的所有类中搜索。显然,这种解决方案效率很低。但是,我真的想摆脱要求用户始终设置搜索路径的要求。- 如果它很重要,请注意:在反射中,您可以使用包名称、url(可以找到类的类路径)或类加载器配置搜索路径。

所以我的问题是:有没有办法找到调用我的库的类的类路径?(来自我的图书馆代码)。这样,我可以检测到如果用户没有明确设置搜索位置,我会默认添加该位置,这似乎比添加整个系统类加载器作为替代搜索路径更好的解决方案。

我知道我可以从我的库代码中手动检查方法调用堆栈,但这似乎有点笨拙和肮脏的解决方案,所以我正在寻找替代的想法。

提前致谢。

0 投票
1 回答
393 浏览

java - 使用 Reflections 库在 Java 类路径中查询具有任意属性的类

我过去曾使用Reflections库来查询类路径中具有特定注释或继承自特定类的类。使用反射很容易做到这一点,因为该库已经提供了回答此类特定查询的方法。

例如,下面的代码说明了如何查询继承自 的SomeType类和用 注释SomeAnnotation的类,查找位于包中的类my.package.prefix(示例取自 Reflection.java 类的文档):

但是,在我当前的问题中,我需要做的是查询具有任意属性集的类(例如,特定类名 + 现有注释 + 的组合)。我事先不知道这些属性,因为它们应该由程序员使用我正在开发的库来定义。

我设想解决方案的一部分类似于要求程序员定义一个带有布尔方法的 Strategy 类,以回答该类是否应该在答案集中。

例如,程序员应该实现一个类似于以下的接口:

所以我的问题是,一旦我有了这个策略对象,我如何指示反射库过滤类,只保留那些根据程序员定义的策略类匹配的类?

更新:

显然过滤部分不是最困难的任务(感谢@MvG的回答),但实际上收集了所有要过滤的类(通常是程序员定义的一组基本包中的类)。所以想法是查询所有类,然后用方法过滤它们: ReflectionUtils.getAll(allClasses, predicate)

然后,为了获得要过滤的初始类集,我尝试查询Object程序员选择的特定包中的所有子类型。

在我用于此的代码下方:

这是最好(更有效)的解决方案吗?

有人有其他想法吗?提前致谢 !

0 投票
1 回答
639 浏览

annotations - 需要扫描 WEB-INF/lib/xxx-ejb.jar 以获取 Type 和 Method 注释

我想使用 Google Reflections 执行以下操作:

  1. 仅扫描 WEB-INF/lib/Patrac-ejb.jar

  2. 仅扫描包 com.patrac 及其所有子包。

  3. 仅扫描类型和方法注释。

以下配置似乎可以正常工作,但我对 Google Reflections 没有任何经验。

它似乎正在工作。我想确保它没有扫描 WEB-INF/lib 中的所有其他 JAR。有没有一种简单的方法可以发现配置中的过滤器输入匹配了哪些 JAR?任何关于我的方法的建议将不胜感激。

0 投票
1 回答
923 浏览

java - Using Reflections for finding the (transitive) subtypes of a class when not all the types in the hierarchy are in the filtered classpath url

I am trying to use the Reflections library to obtain all the subclasses of a particular class. I need to filter the search to only the subclasses in a particular classpath url. This is the code I am using:

My problem is the following: Let's assume that class B extends A, and class C extends B. So we have:

A and B are in a classpath URL different than the one of C. And the url given to reflections is the one of C. Then I would expect C to be in the returned set, and B should not be present since it is in another classpath url.

However, the returned set is empty. C is not in the set of classes returned by reflections, though it is a transitive descendant of A. If I move B to the same classpath URL of C, then both B and C are returned. Then I guess the problem is that Reflections by default does not answer (transitive) subclasses if any of the classes in the middle of the hierarchy are not in the filtered url.

My questions is: How can I instruct reflections to answer (transitive) subclasses of a particular class, even if certain classes in the middle of the hierarchy are not in the filtered url (but the subclass itself IS in the filtered url)?.

0 投票
1 回答
411 浏览

java - 如何使用部署为 uberjar 的库?

我正在尝试使用作为 uber-jar 提供的 Reflections 0.9.8 库。如果我在项目的构建路径中添加它的 JAR(“reflections-0.9.8-uberjar”),那么 Eclipse 将找不到它的任何类类型:Reflection、ClasspathHelper 等等。

如果我提取单个“reflections-0.9.8.jar”(不是uber的)并将其添加到构建路径中,那么在编译时一切正常,但是当我执行我的项目时,我得到一个 NoClassDefFoundError(我是很确定这是由于缺乏依赖关系造成的)。

我还尝试在 uber-jar 中添加每个 .jar,但我仍然会得到 NoClassDefFoundError。

我需要做什么才能使用这个作为 uberjar 分发的库?

编辑: 如果有帮助,这是我想调用反射的方法(这是我从Can you find all classes in a package using reflection? 中摘录的一个片段?):

0 投票
1 回答
482 浏览

java - 在 RCP 应用程序中使用反射实现注释扫描的问题

我的第一次尝试是简单地将罐子包含在我的主应用程序中。(Eclipse 3.7)我知道这是不受欢迎的,但我正在编写一个独立的专业应用程序,我不太担心试图找出捆绑包。

试图运行这个:

在我的独立测试应用程序中,它运行良好,但在 RCP 中尝试时,我得到以下输出:

所以我四处寻找答案,发现这个问题涵盖了我的问题,但没有给我足够的细节来解决它。作为第一步,我将反射包移到一个包中,但是现在当我运行它时,我根本没有输出,我不知道这是因为 log4j 路径错误还是什么,但我现在没有来自反射包的反馈。

我的 activeActions 列表是空的,所以扫描没有奏效(我没想到会这样),因为我没有实现解决方案的第二部分,即创建自定义 UrlType。但是,我现在已经实现了自定义 URL 类型,但仍然没有找到任何东西,我不知道为什么,因为我看不到反射包的任何反馈,只是没有找到任何东西。

我希望有人可以帮助我让反射日志记录正常工作,然后找出为什么 UrlType 解决方案不适合我。

编辑

这是一个笑话,如果我不打扰 Bundle 并且我只是将反射 jar 推到我的类路径中,那么只要注册了自定义 UrlType,扫描就可以工作。并且记录一切正常。我为什么要打扰 Bundles?

0 投票
1 回答
3320 浏览

java - reflections.getTypesAnnotatedWith(...) 找到类,但 getAnnotations().length 为 0

使用 Java反射库时,我可以正确找到用注释装饰的类:

但是当我尝试查看每个类上有多少注释时,我总是看到零:

为什么c.getAnnotations()返回一个长度为 0 的数组?事实上,在调试器中,所有的字段(除了namec都是null.

0 投票
1 回答
1549 浏览

java - 使用 Reflections 库查询给定包中的资源,忽略子包

使用Reflections 库,如何在一个包中获取具有特定文件扩展名的所有资源?

我试过这段代码:

但这是回答包“org.p1”其中的任何子包(例如“org.p1.p2”)中扩展名为“xxx”的所有资源。

我可以将反射配置为仅回答位于“org.p1”中的资源并忽略其子包(如果有)吗?