Search in sources :

Example 6 with XVariableDeclaration

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

the class XtendTypeComputer method _computeTypes.

protected void _computeTypes(RichString object, ITypeComputationState state) {
    List<XExpression> expressions = object.getExpressions();
    if (!expressions.isEmpty()) {
        for (XExpression expression : expressions) {
            ITypeComputationState expressionState = state.withoutExpectation();
            expressionState.computeTypes(expression);
            if (expression instanceof XVariableDeclaration) {
                addLocalToCurrentScope((XVariableDeclaration) expression, state);
            }
        }
    }
    for (ITypeExpectation expectation : state.getExpectations()) {
        LightweightTypeReference expectedType = expectation.getExpectedType();
        if (expectedType != null && expectedType.isType(StringConcatenation.class)) {
            expectation.acceptActualType(expectedType, ConformanceFlags.CHECKED_SUCCESS | ConformanceFlags.DEMAND_CONVERSION);
        } else if (expectedType != null && expectedType.isType(StringConcatenationClient.class)) {
            expectation.acceptActualType(expectedType, ConformanceFlags.CHECKED_SUCCESS | ConformanceFlags.DEMAND_CONVERSION);
        } else if (expectedType != null && expectedType.isType(String.class)) {
            expectation.acceptActualType(expectedType, ConformanceFlags.CHECKED_SUCCESS | ConformanceFlags.DEMAND_CONVERSION);
        // TODO this special treatment here should become obsolete as soon as the expectations are properly propagated
        } else if (!(object.eContainer() instanceof XCastedExpression) && object.eContainingFeature() != XbasePackage.Literals.XMEMBER_FEATURE_CALL__MEMBER_CALL_TARGET && (expectedType != null && !expectedType.isResolved() || expectedType == null && !expectation.isVoidTypeAllowed())) {
            LightweightTypeReference type = getRawTypeForName(String.class, state);
            expectation.acceptActualType(type, ConformanceFlags.UNCHECKED | ConformanceFlags.DEMAND_CONVERSION);
        } else {
            LightweightTypeReference type = getRawTypeForName(CharSequence.class, state);
            expectation.acceptActualType(type, ConformanceFlags.UNCHECKED);
        }
    }
}
Also used : ITypeComputationState(org.eclipse.xtext.xbase.typesystem.computation.ITypeComputationState) XVariableDeclaration(org.eclipse.xtext.xbase.XVariableDeclaration) XCastedExpression(org.eclipse.xtext.xbase.XCastedExpression) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XExpression(org.eclipse.xtext.xbase.XExpression) RichString(org.eclipse.xtend.core.xtend.RichString) ITypeExpectation(org.eclipse.xtext.xbase.typesystem.computation.ITypeExpectation)

Example 7 with XVariableDeclaration

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

the class ExtractMethodRefactoring method calculateExpressions.

protected List<XExpression> calculateExpressions(final List<XExpression> expressions) {
    final XExpression firstExpression = expressions.get(0);
    // instead of the complete XVariableDeclaration
    if (expressions.size() == 1 && firstExpression instanceof XVariableDeclaration) {
        final XtendFunction originalMethod = EcoreUtil2.getContainerOfType(firstExpression, XtendFunction.class);
        for (final EObject element : Iterables.filter(EcoreUtil2.eAllContents(originalMethod.getExpression()), new Predicate<EObject>() {

            @Override
            public boolean apply(EObject input) {
                return !EcoreUtil.isAncestor(expressions, input);
            }
        })) {
            if (element instanceof XFeatureCall) {
                XFeatureCall featureCall = (XFeatureCall) element;
                JvmIdentifiableElement feature = featureCall.getFeature();
                if (EcoreUtil.isAncestor(expressions, feature)) {
                    return singletonList(((XVariableDeclaration) firstExpression).getRight());
                }
            }
        }
    }
    return expressions;
}
Also used : XVariableDeclaration(org.eclipse.xtext.xbase.XVariableDeclaration) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) EObject(org.eclipse.emf.ecore.EObject) XExpression(org.eclipse.xtext.xbase.XExpression)

Example 8 with XVariableDeclaration

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

the class ExtractMethodRefactoring method checkInitialConditions.

@Override
public RefactoringStatus checkInitialConditions(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
    StatusWrapper status = statusProvider.get();
    IResolvedTypes resolvedTypes = typeResolver.resolveTypes(firstExpression, new CancelIndicator() {

        @Override
        public boolean isCanceled() {
            return pm.isCanceled();
        }
    });
    try {
        Set<String> calledExternalFeatureNames = newHashSet();
        returnType = calculateReturnType(resolvedTypes);
        if (returnType != null && !equal("void", returnType.getIdentifier()))
            returnExpression = lastExpression;
        boolean isReturnAllowed = isEndOfOriginalMethod();
        for (EObject element : EcoreUtil2.eAllContents(originalMethod.getExpression())) {
            if (pm.isCanceled()) {
                throw new OperationCanceledException();
            }
            boolean isLocalExpression = EcoreUtil.isAncestor(expressions, element);
            if (element instanceof XFeatureCall) {
                XFeatureCall featureCall = (XFeatureCall) element;
                JvmIdentifiableElement feature = featureCall.getFeature();
                LightweightTypeReference featureType = resolvedTypes.getActualType(featureCall);
                boolean isLocalFeature = EcoreUtil.isAncestor(expressions, feature);
                if (!isLocalFeature && isLocalExpression) {
                    // call-out
                    if (feature instanceof JvmFormalParameter || feature instanceof XVariableDeclaration) {
                        if (!calledExternalFeatureNames.contains(feature.getSimpleName())) {
                            calledExternalFeatureNames.add(feature.getSimpleName());
                            ParameterInfo parameterInfo = new ParameterInfo(featureType.getIdentifier(), feature.getSimpleName(), parameterInfos.size());
                            parameterInfos.add(parameterInfo);
                            parameter2type.put(parameterInfo, featureType);
                        }
                        externalFeatureCalls.put(feature.getSimpleName(), featureCall);
                    }
                } else if (isLocalFeature && !isLocalExpression) {
                    // call-in
                    if (returnExpression != null) {
                        status.add(RefactoringStatus.FATAL, "Ambiguous return value: Multiple local variables are accessed in subsequent code.");
                        break;
                    }
                    returnExpression = featureCall;
                    returnType = featureType;
                }
            } else if (isLocalExpression) {
                if (element instanceof XReturnExpression && !isReturnAllowed) {
                    status.add(RefactoringStatus.FATAL, "Extracting method would break control flow due to return statements.");
                    break;
                } else if (element instanceof JvmTypeReference) {
                    JvmType type = ((JvmTypeReference) element).getType();
                    if (type instanceof JvmTypeParameter) {
                        JvmOperation operation = associations.getDirectlyInferredOperation(originalMethod);
                        if (operation != null) {
                            List<JvmTypeParameter> typeParameters = operation.getTypeParameters();
                            if (typeParameters.contains(type))
                                neededTypeParameters.add((JvmTypeParameter) type);
                        }
                    }
                } else if (element instanceof JvmFormalParameter)
                    localFeatureNames.add(((JvmFormalParameter) element).getName());
                else if (element instanceof XVariableDeclaration)
                    localFeatureNames.add(((XVariableDeclaration) element).getIdentifier());
            }
        }
    } catch (OperationCanceledException e) {
        throw e;
    } catch (Exception exc) {
        handleException(exc, status);
    }
    return status.getRefactoringStatus();
}
Also used : XVariableDeclaration(org.eclipse.xtext.xbase.XVariableDeclaration) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) StatusWrapper(org.eclipse.xtext.ui.refactoring.impl.StatusWrapper) RichString(org.eclipse.xtend.core.xtend.RichString) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) JvmType(org.eclipse.xtext.common.types.JvmType) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) BadLocationException(org.eclipse.jface.text.BadLocationException) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) EObject(org.eclipse.emf.ecore.EObject) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) XReturnExpression(org.eclipse.xtext.xbase.XReturnExpression)

Example 9 with XVariableDeclaration

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

the class XtendHoverSignatureProviderTest method testSignatureForXVariableDeclaration.

@Test
public void testSignatureForXVariableDeclaration() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("package testPackage");
        _builder.newLine();
        _builder.append("class Foo {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("def bar(List<String> list){");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append("val a = \"42\"");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final XtendFile xtendFile = this.parseHelper.parse(_builder, this.getResourceSet());
        final XtendClass clazz = IterableExtensions.<XtendClass>head(Iterables.<XtendClass>filter(xtendFile.getXtendTypes(), XtendClass.class));
        XtendMember _get = clazz.getMembers().get(0);
        final XtendFunction xtendFunction = ((XtendFunction) _get);
        XExpression _expression = xtendFunction.getExpression();
        XExpression _get_1 = ((XBlockExpression) _expression).getExpressions().get(0);
        final XVariableDeclaration variable = ((XVariableDeclaration) _get_1);
        final String signature = this.signatureProvider.getSignature(variable);
        Assert.assertEquals("String a", signature);
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XVariableDeclaration(org.eclipse.xtext.xbase.XVariableDeclaration) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XExpression(org.eclipse.xtext.xbase.XExpression) Test(org.junit.Test)

Example 10 with XVariableDeclaration

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

the class UIStringsTest method testReferenceToString_3.

/**
 * The qualified name of the type is specified.
 * The JvmTypeReference is proxy.
 */
@Test
public void testReferenceToString_3() throws Exception {
    XtendFile file1 = file("package org.eclipse.xtend.core.tests.validation.uistrings\n" + "public interface InterfaceA { }\n" + "public class ClassB implements InterfaceA { }\n" + "public class ClassC extends ClassB {\n" + "}\n");
    assertNotNull(file1);
    XtendFile file2 = file("package org.eclipse.xtend.core.tests.validation.uistrings\n" + "class XtendClass1 {\n" + "  def test() {\n" + "    val x = new List<org.eclipse.xtend.core.tests.validation.uistrings.ClassC>\n" + "  }\n" + "}\n");
    XtendClass clazz = (XtendClass) file2.getXtendTypes().get(0);
    XBlockExpression block = (XBlockExpression) ((XtendFunction) clazz.getMembers().get(0)).getExpression();
    XVariableDeclaration declaration = (XVariableDeclaration) block.getExpressions().get(0);
    XConstructorCall cons = (XConstructorCall) declaration.getRight();
    JvmTypeReference reference = cons.getTypeArguments().get(0);
    assertNotNull(reference);
    assertNotNull(reference.getType());
    assertTrue(reference.getType().eIsProxy());
    assertNotNull(reference.eResource());
    assertEquals("ClassC", this.uiStrings.referenceToString(reference, "the-default-label"));
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) XBlockExpression(org.eclipse.xtext.xbase.XBlockExpression) XVariableDeclaration(org.eclipse.xtext.xbase.XVariableDeclaration) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) XConstructorCall(org.eclipse.xtext.xbase.XConstructorCall) Test(org.junit.Test)

Aggregations

XVariableDeclaration (org.eclipse.xtext.xbase.XVariableDeclaration)12 Test (org.junit.Test)9 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)8 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)8 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)7 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)6 XExpression (org.eclipse.xtext.xbase.XExpression)6 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)5 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)5 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)4 XConstructorCall (org.eclipse.xtext.xbase.XConstructorCall)4 RichString (org.eclipse.xtend.core.xtend.RichString)3 XFeatureCall (org.eclipse.xtext.xbase.XFeatureCall)3 EObject (org.eclipse.emf.ecore.EObject)2 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)2 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)2 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)2 CoreException (org.eclipse.core.runtime.CoreException)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 ParameterInfo (org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)1