package jetbrains.mps.baseLanguage.util.plugin.refactorings;
/*Generated by MPS */
import org.jetbrains.mps.openapi.model.SNode;
import java.util.List;
import java.util.Map;
import jetbrains.mps.internal.collections.runtime.MapSequence;
import java.util.HashMap;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.ArrayList;
import jetbrains.mps.typesystem.inference.SubtypingManager;
import jetbrains.mps.typesystem.inference.TypeChecker;
import jetbrains.mps.typesystem.inference.util.StructuralNodeSet;
import jetbrains.mps.internal.collections.runtime.SetSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
public class MethodParameter extends MethodParameterModel {
private SNode myDeclaration;
private SNode myType;
private SNode myVariableReference;
private boolean mySelected = true;
private List<String> myAvailableTypes;
private Map<String, SNode> myTypesMap = MapSequence.fromMap(new HashMap<String, SNode>());
public MethodParameter(SNode variable, SNode type, String name, SNode reference) {
this.myDeclaration = variable;
this.myType = type;
if (type != null) {
this.setTypeName(type.getPresentation());
} else {
this.setTypeName("null");
}
this.setName(name);
this.myVariableReference = SNodeOperations.cast(reference, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37f506fL, "jetbrains.mps.baseLanguage.structure.Expression"));
this.myAvailableTypes = this.createAvailableTypes();
}
public List<String> createAvailableTypes() {
if (this.myType == null) {
List<String> result = ListSequence.fromList(new ArrayList<String>());
return result;
}
SubtypingManager manager = TypeChecker.getInstance().getSubtypingManager();
StructuralNodeSet<?> frontier = new StructuralNodeSet();
List<SNode> found = new ArrayList<SNode>();
frontier.add(this.myType);
while (!(frontier.isEmpty())) {
StructuralNodeSet<?> ancestors = new StructuralNodeSet();
for (SNode node : SetSequence.fromSet(frontier)) {
ancestors.addAllStructurally(manager.collectImmediateSupertypes(node, false));
ListSequence.fromList(found).addElement(node);
}
for (SNode passed : ListSequence.fromList(found)) {
ancestors.removeStructurally(passed);
}
frontier = ancestors;
}
List<String> result = ListSequence.fromList(new ArrayList<String>());
for (SNode node : ListSequence.fromList(found)) {
ListSequence.fromList(result).addElement(node.getPresentation());
MapSequence.fromMap(this.myTypesMap).put(node.getPresentation(), node);
}
return result;
}
public SNode getType() {
return this.myType;
}
public void setType(SNode node) {
this.myType = node;
}
public void setTypeByName(String name) {
this.setTypeName(name);
this.setType(MapSequence.fromMap(this.myTypesMap).get(name));
}
@Override
public SNode getDeclaration() {
return this.myDeclaration;
}
public boolean isSelected() {
return this.mySelected;
}
public void setSelected(boolean selected) {
this.mySelected = selected;
}
public List<String> getAvailableTypes() {
return this.myAvailableTypes;
}
public SNode getReference() {
return SNodeOperations.copyNode(this.myVariableReference);
}
public boolean isFinal() {
if (SNodeOperations.isInstanceOf(this.myDeclaration, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, "jetbrains.mps.baseLanguage.structure.VariableDeclaration"))) {
return SPropertyOperations.getBoolean(SNodeOperations.cast(this.myDeclaration, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, "jetbrains.mps.baseLanguage.structure.VariableDeclaration")), MetaAdapterFactory.getProperty(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, 0x111f9e9f00cL, "isFinal"));
}
return false;
}
}