Annotation Type CheckedTemplate
-
@Documented @Retention(RUNTIME) @Target(TYPE) public @interface CheckedTemplate
If you place this annotation on a class, all itsnative staticmethods will be used to declare templates and the list of parameters they require.The name of a method and the base path are used to locate the template contents. By default, the base path is derived from the annotation target:
- If this is placed on a static nested class of an enclosing class with a simple name
X, anative staticmethod of the namefoowill refer to a template at the pathX/foo(template file extensions are not part of the method name) relative to the templates root. - If this is placed on a top-level class, a
native staticmethod of the namefoowill refer to a template at the pathfoo(template file extensions are not part of the method name) at the toplevel of the templates root.
basePath().Each parameter of the
native staticwill be used to validate the template at build time, to make sure that those parameters are used properly in a type-safe manner. The return type of eachnative staticmethod should beTemplateInstance.Example:
@Path("item") public class ItemResource { @CheckedTemplate static class Templates { // defines a template at ItemResource/item, taking an Item parameter named item static native TemplateInstance item(Item item); } @GET @Path("{id}") @Produces(MediaType.TEXT_HTML) public TemplateInstance get(@PathParam("id") Integer id) { // instantiate that template and pass it the required template parameter return Templates.item(service.findItem(id)); } }Type-safe Fragments
By default, anative staticmethod with the name that contains a dollar sign$denotes a method that represents a fragment of a type-safe template. It's possible to ignore the fragments and effectively disable this feature viaignoreFragments().The name of the fragment is derived from the annotated method name. The part before the last occurence of a dollar sign
$is the method name of the related type-safe template. The part after the last occurence of a dollar sign is the fragment identifier - the strategy defined by the relevantdefaultName()is used.Parameters of the annotated method are validated. The required names and types are derived from the relevant fragment template.
@CheckedTemplate class Templates { // defines a type-safe template static native TemplateInstance items(List<Item> items); // defines a fragment of Templates#items() with identifier "item" @CheckedFragment static native TemplateInstance items$item(Item item); } - If this is placed on a static nested class of an enclosing class with a simple name
-
-
Field Summary
Fields Modifier and Type Fields Description static StringDEFAULTEDConstant value forbasePath()indicating that the default strategy should be used, i.e.static StringELEMENT_NAMEConstant value fordefaultName()indicating that the method name should be used as-is.static StringHYPHENATED_ELEMENT_NAMEConstant value fordefaultName()indicating that the annotated element's name should be de-camel-cased and hyphenated, and then used.static StringUNDERSCORED_ELEMENT_NAMEConstant value fordefaultName()indicating that the annotated element's name should be de-camel-cased and parts separated by underscores, and then used.
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description StringbasePathExample:StringdefaultNameThe value may be one of the following:ELEMENT_NAME,HYPHENATED_ELEMENT_NAMEandUNDERSCORED_ELEMENT_NAME.booleanignoreFragmentsBy default, anative staticmethod with the name that contains a dollar sign$denotes a method that represents a fragment of a type-safe template.booleanrequireTypeSafeExpressionsIf set to true then the defined templates can only contain type-safe expressions.
-
-
-
Field Detail
-
DEFAULTED
static final String DEFAULTED
Constant value forbasePath()indicating that the default strategy should be used, i.e. the simple name of the declaring class for a nested static class or an empty string for a top level class.
-
-
-
ELEMENT_NAME
static final String ELEMENT_NAME
Constant value fordefaultName()indicating that the method name should be used as-is.
-
-
-
HYPHENATED_ELEMENT_NAME
static final String HYPHENATED_ELEMENT_NAME
Constant value fordefaultName()indicating that the annotated element's name should be de-camel-cased and hyphenated, and then used.
-
-
-
UNDERSCORED_ELEMENT_NAME
static final String UNDERSCORED_ELEMENT_NAME
Constant value fordefaultName()indicating that the annotated element's name should be de-camel-cased and parts separated by underscores, and then used.
-
-
Element Detail
-
basePath
String basePath
Example:@Path("item") public class ItemResource { @CheckedTemplate(basePath = "items_v1") static class Templates { // defines a template at items_v1/item static native TemplateInstance item(Item item); // defines a template at items_v1/allItems static native TemplateInstance allItems(List<Item> items); } }- Returns:
- the base path relative to the templates root
- Default:
- "<<defaulted>>"
-
-
-
defaultName
String defaultName
The value may be one of the following:ELEMENT_NAME,HYPHENATED_ELEMENT_NAMEandUNDERSCORED_ELEMENT_NAME.- Returns:
- the default name
- Default:
- "<<element name>>"
-
-
-
ignoreFragments
boolean ignoreFragments
By default, anative staticmethod with the name that contains a dollar sign$denotes a method that represents a fragment of a type-safe template. It's possible to ignore the fragments and effectively disable this feature.- Returns:
trueif no method should be interpreted as a fragment,falseotherwise- See Also:
Template.getFragment(String)
- Default:
- false
-
-