Бывает попадаются рутинные задачи, которые требуют писать однообразный код, раз за разом.
Давайте посмотрим на мой wrapper, который позволяет генерить классы для решения таких задач.
Внутри поста есть код класса и пример использования. Дальше уже дело фантазии каждого.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
class ZCLASS_CREATE_WRAPPER definition public create public . public section. methods CONSTRUCTOR importing !IV_CLASS_NAME type STRING !IV_DESCRIPTION type STRING optional . methods GENERATE_CLASS importing !IV_DEVCLASS type DEVCLASS !IV_TRCORR type TRKORR optional exceptions ERROR . methods ADD_CONSTANT importing !IV_NAME type SEOCMPNAME !IV_VISIBILITY type SEOEXPOSE default 1 !IV_TYPTYPE type SEOTYPTYPE default 1 !IV_TYPE type RS38L_TYP optional !IV_TYPE_VALUE type RS38L_TYP optional !IT_TYPE_SRC type SEO_SECTION_SOURCE optional . methods ADD_ATTRIBUTE importing !IV_NAME type SEOCMPNAME !IV_VISIBILITY type SEOEXPOSE default 1 !IV_TYPTYPE type SEOTYPTYPE default 1 !IV_TYPE type RS38L_TYP !IV_TYPE_VALUE type RS38L_TYP optional !IV_DECLARATION_TYPE type SEOCOMPODF-ATTDECLTYP default SEOO_ATTDECLTYP_DATA . methods ADD_METHOD importing !IV_METHOD_NAME type SEOCMPNAME !IV_VISIBILITY type SEOEXPOSE default 2 !IV_METHOD_TYPE type SEOMTDTYPE default 0 !IV_DECLARATION_TYPE type SEOMTDDECL default 0 !IV_DESCRIPTION type SEODESCR default '.' . methods ADD_METHOD_PARAMETER importing !IV_METHOD_NAME type SEOCMPNAME !IV_PARAMETER_NAME type SEOSCONAME !IV_PARAMETER_DIRECTION type SEOPARDECL default 0 !IV_TYPTYPE type SEOTYPTYPE default 1 !IV_TYPE type RS38L_TYP . methods ADD_METHOD_IMPLEMENTATION importing !IV_METHOD_NAME type SEOCPDNAME !IT_SOURCE type RSWSOURCET . methods ADD_OWN_TYPE importing !IT_SOURCE type SEO_SECTION_SOURCE !IV_TYPE_NAME type SEOCLSNAME !IV_VISIBILITY type SEOEXPOSE default 2 !IV_EDITORDER type SEOORDER optional !IV_DESCRIPTION type SEODESCR default '.' . methods SET_AS_FINAL . methods SET_CONSTRUCTOR_AS_PRIVATE . protected section. private section. types: BEGIN OF mty_s_configs, class_configs TYPE vseoclass, t_types TYPE seo_types, t_type_source TYPE obl_cl_src, t_attributes TYPE seoo_attributes_r, t_methods TYPE seoo_methods_r, t_parameters TYPE seos_parameters_r, t_method_source TYPE seo_method_source_table, END OF mty_s_configs . data MV_CLASS_NAME type STRING . data MS_CONFIGS type MTY_S_CONFIGS . ENDCLASS. CLASS ZCLASS_CREATE_WRAPPER IMPLEMENTATION. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Public Method ZCLASS_CREATE_WRAPPER->ADD_ATTRIBUTE * +-------------------------------------------------------------------------------------------------+ * | [--->] IV_NAME TYPE SEOCMPNAME * | [--->] IV_VISIBILITY TYPE SEOEXPOSE (default =1) * | [--->] IV_TYPTYPE TYPE SEOTYPTYPE (default =1) * | [--->] IV_TYPE TYPE RS38L_TYP * | [--->] IV_TYPE_VALUE TYPE RS38L_TYP(optional) * | [--->] IV_DECLARATION_TYPE TYPE SEOCOMPODF-ATTDECLTYP (default =SEOO_ATTDECLTYP_DATA) * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD add_attribute. APPEND INITIAL LINE TO ms_configs-t_attributes ASSIGNING FIELD-SYMBOL(<ls_attribute>). <ls_attribute>-clsname = mv_class_name. <ls_attribute>-cmpname = iv_name. <ls_attribute>-state = seoc_state_implemented. <ls_attribute>-version = seoc_version_inactive. <ls_attribute>-langu = sy-langu. *0 собств. *1 защищено *2 общее <ls_attribute>-exposure = iv_visibility. *0 ссылка атрибута (like) *1 ссылка типа (type) *2 объект (type) *3 ссылка объекта (type ref to) *4 см. кодирование *5 динамич. вложенный компонент (type ... boxed) <ls_attribute>-typtype = iv_typtype. *0 Атрибут инстанции *1 Статический атрибут *2 Константа <ls_attribute>-attdecltyp = iv_declaration_type. <ls_attribute>-type = iv_type. <ls_attribute>-attvalue = iv_type_value. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Public Method ZCLASS_CREATE_WRAPPER->ADD_CONSTANT * +-------------------------------------------------------------------------------------------------+ * | [--->] IV_NAME TYPE SEOCMPNAME * | [--->] IV_VISIBILITY TYPE SEOEXPOSE (default =1) * | [--->] IV_TYPTYPE TYPE SEOTYPTYPE (default =1) * | [--->] IV_TYPE TYPE RS38L_TYP(optional) * | [--->] IV_TYPE_VALUE TYPE RS38L_TYP(optional) * | [--->] IT_TYPE_SRC TYPE SEO_SECTION_SOURCE(optional) * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD add_constant. * iv_visibility *0 собств. *1 защищено *2 общее * iv_typtype *0 ссылка атрибута (like) *1 ссылка типа (type) *2 объект (type) *3 ссылка объекта (type ref to) *4 см. кодирование *5 динамич. вложенный компонент (type ... boxed) APPEND INITIAL LINE TO ms_configs-t_attributes ASSIGNING FIELD-SYMBOL(<ls_attribute>). <ls_attribute>-clsname = mv_class_name. <ls_attribute>-cmpname = iv_name. <ls_attribute>-state = seoc_state_implemented. <ls_attribute>-version = seoc_version_active. <ls_attribute>-langu = sy-langu. <ls_attribute>-exposure = iv_visibility. <ls_attribute>-typtype = iv_typtype. <ls_attribute>-attdecltyp = seoo_attdecltyp_constants. <ls_attribute>-type = iv_type. <ls_attribute>-attvalue = iv_type_value. <ls_attribute>-typesrc = cl_oo_section_source=>convert_table_to_string( p_source = it_type_src ). ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Public Method ZCLASS_CREATE_WRAPPER->ADD_METHOD * +-------------------------------------------------------------------------------------------------+ * | [--->] IV_METHOD_NAME TYPE SEOCMPNAME * | [--->] IV_VISIBILITY TYPE SEOEXPOSE (default =2) * | [--->] IV_METHOD_TYPE TYPE SEOMTDTYPE (default =0) * | [--->] IV_DECLARATION_TYPE TYPE SEOMTDDECL (default =0) * | [--->] IV_DESCRIPTION TYPE SEODESCR (default ='.') * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD add_method. APPEND INITIAL LINE TO ms_configs-t_methods ASSIGNING FIELD-SYMBOL(<ls_method>). <ls_method>-clsname = mv_class_name. <ls_method>-cmpname = iv_method_name. <ls_method>-version = seoc_version_inactive. <ls_method>-langu = sy-langu. <ls_method>-descript = iv_description. <ls_method>-state = seoc_state_implemented.. *0 Собств. *1 Защищено *2 Общее <ls_method>-exposure = iv_visibility. *0 Метод *1 Обрабатывающий событие метод *2 Конструктор *3 Деструктор *4 Get-метод виртуального атрибута *5 Set-метод виртуального атрибута *6 Метод теста для единицы ABAP *7 Функция таблицы CDS *8 Объект DDL AMDP <ls_method>-mtdtype = iv_method_type. *0 Метод инстанции *1 Статический метод <ls_method>-mtddecltyp = iv_declaration_type. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Public Method ZCLASS_CREATE_WRAPPER->ADD_METHOD_IMPLEMENTATION * +-------------------------------------------------------------------------------------------------+ * | [--->] IV_METHOD_NAME TYPE SEOCPDNAME * | [--->] IT_SOURCE TYPE RSWSOURCET * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD add_method_implementation. APPEND INITIAL LINE TO ms_configs-t_method_source ASSIGNING FIELD-SYMBOL(<ls_meth_scr>). <ls_meth_scr>-cpdname = iv_method_name. <ls_meth_scr>-source = it_source. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Public Method ZCLASS_CREATE_WRAPPER->ADD_METHOD_PARAMETER * +-------------------------------------------------------------------------------------------------+ * | [--->] IV_METHOD_NAME TYPE SEOCMPNAME * | [--->] IV_PARAMETER_NAME TYPE SEOSCONAME * | [--->] IV_PARAMETER_DIRECTION TYPE SEOPARDECL (default =0) * | [--->] IV_TYPTYPE TYPE SEOTYPTYPE (default =1) * | [--->] IV_TYPE TYPE RS38L_TYP * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD add_method_parameter. APPEND INITIAL LINE TO ms_configs-t_parameters ASSIGNING FIELD-SYMBOL(<ls_param>). <ls_param>-clsname = mv_class_name. <ls_param>-cmpname = iv_method_name. <ls_param>-sconame = iv_parameter_name. *0 Атрибут *1 Метод *2 Событие *3 Тип <ls_param>-cmptype = 1. *0 importing *1 exporting *2 changing *3 returning <ls_param>-pardecltyp = iv_parameter_direction. *0 Ссылка атрибута (LIKE) *1 Ссылка типа (TYPE) *2 Объект (TYPE) *3 Ссылка объекта (TYPE REF TO) *4 см. кодирование *5 Динамич. вложенный компонент (TYPE ... BOXED) <ls_param>-typtype = iv_typtype. <ls_param>-type = iv_type. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Public Method ZCLASS_CREATE_WRAPPER->ADD_OWN_TYPE * +-------------------------------------------------------------------------------------------------+ * | [--->] IT_SOURCE TYPE SEO_SECTION_SOURCE * | [--->] IV_TYPE_NAME TYPE SEOCLSNAME * | [--->] IV_VISIBILITY TYPE SEOEXPOSE (default =2) * | [--->] IV_EDITORDER TYPE SEOORDER(optional) * | [--->] IV_DESCRIPTION TYPE SEODESCR (default ='.') * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD add_own_type. APPEND INITIAL LINE TO ms_configs-t_types ASSIGNING FIELD-SYMBOL(<ls_type>). <ls_type>-clsname = mv_class_name. <ls_type>-cmpname = iv_type_name. <ls_type>-descript = iv_description. <ls_type>-editorder = iv_editorder. <ls_type>-state = seoc_state_implemented. *0 Собств. *1 Защищено *2 Общее <ls_type>-exposure = iv_visibility. <ls_type>-typtype = seoo_typtype_others. <ls_type>-typesrc = cl_oo_section_source=>convert_table_to_string( p_source = it_source ). ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Public Method ZCLASS_CREATE_WRAPPER->CONSTRUCTOR * +-------------------------------------------------------------------------------------------------+ * | [--->] IV_CLASS_NAME TYPE STRING * | [--->] IV_DESCRIPTION TYPE STRING(optional) * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD constructor. mv_class_name = iv_class_name. ms_configs-class_configs-clsname = mv_class_name. ms_configs-class_configs-state = seoc_state_implemented. ms_configs-class_configs-exposure = seoc_exposure_public. ms_configs-class_configs-langu = sy-langu. ms_configs-class_configs-descript = iv_description. ms_configs-class_configs-clsccincl = seox_true. ms_configs-class_configs-unicode = seox_true. ms_configs-class_configs-fixpt = seox_true. ms_configs-class_configs-author = sy-uname. "#EC NOTEXT ms_configs-class_configs-clssharedmemory = seox_false. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Public Method ZCLASS_CREATE_WRAPPER->GENERATE_CLASS * +-------------------------------------------------------------------------------------------------+ * | [--->] IV_DEVCLASS TYPE DEVCLASS * | [--->] IV_TRCORR TYPE TRKORR(optional) * | [EXC!] ERROR * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD generate_class. CALL FUNCTION 'SEO_CLASS_CREATE_COMPLETE' EXPORTING corrnr = iv_trcorr devclass = iv_devclass version = seoc_version_active authority_check = seox_true overwrite = seox_true suppress_method_generation = seox_false genflag = seox_true method_sources = ms_configs-t_method_source CHANGING class = ms_configs-class_configs attributes = ms_configs-t_attributes methods = ms_configs-t_methods types = ms_configs-t_types type_source = ms_configs-t_type_source parameters = ms_configs-t_parameters EXCEPTIONS existing = 1 is_interface = 2 db_error = 3 component_error = 4 no_access = 5 other = 6 OTHERS = 7. IF sy-subrc <> 0. RAISE error. ENDIF. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Public Method ZCLASS_CREATE_WRAPPER->SET_AS_FINAL * +-------------------------------------------------------------------------------------------------+ * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD set_as_final. ms_configs-class_configs-clsfinal = abap_true. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Public Method ZCLASS_CREATE_WRAPPER->SET_CONSTRUCTOR_AS_PRIVATE * +-------------------------------------------------------------------------------------------------+ * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD set_constructor_as_private. ms_configs-class_configs-exposure = seoc_exposure_private. ENDMETHOD. ENDCLASS. |
Пример использования:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
DATA: go_generator TYPE REF TO zclass_create_wrapper. go_generator = NEW #( iv_class_name = 'ZCL_GENERATE_DEMO' ) iv_description = 'DESCRIPTION' ). go_generator->set_as_final( ). go_generator->set_constructor_as_private( ). DATA(lt_source) = VALUE seo_section_source( ). "Добавить типы APPEND |mty_t_range TYPE RANGE OF mara-matnr| TO ltd_source. go_generator->add_own_type( it_source = ltd_source iv_type_name = to_upper( 'mty_t_range' ) iv_visibility = seoc_exposure_public iv_editorder = 1 ). "Константы go_generator->add_constant( iv_name = 'gc_const1' iv_type = 'STRING' iv_visibility = seoc_exposure_private iv_type_value = |'VALUE'| ). "Структурированная константа CLEAR ltd_source. APPEND | begin of gc_const, | TO ltd_source. LOOP AT ltd_parameters ASSIGNING <ls_param>. APPEND |parnm1 TYPE string VALUE `123`,| TO ltd_source. ENDLOOP. APPEND |end of gc_const . | TO ltd_source. go_generator->add_constant( iv_name = to_upper( 'parnm1' ) iv_visibility = seoc_exposure_public iv_typtype = seoo_typtype_others it_type_src = ltd_source ). "singletone go_generator->add_attribute( iv_name = 'mo_instance' ) iv_visibility = seoc_exposure_private iv_typtype = seoo_typtype_ref_to iv_declaration_type = seoo_attdecltyp_statics iv_type = 'ZCL_GENERATE_DEMO' ). "Методы, параметры, реализация go_generator->add_method( iv_method_name = 'GET_ORDER_TYPES' iv_description = 'Получить типы заказов на поставку' ). go_generator->add_method_parameter( iv_method_name = 'GET_ORDER_TYPES' iv_parameter_name = 'rt_order_types' iv_parameter_direction = seos_pardecltyp_returning iv_typtype = seoo_typtype_type iv_type = 'WRF_PBAS_EBSTYP_RTTY' ). DATA(lt_meth_source) = VALUE rswsourcet( ). APPEND |"implement you code here...| TO lt_meth_source. go_generator->add_method_implementation( iv_method_name = 'GET_ORDER_TYPES' it_source = lt_meth_source ). "instance метод CLEAR lt_meth_source. go_generator->add_method( iv_method_name = 'GET_INSTANCE' iv_declaration_type = seoo_attdecltyp_statics iv_description = 'singleton' ). |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
go_generator->add_method_parameter( iv_method_name = 'GET_INSTANCE' iv_parameter_name = 'ro_instance' iv_parameter_direction = seos_pardecltyp_returning iv_typtype = seoo_typtype_ref_to iv_type = 'ZCL_GENERATE_DEMO' ). APPEND |IF mo_instance IS NOT BOUND.| TO lt_meth_source. APPEND | mo_instance = NEW #( ).| TO lt_meth_source. APPEND |ENDIF.| TO lt_meth_source. APPEND |ro_instance = mo_instance .| TO lt_meth_source. go_generator->add_method_implementation( iv_method_name = 'GET_INSTANCE' it_source = lt_meth_source ). "Генерация и сохранение go_generator->generate_class( EXPORTING iv_trcorr = gs_trrequest-trkorr iv_devclass = p_devcl EXCEPTIONS error = 1 OTHERS = 2 ). IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO DATA(lv_dummy). WRITE : / lv_dummy. ELSE. WRITE : / 'Успешно'. ENDIF. |