19
19
20
20
package org .tzi .use .uml .ocl .expr ;
21
21
22
- import org .tzi .use .uml .mm .MClassifier ;
23
22
import org .tzi .use .uml .mm .MOperation ;
24
23
import org .tzi .use .uml .ocl .value .DataTypeValueValue ;
25
24
import org .tzi .use .uml .ocl .value .VarBindings ;
38
37
*
39
38
* @author Stefan Schoon
40
39
*/
41
- public final class ExpInstanceConstructor extends Expression {
42
-
43
- private final MClassifier classifier ;
44
-
45
- private final MOperation constructor ;
46
-
47
- /**
48
- * The arguments.
49
- */
50
- private final Expression [] fArgs ;
51
-
52
- public ExpInstanceConstructor (MClassifier classifier , Expression [] args ) throws ExpInvalidException {
53
- super (classifier );
54
- this .classifier = classifier ;
55
- this .constructor = classifier .operation (classifier .name (), false );
56
- fArgs = args ;
40
+ public final class ExpInstanceConstructor extends ExpInstanceOp {
41
+
42
+ public ExpInstanceConstructor (MOperation constructor , Expression [] args ) throws ExpInvalidException {
43
+ super (constructor , args );
44
+
45
+ if (!(args [0 ].type ().isTypeOfClass () || args [0 ].type ().isTypeOfDataType ()))
46
+ throw new ExpInvalidException (
47
+ "Target expression of instance constructor must have " +
48
+ "object type, found `" + args [0 ].type () + "'." );
49
+
50
+ // check for matching arguments
51
+ VarDeclList params = fOp .paramList ();
52
+ if (params .size () != (args .length - 1 ) )
53
+ throw new ExpInvalidException (
54
+ "Number of arguments does not match declaration of instance constructor `" +
55
+ fOp .name () + "'. Expected " + params .size () + " argument(s), found " +
56
+ (args .length - 1 ) + "." );
57
+
58
+ for (int i = 1 ; i < args .length ; i ++)
59
+ if (! args [i ].type ().conformsTo (params .varDecl (i - 1 ).type ()) )
60
+ throw new ExpInvalidException (
61
+ "Type mismatch in argument `" + params .varDecl (i - 1 ).name () +
62
+ "'. Expected type `" + params .varDecl (i - 1 ).type () +
63
+ "', found `" + args [i ].type () + "'." );
57
64
}
58
65
59
66
@ Override
@@ -64,7 +71,7 @@ public Value eval(EvalContext ctx) {
64
71
65
72
ctx .enter (this );
66
73
67
- List <String > parameterNames = constructor .paramNames ();
74
+ List <String > parameterNames = fOp .paramNames ();
68
75
int argsSize = parameterNames .size ();
69
76
Value [] arguments = new Value [argsSize ];
70
77
Map <String , Value > argValues = new TreeMap <>();
@@ -78,10 +85,10 @@ public Value eval(EvalContext ctx) {
78
85
for (VarBindings .Entry e : ctx .varBindings ()) {
79
86
varBindings .put (e .getVarName (), e .getValue ());
80
87
}
81
- MInstance self = new MDataTypeValue (classifier , classifier .name (), varBindings );
82
- Value result = new DataTypeValueValue (classifier , self , argValues );
88
+ MInstance self = new MDataTypeValue (fClassifier , fClassifier .name (), varBindings );
89
+ Value result = new DataTypeValueValue (fClassifier , self , argValues );
83
90
84
- MOperationCall operationCall = new MOperationCall (this , self , constructor , arguments );
91
+ MOperationCall operationCall = new MOperationCall (this , self , fOp , arguments );
85
92
operationCall .setPreferredPPCHandler (ExpressionPPCHandler .getDefaultOutputHandler ());
86
93
operationCall .setResultValue (result );
87
94
@@ -105,41 +112,10 @@ public Value eval(EvalContext ctx) {
105
112
return result ;
106
113
}
107
114
108
- @ Override
109
- protected boolean childExpressionRequiresPreState () {
110
- for (Expression e : fArgs ) {
111
- if (e .requiresPreState ()) {
112
- return true ;
113
- }
114
- }
115
- return false ;
116
- }
117
-
118
115
@ Override
119
116
public StringBuilder toString (StringBuilder sb ) {
120
- sb .append (classifier .name ()).append ("(" );
117
+ sb .append (fClassifier .name ()).append ("(" );
121
118
StringUtil .fmtSeqBuffered (sb , fArgs , 1 , ", " );
122
119
return sb .append (")" );
123
120
}
124
-
125
- @ Override
126
- public String name () {
127
- return constructor .name ();
128
- }
129
-
130
- /**
131
- * All arguments of the expression.
132
- */
133
- public Expression [] getArguments () {
134
- return fArgs ;
135
- }
136
-
137
- public MOperation getOperation () {
138
- return constructor ;
139
- }
140
-
141
- @ Override
142
- public void processWithVisitor (ExpressionVisitor visitor ) {
143
- visitor .visitInstanceConstructor (this );
144
- }
145
121
}
0 commit comments