[X++] Special: generate parm-methods for your classes automated

As most of the developers (or at least those, who want to improve themselves steady) I am always searching for ways to make things easier, in less time and in a not so error-prone way.

The code uses the classname, grabs all declarations from the classDeclaration and generates parm-methods for those.
It is just a quick and dirty job.

static void rkretCreateParmMethods(Args _args)
{
    #AOT
    #TreeNodeSysNodeType
    ClassName       className   = 'INSERT_CLASS_NAME';
    TreeNode        tn          = TreeNode::findNode(strFmt("%1\\%2", #ClassesPath, className));
    TreeNode        tnNewMethod;
    SysDictClass    dictClass   = SysDictClass::newName(className);
    str             prefix      = 'parm';
    str             source;
    int             matchCount;
    str             tmpStr;
    int             idx;
    str             variableType;
    str             variableName;
    str             variableNameUpr;
    str             methodString;
    System.Text.RegularExpressions.Regex            regEx;
    Str                                             pattern = @"(\s){4,}(\w)+(\s){4,}(\w)+;";
    System.Text.RegularExpressions.Match            myMatch;
    System.Text.RegularExpressions.MatchCollection  matches;

    if (tn
        && (tn.treeNodeType().id() == #NT_CLASS
        || tn.AOTparent().treeNodeType().id() == #NT_CLASS)
        )
    {
        if (tn.AOTchildNodeCount())
        {
            tn = tn.AOTfindChild('classDeclaration');
        }
        source = tn.AOTgetSource();
        if (source)
        {
            regEx = new System.Text.RegularExpressions.Regex(pattern);
            if (regEx)
            {
                matches = regEx.Matches(source);
                matchCount = matches.get_Count();
                for (idx = 0;idx < matchCount;idx++)
                {
                    myMatch = matches.get_Item(idx);
                    if (myMatch)
                    {
                        tmpStr          = CLRInterop::getAnyTypeForObject(myMatch.get_Value());
                        tmpStr          = strRemoveCr(tmpStr);
                        tmpStr          = strLRTrim(tmpStr);
                        variableType    = subStr(tmpStr, 0, strScan(tmpStr, ' ', 0, strLen(tmpStr))-1);
                        variableName    = strReverse(subStr(strReverse(tmpStr), 0, strScan(strReverse(tmpStr), ' ', 0, strLen(tmpStr))-1));
                        variableName    = strRem(variableName, ';');
                        variableNameUpr = strUpr(subStr(variableName, 0, 1)) + subStr(variableName, 2, strLen(variableName)-1);
                        if (!dictClass.hasObjectMethod(prefix + strUpr(variableName)))
                        {
                            tnNewMethod = tn.AOTparent().AOTadd(prefix + variableNameUpr);
                            methodString = 
                                    'public ' + variableType + ' ' + prefix + variableNameUpr + '(' + variableType + ' _' + variableName + ' = ' + variableName + ')\n' +
                                    '{\n' ;
                            methodString +=
                                    '    ' + variableName + ' = _' + variableName + ';\n' +
                                    '\n' +
                                    '    return ' + variableName + ';\n' +
                                    '}';
                            tnNewMethod.AOTsetSource(methodString);
                        }
                    }
                }
                tn.AOTparent().AOTcompile();
            }
        }
    }
}

The code is provided as is and I am not responsible for anything you do with this code.

Robin

Software Engineer / Application Developer

Subscribe to robsCode

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!