`this` in static context

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

`this` in static context

xSeagullx
Hello,

I remember that it was possible to use `this` in static context. It’s seems, IntelliJ idea doesn’t support it.

I tried it in groovy console, and seems it still works in Groovy. Is it behaviour that is desired, or this thing is deprecated?
If it should be supported, I can create a ticket to JetBrains.


Best Regards,
Pavel


Reply | Threaded
Open this post in threaded view
|

RE: `this` in static context

eric.milles

For sure , "this" and "super" are supported within a closure defined in a static context.  Are you saying they also work directly in a static method?  Given "class A { static main(args} { this } }", this should be of type "Class<A>" if it is indeed supported.  It follows that it would work, since you can call directly methods on Class<A> from that scope, like getCanonicalName().

Reply | Threaded
Open this post in threaded view
|

Re: `this` in static context

xSeagullx
No, I’m not talking about closures. Normal static methods and static initialisers.
Honestly, I remember using it years ago. Not recently though.

It’s a small but handy thing to have when you are defining stuff like loggers manually. 

Imagine code like this: // AFAIK it should work now.

```
class LongClassName { 
  private static Logger log = Logger.getLogger(this.name) // instead of duplicating class name here. Logger.getLogger(LongClassName.class.getName())
}
``` 

It simplifies manual class renaming, and eliminates chances of error.

I was sure it should work, but seems not in IDEA. So, I’m guessing, is it IDEA bug, or intended behaviour.

Cheers,
Pavel

On 6 Nov 2017, at 18:45, <[hidden email]> <[hidden email]> wrote:

For sure , "this" and "super" are supported within a closure defined in a static context.  Are you saying they also work directly in a static method?  Given "class A { static main(args} { this } }", this should be of type "Class<A>" if it is indeed supported.  It follows that it would work, since you can call directly methods on Class<A> from that scope, like getCanonicalName().

Reply | Threaded
Open this post in threaded view
|

RE: `this` in static context

eric.milles

You should be able to call `name` or `canonicalName` or `sinpleName` directly if there are no static methods in the class or its supers that would take precedence.

 

```

class LongClassName { 

  private static Logger log = Logger.getLogger(name)

  private static Logger log = Logger.getLogger(this) // another possibility for frameworks that accept a class instance for logger namespacing

}

``` 

-- although this particular case has been much simplified by @Log, et al.

Reply | Threaded
Open this post in threaded view
|

RE: `this` in static context

eric.milles

Quick correction: `name` or `canonicalName` or `simpleName` cannot be used directly in static scopes.  You will get "Groovy:Apparent variable 'canonicalName' was found in a static scope but doesn't refer to a local variable, static field or class."

 

However, you can use `getName()` or `getCanonicalName()` or `getSimpleName()` etc. directly or `this.name` or `this.canonicalName` or ... as you stated in your original message.

 

 

Two funny thing about all this:

class A { static { this; super } } // "this" here is Class<A> and "super" here is Object -- Should it be Class<Object>?

class B extends A { static { this; super } } // "this" here is Class<B> and "super" here is Class<A>

 

class B extends A { static {

  def closure = {

    this; super // "this" here is Class<B> and "super" here is Class<B> -- Should it be Class<A>?

  }

}}

 

From: Milles, Eric (TR Technology & Ops)
Sent: Monday, November 06, 2017 11:59 AM
To: [hidden email]
Subject: RE: `this` in static context

 

You should be able to call `name` or `canonicalName` or `simpleName` directly if there are no static methods in the class or its supers that would take precedence.

 

```

class LongClassName { 

  private static Logger log = Logger.getLogger(name)

  private static Logger log = Logger.getLogger(this) // another possibility for frameworks that accept a class instance for logger namespacing

}

``` 

-- although this particular case has been much simplified by @Log, et al.