Search in sources :

Example 1 with JvmType

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

the class AbstractAssignabilityTest method testIsAssignable.

public boolean testIsAssignable(final LightweightTypeReference lhs, final LightweightTypeReference rhs) {
    Assert.assertTrue(this.doIsAssignable(lhs, lhs));
    Assert.assertTrue(this.doIsAssignable(lhs, this.toLightweightTypeReference(lhs.toTypeReference())));
    Assert.assertTrue(this.doIsAssignable(rhs, this.toLightweightTypeReference(rhs.toTypeReference())));
    final boolean result = this.doIsAssignable(lhs, rhs);
    boolean _isPrimitiveVoid = rhs.isPrimitiveVoid();
    boolean _not = (!_isPrimitiveVoid);
    if (_not) {
        final WildcardTypeReference wcRhs = this.getOwner().newWildcardTypeReference();
        wcRhs.addUpperBound(rhs.getWrapperTypeIfPrimitive());
        Assert.assertEquals(Boolean.valueOf(result), Boolean.valueOf(this.doIsAssignable(lhs, wcRhs)));
        final CompoundTypeReference compoundRhs = this.getOwner().newCompoundTypeReference(true);
        compoundRhs.addComponent(rhs);
        final JvmType object = rhs.getOwner().getServices().getTypeReferences().findDeclaredType(Object.class, rhs.getOwner().getContextResourceSet());
        compoundRhs.addComponent(rhs.getOwner().newParameterizedTypeReference(object));
        String _plus = (lhs + " := ");
        String _string = compoundRhs.toString();
        String _plus_1 = (_plus + _string);
        Assert.assertEquals(_plus_1, Boolean.valueOf(result), Boolean.valueOf(this.doIsAssignable(lhs, compoundRhs)));
    }
    return result;
}
Also used : WildcardTypeReference(org.eclipse.xtext.xbase.typesystem.references.WildcardTypeReference) JvmType(org.eclipse.xtext.common.types.JvmType) CompoundTypeReference(org.eclipse.xtext.xbase.typesystem.references.CompoundTypeReference)

Example 2 with JvmType

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

the class ConstantExpressionsInterpreter method findVisibleFeatures.

/**
 * looks up the static final fields which are accessible in unqualified form for the given expression.
 * That essentially includes static imports and the fields declared in the containing types
 */
protected Map<String, JvmIdentifiableElement> findVisibleFeatures(final XExpression expression) {
    HashMap<String, JvmIdentifiableElement> _xblockexpression = null;
    {
        Resource _eResource = expression.eResource();
        final Resource res = _eResource;
        boolean _matched = false;
        if (res instanceof StorageAwareResource) {
            boolean _isLoadedFromStorage = ((StorageAwareResource) res).isLoadedFromStorage();
            if (_isLoadedFromStorage) {
                _matched = true;
                return CollectionLiterals.<String, JvmIdentifiableElement>newHashMap();
            }
        }
        JvmDeclaredType _switchResult_1 = null;
        JvmIdentifiableElement _nearestLogicalContainer = this.containerProvider.getNearestLogicalContainer(expression);
        final JvmIdentifiableElement cont = _nearestLogicalContainer;
        boolean _matched_1 = false;
        if (cont instanceof JvmGenericType) {
            _matched_1 = true;
            _switchResult_1 = ((JvmGenericType) cont);
        }
        if (!_matched_1) {
            if (cont instanceof JvmMember) {
                _matched_1 = true;
                _switchResult_1 = ((JvmMember) cont).getDeclaringType();
            }
        }
        final JvmDeclaredType container = _switchResult_1;
        Pair<String, JvmDeclaredType> _mappedTo = Pair.<String, JvmDeclaredType>of("visibleFeaturesForAnnotationValues", container);
        final Provider<HashMap<String, JvmIdentifiableElement>> _function = () -> {
            final HashMap<String, JvmIdentifiableElement> result = CollectionLiterals.<String, JvmIdentifiableElement>newHashMap();
            Resource _eResource_1 = expression.eResource();
            final XImportSection section = this.importSectionLocator.getImportSection(((XtextResource) _eResource_1));
            if ((section != null)) {
                EList<XImportDeclaration> _importDeclarations = section.getImportDeclarations();
                for (final XImportDeclaration imp : _importDeclarations) {
                    boolean _isStatic = imp.isStatic();
                    if (_isStatic) {
                        final String importedTypeName = imp.getImportedTypeName();
                        if ((importedTypeName != null)) {
                            final JvmType type = this.findTypeByName(imp, importedTypeName);
                            boolean _matched_2 = false;
                            if (type instanceof JvmGenericType) {
                                _matched_2 = true;
                                this.collectAllVisibleFields(((JvmDeclaredType) type), result);
                            }
                            if (!_matched_2) {
                                if (type instanceof JvmEnumerationType) {
                                    _matched_2 = true;
                                    EList<JvmEnumerationLiteral> _literals = ((JvmEnumerationType) type).getLiterals();
                                    for (final JvmEnumerationLiteral feature : _literals) {
                                        result.put(feature.getSimpleName(), feature);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            this.collectAllVisibleFields(container, result);
            return result;
        };
        _xblockexpression = this.cache.<HashMap<String, JvmIdentifiableElement>>get(_mappedTo, expression.eResource(), _function);
    }
    return _xblockexpression;
}
Also used : JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XImportSection(org.eclipse.xtext.xtype.XImportSection) HashMap(java.util.HashMap) Resource(org.eclipse.emf.ecore.resource.Resource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) XtextResource(org.eclipse.xtext.resource.XtextResource) TypeResource(org.eclipse.xtext.common.types.access.TypeResource) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) XtextResource(org.eclipse.xtext.resource.XtextResource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) JvmType(org.eclipse.xtext.common.types.JvmType) XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration) ProcessorInstanceForJvmTypeProvider(org.eclipse.xtend.core.macro.ProcessorInstanceForJvmTypeProvider) ILogicalContainerProvider(org.eclipse.xtext.xbase.jvmmodel.ILogicalContainerProvider) Provider(com.google.inject.Provider) IScopeProvider(org.eclipse.xtext.scoping.IScopeProvider) JvmEnumerationLiteral(org.eclipse.xtext.common.types.JvmEnumerationLiteral) JvmMember(org.eclipse.xtext.common.types.JvmMember) JvmEnumerationType(org.eclipse.xtext.common.types.JvmEnumerationType) Pair(org.eclipse.xtext.xbase.lib.Pair) XAnnotationElementValuePair(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotationElementValuePair)

Example 3 with JvmType

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

the class ConstantExpressionsInterpreter method _internalEvaluate.

protected Object _internalEvaluate(final XMemberFeatureCall it, final Context ctx) {
    Object _eGet = it.eGet(XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, this.isResolveProxies(it));
    final EObject feature = ((EObject) _eGet);
    boolean _eIsProxy = feature.eIsProxy();
    boolean _not = (!_eIsProxy);
    if (_not) {
        Object _switchResult = null;
        boolean _matched = false;
        if (feature instanceof JvmEnumerationLiteral) {
            _matched = true;
            _switchResult = feature;
        }
        if (!_matched) {
            if (feature instanceof JvmField) {
                _matched = true;
                _switchResult = this.evaluateField(it, ((JvmField) feature), ctx);
            }
        }
        if (!_matched) {
            if (feature instanceof JvmType) {
                _matched = true;
                _switchResult = this.toTypeReference(((JvmType) feature), 0);
            }
        }
        return _switchResult;
    }
    final String featureName = it.getConcreteSyntaxFeatureName();
    try {
        final Object receiver = this.evaluate(it.getMemberCallTarget(), ctx);
        boolean _matched_1 = false;
        if (receiver instanceof JvmTypeReference) {
            _matched_1 = true;
            JvmType _type = ((JvmTypeReference) receiver).getType();
            final JvmType type = _type;
            boolean _matched_2 = false;
            if (type instanceof JvmEnumerationType) {
                _matched_2 = true;
                final Function1<JvmEnumerationLiteral, Boolean> _function = (JvmEnumerationLiteral it_1) -> {
                    String _simpleName = it_1.getSimpleName();
                    return Boolean.valueOf(Objects.equal(_simpleName, featureName));
                };
                final JvmEnumerationLiteral enumValue = IterableExtensions.<JvmEnumerationLiteral>findFirst(((JvmEnumerationType) type).getLiterals(), _function);
                if ((enumValue == null)) {
                    String _simpleName = ((JvmTypeReference) receiver).getSimpleName();
                    String _plus = ((("Couldn\'t find enum value " + featureName) + " on enum ") + _simpleName);
                    throw new ConstantExpressionEvaluationException(_plus, it);
                }
                this.resolveFeature(it, enumValue);
                return enumValue;
            }
            if (!_matched_2) {
                if (type instanceof JvmGenericType) {
                    _matched_2 = true;
                    final Function1<JvmField, Boolean> _function = (JvmField it_1) -> {
                        String _simpleName = it_1.getSimpleName();
                        return Boolean.valueOf(Objects.equal(_simpleName, featureName));
                    };
                    final JvmField field = IterableExtensions.<JvmField>findFirst(Iterables.<JvmField>filter(((JvmGenericType) type).getAllFeatures(), JvmField.class), _function);
                    if ((field == null)) {
                        String _simpleName = ((JvmTypeReference) receiver).getSimpleName();
                        String _plus = ((("Couldn\'t find field " + featureName) + " on type ") + _simpleName);
                        throw new ConstantExpressionEvaluationException(_plus, it);
                    }
                    this.resolveFeature(it, field);
                    return this.evaluateField(it, field, ctx);
                }
            }
        }
        throw new UnresolvableFeatureException(((("Unresolvable feature " + featureName) + " on ") + receiver), it);
    } catch (final Throwable _t) {
        if (_t instanceof UnresolvableFeatureException) {
            final String typeName = this.getFullName(it);
            final JvmType type = this.findTypeByName(it, typeName);
            if ((type != null)) {
                this.resolveType(it, type);
                return this.toTypeReference(type, 0);
            } else {
                throw new UnresolvableFeatureException(("Unresolvable type " + typeName), it);
            }
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    }
}
Also used : JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) UnresolvableFeatureException(org.eclipse.xtext.xbase.interpreter.UnresolvableFeatureException) JvmType(org.eclipse.xtext.common.types.JvmType) ConstantExpressionEvaluationException(org.eclipse.xtext.xbase.interpreter.ConstantExpressionEvaluationException) StackedConstantExpressionEvaluationException(org.eclipse.xtext.xbase.interpreter.StackedConstantExpressionEvaluationException) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) JvmEnumerationLiteral(org.eclipse.xtext.common.types.JvmEnumerationLiteral) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) JvmField(org.eclipse.xtext.common.types.JvmField) JvmEnumerationType(org.eclipse.xtext.common.types.JvmEnumerationType)

Example 4 with JvmType

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

the class AnnotationReferenceBuildContextImpl method findOperation.

protected JvmOperation findOperation(final String name, final boolean mustBeArray) {
    final JvmOperation result = this.findOperation(name);
    JvmTypeReference _returnType = result.getReturnType();
    JvmType _type = null;
    if (_returnType != null) {
        _type = _returnType.getType();
    }
    final JvmType returnType = _type;
    boolean _and = false;
    if (!mustBeArray) {
        _and = false;
    } else {
        EClass _eClass = null;
        if (returnType != null) {
            _eClass = returnType.eClass();
        }
        boolean _notEquals = (!Objects.equal(_eClass, TypesPackage.Literals.JVM_ARRAY_TYPE));
        _and = _notEquals;
    }
    if (_and) {
        throw new IllegalArgumentException("Cannot assign array value to simple annotation property");
    }
    return result;
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) EClass(org.eclipse.emf.ecore.EClass) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmType(org.eclipse.xtext.common.types.JvmType)

Example 5 with JvmType

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

the class AbstractScope method getElements.

@Override
public final Iterable<IEObjectDescription> getElements(EObject object) {
    if (!(object instanceof JvmType) || object.eIsProxy()) {
        throw new IllegalArgumentException(String.valueOf(object));
    }
    List<IEObjectDescription> result = Lists.newLinkedList();
    doGetElements((JvmType) object, result);
    Iterator<IEObjectDescription> iterator = result.iterator();
    while (iterator.hasNext()) {
        IEObjectDescription description = iterator.next();
        IEObjectDescription lookUp = getSingleElement(description.getName());
        if (lookUp == null || lookUp.getEObjectOrProxy() != object) {
            iterator.remove();
        }
    }
    return result;
}
Also used : JvmType(org.eclipse.xtext.common.types.JvmType) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Aggregations

JvmType (org.eclipse.xtext.common.types.JvmType)73 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)29 EObject (org.eclipse.emf.ecore.EObject)18 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)17 Test (org.junit.Test)17 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)14 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)12 XExpression (org.eclipse.xtext.xbase.XExpression)12 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)11 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)11 JvmAnnotationType (org.eclipse.xtext.common.types.JvmAnnotationType)10 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)9 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)9 AnonymousClass (org.eclipse.xtend.core.xtend.AnonymousClass)8 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)8 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)8 Resource (org.eclipse.emf.ecore.resource.Resource)7 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)7 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)6 JvmEnumerationType (org.eclipse.xtext.common.types.JvmEnumerationType)6