Bug 410116 - Consider making "typeof()" mean the current class
Summary: Consider making "typeof()" mean the current class
Status: ASSIGNED
Alias: None
Product: Xtend
Classification: Tools
Component: Backlog (show other bugs)
Version: 2.4.1   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact: Karsten Thoms CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-06-06 15:05 EDT by Ed Staub CLA
Modified: 2016-09-08 07:43 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ed Staub CLA 2013-06-06 15:05:20 EDT
A somewhat frequent source of bugs in Java is the result of a copy/paste that includes the current class name.  For example, consider the ubiquitous Java:

static final private log = LoggerFactory.getLogger(com.package.Clazz.class)

Obviously, "this.getClass()" can't be used in this static context.

Might it make sense to make "typeof()" - that is, typeof with an empty argument - mean the current class?  Or otherwise provide access to this type without explicitly naming it?
Comment 1 Karsten Thoms CLA 2016-09-08 07:43:43 EDT
I understand your issue, and on first sight liked the basic idea.

So lets assume you want to express something like:
    static Logger LOG = Logger.getLogger(typeof(MyClass))
 -> static Logger LOG = Logger.getLogger(typeof())

The typeof operator belongs to the XTypeLiteral in Xbase.xtext:

  XTypeLiteral returns XExpression :
	{XTypeLiteral} 'typeof' '(' type=[types::JvmType|QualifiedName]
    (arrayDimensions+=ArrayBrackets)* ')'
  ;

The proposal above would require to make the block in paranthesis optional, thus type could be null.

When the type attribute can be null this might have many implications on the existing code. The referred JvmType would have to evaluate to the inferred JvmType where the XTypeLiteral is defined in.

Currently I'm not sure if this can be evaluated at the time it is required, since the inferrer might resolve the type at a later stage. I think this has to be evaluated if that is possible.

However, this approach won't work with the other way to express type literals. It is valid to leave out the typeof operator and just name the type literal:
   static Logger LOG = Logger.getLogger(MyClass)

There is no way to abbreviate this, and nowadays most time type literals are used without the operator. This would force people to use the typeof operator artificially.

I am undecided, but tend to reject the feature.