📄 buildtostring.ebs
字号:
'==============================================================================
' Build toString() method on selected classes (overwrites semantics if
' operation with this name exists)
' Warning!
' Generated toString() is not safe for cyclic roles.
' ToDo
' 1. Check for parameters of toString() operation when it searches for existed.
' 2. Add support for attributes without wrappers.
'==============================================================================
' History
'
' 2001/07/04 | Vladislav Dutov | fixed for boolean attributes wich name is
' | | started from "is".
' 2001/06/29 | Vladislav Dutov | created
'==============================================================================
Declare Sub genToString(cls As Class)
Declare Function attrGetter(at As Attribute) As String
Declare Function roleGetter(at As Role) As String
Declare Function method(prefix As String, atName As String) As String
Sub Main
Dim selected As ItemCollection
Dim ri As RoseItem
Dim cls As Class
Viewport.Open "To string"
Viewport.Clear
Set selected = RoseApp.CurrentModel.GetSelectedItems()
For i%=1 To selected.Count
Set ri = selected.GetAt(i%)
If ri.CanTypeCast(cls) Then
Set cls = ri.TypeCast(cls)
Print cls.Name
genToString cls
End If
Next i%
End Sub
Sub genToString(cls As Class)
Dim op As Operation
Dim attributes As AttributeCollection
Dim at As Attribute
Dim atf As Boolean
Dim roles As RoleCollection
Dim r As Role
Dim sm As String
Dim index As Integer
Const CRLF = Chr$(13) + Chr$(10)
pos = cls.Operations.FindFirst("toString")
If pos = 0 Then
Set op = cls.AddOperation("toString", "String")
Else
Set op = cls.Operations.GetAt(pos)
End If
sm = ""
'attributes
Set attributes = cls.Attributes
For j% = 1 To attributes.Count
Set at = attributes.GetAt(j%)
atf = at.getPropertyValue("ngen", "ngen.final")
If Not atf Then
If Len(sm) > 0 Then
sm = sm + """, "
Else
sm = sm + """"
End If
sm = sm + at.Name + ": "" + " + attrGetter(at) + " + " + CRLF
End If
Next j%
'roles
Set roles = cls.GetAssociateRoles()
For j% = 1 To roles.Count
Set r = roles.GetAt(j%)
If Len(r.Name) > 0 Then
If Len(sm) > 0 Then
sm = sm + """, "
Else
sm = sm + """"
End If
sm = sm + r.Name + ": "" + " + roleGetter(r) + " + " + CRLF
End If
Next j%
' class name or call of super.toString()
If Len(sm) > 0 Then
sm = Mid$(sm, 1, Len(sm) - 5)
sm = """" + cls.Name + ": "" + " + sm
Else
sm = "super.toString()"
End If
' return
op.Semantics = "return " + sm + ";"
End Sub
'getter for attribute
Function attrGetter(at As Attribute) As String
If at.Type = "boolean" Then
If Left$(at.Name, 2) = "is" Then
attrGetter = at.Name + "()"
Else
attrGetter = method("is", at.Name)
End If
Else
attrGetter = method("get", at.Name)
End If
End Function
'getter for role
Function roleGetter(at As Role) As String
roleGetter = method("get", at.Name)
End Function
Function method(prefix As String, atName As String) As String
method = prefix + UCase$(Mid$(atName, 1, 1)) + Mid$(atName, 2) + "()"
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -