Search in sources :

Example 1 with AnonymousClass

use of org.eclipse.xtend.core.xtend.AnonymousClass in project xtext-xtend by eclipse.

the class XtendGenerator method generateMembersInBody.

@Override
public ITreeAppendable generateMembersInBody(final JvmDeclaredType it, final ITreeAppendable appendable, final GeneratorConfig config) {
    ITreeAppendable _xifexpression = null;
    boolean _isLocal = it.isLocal();
    if (_isLocal) {
        ITreeAppendable _xblockexpression = null;
        {
            appendable.append("{").increaseIndentation();
            EObject _head = IterableExtensions.<EObject>head(this.getSourceElements(it));
            final AnonymousClass anonymousClass = ((AnonymousClass) _head);
            if (((!appendable.hasName(Pair.<String, JvmDeclaredType>of("this", it))) && this.needSyntheticThisVariable(anonymousClass, it))) {
                final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(anonymousClass);
                final LightweightTypeReference actualType = resolvedTypes.getActualType(anonymousClass);
                Pair<String, JvmDeclaredType> _mappedTo = Pair.<String, JvmDeclaredType>of("this", it);
                final String thisName = appendable.declareSyntheticVariable(_mappedTo, "_this");
                appendable.newLine().append("final ").append(actualType).append(" ").append(thisName).append(" = this;");
            }
            final Function1<JvmField, Boolean> _function = (JvmField it_1) -> {
                boolean _xblockexpression_1 = false;
                {
                    Procedure1<? super ITreeAppendable> _compilationStrategy = this._jvmTypeExtensions.getCompilationStrategy(it_1);
                    boolean _tripleNotEquals = (_compilationStrategy != null);
                    if (_tripleNotEquals) {
                        return Boolean.valueOf(true);
                    } else {
                        StringConcatenationClient _compilationTemplate = this._jvmTypeExtensions.getCompilationTemplate(it_1);
                        boolean _tripleNotEquals_1 = (_compilationTemplate != null);
                        if (_tripleNotEquals_1) {
                            return Boolean.valueOf(true);
                        } else {
                            boolean _not = (!(it_1.isFinal() && it_1.isStatic()));
                            if (_not) {
                                final XExpression expression = this._iLogicalContainerProvider.getAssociatedExpression(it_1);
                                if (((expression != null) && config.isGenerateExpressions())) {
                                    return Boolean.valueOf(true);
                                }
                            }
                        }
                    }
                    _xblockexpression_1 = false;
                }
                return Boolean.valueOf(_xblockexpression_1);
            };
            final Iterable<JvmField> fieldsWithInitializer = IterableExtensions.<JvmField>filter(it.getDeclaredFields(), _function);
            boolean _isEmpty = IterableExtensions.isEmpty(fieldsWithInitializer);
            boolean _not = (!_isEmpty);
            if (_not) {
                appendable.newLine().append("{").increaseIndentation();
                final Procedure1<LoopParams> _function_1 = (LoopParams it_1) -> {
                    final Function1<ITreeAppendable, ITreeAppendable> _function_2 = (ITreeAppendable it_2) -> {
                        return it_2.newLine();
                    };
                    it_1.setSeparator(_function_2);
                };
                final Procedure1<JvmField> _function_2 = (JvmField it_1) -> {
                    final ITreeAppendable memberAppendable = this._treeAppendableUtil.traceWithComments(appendable, it_1);
                    memberAppendable.openScope();
                    appendable.newLine();
                    final ITreeAppendable tracedAppendable = appendable.trace(it_1);
                    this._treeAppendableUtil.traceSignificant(tracedAppendable, it_1).append(it_1.getSimpleName());
                    this.generateInitialization(it_1, tracedAppendable, config);
                    tracedAppendable.append(";");
                    memberAppendable.closeScope();
                };
                this._loopExtensions.<JvmField>forEach(appendable, fieldsWithInitializer, _function_1, _function_2);
                appendable.decreaseIndentation().newLine().append("}");
            }
            final Procedure1<LoopParams> _function_3 = (LoopParams it_1) -> {
                final Function1<ITreeAppendable, ITreeAppendable> _function_4 = (ITreeAppendable it_2) -> {
                    return it_2.newLine();
                };
                it_1.setSeparator(_function_4);
            };
            final Procedure1<JvmMember> _function_4 = (JvmMember it_1) -> {
                final ITreeAppendable memberAppendable = this._treeAppendableUtil.traceWithComments(appendable, it_1);
                memberAppendable.openScope();
                this.generateMember(it_1, memberAppendable, config);
                memberAppendable.closeScope();
            };
            this._loopExtensions.<JvmMember>forEach(appendable, this.getMembersToBeCompiled(it), _function_3, _function_4);
            _xblockexpression = appendable.decreaseIndentation().newLine().append("}");
        }
        _xifexpression = _xblockexpression;
    } else {
        _xifexpression = super.generateMembersInBody(it, appendable, config);
    }
    return _xifexpression;
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) ITreeAppendable(org.eclipse.xtext.xbase.compiler.output.ITreeAppendable) LoopParams(org.eclipse.xtext.xbase.compiler.LoopParams) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) EObject(org.eclipse.emf.ecore.EObject) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) XExpression(org.eclipse.xtext.xbase.XExpression) JvmMember(org.eclipse.xtext.common.types.JvmMember) JvmField(org.eclipse.xtext.common.types.JvmField) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 2 with AnonymousClass

use of org.eclipse.xtend.core.xtend.AnonymousClass in project xtext-xtend by eclipse.

the class XtendImportedNamespaceScopeProvider method getSuperTypeOfLocalTypeNonResolving.

protected JvmDeclaredType getSuperTypeOfLocalTypeNonResolving(JvmDeclaredType declarator) {
    AnonymousClass anonymousClass = associations.getAnonymousClass(declarator);
    if (anonymousClass != null) {
        IScope typeScope = getScope(anonymousClass, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE);
        JvmDeclaredType superType = anonymousClassUtil.getSuperTypeNonResolving(anonymousClass, typeScope);
        if (superType != null) {
            return superType;
        }
    }
    return null;
}
Also used : AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) IScope(org.eclipse.xtext.scoping.IScope) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType)

Example 3 with AnonymousClass

use of org.eclipse.xtend.core.xtend.AnonymousClass in project xtext-xtend by eclipse.

the class XtendImportedNamespaceScopeProvider method getScope.

@Override
public IScope getScope(final EObject context, final EReference reference) {
    EClass referenceType = reference.getEReferenceType();
    if (TypesPackage.Literals.JVM_TYPE.isSuperTypeOf(referenceType)) {
        if (context instanceof XImportDeclaration) {
            Resource resource = context.eResource();
            IJvmTypeProvider typeProvider = typeScopeProvider.getTypeProvider(resource.getResourceSet());
            AbstractTypeScope typeScope = typeScopeProvider.createTypeScope(typeProvider, null);
            IResourceDescriptions descriptions = resourceDescriptionsProvider.getResourceDescriptions(context.eResource().getResourceSet());
            IResourceDescription resourceDescription = descriptions.getResourceDescription(resource.getURI());
            if (resourceDescription != null) {
                typeScope = new LocalResourceFilteringTypeScope(typeScope, resourceDescription);
            }
            RecordingTypeScope recordingTypeScope = new RecordingTypeScope(typeScope, getImportedNamesSet(resource), getQualifiedNameConverter());
            // TODO this scope doesn't support binary syntax for inner types. It should be a KnownTypes scope which doesn't allow simple names
            // Unfortunately I cannot use a RecordingTypeScope as a parent as it is not compatible...
            IScope scope = SelectableBasedScope.createScope(recordingTypeScope, getAllDescriptions(resource), reference.getEReferenceType(), false);
            return scope;
        }
        final XtendFile xtendFile = getXtendFile(context);
        final Resource resource = xtendFile.eResource();
        AbstractScope result = resourceScopeCache.get("type.scope", xtendFile.eResource(), new Provider<AbstractScope>() {

            @Override
            public AbstractScope get() {
                IJvmTypeProvider typeProvider = typeScopeProvider.getTypeProvider(resource.getResourceSet());
                AbstractTypeScope typeScope = typeScopeProvider.createTypeScope(typeProvider, null);
                IResourceDescriptions descriptions = resourceDescriptionsProvider.getResourceDescriptions(context.eResource().getResourceSet());
                IResourceDescription resourceDescription = descriptions.getResourceDescription(resource.getURI());
                if (resourceDescription != null) {
                    typeScope = new LocalResourceFilteringTypeScope(typeScope, resourceDescription);
                }
                RecordingTypeScope recordingTypeScope = new RecordingTypeScope(typeScope, getImportedNamesSet(resource), getQualifiedNameConverter());
                AbstractScope rootTypeScope = getRootTypeScope(xtendFile, recordingTypeScope);
                AbstractScope importScope = getImportScope(xtendFile.getImportSection(), rootTypeScope, recordingTypeScope);
                AbstractScope localTypes = getResourceTypeScope(xtendFile.eResource(), xtendFile.getPackage(), importScope);
                AbstractScope primitiveAware = new PrimitiveAwareScope(localTypes, typeScope);
                AbstractScope caching = new CachingTypeScope(primitiveAware);
                return caching;
            }
        });
        if (context instanceof AnonymousClass) {
            // necessary for the super type of an anonymous class expression
            JvmDeclaredType inferredAnonymousType = associations.getInferredType((AnonymousClass) context);
            if (inferredAnonymousType != null)
                result = new LocalTypeScope(singletonList(inferredAnonymousType), result);
        }
        XtendMember syntacticContainer = EcoreUtil2.getContainerOfType(context, XtendMember.class);
        if (syntacticContainer != null) {
            result = getContainerScope(syntacticContainer, result);
        }
        EObject logicalContainer = logicalContainerProvider.getNearestLogicalContainer(context);
        if (logicalContainer != null) {
            List<List<JvmTypeParameter>> typeParameters = new ArrayList<List<JvmTypeParameter>>();
            while (logicalContainer instanceof JvmTypeParameterDeclarator) {
                JvmTypeParameterDeclarator typeParamProvider = (JvmTypeParameterDeclarator) logicalContainer;
                if (!typeParamProvider.getTypeParameters().isEmpty()) {
                    typeParameters.add(typeParamProvider.getTypeParameters());
                }
                logicalContainer = logicalContainer.eContainer();
            }
            if (!typeParameters.isEmpty())
                result = new TypeParameterScope(typeParameters, result);
        }
        return result;
    } else if (TypesPackage.Literals.JVM_CONSTRUCTOR.isSuperTypeOf(referenceType)) {
        IScope typeScope = getScope(context, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE);
        // this is not called from the type resolution where we want to allow constructors to link to interfaces
        // in order to improve the error message, therefore we use a strict wrapper here
        IScope result = new ConstructorTypeScopeWrapper(context, IVisibilityHelper.ALL, typeScope, true);
        return result;
    } else {
        throw new IllegalArgumentException("Unexpected global request for " + reference);
    }
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) JvmTypeParameterDeclarator(org.eclipse.xtext.common.types.JvmTypeParameterDeclarator) AbstractTypeScope(org.eclipse.xtext.common.types.xtext.AbstractTypeScope) ArrayList(java.util.ArrayList) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) IJvmTypeProvider(org.eclipse.xtext.common.types.access.IJvmTypeProvider) EClass(org.eclipse.emf.ecore.EClass) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) EObject(org.eclipse.emf.ecore.EObject) IScope(org.eclipse.xtext.scoping.IScope) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) Resource(org.eclipse.emf.ecore.resource.Resource) XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) ConstructorTypeScopeWrapper(org.eclipse.xtext.xbase.scoping.batch.ConstructorTypeScopeWrapper)

Example 4 with AnonymousClass

use of org.eclipse.xtend.core.xtend.AnonymousClass in project xtext-xtend by eclipse.

the class XtendCompiler method isVariableDeclarationRequired.

@Override
protected boolean isVariableDeclarationRequired(XExpression expr, ITreeAppendable b, boolean recursive) {
    boolean result = super.isVariableDeclarationRequired(expr, b, recursive);
    if (result && expr instanceof XConstructorCall) {
        EObject container = expr.eContainer();
        if (container instanceof AnonymousClass) {
            AnonymousClass anonymousClass = (AnonymousClass) container;
            result = isVariableDeclarationRequired(anonymousClass, b, recursive);
            if (result) {
                JvmConstructor constructor = anonymousClass.getConstructorCall().getConstructor();
                JvmDeclaredType type = constructor.getDeclaringType();
                if (((JvmGenericType) type).isAnonymous()) {
                    return false;
                }
            }
        }
    }
    return result;
}
Also used : AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) EObject(org.eclipse.emf.ecore.EObject) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) XConstructorCall(org.eclipse.xtext.xbase.XConstructorCall) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType)

Example 5 with AnonymousClass

use of org.eclipse.xtend.core.xtend.AnonymousClass in project xtext-xtend by eclipse.

the class XtendCompiler method _toJavaStatement.

@Override
protected void _toJavaStatement(final XConstructorCall expr, ITreeAppendable b, final boolean isReferenced) {
    for (XExpression arg : expr.getArguments()) {
        prepareExpression(arg, b);
    }
    if (!isReferenced) {
        b.newLine();
        constructorCallToJavaExpression(expr, b);
        if (expr.eContainer() instanceof AnonymousClass) {
            JvmConstructor constructor = expr.getConstructor();
            JvmDeclaredType declaringType = constructor.getDeclaringType();
            compileAnonymousClassBody((AnonymousClass) expr.eContainer(), declaringType, b);
        }
        b.append(";");
    } else if (isVariableDeclarationRequired(expr, b, true)) {
        Later later = new Later() {

            @Override
            public void exec(ITreeAppendable appendable) {
                constructorCallToJavaExpression(expr, appendable);
                if (expr.eContainer() instanceof AnonymousClass) {
                    JvmConstructor constructor = expr.getConstructor();
                    JvmDeclaredType declaringType = constructor.getDeclaringType();
                    compileAnonymousClassBody((AnonymousClass) expr.eContainer(), declaringType, appendable);
                }
            }
        };
        declareFreshLocalVariable(expr, b, later);
    }
}
Also used : Later(org.eclipse.xtext.xbase.compiler.Later) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) XExpression(org.eclipse.xtext.xbase.XExpression) ITreeAppendable(org.eclipse.xtext.xbase.compiler.output.ITreeAppendable)

Aggregations

AnonymousClass (org.eclipse.xtend.core.xtend.AnonymousClass)23 XExpression (org.eclipse.xtext.xbase.XExpression)13 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)12 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)10 Test (org.junit.Test)10 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)9 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)9 EObject (org.eclipse.emf.ecore.EObject)8 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)8 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)8 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)8 JvmType (org.eclipse.xtext.common.types.JvmType)7 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)6 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)5 JvmConstructor (org.eclipse.xtext.common.types.JvmConstructor)4 JvmMember (org.eclipse.xtext.common.types.JvmMember)3 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)3 INode (org.eclipse.xtext.nodemodel.INode)3 IScope (org.eclipse.xtext.scoping.IScope)3 XConstructorCall (org.eclipse.xtext.xbase.XConstructorCall)3