Search in sources :

Example 21 with JvmOperation

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

the class ActualTypeArgumentMergeTest method mappedBy.

public Map<JvmTypeParameter, List<LightweightBoundTypeArgument>> mappedBy(final String typeParameters, final String... alternatingTypeReferences) {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("def ");
        {
            boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(typeParameters);
            boolean _not = (!_isNullOrEmpty);
            if (_not) {
                _builder.append("<");
                _builder.append(typeParameters);
                _builder.append(">");
            }
        }
        _builder.append(" void method(");
        final Function1<String, CharSequence> _function = (String it) -> {
            return it;
        };
        String _join = IterableExtensions.<String>join(((Iterable<String>) Conversions.doWrapArray(alternatingTypeReferences)), null, " p, ", " p", _function);
        _builder.append(_join);
        _builder.append(") {}");
        final String signature = _builder.toString();
        final XtendFunction function = this.function(signature.toString());
        final JvmOperation operation = this._iXtendJvmAssociations.getDirectlyInferredOperation(function);
        EList<JvmTypeParameter> _typeParameters = operation.getTypeParameters();
        ITypeReferenceOwner _owner = this.getOwner();
        final ActualTypeArgumentCollector collector = new ActualTypeArgumentCollector(_typeParameters, BoundTypeArgumentSource.INFERRED, _owner);
        int _size = ((List<String>) Conversions.doWrapArray(alternatingTypeReferences)).size();
        int _minus = (_size - 1);
        IntegerRange _withStep = new IntegerRange(0, _minus).withStep(2);
        for (final Integer i : _withStep) {
            collector.populateTypeParameterMapping(this.toLightweightTypeReference(operation.getParameters().get((i).intValue()).getParameterType()), this.toLightweightTypeReference(operation.getParameters().get(((i).intValue() + 1)).getParameterType()));
        }
        return collector.getTypeParameterMapping();
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) IntegerRange(org.eclipse.xtext.xbase.lib.IntegerRange) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) EList(org.eclipse.emf.common.util.EList) List(java.util.List) ActualTypeArgumentCollector(org.eclipse.xtext.xbase.typesystem.util.ActualTypeArgumentCollector)

Example 22 with JvmOperation

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

the class DispatchHelper method getAllDispatchMethods.

protected List<JvmOperation> getAllDispatchMethods(DispatchSignature signature, JvmDeclaredType type, ContextualVisibilityHelper contextualVisibilityHelper) {
    List<JvmOperation> allOperations = Lists.newArrayListWithExpectedSize(5);
    Iterable<JvmFeature> allFeatures = type.findAllFeaturesByName(signature.getDispatchCaseName());
    for (JvmFeature feature : allFeatures) {
        if (feature instanceof JvmOperation) {
            JvmOperation operationByName = (JvmOperation) feature;
            if (signature.isDispatchCase(operationByName) && contextualVisibilityHelper.isVisible(operationByName)) {
                allOperations.add(operationByName);
            }
        }
    }
    sort(allOperations);
    return allOperations;
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmFeature(org.eclipse.xtext.common.types.JvmFeature)

Example 23 with JvmOperation

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

the class DispatchHelper method getDispatcherOperation.

/* @Nullable */
public JvmOperation getDispatcherOperation(JvmOperation dispatchCase) {
    EObject sourceElement = associations.getPrimarySourceElement(dispatchCase);
    if (sourceElement instanceof XtendFunction) {
        XtendFunction function = (XtendFunction) sourceElement;
        if (function.isDispatch()) {
            Iterable<JvmOperation> operations = filter(associations.getJvmElements(sourceElement), JvmOperation.class);
            for (JvmOperation operation : operations) {
                if (Strings.equal(operation.getSimpleName(), function.getName())) {
                    return operation;
                }
            }
        }
    } else {
        DispatchSignature signature = new DispatchSignature(dispatchCase.getSimpleName().substring(1), dispatchCase.getParameters().size());
        JvmOperation result = getDispatcherOperation(dispatchCase.getDeclaringType(), signature);
        return result;
    }
    return null;
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) EObject(org.eclipse.emf.ecore.EObject)

Example 24 with JvmOperation

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

the class DispatchHelper method getDeclaredOrEnhancedDispatchMethods.

/**
 * Computes all the dispatch methods that are declared in the given type or altered
 * by additional cases in this type. The associated operations are sorted by according their parameter types
 * from left to right where the most special types occur before more common types. Ambiguous
 * ordering is resolved alphabetically.
 *
 * An exemplary order would look like this
 * <pre>
 *   method(String)
 *   method(Serializable)
 *   method(CharSequence)
 *   method(Object)
 * </pre>
 *
 * @return a mapping from {@link DispatchSignature signature} to sorted operations.
 */
public ListMultimap<DispatchSignature, JvmOperation> getDeclaredOrEnhancedDispatchMethods(JvmDeclaredType type) {
    ListMultimap<DispatchSignature, JvmOperation> result = Multimaps2.newLinkedHashListMultimap(2, 4);
    Iterable<JvmOperation> operations = type.getDeclaredOperations();
    ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, type);
    ContextualVisibilityHelper contextualVisibilityHelper = new ContextualVisibilityHelper(visibilityHelper, owner.newParameterizedTypeReference(type));
    for (JvmOperation operation : operations) {
        if (isDispatchFunction(operation)) {
            DispatchSignature signature = new DispatchSignature(operation.getSimpleName().substring(1), operation.getParameters().size());
            if (!result.containsKey(signature)) {
                List<JvmOperation> allOperations = getAllDispatchMethods(signature, type, contextualVisibilityHelper);
                result.putAll(signature, allOperations);
            }
        }
    }
    return result;
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) ContextualVisibilityHelper(org.eclipse.xtext.xbase.typesystem.util.ContextualVisibilityHelper) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 25 with JvmOperation

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

the class DispatchMethodCompileStrategy method apply.

@Override
public void apply(/* @Nullable */
ITreeAppendable a) {
    if (a == null)
        throw new IllegalArgumentException("a is never null");
    boolean needsElse = true;
    int parameterCount = dispatchOperation.getParameters().size();
    List<JvmOperation> sortedDispatchOperations = sorter.getAllDispatchCases(dispatchOperation);
    boolean[] allCasesSameType = new boolean[parameterCount];
    for (int i = 0; i < parameterCount; i++) {
        allCasesSameType[i] = true;
        JvmTypeReference dispatchParameterType = dispatchOperation.getParameters().get(i).getParameterType();
        for (JvmOperation operation : sortedDispatchOperations) {
            JvmFormalParameter parameter = operation.getParameters().get(i);
            JvmTypeReference caseParameterType = parameter.getParameterType();
            if (!Strings.equal(dispatchParameterType.getIdentifier(), caseParameterType.getIdentifier())) {
                allCasesSameType[i] = false;
                break;
            }
        }
    }
    ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, dispatchOperation);
    for (JvmOperation operation : sortedDispatchOperations) {
        ITreeAppendable operationAppendable = treeAppendableUtil.traceSignificant(a, operation, true);
        final List<Later> laters = newArrayList();
        for (int i = 0; i < parameterCount; i++) {
            final JvmFormalParameter dispatchParam = dispatchOperation.getParameters().get(i);
            final LightweightTypeReference dispatchParamType = owner.toLightweightTypeReference(dispatchParam.getParameterType());
            final JvmFormalParameter caseParam = operation.getParameters().get(i);
            final LightweightTypeReference caseParamType = owner.toLightweightTypeReference(caseParam.getParameterType());
            final String name = getVarName(dispatchParam, operationAppendable);
            if (caseParamType.isType(Void.class)) {
                laters.add(new Later() {

                    @Override
                    public void exec(ITreeAppendable appendable) {
                        appendable.append(name).append(" == null");
                    }
                });
            } else if (!allCasesSameType[i]) {
                laters.add(new Later() {

                    @Override
                    public void exec(ITreeAppendable appendable) {
                        if (caseParamType.isAssignableFrom(dispatchParamType, new TypeConformanceComputationArgument(true, false, true, true, false, false)) && !dispatchParamType.isPrimitive()) {
                            appendable.append(name).append(" != null");
                        } else {
                            appendable.append(name).append(" instanceof ");
                            JvmType type = caseParamType.getWrapperTypeIfPrimitive().getType();
                            if (type == null) {
                                throw new IllegalStateException(String.valueOf(caseParamType));
                            }
                            appendable.append(type);
                        }
                    }
                });
            }
        }
        // if it's not the first if append an 'else'
        if (sortedDispatchOperations.get(0) != operation) {
            operationAppendable.append(" else ");
        }
        if (laters.isEmpty()) {
            needsElse = false;
            if (sortedDispatchOperations.size() != 1) {
                operationAppendable.append("{").increaseIndentation();
                operationAppendable.newLine();
            }
        } else {
            operationAppendable.append("if (");
            operationAppendable.increaseIndentation().increaseIndentation();
            Iterator<Later> iterator = laters.iterator();
            while (iterator.hasNext()) {
                iterator.next().exec(operationAppendable);
                if (iterator.hasNext()) {
                    operationAppendable.newLine().append(" && ");
                }
            }
            operationAppendable.decreaseIndentation().decreaseIndentation();
            operationAppendable.append(") {").increaseIndentation();
            operationAppendable.newLine();
        }
        final boolean isCurrentVoid = typeReferences.is(operation.getReturnType(), Void.TYPE);
        final boolean isDispatchVoid = typeReferences.is(dispatchOperation.getReturnType(), Void.TYPE);
        if (isDispatchVoid) {
            generateActualDispatchCall(dispatchOperation, operation, operationAppendable, owner);
            // we generate a redundant return statement here to get a better debugging experience
            operationAppendable.append(";").newLine().append("return;");
        } else {
            if (isCurrentVoid) {
                generateActualDispatchCall(dispatchOperation, operation, operationAppendable, owner);
                operationAppendable.append(";").newLine().append("return null");
            } else {
                operationAppendable.append("return ");
                generateActualDispatchCall(dispatchOperation, operation, operationAppendable, owner);
            }
            operationAppendable.append(";");
        }
        if (sortedDispatchOperations.size() != 1) {
            operationAppendable.decreaseIndentation();
            a.newLine().append("}");
        }
    }
    if (needsElse) {
        a.append(" else {").increaseIndentation();
        a.newLine();
        a.increaseIndentation();
        a.append("throw new IllegalArgumentException(\"Unhandled parameter types: \" +").newLine();
        JvmType jvmType = typeReferences.findDeclaredType("java.util.Arrays", dispatchOperation);
        if (jvmType != null) {
            a.append(jvmType);
        } else {
            a.append(Arrays.class.getSimpleName());
        }
        a.append(".<Object>asList(");
        Iterator<JvmFormalParameter> iterator = dispatchOperation.getParameters().iterator();
        while (iterator.hasNext()) {
            JvmFormalParameter parameter = iterator.next();
            final String name = getVarName(parameter, a);
            a.append(name);
            if (iterator.hasNext()) {
                a.append(", ");
            }
        }
        a.append(").toString());");
        a.decreaseIndentation();
        a.decreaseIndentation().newLine().append("}");
    }
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) TypeConformanceComputationArgument(org.eclipse.xtext.xbase.typesystem.conformance.TypeConformanceComputationArgument) JvmType(org.eclipse.xtext.common.types.JvmType) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) ITreeAppendable(org.eclipse.xtext.xbase.compiler.output.ITreeAppendable) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) Later(org.eclipse.xtext.xbase.compiler.Later) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) Arrays(java.util.Arrays) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Aggregations

JvmOperation (org.eclipse.xtext.common.types.JvmOperation)202 Test (org.junit.Test)127 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)101 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)73 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)71 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)62 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)49 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)38 EObject (org.eclipse.emf.ecore.EObject)30 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)26 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)22 XExpression (org.eclipse.xtext.xbase.XExpression)20 XFeatureCall (org.eclipse.xtext.xbase.XFeatureCall)20 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)20 JvmField (org.eclipse.xtext.common.types.JvmField)18 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)15 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)15 JvmMember (org.eclipse.xtext.common.types.JvmMember)15 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)11 DispatchHelper (org.eclipse.xtend.core.jvmmodel.DispatchHelper)10