/** Replies the resource. */ @Pure public Resource eResource() { return getSarlField().eResource(); }
/** Generate the given object. * * @param field the fields. * @param it the target for the generated content. * @param context the context. */ protected void _generate(SarlField field, PyAppendable it, IExtraLanguageGeneratorContext context) { if (field.isStatic()) { // Static field generatePythonField(field, it, context); } else { final String key = this.qualifiedNameProvider.getFullyQualifiedName(field.getDeclaringType()).toString(); final List<SarlField> fields = context.getMultimapValues(INSTANCE_VARIABLES_MEMENTO, key); fields.add(field); } }
/** Create a field declaration. * * @param field the field to generate. * @param it the output * @param context the generation context. */ protected void generatePythonField(SarlField field, PyAppendable it, IExtraLanguageGeneratorContext context) { generateBlockComment(getTypeBuilder().getDocumentation(field), it); if (!field.isStatic()) { it.append("self."); //$NON-NLS-1$ } final String fieldName = it.declareUniqueNameVariable(field, field.getName()); it.append(fieldName); it.append(" = "); //$NON-NLS-1$ if (field.getInitialValue() != null) { generate(field.getInitialValue(), null, it, context); } else { it.append(PyExpressionGenerator.toDefaultValue(field.getType())); } it.newLine(); }
/** Initialize the Ecore element. * @param container the container of the SarlField. * @param name the name of the SarlField. */ public void eInit(XtendTypeDeclaration container, String name, String modifier, IJvmTypeProvider context) { setTypeResolutionContext(context); if (this.sarlField == null) { this.container = container; this.sarlField = SarlFactory.eINSTANCE.createSarlField(); this.sarlField.setAnnotationInfo(XtendFactory.eINSTANCE.createXtendMember()); this.sarlField.setName(name); if (Strings.equal(modifier, "var") || Strings.equal(modifier, "val")) { this.sarlField.getModifiers().add(modifier); } else { throw new IllegalStateException("Invalid modifier"); } container.getMembers().add(this.sarlField); } }
/** Create an expression but does not change the container. * * @param expression the textual representation of the expression. * @return the expression. */ @Pure protected XExpression fromString(String expression) { if (!Strings.isEmpty(expression)) { ResourceSet resourceSet = this.context.eResource().getResourceSet(); URI uri = computeUnusedUri(resourceSet); Resource resource = getResourceFactory().createResource(uri); resourceSet.getResources().add(resource); try (StringInputStream is = new StringInputStream(generateExpressionCode(expression))) { resource.load(is, null); SarlScript script = resource.getContents().isEmpty() ? null : (SarlScript) resource.getContents().get(0); SarlEvent topElement = (SarlEvent) script.getXtendTypes().get(0); SarlField member = (SarlField) topElement.getMembers().get(0); return member.getInitialValue(); } catch (Throwable exception) { throw new RuntimeException(exception); } finally { resourceSet.getResources().remove(resource); } } throw new IllegalArgumentException("not a valid expression"); }
static JvmParameterizedTypeReference parseType(Notifier context, String typeName, AbstractBuilder caller) { ResourceSet resourceSet = toResource(context).getResourceSet(); URI uri = caller.computeUnusedUri(resourceSet); Resource resource = caller.getResourceFactory().createResource(uri); resourceSet.getResources().add(resource); try (StringInputStream is = new StringInputStream(generateTypenameCode(typeName))) { resource.load(is, null); SarlScript script = resource.getContents().isEmpty() ? null : (SarlScript) resource.getContents().get(0); SarlEvent topElement = (SarlEvent) script.getXtendTypes().get(0); SarlField member = (SarlField) topElement.getMembers().get(0); JvmTypeReference reference = member.getType(); if (reference instanceof JvmParameterizedTypeReference) { final JvmParameterizedTypeReference pref = (JvmParameterizedTypeReference) reference; if (!pref.getArguments().isEmpty()) { EcoreUtil2.resolveAll(resource); return pref; } } } catch (Exception exception) { throw new TypeNotPresentException(typeName, exception); } finally { resourceSet.getResources().remove(resource); } throw new TypeNotPresentException(typeName, null); }
/** Add a modifier. * @param modifier the modifier to add. */ public void addModifier(String modifier) { if (!Strings.isEmpty(modifier)) { getSarlField().getModifiers().add(modifier); } }
/** Change the documentation of the element. * * <p>The documentation will be displayed just before the element. * * @param doc the documentation. */ public void setDocumentation(String doc) { if (Strings.isEmpty(doc)) { getSarlField().eAdapters().removeIf(new Predicate<Adapter>() { public boolean test(Adapter adapter) { return adapter.isAdapterForType(DocumentationAdapter.class); } }); } else { DocumentationAdapter adapter = (DocumentationAdapter) EcoreUtil.getExistingAdapter( getSarlField(), DocumentationAdapter.class); if (adapter == null) { adapter = new DocumentationAdapter(); getSarlField().eAdapters().add(adapter); } adapter.setDocumentation(doc); } }
public void checkFieldNameShadowing(SarlField field) { if (!isIgnored(VARIABLE_NAME_SHADOWING) && !Utils.isHiddenMember(field.getName())) { final JvmField inferredField = this.associations.getJvmField(field); final Map<String, JvmField> inheritedFields = new TreeMap<>(); this.sarlActionSignatures); final JvmField inheritedField = inheritedFields.get(field.getName()); if (inheritedField != null) { int nameIndex = 0; String newName = field.getName() + nameIndex; while (inheritedFields.containsKey(newName)) { ++nameIndex; newName = field.getName() + nameIndex; field.getName(), inferredField.getDeclaringType().getQualifiedName(), inheritedField.getQualifiedName()),
/** Replies the resource. */ @Pure public Resource eResource() { return getSarlField().eResource(); }
/** Check if the given field has a valid name. * * @param field the field to test. * @see SARLFeatureNameValidator */ @Check(CheckType.FAST) public void checkFieldName(SarlField field) { final JvmField inferredType = this.associations.getJvmField(field); final QualifiedName name = Utils.getQualifiedName(inferredType); if (this.featureNames.isDisallowedName(name)) { final String validName = Utils.fixHiddenMember(field.getName()); error(MessageFormat.format( Messages.SARLValidator_41, field.getName()), field, XTEND_FIELD__NAME, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, VARIABLE_NAME_DISALLOWED, validName); } else if (this.grammarAccess.getOccurrenceKeyword().equals(field.getName())) { error(MessageFormat.format( Messages.SARLValidator_41, this.grammarAccess.getOccurrenceKeyword()), field, XTEND_FIELD__NAME, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, VARIABLE_NAME_DISALLOWED); } }