使用 Guice 的逆向工程项目策略?

发布于 2024-09-08 21:18:26 字数 631 浏览 3 评论 0原文

我试图理解依赖注入,但并没有完全理解它,除非我设法理解它使得理解别人的代码变得困难。 :'(

无论如何,我不知道如何简要描述我的问题,但我会尝试。我目前是唯一一个从事 Java 项目的编码员,该项目由数十名孤独者在大约六年的时间内完成。它大量使用了Google 的 Guice 库。我应该采用一些现有代码并以不同的方式实现它;使用现有的密码身份验证方法,而不是将其应用于 JMenu 中的每个 JMenuItem,而是将其应用于整个 JMenu,这样如果输入错误的密码或未输入密码,所有 JMenuItem 都被禁用,如果密码错误,则不会发生这种情况,这使我相信问题出在 if 语句中,它本身就是一长串依赖项:
if (!ViewScanApp.getApplication().getHistoryManager().isAuthenticated())

我追溯这一点,发现 HistoryManager 类是一个接口,并且我的路似乎就此终结;那里没有代码,也没有引用任何其他类。通过随机探索项目中的 100 多个类,我找到了路径的尽头,但我似乎无法将它们联系起来。我找不到在该堆栈的另一端找到的第一个类 AccessManagerImpl 的调用位置。

我可以使用可能适用于这种情况的依赖注入的解释。太感谢了!

I have tried to understand dependency injection and not quite gotten it, except I have managed to pick up the understanding that it makes it hard to understand somebody else's code. :'(

Anyway, I'm not sure how to briefly describe my problem but I will try. I am currently the sole coder working on a Java project that's been worked on by dozens of loners over about six years. It makes heavy use of Google's Guice library. I'm supposed to take some existing code and implement it differently; specifically, use the existing methods for password authentication and, instead of applying it to each JMenuItem in a JMenu, apply it to the whole JMenu, so that if the wrong password or no password is entered, all JMenuItems are disabled. This doesn't happen if the password is wrong, leading me to believe the problem is in the if statement, which is a long string of dependencies in itself:
if (!ViewScanApp.getApplication().getHistoryManager().isAuthenticated())

I trace my way back through this, to find that the HistoryManager class is an interface, and there my path seems to die; there's no code there, and it doesn't make a reference to any other class. I have found the end of the path through random exploration of the 100-odd classes in the project, but I can't quite seem to connect them. I cannot find where the first class I can find on the other end of this stack, AccessManagerImpl, gets called.

I could use an explanation of dependency injection that might be applicable to this situation. Thank you so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

A君 2024-09-15 21:18:26

假设 HistoryManager 接口上没有 @ImplementedBy 注释,您需要检查负责绑定此类型的 Guice Module

在 Eclipse 中,有一个命令可以查找类的出现情况。我敢打赌 Netbeans 也有类似的东西。使用它来查找 HistoryManager 的出现情况。其中至少一个应该出现在实现 com.google.inject.Module(或扩展 AbstractModule)的类中。您可能会看到类似的内容

protected void configure() {
  …
  bind(HistoryManager.class).to(HistoryManagerImpl.class);
  …
}

或者,如果您喜欢快速而肮脏的经验主义,您可以放入 println()

HistoryManager mgr = ViewScanApp.getApplication().getHistoryManager();
System.out.println("HistoryManager implementation: " + mgr.getClass());
if (!mgr.isAuthenticated())
   …

无论您如何找到它,HistoryManagerImpl 类都是您想要寻找踪迹的地方。

我还没用过,但Guice 绘图工具可能是也有帮助。

Assuming there's no @ImplementedBy annotation on the HistoryManager interface, you'll need to examine the Guice Module that is responsible for binding this type.

In Eclipse, there is a command to look for occurrences of a class. I'll bet that Netbeans has something similar. Use it to look for occurrences of HistoryManager. At least one of these should occur in a class that implements com.google.inject.Module (or extends AbstractModule). You'll likely see something like

protected void configure() {
  …
  bind(HistoryManager.class).to(HistoryManagerImpl.class);
  …
}

Or, if you like quick-and-dirty empiricism, you can throw in a println():

HistoryManager mgr = ViewScanApp.getApplication().getHistoryManager();
System.out.println("HistoryManager implementation: " + mgr.getClass());
if (!mgr.isAuthenticated())
   …

However you locate it, HistoryManagerImpl class is where you'll want to pick up the trail.

I haven't used it, but the Guice graphing tool might be helpful too.

静谧幽蓝 2024-09-15 21:18:26

启动调试器。它将引导您完成实现该接口的确切类(假设您有它的源代码)

Fire up a debugger. It will walk you through the exact class that is implementing that interface (assuming you have the source code to it)

掩耳倾听 2024-09-15 21:18:26

每当您在 Eclipse 中有一个用 Guice 注入的接口定义时,不要使用 F3 转到您将执行的定义(如果它是一个类),然后使用 Ctrl-T 在该接口的实现中进行选择。

如果您有多个模块可供选择,那么您需要打印出模块绑定,以便您知道选择哪一个。不幸的是 Eclipse 还不理解注入。

Whenever you have an interface definition in Eclipse which is injected with Guice, instead of using F3 to go to the defintion which you would do if it was a class, then use Ctrl-T to choose among the implementations of that interface.

If you have more than one to choose from, then you need the module bindings printed out so you know which one to pick. Unfortunately Eclipse doesn't understand injection yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文