#231 require explicit "this."

release_3.4
closed
Check (274)
5
2012-10-10
2003-06-16
Stephen Bloch
No

I know I'm not the only Java teacher who finds the
"this." default confusing to beginning students. I'd
prefer, at least at the beginning of my course, that
every method call be of the form
objectOrClass.methodName(args) and that every instance
variable reference be of the form objectOrThis.varName.

I tried writing a Check of my own to enforce this, and
I didn't have too much trouble with methods: it seems
to me that anywhere I find an IDENT whose parent (not
grandparent) is a METHOD_CALL, it's relying on the
"this." default. But instance variables are trickier,
because I don't see any easy way to tell where and how
a given IDENT is declared -- is it an instance
variable, a local variable, a parameter, or not a
variable at all (e.g. a method or a class)?

Discussion

  • Tim Tyler
    Tim Tyler
    2003-06-18

    Logged In: YES
    user_id=796025

    See also RFE 696295.

    Off the top of my head:

    Method: followed by "(";

    Telling local variables and parameters from fields looks
    difficult. AFAICS, you have no choice apart from
    keeping track of their names and scopes yourself - and
    suppressing the check if there's a name clash.

    Classes would also be problematical in general - I reckon.

    I think those in typedefs or casts could be identified. Apart
    from that you could use an "if its name doesn't match an in-
    scope local variable or field it must be a class" rule.

    Quite a bit of work, in all, ISTM.

     
  • Dale King
    Dale King
    2003-06-18

    Logged In: YES
    user_id=130378

    It must be possible to keep track of local variables versus
    fields as the HiddenFieldCheck does it. Take a look at that
    code.

     
  • Stephen Bloch
    Stephen Bloch
    2003-06-19

    RequireThis.java and supporting classes

     
    Attachments
  • Stephen Bloch
    Stephen Bloch
    2003-06-19

    Logged In: YES
    user_id=802564

    Thanks; HiddenFieldCheck gave me some good ideas. Attached
    is what I came up with (not polished or nicely packaged yet,
    but it seems to work). Criticism welcomed; this is my first
    attempt to write a Check.

    The infrastructure of the frame stack would probably be
    useful in a wide variety of Checks; perhaps something like
    this should be incorporated into the api?

     
  • Logged In: YES
    user_id=746148

    The check committed to CVS for 3.4
    module RequireCheck.
    Thank you for contribution.