Search in sources :

Example 46 with JvmConstructor

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

the class XtendReentrantTypeResolver method doPrepareLocalTypes.

protected void doPrepareLocalTypes(final ResolvedTypes resolvedTypes, IFeatureScopeSession featureScopeSession, JvmFeature container, Map<JvmIdentifiableElement, ResolvedTypes> resolvedTypesByContext) {
    List<JvmGenericType> localClasses = container.getLocalClasses();
    for (final JvmGenericType localClass : localClasses) {
        JvmTypeReference superType = localClass.getSuperTypes().get(0);
        final IFeatureScopeSession nestedSession = featureScopeSession;
        if (InferredTypeIndicator.isInferred(superType)) {
            final XComputedTypeReference casted = (XComputedTypeReference) superType;
            InferredTypeIndicator typeProvider = (InferredTypeIndicator) casted.getTypeProvider();
            final AnonymousClass anonymousClass = (AnonymousClass) typeProvider.getExpression();
            XConstructorCall constructorCall = anonymousClass.getConstructorCall();
            IScope typeScope = featureScopeSession.getScope(constructorCall, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, resolvedTypes);
            final JvmDeclaredType type = anonymousClassUtil.getSuperTypeNonResolving(anonymousClass, typeScope);
            if (type == null) {
                JvmUnknownTypeReference superTypeReference = TypesFactory.eINSTANCE.createJvmUnknownTypeReference();
                requestCapturedLocalVariables(superTypeReference, localClass, resolvedTypes, resolvedTypesByContext, new IAcceptor<JvmTypeReference>() {

                    @Override
                    public void accept(JvmTypeReference capturingTypeReference) {
                        casted.setEquivalent(capturingTypeReference);
                        inferAnonymousClassConstructor(anonymousClass, localClass);
                    }
                });
            } else {
                final JvmParameterizedTypeReference superTypeReference = createSuperTypeReference(type, constructorCall);
                requestCapturedLocalVariables(superTypeReference, localClass, resolvedTypes, resolvedTypesByContext, new IAcceptor<JvmTypeReference>() {

                    @Override
                    @SuppressWarnings("deprecation")
                    public void accept(JvmTypeReference capturingTypeReference) {
                        casted.setEquivalent(capturingTypeReference);
                        IFeatureScopeSession mySession = addThisAndSuper(nestedSession, resolvedTypes.getReferenceOwner(), localClass, superTypeReference, false);
                        if (type.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE && ((JvmGenericType) type).isInterface()) {
                            localClass.getSuperTypes().add(0, typesBuilder.newTypeRef(localClass, Object.class));
                            inferAnonymousClassConstructor(anonymousClass, localClass);
                        } else {
                            for (JvmMember superMember : type.getMembers()) {
                                if (superMember instanceof JvmConstructor) {
                                    JvmConstructor superTypeConstructor = (JvmConstructor) superMember;
                                    boolean visible = mySession.isVisible(superTypeConstructor);
                                    inferAnonymousClassConstructor(anonymousClass, localClass, superTypeConstructor, visible);
                                }
                            }
                        }
                    }
                });
            }
        }
    }
}
Also used : IFeatureScopeSession(org.eclipse.xtext.xbase.scoping.batch.IFeatureScopeSession) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) XComputedTypeReference(org.eclipse.xtext.xtype.XComputedTypeReference) XConstructorCall(org.eclipse.xtext.xbase.XConstructorCall) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmUnknownTypeReference(org.eclipse.xtext.common.types.JvmUnknownTypeReference) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) InferredTypeIndicator(org.eclipse.xtext.xbase.typesystem.InferredTypeIndicator) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) IScope(org.eclipse.xtext.scoping.IScope) JvmMember(org.eclipse.xtext.common.types.JvmMember) JvmParameterizedTypeReference(org.eclipse.xtext.common.types.JvmParameterizedTypeReference)

Example 47 with JvmConstructor

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

the class XtendReentrantTypeResolver method inferAnonymousClassConstructor.

protected JvmConstructor inferAnonymousClassConstructor(AnonymousClass anonymousClass, JvmGenericType inferredLocalClass, JvmConstructor superConstructor, boolean visible) {
    JvmConstructor constructor = TypesFactory.eINSTANCE.createJvmConstructor();
    inferredLocalClass.getMembers().add(constructor);
    associator.associatePrimary(anonymousClass.getConstructorCall(), constructor);
    if (visible) {
        constructor.setVisibility(JvmVisibility.DEFAULT);
    } else
        constructor.setVisibility(JvmVisibility.PRIVATE);
    constructor.setSimpleName(inferredLocalClass.getSimpleName());
    constructor.setVarArgs(superConstructor.isVarArgs());
    final List<JvmFormalParameter> parameters = superConstructor.getParameters();
    for (JvmFormalParameter parameter : parameters) {
        // trigger name computation
        parameter.getName();
        constructor.getParameters().add(typesBuilder.cloneWithProxies(parameter));
    }
    for (JvmTypeReference exception : superConstructor.getExceptions()) constructor.getExceptions().add(typesBuilder.cloneWithProxies(exception));
    if (!parameters.isEmpty()) {
        typesBuilder.setBody(constructor, new Procedures.Procedure1<ITreeAppendable>() {

            @Override
            public void apply(ITreeAppendable a) {
                a.append("super(");
                for (int i = 0; i < parameters.size(); i++) {
                    if (i != 0) {
                        a.append(", ");
                    }
                    a.append(parameters.get(i).getSimpleName());
                }
                a.append(");");
            }
        });
    }
    return constructor;
}
Also used : JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) Procedures(org.eclipse.xtext.xbase.lib.Procedures) ITreeAppendable(org.eclipse.xtext.xbase.compiler.output.ITreeAppendable)

Example 48 with JvmConstructor

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

the class XtendGenerator method getAddedDeclarations.

protected ArrayList<JvmMember> getAddedDeclarations(final JvmGenericType it, final AnonymousClass anonymousClass) {
    final ArrayList<JvmMember> result = CollectionLiterals.<JvmMember>newArrayList();
    final JvmConstructor constructor = anonymousClass.getConstructorCall().getConstructor();
    int _size = constructor.getParameters().size();
    boolean _greaterEqualsThan = (_size >= 1);
    if (_greaterEqualsThan) {
        result.add(0, constructor);
    }
    Iterable<JvmField> _declaredFields = it.getDeclaredFields();
    Iterables.<JvmMember>addAll(result, _declaredFields);
    final Function1<JvmOperation, Boolean> _function = (JvmOperation it_1) -> {
        EObject _head = IterableExtensions.<EObject>head(this.getSourceElements(it_1));
        final XtendFunction function = ((XtendFunction) _head);
        boolean _isOverride = function.isOverride();
        return Boolean.valueOf((!_isOverride));
    };
    Iterable<JvmOperation> _filter = IterableExtensions.<JvmOperation>filter(it.getDeclaredOperations(), _function);
    Iterables.<JvmMember>addAll(result, _filter);
    Iterable<JvmDeclaredType> _filter_1 = Iterables.<JvmDeclaredType>filter(it.getMembers(), JvmDeclaredType.class);
    Iterables.<JvmMember>addAll(result, _filter_1);
    return result;
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) EObject(org.eclipse.emf.ecore.EObject) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) JvmMember(org.eclipse.xtext.common.types.JvmMember) JvmField(org.eclipse.xtext.common.types.JvmField)

Example 49 with JvmConstructor

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

the class XtendJvmModelInferrer method transform.

protected void transform(XtendConstructor source, JvmGenericType container) {
    JvmConstructor constructor = typesFactory.createJvmConstructor();
    container.getMembers().add(constructor);
    associator.associatePrimary(source, constructor);
    JvmVisibility visibility = source.getVisibility();
    constructor.setSimpleName(container.getSimpleName());
    constructor.setVisibility(visibility);
    for (XtendParameter parameter : source.getParameters()) {
        translateParameter(constructor, parameter);
    }
    copyAndFixTypeParameters(source.getTypeParameters(), constructor);
    for (JvmTypeReference exception : source.getExceptions()) {
        constructor.getExceptions().add(jvmTypesBuilder.cloneWithProxies(exception));
    }
    translateAnnotationsTo(source.getAnnotations(), constructor);
    setBody(constructor, source.getExpression());
    jvmTypesBuilder.copyDocumentationTo(source, constructor);
}
Also used : XtendParameter(org.eclipse.xtend.core.xtend.XtendParameter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) JvmVisibility(org.eclipse.xtext.common.types.JvmVisibility)

Example 50 with JvmConstructor

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

the class CreateMemberQuickfixes method newConstructorQuickfix.

protected void newConstructorQuickfix(Issue issue, IssueResolutionAcceptor issueResolutionAcceptor, XFeatureCall call) {
    JvmDeclaredType ownerType = ((JvmConstructor) call.getFeature()).getDeclaringType();
    newConstructorQuickfix(issue, issueResolutionAcceptor, ownerType, call, call.getActualArguments());
}
Also used : JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType)

Aggregations

JvmConstructor (org.eclipse.xtext.common.types.JvmConstructor)82 Test (org.junit.Test)42 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)33 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)18 JvmMember (org.eclipse.xtext.common.types.JvmMember)17 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)16 EObject (org.eclipse.emf.ecore.EObject)15 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)15 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)9 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)9 TestAnnotation (org.eclipse.xtext.common.types.testSetups.TestAnnotation)9 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)8 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)7 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)6 JvmField (org.eclipse.xtext.common.types.JvmField)6 XConstructorCall (org.eclipse.xtext.xbase.XConstructorCall)6 Predicate (com.google.common.base.Predicate)5 JvmAnnotationTarget (org.eclipse.xtext.common.types.JvmAnnotationTarget)5 JvmAnnotationType (org.eclipse.xtext.common.types.JvmAnnotationType)5 JvmEnumerationType (org.eclipse.xtext.common.types.JvmEnumerationType)5