简体   繁体   English

如何以编程方式查找项目中的所有Java方法调用和给定参数?

[英]How to find all Java method calls and given parameters in a project programmatically?

I have a multilingual web application that gets all of the translations from a single object, for example lang.getTranslation("Login") and the object is responsible for finding the translation in an xml file in the user's language. 我有一个多语言的Web应用程序,它从单个对象(例如lang.getTranslation("Login")获取所有翻译,并且该对象负责以用户语言在xml文件中查找翻译。

What I'd like to do is a script / custom static analysis that outputs all the missing translations and translations that are no more used in the application. 我想做的是一个脚本/自定义静态分析,该分析输出所有缺少的翻译和在应用程序中不再使用的翻译。 I believe I should start by programmatically finding every call to the getTranslation method and the string parameter, storing the data in a special structure and comparing it to all the translation files. 我相信我应该首先以编程方式找到对getTranslation方法和string参数的每次调用,将数据存储在特殊的结构中,然后将其与所有翻译文件进行比较。

Is there a library that will allow me to do this easily? 有没有可以让我轻松完成此操作的库? I already found Javassist but I can't use it to read the parameter values. 我已经找到了Javassist,但是我不能用它来读取参数值。 I also tried grep ping, but I'm not sure if that's a robust solution (in case there will be a call to another class that has a getTranslation method). 我也尝试了grep ping,但是我不确定这是否是一个可靠的解决方案(以防万一将调用具有getTranslation方法的另一个类)。 I know Qt has a similar mechanism for finding translatable strings in the code, but that's a totally different technology.. 我知道Qt具有类似的机制来在代码中查找可翻译的字符串,但这是一种完全不同的技术。

I'm asking this because I'm quite sure there's a good existing solution for this and I don't want to reinvent the wheel. 我之所以这样问,是因为我非常确定有一个很好的现有解决方案,而且我也不想重蹈覆辙。

Ok, here's how I did it. 好的,这就是我的做法。 Not the optimal solution, but works for me. 不是最佳解决方案,但对我有用。 I created a utility program in Java to find all the method calls and compare the parameters to existing translations. 我用Java创建了一个实用程序,以查找所有方法调用并将参数与现有转换进行比较。

  1. Find all classes in my project's root package using the Reflections library 使用Reflections库在项目的根包中找到所有类
  2. Find all getTranslation method calls of the correct class in the classes using the Javassist library and create a list of them (contains: package, class, row number) 使用Javassist库在类中找到正确类的所有getTranslation方法调用,并创建它们的列表(包含:包,类,行号)
  3. Read the appropriate .java files in the project directory from the given row until the ';' 从给定的行中读取项目目录中的相应.java文件,直到“;”为止 character 字符
  4. Extract the parameter value and add it to a list 提取参数值并将其添加到列表中
  5. Find the missing translations and output them 找到缺少的翻译并输出
  6. Find the redundant translations and output them 找到多余的翻译并输出

It took me a while to do this, but at least I now have a reusable utility to keep the translation files up to date. 我花了一段时间才能做到这一点,但至少现在我有了一个可重用的实用程序,可以使翻译文件保持最新状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM