How to get current class name in Netbeans code template?

穿精又带淫゛_ 提交于 2019-12-07 14:20:00

问题


I want to write code template for logger in Netbeans:

org.slf4j.LoggerFactory.getLogger(XXXXXX.class).warn("${cursor}");

I can't find syntax to insert current class name into template (see XXXXXX placeholder above) in reference.


回答1:


I have created the following code template:

private static final ${LOGGER_TYPE type="org.slf4j.Logger" default="Logger" editable=false} LOGGER = ${LOGGER_FACTORY type="org.slf4j.LoggerFactory" default="LoggerFactory" editable=false}.getLogger(${CLASS editable="false" currClassName}.class);

and I have associated it to the abbreviation log.

So at the cursor:

...
public class MyClass {
    |
...

I type log + TAB and NetBeans expands the text:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...
public class MyClass {
    private static final Logger LOGGER = LoggerFactory.getLogger(MyClass.class);
...

I have also:

  • logd + TAB
  • logw + TAB
  • logi + TAB
  • loge + TAB

coded as:

${LOGGER_CONST default="LOGGER" editable=false}.debug("${logMessage}");
${LOGGER_CONST default="LOGGER" editable=false}.warn("${logMessage}");
${LOGGER_CONST default="LOGGER" editable=false}.info("${logMessage}");
${LOGGER_CONST default="LOGGER" editable=false}.error("${logMessage}", ex);

that generate:

LOGGER.debug("logMessage");
LOGGER.warn("logMessage");
LOGGER.info("logMessage");
LOGGER.error("logMessage", ex);



回答2:


To get the current class name you need to use this in your code template: ${classVar editable=false currClassName default=getClass()}



来源:https://stackoverflow.com/questions/18827020/how-to-get-current-class-name-in-netbeans-code-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!