0

I am currently on a task, where I need to fix the deprecated items in the existing projects. I mean I have to replace the deprecated items with the corresponding replacing items as given in javadocs,

Eg: java.util.Date.setSeconds(int): Instead of this deprecated method, we need to use Calendar.set(Calendar.SECOND, int seconds).

I need to automate this using java code in eclipse IDE, by giving a project name in the workspace as input.

Kindly suggest me in doing this.

Thanks in advance.

Mat
  • 202,337
  • 40
  • 393
  • 406
Easwaramoorthy Kanagaraj
  • 3,925
  • 8
  • 36
  • 62

2 Answers2

3

I would go with the search & replace functionality of your IDE, by utilizing regex. (your parameter values should be captured with regex)

There isn't any specific utility to replace deprecated code, because it is not always that case that there is a straightforward replacement. Sometimes there is not replacement, and in other cases there is a completely different approach.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Hi Bozho, Thanks for your comment. Is there any class or some source in java which can give the replacement of the deprecated items. If that is available I can just utilize it otherwise is there any other method to do this. I can go for regex, but I need to do a generic functionality which will just remove the deprecated items and replace it with the available replacement. Is it possible? – Easwaramoorthy Kanagaraj May 18 '11 at 12:07
  • 1
    generically - no. there's too much variation. – Bozho May 18 '11 at 12:12
  • Usually the @Deprecated annotation gives you a pointer to the API that supersedes the deprecated one. – Tassos Bassoukos May 18 '11 at 13:35
  • yes, you can know which ones are deprecated, but not how to replace them. – Bozho May 18 '11 at 13:38
0

If the project is not really big probably easiest way is to do this by hand. It also handles the situations where there is no direct replacement.

Alternative (and definitely more interesting) way would be to write an eclipse plugin that extends eclipses JDT. Also if you are on Java 6 one possibility is to use Java compiler API.

Ahe
  • 2,124
  • 3
  • 19
  • 29
  • Hi Ahe, Is there any utility that will give the the replacement for the deprecated item if we are passing the deprecated item. Or is there any possible way for creating the utility by ourselves. – Easwaramoorthy Kanagaraj May 18 '11 at 12:14
  • As @Ahe mentioned, you could use the JDT search APIs to find all references to setSecond(*), get the AST for that method, and use ASTRewrite to "refactor" the code. See http://stackoverflow.com/questions/5887132/how-could-i-search-references-to-a-field-on-a-ast-or-a-compilationunit-in-eclipse/5887882#5887882 for an example. – Paul Webster May 18 '11 at 13:17
  • HI Ahe/Paul, I want a generic functionality which will replace all the deprecated items in the given project. I too was trying to develop a plugin, but I dont find a source which will provide the replacement for the deprecated items. Kindly suggest your ideas. – Easwaramoorthy Kanagaraj May 25 '11 at 09:31
  • @Paul Webster HI Ahe/Paul, I want a generic functionality which will replace all the deprecated items in the given project. I too was trying to develop a plugin, but I dont find a source which will provide the replacement for the deprecated items. Kindly suggest your ideas – Easwaramoorthy Kanagaraj May 25 '11 at 10:55
  • As answered in your other question, there's no generic way to do this as there is no well defined format to `@Deprecated` that would allow a tool to change the code, especially for semantic changes. – Paul Webster May 25 '11 at 11:05
  • As @Paul Webster said there is way too much variation to do this fully automatically + there could be @Deprecated markings on your companies internal API also. By writing your own program you can probably handle few most common situations. But as I said, in the end the easiest way is probably to handle each deprecation by hand. Good IDE will help you to find all the places you need to change. – Ahe May 25 '11 at 12:33