Search in sources :

Example 61 with VariableDefinition

use of org.yakindu.sct.model.stext.stext.VariableDefinition 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);
            }
        }
    }
}
Also used : VariableDefinition(org.yakindu.sct.model.stext.stext.VariableDefinition) EObject(org.eclipse.emf.ecore.EObject) AbstractSCTResource(org.yakindu.sct.model.sgraph.resource.AbstractSCTResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) InternalScope(org.yakindu.sct.model.stext.stext.InternalScope) Statechart(org.yakindu.sct.model.sgraph.Statechart) Declaration(org.yakindu.base.types.Declaration) EventDefinition(org.yakindu.sct.model.stext.stext.EventDefinition) ElementReferenceExpression(org.yakindu.base.expressions.expressions.ElementReferenceExpression) OperationDefinition(org.yakindu.sct.model.stext.stext.OperationDefinition) Check(org.eclipse.xtext.validation.Check)

Example 62 with VariableDefinition

use of org.yakindu.sct.model.stext.stext.VariableDefinition in project statecharts by Yakindu.

the class STextJavaValidator method checkValueReferenedBeforeDefined.

@Check(CheckType.NORMAL)
public void checkValueReferenedBeforeDefined(Scope scope) {
    EList<Declaration> declarations = scope.getDeclarations();
    Set<QualifiedName> defined = Sets.newHashSet();
    for (Declaration declaration : declarations) {
        if (declaration instanceof VariableDefinition) {
            VariableDefinition definition = (VariableDefinition) declaration;
            if (!definition.isConst())
                return;
            Expression initialValue = definition.getInitialValue();
            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 (!defined.contains(nameProvider.getFullyQualifiedName(referencedObject)))
                        error(REFERENCE_CONSTANT_BEFORE_DEFINED, definition, StextPackage.Literals.VARIABLE_DEFINITION__INITIAL_VALUE);
                }
            }
            defined.add(nameProvider.getFullyQualifiedName(definition));
        }
    }
}
Also used : VariableDefinition(org.yakindu.sct.model.stext.stext.VariableDefinition) EventRaisingExpression(org.yakindu.sct.model.stext.stext.EventRaisingExpression) Expression(org.yakindu.base.expressions.expressions.Expression) AssignmentExpression(org.yakindu.base.expressions.expressions.AssignmentExpression) EventValueReferenceExpression(org.yakindu.sct.model.stext.stext.EventValueReferenceExpression) ElementReferenceExpression(org.yakindu.base.expressions.expressions.ElementReferenceExpression) PostFixUnaryExpression(org.yakindu.base.expressions.expressions.PostFixUnaryExpression) QualifiedName(org.eclipse.xtext.naming.QualifiedName) EObject(org.eclipse.emf.ecore.EObject) Declaration(org.yakindu.base.types.Declaration) FeatureCall(org.yakindu.base.expressions.expressions.FeatureCall) ElementReferenceExpression(org.yakindu.base.expressions.expressions.ElementReferenceExpression) Check(org.eclipse.xtext.validation.Check)

Aggregations

VariableDefinition (org.yakindu.sct.model.stext.stext.VariableDefinition)62 StextTestFactory._createVariableDefinition (org.yakindu.sct.model.stext.test.util.StextTestFactory._createVariableDefinition)56 Test (org.junit.Test)55 ExecutionFlow (org.yakindu.sct.model.sexec.ExecutionFlow)54 ExecutionState (org.yakindu.sct.model.sexec.ExecutionState)52 Statechart (org.yakindu.sct.model.sgraph.Statechart)48 Region (org.yakindu.sct.model.sgraph.Region)47 SGraphTestFactory._createRegion (org.yakindu.sct.model.sgraph.test.util.SGraphTestFactory._createRegion)47 SGraphTestFactory._createStatechart (org.yakindu.sct.model.sgraph.test.util.SGraphTestFactory._createStatechart)47 State (org.yakindu.sct.model.sgraph.State)46 SGraphTestFactory._createState (org.yakindu.sct.model.sgraph.test.util.SGraphTestFactory._createState)46 StextTestFactory._createInterfaceScope (org.yakindu.sct.model.stext.test.util.StextTestFactory._createInterfaceScope)46 Sequence (org.yakindu.sct.model.sexec.Sequence)41 InterfaceScope (org.yakindu.sct.model.stext.stext.InterfaceScope)39 SCTTestUtil.findState (org.yakindu.sct.model.sexec.transformation.test.SCTTestUtil.findState)28 Entry (org.yakindu.sct.model.sgraph.Entry)27 SGraphTestFactory._createEntry (org.yakindu.sct.model.sgraph.test.util.SGraphTestFactory._createEntry)27 LocalReaction (org.yakindu.sct.model.stext.stext.LocalReaction)23 Reaction (org.yakindu.sct.model.sexec.Reaction)22 StextTestFactory._createLocalReaction (org.yakindu.sct.model.stext.test.util.StextTestFactory._createLocalReaction)22