#231 require explicit "this."

Check (274)

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)?


  • Tim Tyler

    Tim Tyler - 2003-06-18

    Logged In: YES

    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

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

  • Stephen Bloch

    Stephen Bloch - 2003-06-19

    RequireThis.java and supporting classes

  • Stephen Bloch

    Stephen Bloch - 2003-06-19

    Logged In: YES

    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?

  • Oleg Sukhodolsky

    Logged In: YES

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


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks