How to remove a generic type from a class using refactoring

I have this class:

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

I decided that type T is no longer needed, and it is not used in the class (I know that it is always Integer).
However, I have many references for a class that looks like TimeIntCo<Integer> .
Is there a way to use Eclipse refactoring to remove type T without any errors in the links?
I want to do it in one step and not find and replace.

EDIT: to be more clear - if I just remove T from the 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 find refactoring safer.

+7
source share
7 answers

I doubt there is a refactor for this. Just delete <T> , save the file and go through and fix any compilation errors that may occur. (If a parameter of type T not used, there should be no compilation errors to fix ...)


Comments by @ohadshai:

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

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

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

No, I do not. Reactors are commonly used for simple conversions that preserve the meaning of the source code. This conversion is unlikely to do this.

+1
source

Just do a text find / replace TimeIntCo<Integer> β†’ TimeIntCo on all Java classes.

+4
source

Enter superclass

 public class TimeIntCoSup extends TimeValueCo<Integer> { } 

and change TimeIntCo to expand it.

 public class TimeIntCo<T> extends TimeIntCoSup { } 

Then use Eclipse Refactor -> Use Supertype Where Possible

+3
source

Since refactoring the buildin IDE cannot handle what you want, then if you're bored in the find / replace manual, 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, since any TimeIntCo<T> or TimeIntCo<Integer> will be deleted in it, but it will not fix your decrlarations T someMethod(T t) inside TimeIntCo

Offering an alternative, you also cannot use an environment like unix or Bash, so just ignore it.

+1
source

if T is of no use to you, just release it from the class declaration:

 public class TimeIntCo extends TimeValueCo<Integer> { } 

He can safely remain in the parent class of TimeValueCo .

0
source

Use search and replace regular expressions in Eclipse (Search | File): search: SomeClass\<.*\> And replace with: SomeClass . Repeat the procedure for subclasses if necessary.

0
source

If you use Intellij: signature refactoring

0
source

All Articles