15 March 2009

Return 'this' proposal

GENESIS

It's a simplified 'This' type problem, which seems to be too much complicated as for now and need more analyses. Construction is designed not to interact with possibility that 'This' type will be introduced.

OVERVIEW

FEATURE SUMMARY:
It allows the method to return reference to 'this' object.

MAJOR ADVANTAGE:
Simplification of return this; statement.
'void' can be easy replaced with 'this'.

MAJOR BENEFIT(s): It would prevent NullPointerException, and make some 'builder' like interfaces look really clear and simple.

MAJOR DISADVANTAGE:
Returned value cannot be changed while inheritance.

ALTERNATIVES:
Self-bounded generics, This type.

EXAMPLES

SIMPLE/ADVANCED EXAMPLE:

public class Builder {

  String text = "";

  public this add (char c){text+=c;}
  
  public this add (String c){
    if (c==null){
      text +="null";
      return; // this will be returned
    }
    text+=c;
  }
  
  public static void test(Builder builder) {
    builder.add('.').add("test()").add(':');
  }

}

package test;

public class ReturnThis {

public static Class<?> getTestingReturnType(Object object) throws SecurityException, NoSuchMethodException {
return object.getClass().getMethod("testing").getReturnType();
}

static class A { public this testing() {}; }

static class B extends A {}

public static void main(String[] args) throws SecurityException, NoSuchMethodException {
System.out.println(getTestingReturnType(new A()));
// will print: class test.ReturnThis$A

System.out.println(getTestingReturnType(new B()));
// will print: class test.ReturnThis$B
}
}

  static class A { public this test() { } }

  static class B extends A { }

  static Class<?> getReturnedType(Class<?> classs) {
    try {
      return classs.getMethod("test").getReturnType();
    } catch (SecurityException e) {
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    }
    return null;
  }

  public static void main(String[] args) {
    System.out.println(getReturnedType(A.class));
    // will print: class returnThis.ReturnThis$A
    
    System.out.println(getReturnedType((new A()).getClass()));
    // will print: class returnThis.ReturnThis$A
    
    System.out.println(getReturnedType(B.class));
    // will print: class returnThis.ReturnThis$B
    
    System.out.println(getReturnedType((new B()).getClass()));
    // will print: class returnThis.ReturnThis$B
  }
DETAILS

SPECIFICATION:
JLS 8.4

ResultType:
  Type
  void
  this

A method declaration either specifies the type of value that the method returns, uses the keyword void to indicate that the method does not return a value, or uses the keyword this to indicate that the method return the reference to 'this' object.

Keyword this cannot occurs if method is static.


JLS 14.17

ReturnStatement:
  return Expressionopt ;

A return statement with no Expression must be contained in the body of a method that is declared, using the keyword void, not to return any value (§8.4), or in the body of a method that is declared, using the keyword this , to return this reference (§8...),or in the body of a constructor (§8.8).

Method Overriding:
Method with 'this' as ResultType can be override only with method with the same ResultType (proposal in this form can be, however, extended to work with backward covariant return types in future)

COMPILATION:

  • Method just before exiting from it's scope returns 'this'.
  • Returned object type is ALWAYS actual object type.
  • Method in compiled form have special 'this' type as ResultType, which is exchanged during loading to be actual object type:

    interface A{ this sample(); }
    interface B extends A { }
    class C implements B { this sample(){...}; }
    So compiled classes contain only methods with A and C, but after loading they are extended to ensure that Method.getReturnType() will be always actual object type.

TESTING:
Most efforts need to be focused on ensuring that Method.getReturnType() will work in a correct way.

LIBRARY SUPPORT:
No.

REFLECTIVE APIS:
No: From this perspective methods are simply overridden in runtime.

OTHER CHANGES:
Javadoc:
return type is visible as 'this' and linked to current class.

MIGRATION:
None.

COMPATIBILITY

Backward: full.
Forward: can be handled like other void type to be partial compatibile.

REFERENCES

Bug: 6479372
http://java.net/cs/user/view/cs_msg/37432

0 comments / komentarz(y):

Post a Comment