Search in sources :

Example 86 with XExpression

use of org.eclipse.xtext.xbase.XExpression 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 87 with XExpression

use of org.eclipse.xtext.xbase.XExpression in project xtext-xtend by eclipse.

the class ExtractVariableIntegrationTest method assertAfterExtract.

protected void assertAfterExtract(final CharSequence input, final CharSequence expected, final boolean isFinal) {
    try {
        final String inputString = input.toString();
        final String model = inputString.replace("$", "");
        final IFile file = this.workbenchTestHelper.createFile("Foo", model);
        final XtextEditor editor = this.workbenchTestHelper.openEditor(file);
        try {
            final IUnitOfWork<Change, XtextResource> _function = (XtextResource it) -> {
                Change _xblockexpression = null;
                {
                    final int offset = inputString.indexOf("$");
                    int _lastIndexOf = inputString.lastIndexOf("$");
                    int _minus = (_lastIndexOf - 1);
                    final int length = (_minus - offset);
                    final TextSelection textSelection = new TextSelection(offset, length);
                    final XExpression selection = this.util.findSelectedExpression(it, textSelection);
                    final ExtractVariableRefactoring refactoring = this.refactoringProvider.get();
                    refactoring.setFinal(isFinal);
                    refactoring.initialize(editor, selection);
                    NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
                    final RefactoringStatus status = refactoring.checkAllConditions(_nullProgressMonitor);
                    Assert.assertTrue(status.toString(), status.isOK());
                    NullProgressMonitor _nullProgressMonitor_1 = new NullProgressMonitor();
                    Change _createChange = refactoring.createChange(_nullProgressMonitor_1);
                    NullProgressMonitor _nullProgressMonitor_2 = new NullProgressMonitor();
                    _xblockexpression = _createChange.perform(_nullProgressMonitor_2);
                }
                return _xblockexpression;
            };
            editor.getDocument().<Change>readOnly(_function);
            Assert.assertEquals(expected.toString(), editor.getDocument().get());
        } finally {
            editor.close(false);
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) TextSelection(org.eclipse.jface.text.TextSelection) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) ExtractVariableRefactoring(org.eclipse.xtext.xbase.ui.refactoring.ExtractVariableRefactoring) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) XtextResource(org.eclipse.xtext.resource.XtextResource) Change(org.eclipse.ltk.core.refactoring.Change) XExpression(org.eclipse.xtext.xbase.XExpression)

Example 88 with XExpression

use of org.eclipse.xtext.xbase.XExpression in project xtext-xtend by eclipse.

the class ExtractMethodIntegrationTest method assertExtractForbidden.

protected void assertExtractForbidden(final CharSequence input, final Procedure1<? super ExtractMethodRefactoring> initializer, final String messageFragment) {
    try {
        final String inputString = input.toString();
        final IFile file = this.workbenchTestHelper.createFile("Foo", inputString.replace("$", ""));
        final XtextEditor editor = this.workbenchTestHelper.openEditor(file);
        try {
            final IUnitOfWork<String, XtextResource> _function = (XtextResource it) -> {
                String _xblockexpression = null;
                {
                    int _indexOf = inputString.indexOf("$");
                    int _lastIndexOf = inputString.lastIndexOf("$");
                    int _indexOf_1 = inputString.indexOf("$");
                    int _minus = (_lastIndexOf - _indexOf_1);
                    int _minus_1 = (_minus - 1);
                    TextSelection _textSelection = new TextSelection(_indexOf, _minus_1);
                    final List<XExpression> selection = this.util.findSelectedSiblingExpressions(it, _textSelection);
                    final ExtractMethodRefactoring refactoring = this.refactoringProvider.get();
                    refactoring.initialize(editor, selection, true);
                    refactoring.setMethodName("bar");
                    NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
                    final RefactoringStatus status = refactoring.checkInitialConditions(_nullProgressMonitor);
                    initializer.apply(refactoring);
                    NullProgressMonitor _nullProgressMonitor_1 = new NullProgressMonitor();
                    status.merge(refactoring.checkFinalConditions(_nullProgressMonitor_1));
                    Assert.assertTrue(status.toString(), status.hasError());
                    final String message = status.getMessageMatchingSeverity(RefactoringStatus.ERROR);
                    Assert.assertTrue(message, message.toLowerCase().contains(messageFragment.toLowerCase()));
                    _xblockexpression = "";
                }
                return _xblockexpression;
            };
            editor.getDocument().<String>readOnly(_function);
        } finally {
            editor.close(false);
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) TextSelection(org.eclipse.jface.text.TextSelection) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) XtextResource(org.eclipse.xtext.resource.XtextResource) ExtractMethodRefactoring(org.eclipse.xtend.ide.refactoring.ExtractMethodRefactoring) XExpression(org.eclipse.xtext.xbase.XExpression)

Example 89 with XExpression

use of org.eclipse.xtext.xbase.XExpression 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 90 with XExpression

use of org.eclipse.xtext.xbase.XExpression 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)

Aggregations

XExpression (org.eclipse.xtext.xbase.XExpression)266 Test (org.junit.Test)134 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)97 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)86 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)85 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)84 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)83 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)64 EObject (org.eclipse.emf.ecore.EObject)40 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)37 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)29 IFeatureCallArguments (org.eclipse.xtext.xbase.typesystem.arguments.IFeatureCallArguments)28 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)25 XtendField (org.eclipse.xtend.core.xtend.XtendField)24 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)23 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)22 XNumberLiteral (org.eclipse.xtext.xbase.XNumberLiteral)22 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)21 XtextResource (org.eclipse.xtext.resource.XtextResource)18 XClosure (org.eclipse.xtext.xbase.XClosure)18