Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 10934

Summary: New Refactoring: Hide/Create Delegate [refactoring]
Product: [Eclipse Project] JDT Reporter: Leon J. Breedt <ljb>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: RESOLVED WONTFIX QA Contact:
Severity: enhancement    
Priority: P3 CC: dirk_baeumer
Version: 2.0Keywords: helpwanted
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

Description Leon J. Breedt CLA 2002-03-07 05:50:06 EST
Description:

This refactoring is an extension to 'Hide Delegate' discussed here:

http://www.refactoring.com/catalog/hideDelegate.html

Context of use:

A member previously accessible via getter and setter exposes too
much about the implementation of its owner. Selecting Hide Delegate would
generate wrapping methods which would call the related methods on the
member.

If no getter and setter were present, option would be named
"Create Delegate".

Would not work with primitives.

The wizard to perform this refactoring should display a selection
list (much like the current override dialog) of delegate methods to
expose. It should also offer the option to remove the getters and
setters.
Comment 1 Erich Gamma CLA 2002-03-08 06:49:04 EST
we can't do it for 2.0 unless we get external help
Comment 2 Leon J. Breedt CLA 2002-03-08 06:54:54 EST
if someone can give me a pointer to an existing similar refactoring to look at, 
i'd be willing to dig into it. also, is refactoring a moving target (i.e. which 
builds' source should i be working on?)
Comment 3 Dirk Baeumer CLA 2002-03-08 13:16:22 EST
We don't have a compareable refactoring but a good general starting point is 
the implementation of the Self Encapsulate Field refactoring. It is in 
org.eclipse.jdt.internal.corext.refactoring.sef.

Currently only the AST is public API. The rest of the refactoring stuff is 
still internal. There are plans to make it API > 2.0. 

However the changes are high that most of the internal API is kept stable for 
2.0. We are pretty much done with the refactorings we want to implement and the 
internal API has been stable since 6 weeks.

So I encourage you to give it a try. Simply use the internal API. But be aware 
that we only accept contributions with test cases <g>. There are two project on 
the Eclipse CVS server that help you to write refactoring tests: 
org.eclipse.jdt.ui.test and org.eclipse.jdt.ui.tests.refactoring. 

Looking forward to hearing from you
Comment 4 Leon J. Breedt CLA 2002-03-14 03:48:00 EST
regarding packaging, should I implement it in its own package? i see that a lot 
of other refactorings are in shared packages, i'd prefer to put it in a package
like org.eclipse.jdt.internal.corext.refactoring.delegate.

additional comments on how the two actions provided should work:

>> Create Delegate Action

Validity:
    Selected field(s) non-primitive. Selected field(s) NOT externally
    visible (private).

Create delegate adds delegate methods to owner class. Which delegate
methods are added can be selected by user (similar to override methods).

>> Hide Delegate Refactoring Action

Validity:
    Selected field(s) non-primitive. Selected field(s) are externally visible
    outside of the declaring class: have getter. Should we allow public,
    protected, package as well?

Hide Delegate removes getter (and changes field access to private)
and creates delegate methods in owner class to access
referenced methods of field(s).

    * All compilation units in the search space (project? workspace?) which
      accessed methods of field(s) are modified to access delegate 
      methods of the owner class.

Delegate methods have the exact same name as methods of the member, or
the same name but with a user-supplied prefix.

Errors:
    * If new delegate method has naming clash with existing method in owner
      class.
    * If compilation unit in search space used a field of the newly hidden 
      field(s) and not a method...or should we just create accessors for these
      as well?
    * If unit in search space just grabbed a reference and didn't use
      a method of the member immediately...later on when actual methods are
      called we don't have a reference to the owner class.
Comment 5 Dirk Baeumer CLA 2002-03-15 03:35:22 EST
Yes, start with your own package. If we decide to integrate it into another 
package we can use the move refactoring ;-)).

Create Delegate:
  - validation: field not visible and there aren't any getter and setter
    methods for the private field.

Hide Delegate:
  - in a first implementation I would only allow hide delegate on fields that
    are private and have a visible getter method. If a user wants to use hide
    delegate on public fields than he first have to use self encapsulate field.

  - to search for all references to an element using the right scope have a look
    at class RefactoringScopeFactory.

  - Errors:
      o the new method can also overload an existing method and change the 
        semantic of the program without producing a compile error. But for the
        start I would ignore that case.
      o flagging the second and third case as an error is fine for me. IMO they
        are seldom cases anyway.

Just to let you know: I am on vacation the next week ;-).
Comment 6 Dirk Baeumer CLA 2002-07-23 09:31:35 EDT
[refactoring]

No action planned for 2.1. Bug is tagged as help wanted
Comment 7 Dirk Baeumer CLA 2003-04-24 11:42:38 EDT
Create delegate is in place for 2.1. Hide delegate could be done via inline 
method. No further action planned.