You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The dynamic template parameters would let us decouple template model and template parameters. It would bring
simpler template definition (see Example 1)
any part of spoon model might be used as a template model and with appropriate template parameters definition it might be used to generate code (see Example 2)
simpler template engine implementation (because template parameters would exist as extra data out of template model, therefore code of substitution engine might be simpler.
Template matcher might use template parameters with better parameter matching filters, because template parameter definition would be not limited to annotation parameters, but might contain any java structures, because it would be configured dynamically by java calls at runtime (see Example 3).
Example 1
publicclassFluentSetterTemplateextendsExtensionTemplate {
/** * @param propertyName propertyDescription * @return this to support fluent API */publicFluentSetterTemplatepropertyName(ParamTypepropertyName) {
request.setPropertyName(propertyName);
returnthis;
}
@LocalpublicFluentSetterTemplate(StringpropertyName, StringpropertyDescription, CtTypeReference<?> requestType, CtTypeReference<?> paramType) {
parameter("propertyName").value(lowerCaseFirst(propertyName));
parameter("PropertyName").value(upperCaseFirst(propertyName));
parameter("propertyDescription").value(propertyDescription);
parameter("Request").value(requestType);
parameter("ParamType").value(paramType);
}
@LocalRequestrequest;
@LocalinterfaceRequest {
voidsetPropertyName(ParamTypeprop);
}
@LocalinterfaceParamType {}
}
Example 2
arbitrary code as a template
//this is example of any class of spoon model, whose method is used as `template model`publicclassSomeClass {
...
Serviceservice;
...
/** * @param readOnly defines whether document is read only * @return this to support fluent API */publicSomeClassreadOnly (booleanreadOnly) {
service.setReadOnly(readOnly);
returnthis;
}
...
}
//this is a template definition, which uses the SomeClass#readOnly method as template modelpublicclassFluentSetterTemplateextendsExtensionTemplate {
@LocalpublicFluentSetterTemplate(StringpropertyName, StringpropertyDescription, CtTypeReference<?> requestType, CtTypeReference<?> paramType, CtMethodtemplateMethod) {
parameter("readOnly").value(lowerCaseFirst(propertyName));
parameter("ReadOnlyvalue(upperCaseFirst(propertyName)); parameter("defineswhetherdocumentisreadonly").value(propertyDescription);
parameter("Service").value(requestType);
parameter("boolean").value(paramType);
setTemplateModel(templateMethod);
}
}
The templates of Example 1 and Example 2 would generate same code.
Example 3
it is same like Example 1, but the extra parameter matching fitler is configured by setFilter - can be helpful to drive template matcher.
publicclassFluentSetterTemplateextendsExtensionTemplate {
/** * @param propertyName propertyDescription * @return this to support fluent API */publicFluentSetterTemplatepropertyName(ParamTypepropertyName) {
request.setPropertyName(propertyName);
returnthis;
}
@LocalpublicFluentSetterTemplate(StringpropertyName, StringpropertyDescription, CtTypeReference<?> requestType, CtTypeReference<?> paramType) {
parameter("propertyName").value(lowerCaseFirst(propertyName));
parameter("PropertyName").value(upperCaseFirst(propertyName));
parameter("propertyDescription").value(propertyDescription);
parameter("Request").value(requestType);
//See the custom parameter matching filter, which is configured dynamicallyparameter("ParamType").value(paramType).setFilter((CtTypeReferencetr->{
returntr.subTypeOf(factory.createTypeRefarence(AutoClosable.class))
});
}
@LocalRequestrequest;
@LocalinterfaceRequest {
voidsetPropertyName(ParamTypeprop);
}
@LocalinterfaceParamType {}
}
Notes:
the usage of dynamic template parameters would be optional. Legacy TemplateParameter and @Parameter can be still used to define template parameters.
The code which creates a template, would always check for TemplateParameter and @Parameter and initialize appropriate dynamic template parameters. Then the template substitution engine, might be simplified, because it will understand only one way for definition of template parameters - dynamic template parameters.
Would you like something like that, or do you see some conceptual/other problem?
The text was updated successfully, but these errors were encountered:
The dynamic template parameters would let us decouple
template model
andtemplate parameters
. It would bringtemplate model
and with appropriatetemplate parameters
definition it might be used to generate code (see Example 2)template model
, therefore code of substitution engine might be simpler.Example 1
Example 2
arbitrary code as a template
The templates of Example 1 and Example 2 would generate same code.
Example 3
it is same like Example 1, but the extra parameter matching fitler is configured by
setFilter
- can be helpful to drive template matcher.Notes:
TemplateParameter
and@Parameter
can be still used to define template parameters.TemplateParameter
and@Parameter
and initialize appropriate dynamic template parameters. Then the template substitution engine, might be simplified, because it will understand only one way for definition of template parameters - dynamic template parameters.Would you like something like that, or do you see some conceptual/other problem?
The text was updated successfully, but these errors were encountered: