use of org.yakindu.base.expressions.expressions.ElementReferenceExpression in project statecharts by Yakindu.
the class TypeInferrerTest method testOperationCallWithOptionalParameter.
@Test
public void testOperationCallWithOptionalParameter() {
OperationDefinition opDef = StextTestFactory._createOperation("opWithOptionals", StextFactory.eINSTANCE.createInternalScope());
Parameter pReq = typesFactory.createParameter("pReq", ITypeSystem.INTEGER, false);
Parameter pOpt = typesFactory.createParameter("pOpt", ITypeSystem.INTEGER, true);
opDef.getParameters().add(pReq);
opDef.getParameters().add(pOpt);
Argument boolArg = (Argument) parseExpression("true", Argument.class.getSimpleName());
Argument intArg = (Argument) parseExpression("17", Argument.class.getSimpleName());
// opWithOptionals(17, 17) => valid
ElementReferenceExpression opCall1 = StextTestFactory._createOperationCall(opDef, intArg, intArg);
expectNoErrors(opCall1);
// opWithOptionals(17) => valid, because of optional parameter
ElementReferenceExpression opCall2 = StextTestFactory._createOperationCall(opDef, intArg);
expectNoErrors(opCall2);
// opWithOptionals(true) => invalid
ElementReferenceExpression opCall3 = StextTestFactory._createOperationCall(opDef, boolArg);
expectError(opCall3, ITypeSystemInferrer.NOT_COMPATIBLE_CODE);
}
use of org.yakindu.base.expressions.expressions.ElementReferenceExpression in project statecharts by Yakindu.
the class Assert method assertAssignment.
public static void assertAssignment(Step step, String variableName, AssignmentOperator operator, String value) {
assertClass(Execution.class, step);
Execution exec = (Execution) step;
assertTrue(exec.getStatement() instanceof AssignmentExpression);
AssignmentExpression assignment = (AssignmentExpression) exec.getStatement();
assertEquals(operator, assignment.getOperator());
Expression varRef = assignment.getVarRef();
if (varRef instanceof ElementReferenceExpression) {
ElementReferenceExpression elementRef = (ElementReferenceExpression) varRef;
assertEquals(variableName, ((NamedElement) elementRef.getReference()).getName());
} else if (varRef instanceof FeatureCall) {
FeatureCall call = (FeatureCall) varRef;
assertEquals(variableName, ((NamedElement) call.getFeature()).getName());
}
assertExpressionEquals(value, assignment.getExpression());
}
use of org.yakindu.base.expressions.expressions.ElementReferenceExpression in project statecharts by Yakindu.
the class StextTestFactory method _createElementReferenceExpression.
public static ElementReferenceExpression _createElementReferenceExpression(NamedElement target) {
ElementReferenceExpression referenceExpression = ExpressionsFactory.eINSTANCE.createElementReferenceExpression();
referenceExpression.setReference(target);
return referenceExpression;
}
use of org.yakindu.base.expressions.expressions.ElementReferenceExpression in project statecharts by Yakindu.
the class STextJavaValidator method checkValueDefinitionExpression.
@Check(CheckType.FAST)
public void checkValueDefinitionExpression(VariableDefinition definition) {
// applies only to constants
if (!definition.isConst())
return;
Expression initialValue = definition.getInitialValue();
if (initialValue == null) {
error(CONST_MUST_HAVE_VALUE_MSG, definition, null, CONST_MUST_HAVE_VALUE_CODE);
return;
}
List<Expression> toCheck = Lists.newArrayList(initialValue);
TreeIterator<EObject> eAllContents = initialValue.eAllContents();
while (eAllContents.hasNext()) {
EObject next = eAllContents.next();
if (next instanceof Expression)
toCheck.add((Expression) next);
}
for (Expression expression : toCheck) {
EObject referencedObject = null;
if (expression instanceof FeatureCall)
referencedObject = ((FeatureCall) expression).getFeature();
else if (expression instanceof ElementReferenceExpression)
referencedObject = ((ElementReferenceExpression) expression).getReference();
if (referencedObject instanceof VariableDefinition) {
if (!((VariableDefinition) referencedObject).isConst()) {
error(REFERENCE_TO_VARIABLE, StextPackage.Literals.VARIABLE_DEFINITION__INITIAL_VALUE);
}
}
}
}
use of org.yakindu.base.expressions.expressions.ElementReferenceExpression in project statecharts by Yakindu.
the class STextJavaValidator method checkUnusedVariablesInInternalScope.
@Check(CheckType.FAST)
public void checkUnusedVariablesInInternalScope(InternalScope internalScope) {
EList<Declaration> internalScopeDeclarations = internalScope.getDeclarations();
EObject rootContainer = EcoreUtil.getRootContainer(internalScope);
Resource rootRes = getResource(rootContainer);
Statechart statechart = (Statechart) EcoreUtil.getObjectByType(rootRes.getContents(), SGraphPackage.Literals.STATECHART);
if (statechart == null)
return;
List<ElementReferenceExpression> allUsedElementReferences = EcoreUtil2.getAllContentsOfType(statechart, ElementReferenceExpression.class);
if (statechart.getSpecification() != null) {
for (Declaration internalDeclaration : internalScopeDeclarations) {
boolean internalDeclarationUsed = false;
for (ElementReferenceExpression elementReference : allUsedElementReferences) {
if (elementReference.getReference().eContainer() instanceof InternalScope) {
if (elementReference.getReference() instanceof VariableDefinition) {
if (((VariableDefinition) elementReference.getReference()).getName().equals(internalDeclaration.getName()) && internalDeclaration instanceof VariableDefinition) {
internalDeclarationUsed = true;
break;
}
} else if (elementReference.getReference() instanceof EventDefinition) {
if (((EventDefinition) elementReference.getReference()).getName().equals(internalDeclaration.getName()) && internalDeclaration instanceof EventDefinition) {
internalDeclarationUsed = true;
break;
}
} else if (elementReference.getReference() instanceof OperationDefinition) {
if (((OperationDefinition) elementReference.getReference()).getName().equals(internalDeclaration.getName()) && internalDeclaration instanceof OperationDefinition) {
internalDeclarationUsed = true;
break;
}
}
}
}
if (!internalDeclarationUsed) {
if (internalDeclaration instanceof VariableDefinition || internalDeclaration instanceof EventDefinition || internalDeclaration instanceof OperationDefinition)
warning(INTERNAL_DECLARATION_UNUSED, internalDeclaration, null, -1);
}
}
}
}
Aggregations