Search in sources :

Example 11 with JvmType

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

the class XtendValidator method checkDefaultSuperConstructor.

@Check
public void checkDefaultSuperConstructor(XtendClass xtendClass) {
    JvmGenericType inferredType = associations.getInferredType(xtendClass);
    if (inferredType == null)
        return;
    Iterable<JvmConstructor> constructors = filter(inferredType.getMembers(), JvmConstructor.class);
    if (inferredType.getExtendedClass() != null) {
        JvmType superType = inferredType.getExtendedClass().getType();
        if (superType instanceof JvmGenericType) {
            Iterable<JvmConstructor> superConstructors = ((JvmGenericType) superType).getDeclaredConstructors();
            for (JvmConstructor superConstructor : superConstructors) {
                if (superConstructor.getParameters().isEmpty())
                    // there is a default super constructor. nothing more to check
                    return;
            }
            if (size(constructors) == 1 && typeExtensions.isSingleSyntheticDefaultConstructor(constructors.iterator().next())) {
                List<String> issueData = newArrayList();
                for (JvmConstructor superConstructor : superConstructors) {
                    issueData.add(EcoreUtil.getURI(superConstructor).toString());
                    issueData.add(doGetReadableSignature(xtendClass.getName(), superConstructor.getParameters()));
                }
                error("No default constructor in super type " + superType.getSimpleName() + "." + xtendClass.getName() + " must define an explicit constructor.", xtendClass, XTEND_TYPE_DECLARATION__NAME, MISSING_CONSTRUCTOR, toArray(issueData, String.class));
            } else {
                for (JvmConstructor constructor : constructors) {
                    XExpression expression = containerProvider.getAssociatedExpression(constructor);
                    if (expression instanceof XBlockExpression) {
                        List<XExpression> expressions = ((XBlockExpression) expression).getExpressions();
                        if (expressions.isEmpty() || !isDelegateConstructorCall(expressions.get(0))) {
                            EObject source = associations.getPrimarySourceElement(constructor);
                            error("No default constructor in super type " + superType.getSimpleName() + ". Another constructor must be invoked explicitly.", source, null, MUST_INVOKE_SUPER_CONSTRUCTOR);
                        }
                    }
                }
            }
        }
    }
}
Also used : XBlockExpression(org.eclipse.xtext.xbase.XBlockExpression) EObject(org.eclipse.emf.ecore.EObject) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) XExpression(org.eclipse.xtext.xbase.XExpression) JvmType(org.eclipse.xtext.common.types.JvmType) RichString(org.eclipse.xtend.core.xtend.RichString) Check(org.eclipse.xtext.validation.Check)

Example 12 with JvmType

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

the class XtendValidator method checkMemberNamesAreUnique.

@Check
public void checkMemberNamesAreUnique(XtendTypeDeclaration xtendType) {
    final Multimap<String, XtendField> name2field = HashMultimap.create();
    final Multimap<String, XtendTypeDeclaration> name2type = HashMultimap.create();
    final Multimap<JvmType, XtendField> type2extension = HashMultimap.create();
    for (XtendMember member : xtendType.getMembers()) {
        if (member instanceof XtendField) {
            XtendField field = (XtendField) member;
            if (isEmpty(field.getName())) {
                if (field.isExtension()) {
                    JvmTypeReference typeReference = field.getType();
                    if (typeReference != null) {
                        JvmType type = typeReference.getType();
                        if (type != null)
                            type2extension.put(type, field);
                    }
                }
            } else {
                name2field.put(field.getName(), field);
            }
        } else if (member instanceof XtendTypeDeclaration) {
            String name = ((XtendTypeDeclaration) member).getName();
            if (name != null && name.length() > 0) {
                name2type.put(name, (XtendTypeDeclaration) member);
            }
        }
    }
    for (String name : name2field.keySet()) {
        Collection<XtendField> fields = name2field.get(name);
        if (fields.size() > 1) {
            for (XtendField field : fields) error("Duplicate field " + name, field, XtendPackage.Literals.XTEND_FIELD__NAME, DUPLICATE_FIELD);
        }
    }
    for (String name : name2type.keySet()) {
        Collection<XtendTypeDeclaration> types = name2type.get(name);
        if (types.size() > 1) {
            for (XtendTypeDeclaration type : types) error("Duplicate nested type " + name, type, XtendPackage.Literals.XTEND_TYPE_DECLARATION__NAME, DUPLICATE_TYPE_NAME);
        }
    }
    for (JvmType type : type2extension.keySet()) {
        Collection<XtendField> fields = type2extension.get(type);
        if (fields.size() > 1) {
            for (XtendField field : fields) error("Duplicate extension with same type", field, XTEND_FIELD__TYPE, DUPLICATE_FIELD);
        }
    }
}
Also used : XtendMember(org.eclipse.xtend.core.xtend.XtendMember) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) RichString(org.eclipse.xtend.core.xtend.RichString) JvmType(org.eclipse.xtext.common.types.JvmType) XtendField(org.eclipse.xtend.core.xtend.XtendField) Check(org.eclipse.xtext.validation.Check)

Example 13 with JvmType

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

the class XtendAnnotationReferenceImpl method getAnnotationTypeDeclaration.

@Override
public AnnotationTypeDeclaration getAnnotationTypeDeclaration() {
    AnnotationTypeDeclaration _switchResult = null;
    JvmType _annotationType = this.getAnnotationType();
    final JvmType type = _annotationType;
    boolean _matched = false;
    if (type instanceof JvmAnnotationType) {
        _matched = true;
        TypeDeclaration _typeDeclaration = this.getCompilationUnit().toTypeDeclaration(((JvmDeclaredType) type));
        _switchResult = ((AnnotationTypeDeclaration) _typeDeclaration);
    }
    if (!_matched) {
        _switchResult = null;
    }
    return _switchResult;
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) AnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmType(org.eclipse.xtext.common.types.JvmType) AnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration) TypeDeclaration(org.eclipse.xtend.lib.macro.declaration.TypeDeclaration)

Example 14 with JvmType

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

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

the class XtendHighlightingCalculator method highlightAnnotation.

@Override
protected void highlightAnnotation(XAnnotation annotation, IHighlightedPositionAcceptor acceptor) {
    JvmType annotationType = annotation.getAnnotationType();
    if (annotationType instanceof JvmAnnotationTarget) {
        for (JvmAnnotationReference annotationReference : ((JvmAnnotationTarget) annotationType).getAnnotations()) {
            JvmAnnotationType otherAnnotation = annotationReference.getAnnotation();
            if (otherAnnotation != null && !otherAnnotation.eIsProxy() && Active.class.getName().equals(otherAnnotation.getIdentifier())) {
                highlightAnnotation(annotation, acceptor, ACTIVE_ANNOTATION);
                return;
            }
        }
    }
    super.highlightAnnotation(annotation, acceptor);
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) JvmAnnotationTarget(org.eclipse.xtext.common.types.JvmAnnotationTarget) JvmType(org.eclipse.xtext.common.types.JvmType) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference)

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