AutoRefactor icon indicating copy to clipboard operation
AutoRefactor copied to clipboard

New refactoring - Meta - Suggest methods that can be extracted

Open JnRouvignac opened this issue 12 years ago • 1 comments

If possible, improve or build on the JDT extract method.

Extract method across type of variables, types, and casts

It would be nice to provide Extract method refactoring across:

  • any kind of expression, just look at the expression type (all can be extracted as parameters),
  • types and supertypes (choose supertype over subtype when more code can be extracted)
  • type casts (you have code that uses ints in one place and longs in other? Then use longs to extract more code)
  • the presence of local variables or inlined code (very useful because of line length limits)
  • etc.

Move statements together

Probably too hard to do, would be to move statements together, but this might require deep inter procedural analysis. Maybe simple arithmetics or boolean expressions that all live in the current method body can be moved like this.

Consecutive lines threshold

Also, if we hit specific thresholds: If more than X (10? 20? 30?) consecutive lines of code are only here to output one variable: then we suggest to extract a method.

Eclipse's JDT Extract Method

See info about Eclipse's JDT Extract Method here:

  • http://wiki.eclipse.org/Extract_method_revival
  • org.eclipse.jdt.ui.extract.method
  • org.eclipse.jdt.ui.actions.ExtractMethodAction
  • org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodRefactoring
  • org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodAnalyzer

For development, see:

  • https://github.com/eclipse/eclipse.jdt.ui/tree/master/org.eclipse.jdt.ui/core%20refactoring/org/eclipse/jdt/internal/corext/refactoring/code

Example tests with in and out folders:

  • https://github.com/eclipse/eclipse.jdt.ui/tree/e5035aaa0b268ce1b0110eaddb3424e954aa45eb/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractMethodWorkSpace/ExtractMethodTests

JnRouvignac avatar Mar 05 '13 00:03 JnRouvignac

It is possible to programatically call Eclipse's refactorings: read the following javadocs: http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjdt%2Fcore%2Frefactoring%2FIJavaRefactorings.html

So once potential refactorings have been found, it should be possible to do a few simple refactorings to extract local variables and move them around, then use the Eclipse "Extract Method" refactoring.

JnRouvignac avatar Oct 14 '13 20:10 JnRouvignac