Search in sources :

Example 31 with JvmTypeReference

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

the class XtendJvmModelInferrer method initialize.

protected void initialize(XtendClass source, JvmGenericType inferredJvmType) {
    inferredJvmType.setVisibility(source.getVisibility());
    boolean isStatic = source.isStatic() && !isTopLevel(source);
    if (!isStatic) {
        JvmDeclaredType declaringType = inferredJvmType.getDeclaringType();
        if (declaringType instanceof JvmGenericType) {
            if (((JvmGenericType) declaringType).isInterface())
                isStatic = true;
        } else if (declaringType instanceof JvmAnnotationType) {
            isStatic = true;
        }
    }
    inferredJvmType.setStatic(isStatic);
    inferredJvmType.setAbstract(source.isAbstract());
    inferredJvmType.setStrictFloatingPoint(source.isStrictFloatingPoint());
    if (!inferredJvmType.isAbstract()) {
        inferredJvmType.setFinal(source.isFinal());
    }
    translateAnnotationsTo(source.getAnnotations(), inferredJvmType);
    JvmTypeReference extendsClause = source.getExtends();
    if (extendsClause == null || extendsClause.getType() == null) {
        JvmTypeReference typeRefToObject = typeReferences.getTypeForName(Object.class, source);
        if (typeRefToObject != null)
            inferredJvmType.getSuperTypes().add(typeRefToObject);
    } else {
        inferredJvmType.getSuperTypes().add(jvmTypesBuilder.cloneWithProxies(extendsClause));
    }
    for (JvmTypeReference intf : source.getImplements()) {
        inferredJvmType.getSuperTypes().add(jvmTypesBuilder.cloneWithProxies(intf));
    }
    fixTypeParameters(inferredJvmType);
    addDefaultConstructor(source, inferredJvmType);
    for (XtendMember member : source.getMembers()) {
        if (member instanceof XtendField || (member instanceof XtendFunction && ((XtendFunction) member).getName() != null) || member instanceof XtendConstructor) {
            transform(member, inferredJvmType, true);
        }
    }
    appendSyntheticDispatchMethods(source, inferredJvmType);
    jvmTypesBuilder.copyDocumentationTo(source, inferredJvmType);
    nameClashResolver.resolveNameClashes(inferredJvmType);
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) XtendConstructor(org.eclipse.xtend.core.xtend.XtendConstructor) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) XtendField(org.eclipse.xtend.core.xtend.XtendField)

Example 32 with JvmTypeReference

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

the class XtendJvmModelInferrer method deriveGenericDispatchOperationSignature.

/**
 * @return a {@link JvmOperation} with common denominator argument types of all given operations
 */
/* @Nullable */
protected JvmOperation deriveGenericDispatchOperationSignature(Iterable<JvmOperation> localOperations, JvmGenericType target) {
    final Iterator<JvmOperation> iterator = localOperations.iterator();
    if (!iterator.hasNext())
        return null;
    JvmOperation first = iterator.next();
    JvmOperation result = typesFactory.createJvmOperation();
    target.getMembers().add(result);
    for (int i = 0; i < first.getParameters().size(); i++) {
        JvmFormalParameter parameter = typesFactory.createJvmFormalParameter();
        result.getParameters().add(parameter);
        parameter.setParameterType(jvmTypesBuilder.inferredType());
        JvmFormalParameter parameter2 = first.getParameters().get(i);
        parameter.setName(parameter2.getName());
    }
    jvmTypesBuilder.setBody(result, compileStrategies.forDispatcher(result));
    JvmVisibility commonVisibility = null;
    boolean isFirst = true;
    boolean allStatic = true;
    boolean override = false;
    for (JvmOperation jvmOperation : localOperations) {
        Iterable<XtendFunction> xtendFunctions = Iterables.filter(associations.getSourceElements(jvmOperation), XtendFunction.class);
        for (XtendFunction func : xtendFunctions) {
            JvmVisibility xtendVisibility = func.getDeclaredVisibility();
            if (isFirst) {
                commonVisibility = xtendVisibility;
                isFirst = false;
            } else if (commonVisibility != xtendVisibility) {
                commonVisibility = null;
            }
            associator.associate(func, result);
            if (!func.isStatic())
                allStatic = false;
            if (func.isOverride())
                override = true;
        }
        for (JvmTypeReference declaredException : jvmOperation.getExceptions()) result.getExceptions().add(jvmTypesBuilder.cloneWithProxies(declaredException));
    }
    if (commonVisibility == null)
        result.setVisibility(JvmVisibility.PUBLIC);
    else
        result.setVisibility(commonVisibility);
    result.setStatic(allStatic);
    if (override)
        setOverride(result);
    return result;
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmVisibility(org.eclipse.xtext.common.types.JvmVisibility) JvmTypeConstraint(org.eclipse.xtext.common.types.JvmTypeConstraint)

Example 33 with JvmTypeReference

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

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

the class XtendValidator method checkNonRawTypeInferred.

@Check
public void checkNonRawTypeInferred(XtendFunction function) {
    if (function.getReturnType() == null) {
        JvmOperation operation = associations.getDirectlyInferredOperation(function);
        // operation could have been removed by AA, thus the resource is possibly null
        if (operation != null && operation.eResource() != null) {
            JvmTypeReference returnType = operation.getReturnType();
            validateInferredType(returnType, function, "The inferred return type ", XTEND_FUNCTION__NAME);
        }
    }
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) Check(org.eclipse.xtext.validation.Check)

Example 35 with JvmTypeReference

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

the class XtendValidator method getReadableSignature.

protected String getReadableSignature(String elementName, List<JvmTypeReference> parameterTypes) {
    StringBuilder result = new StringBuilder(elementName);
    result.append('(');
    for (int i = 0; i < parameterTypes.size(); i++) {
        if (i != 0) {
            result.append(", ");
        }
        JvmTypeReference parameterType = parameterTypes.get(i);
        if (parameterType != null)
            result.append(parameterType.getSimpleName());
        else
            result.append("null");
    }
    result.append(')');
    return result.toString();
}
Also used : ToStringBuilder(org.eclipse.xtext.xbase.lib.util.ToStringBuilder) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference)

Aggregations

JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)194 Test (org.junit.Test)98 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)68 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)47 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)46 JvmType (org.eclipse.xtext.common.types.JvmType)43 JvmParameterizedTypeReference (org.eclipse.xtext.common.types.JvmParameterizedTypeReference)41 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)36 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)27 XExpression (org.eclipse.xtext.xbase.XExpression)22 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)18 JvmUpperBound (org.eclipse.xtext.common.types.JvmUpperBound)18 EObject (org.eclipse.emf.ecore.EObject)17 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)17 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)16 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)15 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)14 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)12 JvmField (org.eclipse.xtext.common.types.JvmField)12 JvmWildcardTypeReference (org.eclipse.xtext.common.types.JvmWildcardTypeReference)12