Bug 429162 - [active annotations] support setting static initialization blocks to classes
Summary: [active annotations] support setting static initialization blocks to classes
Status: NEW
Alias: None
Product: Xtend
Classification: Tools
Component: Core (show other bugs)
Version: 2.5.3   Edit
Hardware: PC Windows 7
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-02-26 10:51 EST by Joerg Reichert CLA
Modified: 2014-02-26 14:58 EST (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 Joerg Reichert CLA 2014-02-26 10:51:06 EST
MutableClassDeclaration has no ability to set a static initialization block.

A bit related to https://bugs.eclipse.org/bugs/show_bug.cgi?id=429141

As work around you can generate a static field instead, like this:

def private addStaticBlock(MutableClassDeclaration annotatedClass, extension TransformationContext context) {
      val fieldName = "INITIALIZER";
      annotatedClass.addField(fieldName) [
         visibility = Visibility.PRIVATE
         static = true
         final = true
         type = string
         initializer = [
            '''
            new «toJavaCode(Function0.newTypeReference(string))»() {
                public «string» apply() {
                  «myStaticVoidMessageCall»;
                  return "";
                }
              }.apply();
              '''
         ]
      ]      
   }