package jetbrains.mps.baseLanguage.util.plugin.refactorings; /*Generated by MPS */ import org.jetbrains.mps.openapi.model.SNode; import java.util.List; import jetbrains.mps.internal.collections.runtime.ListSequence; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations; import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations; import jetbrains.mps.smodel.behaviour.BHReflection; import jetbrains.mps.core.aspects.behaviour.SMethodTrimmedId; import org.jetbrains.mps.openapi.persistence.PersistenceFacade; import jetbrains.mps.smodel.SModelUtil_new; import jetbrains.mps.lang.typesystem.runtime.HUtil; /*package*/ class ExtractMethodWithOutputVariable extends ExtractMethodFromStatementsRefactoring { private SNode myOutputVariable; private SNode myDeclarationStatement = null; /*package*/ ExtractMethodWithOutputVariable(ExtractMethodRefactoringParameters parameters) { super(parameters); List<SNode> output = this.myAnalyzer.getOutputVariables(); if (ListSequence.fromList(output).isNotEmpty()) { this.myOutputVariable = SNodeOperations.cast(ListSequence.fromList(output).first(), MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, "jetbrains.mps.baseLanguage.structure.VariableDeclaration")); } for (SNode statement : ListSequence.fromList(this.myStatements)) { if (SNodeOperations.isInstanceOf(statement, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc67c7f0L, "jetbrains.mps.baseLanguage.structure.LocalVariableDeclarationStatement"))) { SNode tmp = SNodeOperations.cast(statement, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc67c7f0L, "jetbrains.mps.baseLanguage.structure.LocalVariableDeclarationStatement")); if (SLinkOperations.getTarget(tmp, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc67c7f0L, 0xf8cc67c7f1L, "localVariableDeclaration")) == this.myOutputVariable) { this.myDeclarationStatement = tmp; } } } } @Override protected void modifyPartToExtract() { SNode returnStatement = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc67c7feL, "jetbrains.mps.baseLanguage.structure.ReturnStatement")); SLinkOperations.setTarget(returnStatement, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc67c7feL, 0xf8cc6bf96cL, "expression"), this.createReference(this.myOutputVariable)); ListSequence.fromList(this.myStatements).addElement(returnStatement); } @Override public SNode getMethodType() { return SNodeOperations.copyNode(SLinkOperations.getTarget(this.myOutputVariable, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x450368d90ce15bc3L, 0x4ed4d318133c80ceL, "type"))); } @Override public void replaceMatch(MethodMatch match, SNode methodDeclaration) { SNode methodCall = this.createMethodCall(match, methodDeclaration); List<SNode> statements = match.getNodes(); if ((this.myDeclarationStatement != null)) { SLinkOperations.setTarget(SLinkOperations.getTarget(this.myDeclarationStatement, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc67c7f0L, 0xf8cc67c7f1L, "localVariableDeclaration")), MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, 0xf8c37f506eL, "initializer"), methodCall); } else { SNode newStatement = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b213L, "jetbrains.mps.baseLanguage.structure.ExpressionStatement")); SLinkOperations.setTarget(newStatement, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b213L, 0xf8cc56b214L, "expression"), _quotation_createNode_n3576q_a0b0a2a5(methodCall, ((SNode) BHReflection.invoke(this.myOutputVariable, SMethodTrimmedId.create("createReference", null, "hEwJfME"))))); SNodeOperations.insertPrevSiblingChild(ListSequence.fromList(statements).first(), newStatement); } for (SNode statement : ListSequence.fromList(statements)) { if (statement != this.myDeclarationStatement) { SNodeOperations.deleteNode(statement); } } } private static SNode _quotation_createNode_n3576q_a0b0a2a5(Object parameter_1, Object parameter_2) { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode quotedNode_3 = null; SNode quotedNode_4 = null; SNode quotedNode_5 = null; quotedNode_3 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf8c77f1e96L, "AssignmentExpression"), null, null, false); quotedNode_4 = (SNode) parameter_1; if (quotedNode_4 != null) { quotedNode_3.addChild(MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11b0d00332cL, 0xf8c77f1e99L, "rValue"), HUtil.copyIfNecessary(quotedNode_4)); } quotedNode_5 = (SNode) parameter_2; if (quotedNode_5 != null) { quotedNode_3.addChild(MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11b0d00332cL, 0xf8c77f1e97L, "lValue"), HUtil.copyIfNecessary(quotedNode_5)); } return quotedNode_3; } }