开发者

how can I remove generic type from class using refactoring

开发者 https://www.devze.com 2023-03-15 03:11 出处:网络
I have this开发者_开发百科 class: public class TimeIntCo<T> extends TimeValueCo<Integer>

I have this开发者_开发百科 class:

public class TimeIntCo<T> extends TimeValueCo<Integer>
{

}

I decided that the T type is not needed anymore, and it is not used in the class (I know it is always Integer).

However, I have a lot of references for the class that looks like TimeIntCo<Integer>.

Is there a way using Eclipse refactoring to remove the T type without causing any error in references?

I mean to do it in one step and not find&replace.

EDIT: to be more clear - if I just remove T from class I have about 10,000 errors, I don't want to do it manually, and I prefer not to use find&replace because I consider refactor safer.


Introduce a superclass

public class TimeIntCoSup extends TimeValueCo<Integer> {

}

and change TimeIntCo to extend it.

public class TimeIntCo<T> extends TimeIntCoSup {

}

Then use Eclipse's Refactor -> Use Supertype Where Possible


Just do a textual find/replace TimeIntCo<Integer> -> TimeIntCo on all java classes.


I doubt that there is a refactor for this. Just delete the <T>, save the file, and go through and fix any compilation errors that might result. (If the T type parameter is not used, there shouldn't be any compilation errors to correct ...)


@ohadshai comments:

then I have about 10,000 errors in eclipse...

I bet that most of them can be fixed by changing a small number of subclass declarations.

Do you know any other IDE that does that? IDEA?

No I don't. Refactors are generally used for simple transformations that preserve the meaning of the original source code. This transformation is unlikely to do that.


As the buildin IDE refactoring can't handle what you want, then if you're bored of the manual find/replace, this will work

while read -r file; do 
    sed -i 's/TimeIntCo<[[:alpha:]]*>/TimeIntCo/g' "$file"; 
done < <(find /path/to/src/ -type f -iname "*.java")` 

It will work as in it will remove any TimeIntCo<T> or TimeIntCo<Integer> but it won't fix your decrlarations of T someMethod(T t) inside TimeIntCo

Just proposing an alternative, also you may not be using a unix like environment or Bash, so just ignore this.


if T isn't providing you any benefit, just drop it from the class declaration:

public class TimeIntCo extends TimeValueCo<Integer>
{

}

It can safely remain in the parent class TimeValueCo.


Use regular expression search and replace in Eclipse (Search | File): search for: SomeClass\<.*\> and replace with: SomeClass. Repeat for subclassses as necessary.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号