Search in sources :

Example 6 with JvmOperation

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

the class MutableJvmAnnotationTypeDeclarationImpl method addAnnotationTypeElement.

@Override
public MutableAnnotationTypeElementDeclaration addAnnotationTypeElement(final String name, final Procedure1<MutableAnnotationTypeElementDeclaration> initializer) {
    this.checkMutable();
    ConditionUtils.checkJavaIdentifier(name, "name");
    Preconditions.checkArgument((initializer != null), "initializer cannot be null");
    final JvmOperation newAnnotationElement = TypesFactory.eINSTANCE.createJvmOperation();
    newAnnotationElement.setSimpleName(name);
    newAnnotationElement.setVisibility(JvmVisibility.PUBLIC);
    this.getDelegate().getMembers().add(newAnnotationElement);
    MemberDeclaration _memberDeclaration = this.getCompilationUnit().toMemberDeclaration(newAnnotationElement);
    final MutableAnnotationTypeElementDeclaration mutableAnnotationTypeElementDeclaration = ((MutableAnnotationTypeElementDeclaration) _memberDeclaration);
    initializer.apply(mutableAnnotationTypeElementDeclaration);
    return mutableAnnotationTypeElementDeclaration;
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) MutableAnnotationTypeElementDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableAnnotationTypeElementDeclaration) MutableMemberDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMemberDeclaration) MemberDeclaration(org.eclipse.xtend.lib.macro.declaration.MemberDeclaration)

Example 7 with JvmOperation

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

the class XtendJvmModelInferrer method transformCreateExtension.

protected void transformCreateExtension(XtendFunction source, CreateExtensionInfo createExtensionInfo, JvmGenericType container, JvmOperation operation, /* @Nullable */
JvmTypeReference returnType) {
    JvmField cacheVar = jvmTypesBuilder.toField(source, CREATE_CHACHE_VARIABLE_PREFIX + source.getName(), jvmTypesBuilder.inferredType());
    if (cacheVar != null) {
        cacheVar.setFinal(true);
        jvmTypesBuilder.setInitializer(cacheVar, compileStrategies.forCacheVariable(source));
        container.getMembers().add(cacheVar);
        JvmOperation initializer = typesFactory.createJvmOperation();
        container.getMembers().add(initializer);
        initializer.setSimpleName(CREATE_INITIALIZER_PREFIX + source.getName());
        initializer.setVisibility(JvmVisibility.PRIVATE);
        initializer.setReturnType(typeReferences.getTypeForName(Void.TYPE, source));
        for (JvmTypeReference exception : source.getExceptions()) {
            initializer.getExceptions().add(jvmTypesBuilder.cloneWithProxies(exception));
        }
        jvmTypesBuilder.setBody(operation, compileStrategies.forCacheMethod(createExtensionInfo, cacheVar, initializer));
        // the first parameter is the created object
        JvmFormalParameter jvmParam = typesFactory.createJvmFormalParameter();
        jvmParam.setName(createExtensionInfo.getName());
        // TODO consider type parameters
        jvmParam.setParameterType(jvmTypesBuilder.inferredType());
        initializer.getParameters().add(jvmParam);
        associator.associate(createExtensionInfo, jvmParam);
        // add all others
        for (XtendParameter parameter : source.getParameters()) {
            jvmParam = typesFactory.createJvmFormalParameter();
            jvmParam.setName(parameter.getName());
            jvmParam.setParameterType(jvmTypesBuilder.cloneWithProxies(parameter.getParameterType()));
            initializer.getParameters().add(jvmParam);
            associator.associate(parameter, jvmParam);
        }
        associator.associate(source, initializer);
        setBody(operation, createExtensionInfo.getCreateExpression());
        setBody(initializer, source.getExpression());
    }
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XtendParameter(org.eclipse.xtend.core.xtend.XtendParameter) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmField(org.eclipse.xtext.common.types.JvmField)

Example 8 with JvmOperation

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

the class XtendJvmModelInferrer method initialize.

protected void initialize(XtendAnnotationType source, JvmAnnotationType inferredJvmType) {
    inferredJvmType.setVisibility(source.getVisibility());
    inferredJvmType.setStatic(source.isStatic() && !isTopLevel(source));
    inferredJvmType.setAbstract(true);
    translateAnnotationsTo(source.getAnnotations(), inferredJvmType);
    jvmTypesBuilder.copyDocumentationTo(source, inferredJvmType);
    for (XtendMember member : source.getMembers()) {
        if (member instanceof XtendField) {
            XtendField field = (XtendField) member;
            if (!Strings.isEmpty(field.getName())) {
                JvmOperation operation = typesFactory.createJvmOperation();
                associator.associatePrimary(member, operation);
                operation.setSimpleName(field.getName());
                JvmTypeReference returnType = null;
                XExpression initialValue = field.getInitialValue();
                if (field.getType() != null) {
                    returnType = jvmTypesBuilder.cloneWithProxies(field.getType());
                } else if (initialValue != null) {
                    returnType = jvmTypesBuilder.inferredType(initialValue);
                }
                operation.setReturnType(returnType);
                if (initialValue != null) {
                    JvmAnnotationValue jvmAnnotationValue = jvmTypesBuilder.toJvmAnnotationValue(initialValue);
                    if (jvmAnnotationValue != null) {
                        operation.setDefaultValue(jvmAnnotationValue);
                        jvmAnnotationValue.setOperation(operation);
                    }
                    jvmTypesBuilder.setBody(operation, initialValue);
                }
                operation.setVisibility(JvmVisibility.PUBLIC);
                translateAnnotationsTo(member.getAnnotations(), operation);
                jvmTypesBuilder.copyDocumentationTo(member, operation);
                inferredJvmType.getMembers().add(operation);
            }
        }
    }
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) XExpression(org.eclipse.xtext.xbase.XExpression) XtendField(org.eclipse.xtend.core.xtend.XtendField) JvmAnnotationValue(org.eclipse.xtext.common.types.JvmAnnotationValue)

Example 9 with JvmOperation

use of org.eclipse.xtext.common.types.JvmOperation 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;
    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;
        }
        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);
    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 10 with JvmOperation

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

the class XtendReentrantTypeResolver method getReturnTypeOfOverriddenOperation.

@Override
protected LightweightTypeReference getReturnTypeOfOverriddenOperation(JvmOperation operation, ResolvedTypes resolvedTypes, IFeatureScopeSession session) {
    if (operation.getVisibility() == JvmVisibility.PRIVATE)
        return null;
    if (InferredTypeIndicator.isInferred(operation.getReturnType())) {
        LightweightTypeReference declaringType = resolvedTypes.getActualType(operation.getDeclaringType());
        if (declaringType == null) {
            throw new IllegalStateException("Cannot determine declaring type of operation: " + operation);
        }
        BottomResolvedOperation resolvedOperation = new BottomResolvedOperation(operation, declaringType, overrideTester);
        List<IResolvedOperation> overriddenMethods = resolvedOperation.getOverriddenAndImplementedMethods();
        if (overriddenMethods.isEmpty())
            return null;
        IResolvedOperation overriddenMethod = overriddenMethods.get(0);
        JvmOperation declaration = overriddenMethod.getDeclaration();
        XExpression inferredFrom = getInferredFrom(declaration.getReturnType());
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=439535
        if (inferredFrom != null && (inferredFrom == getInferredFrom(operation.getReturnType()) || isHandled(inferredFrom))) {
            return null;
        }
        LightweightTypeReference result = overriddenMethod.getResolvedReturnType();
        return result;
    }
    return null;
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) XExpression(org.eclipse.xtext.xbase.XExpression) BottomResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.BottomResolvedOperation) IResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.IResolvedOperation)

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