Scanning code base for use of deprecated methods
I've been tasked with developing a tool that scans a large collection of Java projects (~7000 .java files) for the use of deprecated methods/properties/APIs. It's been specified that the tool scans in a single file at a time, performs some analysis, and outputs a list of offending files, along with the line number the deprecated piece of code appeared at. I've been using the Eclipse JDT Java parser to effect this, however I just can't seem to figure this out.
Because each file is scanned in one at a time, the compiler doesn't know which methods are deprecated and which aren't, so I can't just call some isDeprecated() method and leave it at that.
I compiled a list of all the deprecated methods to look for in the code, and checked that against every method invocation in the code base (there are a lot of them), but that was an ugly solution and produced an unacceptably large number of false positives, due to either the limitations of the parser or the limitations of my knowledge of the parser.
Would I be able to just build each individual project and output the compiler warnings to some .txt file or something? This is my first time dealing with projects of this scale so I'm not really sure how stuff like this works. And this doesn't exactly fit the specification and it would be rather time consuming I'd imagine but if that's what it takes then that's what it takes.
Eclipse already does that for you. You just need to enable the compiler warning Java development user guide > Reference > Preferences > Java > Compiler > Deprecated API
In case you did not know, the Eclipse compiler can also be used outside the Eclipse IDE, instructions can be found in Eclipse help.
Interesting question! You said you compiled a list of all the deprecated methods to look for in the code, and checked that against every method invocation in the code base.
I think this is a good way for doing that if you use JDT. What can be improved is getting the deprecated methods with their corresponding class and package when you parsing the target library/package. For example, if you parse SDK, you get all deprecated method and their corresponding class and package. With those qualified method names, you can do search on the large collection of Java projects by matching names of method, class and package. This should greatly reduce the false positives.
very interestingly the problem you try to solve is the exact problem(plus a lot more problems relevant to this ) i had at my job ... i think i have a good solution for you that worked for me .
step 1: build a file using a crawler like program , this crawler's purpose is to create a file, for example a csv file (very common at workplaces), which contain all the features that are deprecated from the api,(a method,field,interface,class, constructor and etc). building this file is not for begginers but allso if you have some experience it can be done by 3-4 hours of work.
step 2: after you have your file from step 1, for every java file you need to check-scan what you need to do is to compare it with the file from step 1, if your dataset from step1 is updated and was progrramed good then you wont miss a thing ...
step 3: output the list - i'll leave this to you as practice . have fun !
intuition: write the dataset with a simple html-parser such as jsoup or json using Java or whatever language you like , then write a bash script comparing dataset and your .java files that you need to scan. in the script - for every .java that uses a deprecated api create a .txt file with the wanted information about this use. good luck.