@Log annotation to initialize Logger field

201 views
Skip to first unread message

John Kozlov

unread,
Jun 10, 2013, 8:41:07 AM6/10/13
to xtend...@googlegroups.com
I would like to create @Log annotation to generate initializers for Logger fields:

import java.util.logging.Logger

class LoggerExample {
@Log Logger log
}

This is supposed to be translated to:

public class LoggerExample {
  @Log
  private final static Logger log = java.util.logging.Logger.getLogger("log.LoggerExample");
}


I defined the following processor:

@Target(ElementType::FIELD)
@Active(typeof(LogProcessor))
annotation Log {
}

class LogProcessor extends AbstractFieldProcessor {

override doTransform(MutableFieldDeclaration field, extension TransformationContext context) {
field.initializer = [ '''java.util.logging.Logger.getLogger("«field.declaringType.qualifiedName»")''' ]
field.final = true
field.static = true
}

}


However the compiler reports an error for LoggerExample : "The blank final field log may not have been initialized"


What should I do to make my processor work?

Thanks in advance!

Sven Efftinge

unread,
Jun 10, 2013, 8:55:37 AM6/10/13
to xtend...@googlegroups.com
Hi John,

the analyzation of proper initialization currently doesn't work nicely with active annotations.
A workaround would be to make the @Log annotations a class level annotation, which syhthetically creates the Log field.
With that you have even less to write :-)

This bug is related : Bug 404167

Sven

--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

John Kozlov

unread,
Jun 10, 2013, 9:07:30 AM6/10/13
to xtend...@googlegroups.com
Thanks! I have already came to the same idea :)


понедельник, 10 июня 2013 г., 19:55:37 UTC+7 пользователь Sven Efftinge написал:

Zsombor

unread,
Jun 10, 2013, 9:14:00 AM6/10/13
to xtend...@googlegroups.com
Yes, and it's a working idea, as I've already implemented this annotation:
 https://github.com/gzsombor/logging-annotation



--
Reply all
Reply to author
Forward
0 new messages