Search in sources :

Example 56 with JvmFormalParameter

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

the class LinkingTest method testImplicitFirstArgument_01.

@Test
public void testImplicitFirstArgument_01() throws Exception {
    XtendClass clazz = clazz("import static extension test.ImplicitFirstArgumentStatics.*\n" + "class MyXtendClass {\n" + "  def testExtensionMethods(CharSequence it) {\n" + "    length\n" + "  }\n" + "  extension String" + "  def length() { null }\n" + "}");
    XtendFunction func = (XtendFunction) clazz.getMembers().get(0);
    XFeatureCall first = (XFeatureCall) ((XBlockExpression) func.getExpression()).getExpressions().get(0);
    JvmOperation firstFeature = (JvmOperation) first.getFeature();
    assertEquals("java.lang.CharSequence.length()", firstFeature.getIdentifier());
    assertEquals("java.lang.CharSequence", firstFeature.getDeclaringType().getQualifiedName());
    assertNull(first.getImplicitFirstArgument());
    XFeatureCall firstReceiver = (XFeatureCall) first.getImplicitReceiver();
    assertTrue(firstReceiver.getFeature() instanceof JvmFormalParameter);
    assertNull(first.getInvalidFeatureIssueCode());
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XBlockExpression(org.eclipse.xtext.xbase.XBlockExpression) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) Test(org.junit.Test)

Example 57 with JvmFormalParameter

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

the class LinkingTest method testDeclaredConstructor_01.

@Test
public void testDeclaredConstructor_01() throws Exception {
    XtendClass clazz = clazz("class Foo { " + "  int i" + "  new(int i) { this.i = i }" + "}");
    assertEquals(2, clazz.getMembers().size());
    XtendConstructor constructor = (XtendConstructor) clazz.getMembers().get(1);
    XAssignment assignment = (XAssignment) ((XBlockExpression) constructor.getExpression()).getExpressions().get(0);
    JvmField field = (JvmField) assignment.getFeature();
    assertEquals("i", field.getSimpleName());
    XFeatureCall target = (XFeatureCall) assignment.getAssignable();
    JvmDeclaredType identifiableElement = (JvmDeclaredType) target.getFeature();
    assertEquals("Foo", identifiableElement.getSimpleName());
    XFeatureCall value = (XFeatureCall) assignment.getValue();
    JvmFormalParameter parameter = (JvmFormalParameter) value.getFeature();
    assertEquals("i", parameter.getSimpleName());
}
Also used : XBlockExpression(org.eclipse.xtext.xbase.XBlockExpression) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) XtendConstructor(org.eclipse.xtend.core.xtend.XtendConstructor) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmField(org.eclipse.xtext.common.types.JvmField) XAssignment(org.eclipse.xtext.xbase.XAssignment) Test(org.junit.Test)

Example 58 with JvmFormalParameter

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

the class TypeReferenceAssignabilityTest method isAssignableFrom.

@Override
public void isAssignableFrom(final Pair<String, String> lhsAndParams, final String rhs, final boolean expectation) {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("def ");
        {
            boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(lhsAndParams.getValue());
            boolean _not = (!_isNullOrEmpty);
            if (_not) {
                _builder.append("<");
                String _value = lhsAndParams.getValue();
                _builder.append(_value);
                _builder.append("> ");
            }
        }
        _builder.append("void method(");
        String _fixup = this.fixup(lhsAndParams.getKey());
        _builder.append(_fixup);
        _builder.append(" lhs, ");
        String _fixup_1 = this.fixup(rhs);
        _builder.append(_fixup_1);
        _builder.append(" rhs) {}");
        final String signature = _builder.toString();
        final XtendFunction function = this.function(signature.toString());
        final JvmOperation operation = this._iXtendJvmAssociations.getDirectlyInferredOperation(function);
        EObject _rootContainer = EcoreUtil.getRootContainer(function);
        final XtendFile xtendFile = ((XtendFile) _rootContainer);
        final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
            TypeReference _xifexpression = null;
            String _key = lhsAndParams.getKey();
            boolean _tripleNotEquals = (_key != null);
            if (_tripleNotEquals) {
                _xifexpression = it.toTypeReference(IterableExtensions.<JvmFormalParameter>head(operation.getParameters()).getParameterType());
            } else {
                _xifexpression = it.toTypeReference(this.getOwner().newAnyTypeReference());
            }
            final TypeReference lhsType = _xifexpression;
            TypeReference _xifexpression_1 = null;
            if ((rhs != null)) {
                _xifexpression_1 = it.toTypeReference(IterableExtensions.<JvmFormalParameter>last(operation.getParameters()).getParameterType());
            } else {
                _xifexpression_1 = it.toTypeReference(this.getOwner().newAnyTypeReference());
            }
            final TypeReference rhsType = _xifexpression_1;
            String _simpleName = lhsType.getSimpleName();
            String _plus = (_simpleName + " := ");
            String _simpleName_1 = rhsType.getSimpleName();
            String _plus_1 = (_plus + _simpleName_1);
            Assert.assertEquals(_plus_1, Boolean.valueOf(expectation), Boolean.valueOf(this.testIsAssignable(it, lhsType, rhsType)));
            if (expectation) {
                Iterable<? extends TypeReference> _declaredSuperTypes = lhsType.getDeclaredSuperTypes();
                for (final TypeReference superType : _declaredSuperTypes) {
                    if (((superType.isArray() == lhsType.isArray()) || (lhsType.isArray() == rhsType.isArray()))) {
                        Assert.assertEquals(superType.toString(), Boolean.valueOf(expectation), Boolean.valueOf(this.testIsAssignable(it, superType, rhsType)));
                    }
                }
            }
        };
        this.asCompilationUnit(xtendFile, _function);
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference)

Example 59 with JvmFormalParameter

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

the class XtendValidator method getParamTypes.

protected List<JvmType> getParamTypes(JvmOperation jvmOperation, boolean wrapPrimitives) {
    List<JvmType> types = newArrayList();
    for (JvmFormalParameter p : jvmOperation.getParameters()) {
        LightweightTypeReference typeReference = toLightweightTypeReference(p.getParameterType());
        if (wrapPrimitives) {
            typeReference = typeReference.getWrapperTypeIfPrimitive();
        }
        types.add(typeReference.getType());
    }
    return types;
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmType(org.eclipse.xtext.common.types.JvmType)

Example 60 with JvmFormalParameter

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

the class XtendReentrantTypeResolver method inferAnonymousClassConstructor.

protected JvmConstructor inferAnonymousClassConstructor(AnonymousClass anonymousClass, JvmGenericType inferredLocalClass, JvmConstructor superConstructor, boolean visible) {
    JvmConstructor constructor = TypesFactory.eINSTANCE.createJvmConstructor();
    inferredLocalClass.getMembers().add(constructor);
    associator.associatePrimary(anonymousClass.getConstructorCall(), constructor);
    if (visible) {
        constructor.setVisibility(JvmVisibility.DEFAULT);
    } else
        constructor.setVisibility(JvmVisibility.PRIVATE);
    constructor.setSimpleName(inferredLocalClass.getSimpleName());
    constructor.setVarArgs(superConstructor.isVarArgs());
    final List<JvmFormalParameter> parameters = superConstructor.getParameters();
    for (JvmFormalParameter parameter : parameters) {
        // trigger name computation
        parameter.getName();
        constructor.getParameters().add(typesBuilder.cloneWithProxies(parameter));
    }
    for (JvmTypeReference exception : superConstructor.getExceptions()) constructor.getExceptions().add(typesBuilder.cloneWithProxies(exception));
    if (!parameters.isEmpty()) {
        typesBuilder.setBody(constructor, new Procedures.Procedure1<ITreeAppendable>() {

            @Override
            public void apply(ITreeAppendable a) {
                a.append("super(");
                for (int i = 0; i < parameters.size(); i++) {
                    if (i != 0) {
                        a.append(", ");
                    }
                    a.append(parameters.get(i).getSimpleName());
                }
                a.append(");");
            }
        });
    }
    return constructor;
}
Also used : JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) Procedures(org.eclipse.xtext.xbase.lib.Procedures) ITreeAppendable(org.eclipse.xtext.xbase.compiler.output.ITreeAppendable)

Aggregations

JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)84 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)37 Test (org.junit.Test)30 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)20 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)16 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)15 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)15 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)14 JvmType (org.eclipse.xtext.common.types.JvmType)13 EObject (org.eclipse.emf.ecore.EObject)12 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)11 XExpression (org.eclipse.xtext.xbase.XExpression)11 JvmConstructor (org.eclipse.xtext.common.types.JvmConstructor)10 XClosure (org.eclipse.xtext.xbase.XClosure)10 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)9 IResolvedTypes (org.eclipse.xtext.xbase.typesystem.IResolvedTypes)9 JvmMember (org.eclipse.xtext.common.types.JvmMember)8 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)7 JvmParameterizedTypeReference (org.eclipse.xtext.common.types.JvmParameterizedTypeReference)7 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)7