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

StackOverflow https://stackoverflow.com/questions/17613668

  •  02-06-2022
  •  | 
  •  

Domanda

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.

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.

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. I also tried grepping, 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). I know Qt has a similar mechanism for finding translatable strings in the code, but that's a totally different technology..

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.

È stato utile?

Soluzione

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.

  1. Find all classes in my project's root package using the Reflections library
  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)
  3. Read the appropriate .java files in the project directory from the given row until the ';' 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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top