Programing

freemarker boolean type (ver 2.3.33 - Could not load FreeMarker template)

handam 2024. 12. 31. 14:18

사용버전 : freemarker version 2.3.33

https://freemarker.apache.org/docs/ref_builtins_boolean.html

 

data class A 필드로 `isCodes` 라는 boolean 필드가 존재했고,

 

`?string`

`?then`

`?c` 

 

문법에 대한 오류가 계속 발생했다. 오류 내용을 찬찬히 보니, 필드명을 `isCodes`에서 `codes`를 사용하라는 Tip을 보았다.

----
Tip: Maybe using obj.something instead of obj.isSomething will yield the desired value.
----

 

 

아직도 이유는 모르겠다... (몇 시간을 날렸는지)

찾아보니 필드명 시작을 "get", "is"인 경우 오류가 발생한다.

/**
 * Implementation of experimental interface; don't use it, no backward compatibility guarantee!
 */
public Object[] explainTypeError(Class[] expectedClasses) {
    final Member member = getMember();
    if (!(member instanceof Method)) {
        return null;  // This shouldn't occur
    }
    Method m = (Method) member;

    final Class returnType = m.getReturnType();
    if (returnType == null || returnType == void.class || returnType == Void.class) {
        return null;  // Calling it won't help
    }

    String mName = m.getName();
    if (mName.startsWith("get") && mName.length() > 3 && Character.isUpperCase(mName.charAt(3))
            && (m.getParameterTypes().length == 0)) {
        return new Object[] {
                "Maybe using obj.something instead of obj.getSomething will yield the desired value." };
    } else if (mName.startsWith("is") && mName.length() > 2 && Character.isUpperCase(mName.charAt(2))
            && (m.getParameterTypes().length == 0)) {
        return new Object[] {
                "Maybe using obj.something instead of obj.isSomething will yield the desired value." };
    } else {
        return new Object[] {
                "Maybe using obj.something(",
                (m.getParameterTypes().length != 0 ? "params" : ""),
                ") instead of obj.something will yield the desired value" };
    }
}

 

 

boolean 필드 이름을 `is...Codes` 에서 `...Codes` 으로  변경했더니 오류가 발생하지 않았다.

 

그리고 버전 2.3.20 이상이라면 `?string`  문법 보다는 `?c` 문법을 사용하도록 하자. 

Caused by: freemarker.core._MiscTemplateException: Can't convert boolean to string automatically, because the "boolean_format" setting was "true,false", which is the legacy deprecated default, and we treat it as if no format was set. This is the default configuration; you should provide the format explicitly for each place where you print a boolean.

----
Tip: Write something like myBool?string('yes', 'no') to specify boolean formatting in place.
----
Tip: If you want "true"/"false" result as you are generating computer-language output (not for direct human consumption), then use "?c", like ${myBool?c}. (If you always generate computer-language output, then it's might be reasonable to set the "boolean_format" setting to "c" instead.)
----
Tip: If you need the same two values on most places, the programmers can set the "boolean_format" setting to something like "yes,no". However, then it will be easy to unwillingly format booleans like that.
----

 

 

반응형