|
| 1 | +package cipm.consistency.initialisers.jamopp.initadapters; |
| 2 | + |
| 3 | +import org.eclipse.emf.ecore.EObject; |
| 4 | +import org.emftext.language.java.arrays.ArraySelector; |
| 5 | +import org.emftext.language.java.expressions.Expression; |
| 6 | +import org.emftext.language.java.references.IdentifierReference; |
| 7 | + |
| 8 | +import cipm.consistency.initialisers.IInitialiser; |
| 9 | +import cipm.consistency.initialisers.IInitialiserAdapterStrategy; |
| 10 | +import cipm.consistency.initialisers.jamopp.instantiations.ExplicitConstructorCallInitialiser; |
| 11 | +import cipm.consistency.initialisers.jamopp.statements.ExpressionStatementInitialiser; |
| 12 | + |
| 13 | +/** |
| 14 | + * An {@link IInitialiserAdapterStrategy} implementation that can be used with |
| 15 | + * {@link IInitialiserBase} implementors that instantiate |
| 16 | + * {@link IdentifierReference}. <br> |
| 17 | + * <br> |
| 18 | + * Let <b>IR</b> be an {@link IdentifierReference} instance. |
| 19 | + * {@link IdentifierReferenceInitialiserAdapter} then nests an uninitialised |
| 20 | + * {@link ExpressionStatement} <b>es</b> instance within an uninitialised |
| 21 | + * {@link ExplicitConstructorCall} <b>ecc</b> instance and sets <b>IR</b>'s |
| 22 | + * container to <b>ecc</b>. This way, <b>IR</b> will have a container, which is |
| 23 | + * neither of type {@link Expression} nor {@link ArraySelector} (i.e. an |
| 24 | + * eligible container). <br> |
| 25 | + * <br> |
| 26 | + * <b>Note: <b>IR</b>'s eligible container will be es.</b> |
| 27 | + * |
| 28 | + * @author Alp Torac Genc |
| 29 | + */ |
| 30 | +public class IdentifierReferenceInitialiserAdapter implements IInitialiserAdapterStrategy { |
| 31 | + |
| 32 | + /** |
| 33 | + * Realises the functionality of |
| 34 | + * {@code JaMoPPElementUtil.getFirstContainerNotOfGivenType(...)} |
| 35 | + */ |
| 36 | + private EObject getFirstEligibleContainer(EObject obj) { |
| 37 | + var firstEligibleContainer = obj.eContainer(); |
| 38 | + |
| 39 | + while (firstEligibleContainer != null) { |
| 40 | + if (Expression.class.isAssignableFrom(firstEligibleContainer.getClass()) |
| 41 | + || ArraySelector.class.isAssignableFrom(firstEligibleContainer.getClass())) { |
| 42 | + firstEligibleContainer = firstEligibleContainer.eContainer(); |
| 43 | + } else { |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return firstEligibleContainer; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public boolean apply(IInitialiser init, Object obj) { |
| 53 | + var castedO = (IdentifierReference) obj; |
| 54 | + |
| 55 | + var firstEligibleContainer = this.getFirstEligibleContainer(castedO); |
| 56 | + |
| 57 | + if (firstEligibleContainer == null) { |
| 58 | + var insInit = new ExplicitConstructorCallInitialiser(); |
| 59 | + var ecc = insInit.instantiate(); |
| 60 | + insInit.addArgument(ecc, castedO); |
| 61 | + |
| 62 | + var esInit = new ExpressionStatementInitialiser(); |
| 63 | + var es = esInit.instantiate(); |
| 64 | + esInit.setExpression(es, ecc); |
| 65 | + |
| 66 | + return this.getFirstEligibleContainer(castedO) == es; |
| 67 | + } |
| 68 | + |
| 69 | + return this.getFirstEligibleContainer(castedO) != null; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public IInitialiserAdapterStrategy newStrategy() { |
| 74 | + return new IdentifierReferenceInitialiserAdapter(); |
| 75 | + } |
| 76 | +} |
0 commit comments