Search in sources :

Example 16 with JvmField

use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.

the class UIStringsTest method testReferenceToString_4.

@Test
public void testReferenceToString_4() throws Exception {
    XtendFile file = file("class Foo { var test }");
    assertFalse(validationTestHelper.validate(file).isEmpty());
    XtendClass clazz = (XtendClass) file.getXtendTypes().get(0);
    XtendField field = (XtendField) clazz.getMembers().get(0);
    JvmField jvmField = associations.getJvmField(field);
    JvmTypeReference reference = jvmField.getType();
    assertNotNull(reference);
    assertNotNull(reference.getType());
    assertFalse(reference.getType().eIsProxy());
    assertNotNull(reference.eResource());
    assertEquals("Object", this.uiStrings.referenceToString(reference, "the-default-label"));
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmField(org.eclipse.xtext.common.types.JvmField) XtendField(org.eclipse.xtend.core.xtend.XtendField) Test(org.junit.Test)

Example 17 with JvmField

use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testTracing.

@Test
public void testTracing() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.AbstractClassProcessor");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(GettersProcessor)");
    _builder.newLine();
    _builder.append("annotation Getters {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class GettersProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.declaredFields.filter[isThePrimaryGeneratedJavaElement].forEach [field|");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("cls.addMethod(\"get\" + field.simpleName.toFirstUpper) [");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("primarySourceElement = field");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("returnType = field.type");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("body = [\"return this.\" + field.simpleName + \";\"]");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("field.markAsRead");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/Getters.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.Getters");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@Getters");
    _builder_1.newLine();
    _builder_1.append("class Client {");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("val int bar = 1");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def create new Integer(1) foo() {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration cls = it.getTypeLookup().findClass("myusercode.Client");
        final MutableFieldDeclaration barField = cls.findDeclaredField("bar");
        final Function1<MutableMethodDeclaration, Boolean> _function_1 = (MutableMethodDeclaration it_1) -> {
            return Boolean.valueOf(it_1.getSimpleName().startsWith("get"));
        };
        final Iterable<? extends MutableMethodDeclaration> getters = IterableExtensions.filter(cls.getDeclaredMethods(), _function_1);
        Assert.assertEquals(1, IterableExtensions.size(getters));
        final MutableMethodDeclaration getBar = IterableExtensions.head(getters);
        Assert.assertEquals("getBar", getBar.getSimpleName());
        final JvmField barJvmField = ((MutableJvmFieldDeclarationImpl) barField).getDelegate();
        final JvmOperation getBarJvmMethod = ((MutableJvmMethodDeclarationImpl) getBar).getDelegate();
        final Set<EObject> elementsAssociatedWithBarField = it.getJvmModelAssociations().getJvmElements(it.getJvmModelAssociations().getPrimarySourceElement(barJvmField));
        Assert.assertEquals(2, elementsAssociatedWithBarField.size());
        Assert.assertEquals(barJvmField, ((Object[]) Conversions.unwrapArray(elementsAssociatedWithBarField, Object.class))[0]);
        Assert.assertEquals(getBarJvmMethod, ((Object[]) Conversions.unwrapArray(elementsAssociatedWithBarField, Object.class))[1]);
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : Set(java.util.Set) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) EObject(org.eclipse.emf.ecore.EObject) JvmField(org.eclipse.xtext.common.types.JvmField) Test(org.junit.Test)

Example 18 with JvmField

use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.

the class XtendJvmModelInferrer method transform.

protected void transform(XtendField source, JvmGenericType container) {
    if ((source.isExtension() && source.getType() != null) || source.getName() != null) {
        JvmField field = typesFactory.createJvmField();
        final String computeFieldName = computeFieldName(source);
        field.setSimpleName(computeFieldName);
        container.getMembers().add(field);
        associator.associatePrimary(source, field);
        field.setVisibility(source.getVisibility());
        field.setStatic(source.isStatic());
        field.setTransient(source.isTransient());
        field.setVolatile(source.isVolatile());
        field.setFinal(source.isFinal());
        if (source.getType() != null) {
            field.setType(jvmTypesBuilder.cloneWithProxies(source.getType()));
        } else if (source.getInitialValue() != null) {
            field.setType(jvmTypesBuilder.inferredType(source.getInitialValue()));
        }
        for (XAnnotation anno : source.getAnnotations()) {
            if (!annotationTranslationFilter.apply(anno))
                continue;
            JvmAnnotationReference annotationReference = jvmTypesBuilder.getJvmAnnotationReference(anno);
            if (annotationReference != null)
                field.getAnnotations().add(annotationReference);
        }
        if (source.isExtension() && typeReferences.findDeclaredType(Extension.class, source) != null) {
            field.getAnnotations().add(_annotationTypesBuilder.annotationRef(Extension.class));
        }
        jvmTypesBuilder.copyDocumentationTo(source, field);
        jvmTypesBuilder.setInitializer(field, source.getInitialValue());
        initializeLocalTypes(field, source.getInitialValue());
    }
}
Also used : Extension(org.eclipse.xtext.xbase.lib.Extension) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) JvmField(org.eclipse.xtext.common.types.JvmField) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference)

Example 19 with JvmField

use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.

the class XtendValidator method checkLocalUsageOfDeclaredFields.

@Check
public void checkLocalUsageOfDeclaredFields(XtendField field) {
    if (doCheckValidMemberName(field) && !isIgnored(UNUSED_PRIVATE_MEMBER)) {
        JvmField jvmField = associations.getJvmField(field);
        if (jvmField == null || jvmField.getVisibility() != JvmVisibility.PRIVATE || jvmField.eContainer() == null)
            return;
        if (isLocallyUsed(jvmField, getOutermostType(field)))
            return;
        String message;
        if (field.isExtension()) {
            if (field.getName() == null && jvmField.getType() != null)
                message = "The extension " + jvmField.getType().getIdentifier() + " is not used in " + getDeclaratorName(jvmField);
            else
                message = "The extension " + getDeclaratorName(jvmField) + "." + jvmField.getSimpleName() + " is not used";
        } else {
            message = "The value of the field " + getDeclaratorName(jvmField) + "." + jvmField.getSimpleName() + " is not used";
        }
        addIssueToState(UNUSED_PRIVATE_MEMBER, message, XtendPackage.Literals.XTEND_FIELD__NAME);
    }
}
Also used : JvmField(org.eclipse.xtext.common.types.JvmField) RichString(org.eclipse.xtend.core.xtend.RichString) Check(org.eclipse.xtext.validation.Check)

Example 20 with JvmField

use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.

the class XtendGenerator method getAddedDeclarations.

protected ArrayList<JvmMember> getAddedDeclarations(final JvmGenericType it, final AnonymousClass anonymousClass) {
    final ArrayList<JvmMember> result = CollectionLiterals.<JvmMember>newArrayList();
    final JvmConstructor constructor = anonymousClass.getConstructorCall().getConstructor();
    int _size = constructor.getParameters().size();
    boolean _greaterEqualsThan = (_size >= 1);
    if (_greaterEqualsThan) {
        result.add(0, constructor);
    }
    Iterable<JvmField> _declaredFields = it.getDeclaredFields();
    Iterables.<JvmMember>addAll(result, _declaredFields);
    final Function1<JvmOperation, Boolean> _function = (JvmOperation it_1) -> {
        EObject _head = IterableExtensions.<EObject>head(this.getSourceElements(it_1));
        final XtendFunction function = ((XtendFunction) _head);
        boolean _isOverride = function.isOverride();
        return Boolean.valueOf((!_isOverride));
    };
    Iterable<JvmOperation> _filter = IterableExtensions.<JvmOperation>filter(it.getDeclaredOperations(), _function);
    Iterables.<JvmMember>addAll(result, _filter);
    Iterable<JvmDeclaredType> _filter_1 = Iterables.<JvmDeclaredType>filter(it.getMembers(), JvmDeclaredType.class);
    Iterables.<JvmMember>addAll(result, _filter_1);
    return result;
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) EObject(org.eclipse.emf.ecore.EObject) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) JvmMember(org.eclipse.xtext.common.types.JvmMember) JvmField(org.eclipse.xtext.common.types.JvmField)

Aggregations

JvmField (org.eclipse.xtext.common.types.JvmField)36 Test (org.junit.Test)24 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)19 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)18 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)17 JvmMember (org.eclipse.xtext.common.types.JvmMember)12 EObject (org.eclipse.emf.ecore.EObject)9 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)9 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)8 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)7 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)5 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)4 JvmConstructor (org.eclipse.xtext.common.types.JvmConstructor)3 JvmEnumerationLiteral (org.eclipse.xtext.common.types.JvmEnumerationLiteral)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Set (java.util.Set)2 InternalEObject (org.eclipse.emf.ecore.InternalEObject)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)2