Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

51
Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default Raffi Khatchadourian 1,2 Hidehiko Masuhara 3 International Conference on Automated Software Engineering, 2017 1 Computer Science, Hunter College, City University of New York, USA 2 Computer Science, The Graduate Center, City University of New York, USA 3 Mathematical and Computing Science, Tokyo Institute of Technology, Japan

Transcript of Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Page 1: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Defaultification Refactoring: A Tool forAutomatically Converting Java Methods toDefault

Raffi Khatchadourian1,2 Hidehiko Masuhara3

International Conference on Automated Software Engineering, 20171Computer Science, Hunter College, City University of New York, USA

2Computer Science, The Graduate Center, City University of New York, USA

3Mathematical and Computing Science, Tokyo Institute of Technology, Japan

Page 2: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Motivation

Page 3: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Interfaces Are Traditionally Lists of Method Declarations

• Traditionally, an interface is a Java type that lists methoddeclarations.

• Clients are guaranteed that concrete interface implementersprovide implementations for all listed methods.

interface Collection<E> {int size();void add(E elem);boolean isEmpty();int capacity();abstract boolean atCapacity();} 1

Page 4: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Interfaces Are Traditionally Lists of Method Declarations

• Traditionally, an interface is a Java type that lists methoddeclarations.

• Clients are guaranteed that concrete interface implementersprovide implementations for all listed methods.

interface Collection<E> {int size();void add(E elem);boolean isEmpty();int capacity();abstract boolean atCapacity();} 1

Page 5: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Some Interface Methods Are Optional

• Interface methods can be listed as optional operations.

• Implementers may choose to support them or not.• If operations are unsupported, they conventionally throw anUnsupportedOperationException.

interface Collection<E> {// ...void add(E elem); /* optional */ }}

2

Page 6: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Some Interface Methods Are Optional

• Interface methods can be listed as optional operations.• Implementers may choose to support them or not.

• If operations are unsupported, they conventionally throw anUnsupportedOperationException.

interface Collection<E> {// ...void add(E elem); /* optional */ }}

class ImmutableList<E> implements Collection<E> {// ...

}

2

Page 7: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Some Interface Methods Are Optional

• Interface methods can be listed as optional operations.• Implementers may choose to support them or not.• If operations are unsupported, they conventionally throw anUnsupportedOperationException.

interface Collection<E> {// ...void add(E elem); /* optional */ }}

class ImmutableList<E> implements Collection<E> {// ...@Override public void add(E elem) {throw new UnsupportedOperationException();}}

2

Page 8: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Skeletal Implementation Classes Help Implement Interfaces

• The skeletal implementation design pattern [Bloch, 2008] isused to make implementing interfaces easier.

• Abstract skeletal implementation class provides partialimplementations.

• Implementers extend the skeletal implementation class ratherthan directly implementing the interface.

abstract class AbstractImmutableList<E> implementsCollection<E> {

@Override public void add(E elem) {throw new UnsupportedOperationException();}}

class ImmutableList<E> extends AbstractImmutableList<E>{// ...@Override public void add(E elem) {throw new UnsupportedOperationException();}}}

3

Page 9: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Skeletal Implementation Classes Help Implement Interfaces

• The skeletal implementation design pattern [Bloch, 2008] isused to make implementing interfaces easier.

• Abstract skeletal implementation class provides partialimplementations.

• Implementers extend the skeletal implementation class ratherthan directly implementing the interface.

abstract class AbstractImmutableList<E> implementsCollection<E> {

@Override public void add(E elem) {throw new UnsupportedOperationException();}}

class ImmutableList<E> extends AbstractImmutableList<E>{// ...@Override public void add(E elem) {throw new UnsupportedOperationException();}}}

3

Page 10: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Skeletal Implementation Classes Help Implement Interfaces

• The skeletal implementation design pattern [Bloch, 2008] isused to make implementing interfaces easier.

• Abstract skeletal implementation class provides partialimplementations.

• Implementers extend the skeletal implementation class ratherthan directly implementing the interface.

abstract class AbstractImmutableList<E> implementsCollection<E> {

@Override public void add(E elem) {throw new UnsupportedOperationException();}}

class ImmutableList<E> extends AbstractImmutableList<E>{// ...@Override public void add(E elem) {throw new UnsupportedOperationException();}}}

3

Page 11: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

The Skeletal Implementation Pattern Has Several Drawbacks

The skeletal implementation pattern has several drawbacks.

ExampleImmutableList cannot:

• Subclass another class.• Inherit skeletal implementations split over multipleclasses [Horstmann, 2014].

• Inherit skeletal implementations for multiple interfaces.

4

Page 12: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

The Skeletal Implementation Pattern Has Several Drawbacks

The skeletal implementation pattern has several drawbacks.

ExampleImmutableList cannot:

• Subclass another class.• Inherit skeletal implementations split over multipleclasses [Horstmann, 2014].

• Inherit skeletal implementations for multiple interfaces.

4

Page 13: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

The Skeletal Implementation Pattern Has Several Drawbacks

The skeletal implementation pattern has several drawbacks.

ExampleImmutableList cannot:

• Subclass another class.

• Inherit skeletal implementations split over multipleclasses [Horstmann, 2014].

• Inherit skeletal implementations for multiple interfaces.

4

Page 14: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

The Skeletal Implementation Pattern Has Several Drawbacks

The skeletal implementation pattern has several drawbacks.

ExampleImmutableList cannot:

• Subclass another class.• Inherit skeletal implementations split over multipleclasses [Horstmann, 2014].

• Inherit skeletal implementations for multiple interfaces.

4

Page 15: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

The Skeletal Implementation Pattern Has Several Drawbacks

The skeletal implementation pattern has several drawbacks.

ExampleImmutableList cannot:

• Subclass another class.• Inherit skeletal implementations split over multipleclasses [Horstmann, 2014].

• Inherit skeletal implementations for multiple interfaces.

4

Page 16: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Java 8 Default Methods Can Replace Skeletal Implementations

• Java 8 enhanced interfaces allow both method declarations anddefinitions.

• Implementers inherit the (default) implementation if noneprovided.

• Original motivation to facilitate interface evolution.• Can also be used as a replacement of the skeletalimplementation pattern [Goetz, 2011].

interface Collection<E> {default void add(E elem) { // optional.throw new UnsupportedOperationException();}}

class ImmutableList<E> implements Collection<E> {}

abstract class AbstractImmutableList<E> implementsCollection<E> {

@Override public void add(E elem) {throw new UnsupportedOperationException();}}

5

Page 17: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Java 8 Default Methods Can Replace Skeletal Implementations

• Java 8 enhanced interfaces allow both method declarations anddefinitions.

• Implementers inherit the (default) implementation if noneprovided.

• Original motivation to facilitate interface evolution.• Can also be used as a replacement of the skeletalimplementation pattern [Goetz, 2011].

interface Collection<E> {default void add(E elem) { // optional.throw new UnsupportedOperationException();}}

class ImmutableList<E> implements Collection<E> {}

abstract class AbstractImmutableList<E> implementsCollection<E> {

@Override public void add(E elem) {throw new UnsupportedOperationException();}}

5

Page 18: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Java 8 Default Methods Can Replace Skeletal Implementations

• Java 8 enhanced interfaces allow both method declarations anddefinitions.

• Implementers inherit the (default) implementation if noneprovided.

• Original motivation to facilitate interface evolution.

• Can also be used as a replacement of the skeletalimplementation pattern [Goetz, 2011].

interface Collection<E> {default void add(E elem) { // optional.throw new UnsupportedOperationException();}}

class ImmutableList<E> implements Collection<E> {}

abstract class AbstractImmutableList<E> implementsCollection<E> {

@Override public void add(E elem) {throw new UnsupportedOperationException();}}

5

Page 19: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Java 8 Default Methods Can Replace Skeletal Implementations

• Java 8 enhanced interfaces allow both method declarations anddefinitions.

• Implementers inherit the (default) implementation if noneprovided.

• Original motivation to facilitate interface evolution.• Can also be used as a replacement of the skeletalimplementation pattern [Goetz, 2011].

interface Collection<E> {default void add(E elem) { // optional.throw new UnsupportedOperationException();}}

class ImmutableList<E> implements Collection<E> {}

abstract class AbstractImmutableList<E> implementsCollection<E> {

@Override public void add(E elem) {throw new UnsupportedOperationException();}}

5

Page 20: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Implementation

Page 21: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

• Implemented as an open source plug-in for the Eclipse IDE(available at http://cuny.is/interefact).

• Built on existing refactoring support in Eclipse.• Conceptual approach based on type-constraints [Palsberg andSchwartzbach, 1994; Tip et al., 2011].

• See ICSE 2017 paper [Khatchadourian and Masuhara, 2017] forapproach details.

• Implementation is in procedural-style, similar to Pull UpMethod refactoring.

6

Page 22: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

• Implemented as an open source plug-in for the Eclipse IDE(available at http://cuny.is/interefact).

• Built on existing refactoring support in Eclipse.

• Conceptual approach based on type-constraints [Palsberg andSchwartzbach, 1994; Tip et al., 2011].

• See ICSE 2017 paper [Khatchadourian and Masuhara, 2017] forapproach details.

• Implementation is in procedural-style, similar to Pull UpMethod refactoring.

6

Page 23: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

• Implemented as an open source plug-in for the Eclipse IDE(available at http://cuny.is/interefact).

• Built on existing refactoring support in Eclipse.• Conceptual approach based on type-constraints [Palsberg andSchwartzbach, 1994; Tip et al., 2011].

• See ICSE 2017 paper [Khatchadourian and Masuhara, 2017] forapproach details.

• Implementation is in procedural-style, similar to Pull UpMethod refactoring.

6

Page 24: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

• Implemented as an open source plug-in for the Eclipse IDE(available at http://cuny.is/interefact).

• Built on existing refactoring support in Eclipse.• Conceptual approach based on type-constraints [Palsberg andSchwartzbach, 1994; Tip et al., 2011].

• See ICSE 2017 paper [Khatchadourian and Masuhara, 2017] forapproach details.

• Implementation is in procedural-style, similar to Pull UpMethod refactoring.

6

Page 25: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

• Implemented as an open source plug-in for the Eclipse IDE(available at http://cuny.is/interefact).

• Built on existing refactoring support in Eclipse.• Conceptual approach based on type-constraints [Palsberg andSchwartzbach, 1994; Tip et al., 2011].

• See ICSE 2017 paper [Khatchadourian and Masuhara, 2017] forapproach details.

• Implementation is in procedural-style, similar to Pull UpMethod refactoring. 6

Page 26: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

High-level System Workflow

2. Options control refactoring “invasiveness.”

• Remove or deprecate empty classes.• Consider non-standard annotation differences.

3. Simple initial checks, e.g., file writability.4. Bulk of processing in final check.

etween method declarations and definitions. an be useful forprojects not using annotation processing frameworks.

7

Page 27: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

High-level System Workflow

2. Options control refactoring “invasiveness.”

• Remove or deprecate empty classes.• Consider non-standard annotation differences.

3. Simple initial checks, e.g., file writability.4. Bulk of processing in final check.

etween method declarations and definitions. an be useful forprojects not using annotation processing frameworks.

7

Page 28: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

High-level System Workflow

2. Options control refactoring “invasiveness.”• Remove or deprecate empty classes.

• Consider non-standard annotation differences.3. Simple initial checks, e.g., file writability.4. Bulk of processing in final check.

etween method declarations and definitions. an be useful forprojects not using annotation processing frameworks.

7

Page 29: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

High-level System Workflow

2. Options control refactoring “invasiveness.”• Remove or deprecate empty classes.• Consider non-standard annotation differences.

3. Simple initial checks, e.g., file writability.4. Bulk of processing in final check.

etween method declarations and definitions. an be useful forprojects not using annotation processing frameworks.

7

Page 30: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

High-level System Workflow

2. Options control refactoring “invasiveness.”• Remove or deprecate empty classes.• Consider non-standard annotation differences.

3. Simple initial checks, e.g., file writability.

4. Bulk of processing in final check.

etween method declarations and definitions. an be useful forprojects not using annotation processing frameworks.

7

Page 31: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

High-level System Workflow

2. Options control refactoring “invasiveness.”• Remove or deprecate empty classes.• Consider non-standard annotation differences.

3. Simple initial checks, e.g., file writability.4. Bulk of processing in final check.

etween method declarations and definitions. an be useful forprojects not using annotation processing frameworks.

7

Page 32: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Architecture and Plug-In Dependencies

• Four plugins: two internal and two with UIs.

• Internal plug-ins include core and test plug-ins.• 259 automated refactoring tests.• UI plug-ins include both end-user tool and evaluator.• Depends on Eclipse refactoring support.

8

Page 33: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Architecture and Plug-In Dependencies

• Four plugins: two internal and two with UIs.• Internal plug-ins include core and test plug-ins.

• 259 automated refactoring tests.• UI plug-ins include both end-user tool and evaluator.• Depends on Eclipse refactoring support.

8

Page 34: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Architecture and Plug-In Dependencies

• Four plugins: two internal and two with UIs.• Internal plug-ins include core and test plug-ins.• 259 automated refactoring tests.

• UI plug-ins include both end-user tool and evaluator.• Depends on Eclipse refactoring support.

8

Page 35: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Architecture and Plug-In Dependencies

• Four plugins: two internal and two with UIs.• Internal plug-ins include core and test plug-ins.• 259 automated refactoring tests.• UI plug-ins include both end-user tool and evaluator.

• Depends on Eclipse refactoring support.

8

Page 36: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Architecture and Plug-In Dependencies

• Four plugins: two internal and two with UIs.• Internal plug-ins include core and test plug-ins.• 259 automated refactoring tests.• UI plug-ins include both end-user tool and evaluator.• Depends on Eclipse refactoring support. 8

Page 37: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Evaluation

Page 38: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Useful?

• Successfully converted ∼20%of methods possiblyparticipating in the pattern todefault methods incorresponding interfaces(see [Khatchadourian andMasuhara, 2017] for details).

• Many failures related to:

• Inaccessibility of membersbetween skeletalimplementers andinterfaces.

• Access to instance fields.

9

Page 39: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Useful?

• Successfully converted ∼20%of methods possiblyparticipating in the pattern todefault methods incorresponding interfaces(see [Khatchadourian andMasuhara, 2017] for details).

• Many failures related to:

• Inaccessibility of membersbetween skeletalimplementers andinterfaces.

• Access to instance fields.

9

Page 40: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Useful?

• Successfully converted ∼20%of methods possiblyparticipating in the pattern todefault methods incorresponding interfaces(see [Khatchadourian andMasuhara, 2017] for details).

• Many failures related to:• Inaccessibility of membersbetween skeletalimplementers andinterfaces.

• Access to instance fields.

9

Page 41: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Useful?

• Successfully converted ∼20%of methods possiblyparticipating in the pattern todefault methods incorresponding interfaces(see [Khatchadourian andMasuhara, 2017] for details).

• Many failures related to:• Inaccessibility of membersbetween skeletalimplementers andinterfaces.

• Access to instance fields.

9

Page 42: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Correct?

• Ensured that no compilationerrors existed before andafter refactoring.

• Verified unit tests resultsidentical before and after therefactoring.

• Preliminary pull requeststudy ensures that theautomated results matchedwhat experienced developersmay have written.

• Four projects accepted ourpull requests so far.

10

Page 43: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Correct?

• Ensured that no compilationerrors existed before andafter refactoring.

• Verified unit tests resultsidentical before and after therefactoring.

• Preliminary pull requeststudy ensures that theautomated results matchedwhat experienced developersmay have written.

• Four projects accepted ourpull requests so far.

10

Page 44: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Correct?

• Ensured that no compilationerrors existed before andafter refactoring.

• Verified unit tests resultsidentical before and after therefactoring.

• Preliminary pull requeststudy ensures that theautomated results matchedwhat experienced developersmay have written.

• Four projects accepted ourpull requests so far.

10

Page 45: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Correct?

• Ensured that no compilationerrors existed before andafter refactoring.

• Verified unit tests resultsidentical before and after therefactoring.

• Preliminary pull requeststudy ensures that theautomated results matchedwhat experienced developersmay have written.

• Four projects accepted ourpull requests so far.

10

Page 46: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Summary

Page 47: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Summary & Future Work

• A refactoring tool that migrates the skeletal implementationpattern to instead use Java 8 default methods based ontype-constraints.

• Implemented as an Eclipse IDE plug-in (available athttp://cuny.is/interefact).

• Evaluated using several techniques.• In the future, composite refactorings (field encapsulation, etc.).

11

Page 48: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Summary & Future Work

• A refactoring tool that migrates the skeletal implementationpattern to instead use Java 8 default methods based ontype-constraints.

• Implemented as an Eclipse IDE plug-in (available athttp://cuny.is/interefact).

• Evaluated using several techniques.• In the future, composite refactorings (field encapsulation, etc.).

11

Page 49: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Summary & Future Work

• A refactoring tool that migrates the skeletal implementationpattern to instead use Java 8 default methods based ontype-constraints.

• Implemented as an Eclipse IDE plug-in (available athttp://cuny.is/interefact).

• Evaluated using several techniques.

• In the future, composite refactorings (field encapsulation, etc.).

11

Page 50: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

Summary & Future Work

• A refactoring tool that migrates the skeletal implementationpattern to instead use Java 8 default methods based ontype-constraints.

• Implemented as an Eclipse IDE plug-in (available athttp://cuny.is/interefact).

• Evaluated using several techniques.• In the future, composite refactorings (field encapsulation, etc.).

11

Page 51: Defaultification Refactoring: A Tool for Automatically Converting Java Methods to Default

For Further Reading

Joshua Bloch. Effective Java. Addison Wesley, 2 edition, 2008. ISBN 0321356683.Brian Goetz. Interface evolution via virtual extensions methods. Technical report,Oracle Corporation, June 2011. URL http://cr.openjdk.java.net/~briangoetz/lambda/Defender%20Methods%20v4.pdf.

Cay S. Horstmann. Java SE 8 for the Really Impatient. Addison-Wesley Professional,2014.

Raffi Khatchadourian and Hidehiko Masuhara. Automated refactoring of legacy Javasoftware to default methods. In International Conference on Software Engineering,2017.

Jens Palsberg and Michael I. Schwartzbach. Object-oriented type systems. John Wileyand Sons Ltd., 1994. ISBN 0-471-94128-X.

Frank Tip, Robert M. Fuhrer, Adam Kieżun, Michael D. Ernst, Ittai Balaban, and BjornDe Sutter. Refactoring using type constraints. ACM Transactions on ProgrammingLanguages and Systems, 2011. doi: 10.1145/1961204.1961205.

12