Search in sources :

Example 1 with JvmEnumerationType

use of org.eclipse.xtext.common.types.JvmEnumerationType 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 2 with JvmEnumerationType

use of org.eclipse.xtext.common.types.JvmEnumerationType 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 3 with JvmEnumerationType

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

the class CompilationUnitImpl method toTypeDeclaration.

public TypeDeclaration toTypeDeclaration(final JvmDeclaredType delegate) {
    final Function1<JvmDeclaredType, TypeDeclaration> _function = (JvmDeclaredType it) -> {
        JvmTypeDeclarationImpl<? extends JvmDeclaredType> _switchResult = null;
        boolean _matched = false;
        if (delegate instanceof JvmGenericType) {
            boolean _isInterface = ((JvmGenericType) delegate).isInterface();
            if (_isInterface) {
                _matched = true;
                JvmInterfaceDeclarationImpl _xifexpression = null;
                boolean _isBelongedToCompilationUnit = this.isBelongedToCompilationUnit(delegate);
                if (_isBelongedToCompilationUnit) {
                    MutableJvmInterfaceDeclarationImpl _mutableJvmInterfaceDeclarationImpl = new MutableJvmInterfaceDeclarationImpl();
                    final Procedure1<MutableJvmInterfaceDeclarationImpl> _function_1 = (MutableJvmInterfaceDeclarationImpl it_1) -> {
                        it_1.setDelegate(((JvmGenericType) delegate));
                        it_1.setCompilationUnit(this);
                    };
                    _xifexpression = ObjectExtensions.<MutableJvmInterfaceDeclarationImpl>operator_doubleArrow(_mutableJvmInterfaceDeclarationImpl, _function_1);
                } else {
                    JvmInterfaceDeclarationImpl _jvmInterfaceDeclarationImpl = new JvmInterfaceDeclarationImpl();
                    final Procedure1<JvmInterfaceDeclarationImpl> _function_2 = (JvmInterfaceDeclarationImpl it_1) -> {
                        it_1.setDelegate(((JvmGenericType) delegate));
                        it_1.setCompilationUnit(this);
                    };
                    _xifexpression = ObjectExtensions.<JvmInterfaceDeclarationImpl>operator_doubleArrow(_jvmInterfaceDeclarationImpl, _function_2);
                }
                _switchResult = _xifexpression;
            }
        }
        if (!_matched) {
            if (delegate instanceof JvmGenericType) {
                _matched = true;
                JvmClassDeclarationImpl _xifexpression = null;
                boolean _isBelongedToCompilationUnit = this.isBelongedToCompilationUnit(delegate);
                if (_isBelongedToCompilationUnit) {
                    MutableJvmClassDeclarationImpl _mutableJvmClassDeclarationImpl = new MutableJvmClassDeclarationImpl();
                    final Procedure1<MutableJvmClassDeclarationImpl> _function_1 = (MutableJvmClassDeclarationImpl it_1) -> {
                        it_1.setDelegate(((JvmGenericType) delegate));
                        it_1.setCompilationUnit(this);
                    };
                    _xifexpression = ObjectExtensions.<MutableJvmClassDeclarationImpl>operator_doubleArrow(_mutableJvmClassDeclarationImpl, _function_1);
                } else {
                    JvmClassDeclarationImpl _jvmClassDeclarationImpl = new JvmClassDeclarationImpl();
                    final Procedure1<JvmClassDeclarationImpl> _function_2 = (JvmClassDeclarationImpl it_1) -> {
                        it_1.setDelegate(((JvmGenericType) delegate));
                        it_1.setCompilationUnit(this);
                    };
                    _xifexpression = ObjectExtensions.<JvmClassDeclarationImpl>operator_doubleArrow(_jvmClassDeclarationImpl, _function_2);
                }
                _switchResult = _xifexpression;
            }
        }
        if (!_matched) {
            if (delegate instanceof JvmAnnotationType) {
                _matched = true;
                JvmAnnotationTypeDeclarationImpl _xifexpression = null;
                boolean _isBelongedToCompilationUnit = this.isBelongedToCompilationUnit(delegate);
                if (_isBelongedToCompilationUnit) {
                    MutableJvmAnnotationTypeDeclarationImpl _mutableJvmAnnotationTypeDeclarationImpl = new MutableJvmAnnotationTypeDeclarationImpl();
                    final Procedure1<MutableJvmAnnotationTypeDeclarationImpl> _function_1 = (MutableJvmAnnotationTypeDeclarationImpl it_1) -> {
                        it_1.setDelegate(((JvmAnnotationType) delegate));
                        it_1.setCompilationUnit(this);
                    };
                    _xifexpression = ObjectExtensions.<MutableJvmAnnotationTypeDeclarationImpl>operator_doubleArrow(_mutableJvmAnnotationTypeDeclarationImpl, _function_1);
                } else {
                    JvmAnnotationTypeDeclarationImpl _jvmAnnotationTypeDeclarationImpl = new JvmAnnotationTypeDeclarationImpl();
                    final Procedure1<JvmAnnotationTypeDeclarationImpl> _function_2 = (JvmAnnotationTypeDeclarationImpl it_1) -> {
                        it_1.setDelegate(((JvmAnnotationType) delegate));
                        it_1.setCompilationUnit(this);
                    };
                    _xifexpression = ObjectExtensions.<JvmAnnotationTypeDeclarationImpl>operator_doubleArrow(_jvmAnnotationTypeDeclarationImpl, _function_2);
                }
                _switchResult = _xifexpression;
            }
        }
        if (!_matched) {
            if (delegate instanceof JvmEnumerationType) {
                _matched = true;
                JvmEnumerationTypeDeclarationImpl _xifexpression = null;
                boolean _isBelongedToCompilationUnit = this.isBelongedToCompilationUnit(delegate);
                if (_isBelongedToCompilationUnit) {
                    MutableJvmEnumerationTypeDeclarationImpl _mutableJvmEnumerationTypeDeclarationImpl = new MutableJvmEnumerationTypeDeclarationImpl();
                    final Procedure1<MutableJvmEnumerationTypeDeclarationImpl> _function_1 = (MutableJvmEnumerationTypeDeclarationImpl it_1) -> {
                        it_1.setDelegate(((JvmEnumerationType) delegate));
                        it_1.setCompilationUnit(this);
                    };
                    _xifexpression = ObjectExtensions.<MutableJvmEnumerationTypeDeclarationImpl>operator_doubleArrow(_mutableJvmEnumerationTypeDeclarationImpl, _function_1);
                } else {
                    JvmEnumerationTypeDeclarationImpl _jvmEnumerationTypeDeclarationImpl = new JvmEnumerationTypeDeclarationImpl();
                    final Procedure1<JvmEnumerationTypeDeclarationImpl> _function_2 = (JvmEnumerationTypeDeclarationImpl it_1) -> {
                        it_1.setDelegate(((JvmEnumerationType) delegate));
                        it_1.setCompilationUnit(this);
                    };
                    _xifexpression = ObjectExtensions.<JvmEnumerationTypeDeclarationImpl>operator_doubleArrow(_jvmEnumerationTypeDeclarationImpl, _function_2);
                }
                _switchResult = _xifexpression;
            }
        }
        return ((TypeDeclaration) _switchResult);
    };
    return this.<JvmDeclaredType, TypeDeclaration>getOrCreate(delegate, _function);
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) JvmAnnotationTypeDeclarationImpl(org.eclipse.xtend.core.macro.declaration.JvmAnnotationTypeDeclarationImpl) MutableJvmAnnotationTypeDeclarationImpl(org.eclipse.xtend.core.macro.declaration.MutableJvmAnnotationTypeDeclarationImpl) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) MutableJvmInterfaceDeclarationImpl(org.eclipse.xtend.core.macro.declaration.MutableJvmInterfaceDeclarationImpl) MutableJvmAnnotationTypeDeclarationImpl(org.eclipse.xtend.core.macro.declaration.MutableJvmAnnotationTypeDeclarationImpl) JvmTypeDeclarationImpl(org.eclipse.xtend.core.macro.declaration.JvmTypeDeclarationImpl) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) JvmClassDeclarationImpl(org.eclipse.xtend.core.macro.declaration.JvmClassDeclarationImpl) MutableJvmClassDeclarationImpl(org.eclipse.xtend.core.macro.declaration.MutableJvmClassDeclarationImpl) MutableJvmInterfaceDeclarationImpl(org.eclipse.xtend.core.macro.declaration.MutableJvmInterfaceDeclarationImpl) JvmInterfaceDeclarationImpl(org.eclipse.xtend.core.macro.declaration.JvmInterfaceDeclarationImpl) MutableJvmClassDeclarationImpl(org.eclipse.xtend.core.macro.declaration.MutableJvmClassDeclarationImpl) JvmEnumerationType(org.eclipse.xtext.common.types.JvmEnumerationType) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) TypeDeclaration(org.eclipse.xtend.lib.macro.declaration.TypeDeclaration) MutableJvmEnumerationTypeDeclarationImpl(org.eclipse.xtend.core.macro.declaration.MutableJvmEnumerationTypeDeclarationImpl) MutableJvmEnumerationTypeDeclarationImpl(org.eclipse.xtend.core.macro.declaration.MutableJvmEnumerationTypeDeclarationImpl) JvmEnumerationTypeDeclarationImpl(org.eclipse.xtend.core.macro.declaration.JvmEnumerationTypeDeclarationImpl)

Example 4 with JvmEnumerationType

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

the class AnnotationValidation method isValidAnnotationValueType.

public boolean isValidAnnotationValueType(final JvmTypeReference reference) {
    JvmTypeReference _switchResult = null;
    boolean _matched = false;
    if (reference instanceof JvmGenericArrayTypeReference) {
        _matched = true;
        _switchResult = ((JvmGenericArrayTypeReference) reference).getComponentType();
    }
    if (!_matched) {
        _switchResult = reference;
    }
    final JvmTypeReference toCheck = _switchResult;
    if ((toCheck == null)) {
        return true;
    }
    JvmType _type = toCheck.getType();
    if ((_type instanceof JvmPrimitiveType)) {
        return true;
    }
    JvmType _type_1 = toCheck.getType();
    if ((_type_1 instanceof JvmEnumerationType)) {
        return true;
    }
    JvmType _type_2 = toCheck.getType();
    if ((_type_2 instanceof JvmAnnotationType)) {
        return true;
    }
    if ((Objects.equal(toCheck.getType().getQualifiedName(), "java.lang.String") || Objects.equal(toCheck.getType().getQualifiedName(), "java.lang.Class"))) {
        return true;
    }
    return false;
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) JvmGenericArrayTypeReference(org.eclipse.xtext.common.types.JvmGenericArrayTypeReference) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmPrimitiveType(org.eclipse.xtext.common.types.JvmPrimitiveType) JvmType(org.eclipse.xtext.common.types.JvmType) JvmEnumerationType(org.eclipse.xtext.common.types.JvmEnumerationType)

Example 5 with JvmEnumerationType

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

the class JvmModelTests method testEnumImplicitSuperType.

@Test
public void testEnumImplicitSuperType() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("enum Foo {");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final JvmEnumerationType inferred = this._iXtendJvmAssociations.getInferredEnumerationType(this.enumeration(_builder.toString()));
        Assert.assertEquals(1, inferred.getSuperTypes().size());
        Assert.assertEquals("java.lang.Enum<Foo>", IterableExtensions.<JvmTypeReference>head(inferred.getSuperTypes()).getIdentifier());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) JvmEnumerationType(org.eclipse.xtext.common.types.JvmEnumerationType) Test(org.junit.Test)

Aggregations

JvmEnumerationType (org.eclipse.xtext.common.types.JvmEnumerationType)14 JvmAnnotationType (org.eclipse.xtext.common.types.JvmAnnotationType)6 Test (org.junit.Test)6 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)5 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)5 JvmType (org.eclipse.xtext.common.types.JvmType)5 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)5 JvmEnumerationLiteral (org.eclipse.xtext.common.types.JvmEnumerationLiteral)4 JvmMember (org.eclipse.xtext.common.types.JvmMember)3 HashMap (java.util.HashMap)2 EObject (org.eclipse.emf.ecore.EObject)2 InternalEObject (org.eclipse.emf.ecore.InternalEObject)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 XtendEnum (org.eclipse.xtend.core.xtend.XtendEnum)2 JvmArrayType (org.eclipse.xtext.common.types.JvmArrayType)2 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)2 JvmField (org.eclipse.xtext.common.types.JvmField)2 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)2 UnresolvableFeatureException (org.eclipse.xtext.xbase.interpreter.UnresolvableFeatureException)2 Provider (com.google.inject.Provider)1