Search in sources :

Example 21 with IResolvedTypes

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

the class CreateMemberQuickfixes method getResolvedArgumentTypes.

protected List<LightweightTypeReference> getResolvedArgumentTypes(EObject context, JvmIdentifiableElement logicalContainer, List<XExpression> arguments) {
    List<LightweightTypeReference> argumentTypes = newArrayList();
    IResolvedTypes resolvedTypes = typeResolver.resolveTypes(context);
    for (XExpression argument : arguments) {
        LightweightTypeReference resolved = resolvedTypes.getActualType(argument);
        if (resolved == null) {
            StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(services, context);
            argumentTypes.add(owner.newReferenceToObject());
        } else {
            LocalTypeSubstitutor substitutor = new LocalTypeSubstitutor(resolved.getOwner(), logicalContainer);
            argumentTypes.add(substitutor.withoutLocalTypes(resolved));
        }
    }
    return argumentTypes;
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) XExpression(org.eclipse.xtext.xbase.XExpression) LocalTypeSubstitutor(org.eclipse.xtext.xbase.typesystem.util.LocalTypeSubstitutor) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 22 with IResolvedTypes

use of org.eclipse.xtext.xbase.typesystem.IResolvedTypes 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 23 with IResolvedTypes

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

the class ErrorTest method testErrorModel_008.

@Test
public void testErrorModel_008() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("annotation Bar { @");
    _builder.newLine();
    final XtendFile file = this.processWithoutException(_builder);
    final XtendTypeDeclaration typeDeclaration = IterableExtensions.<XtendTypeDeclaration>last(file.getXtendTypes());
    final EList<XAnnotation> annotations = IterableExtensions.<XtendMember>last(typeDeclaration.getMembers()).getAnnotations();
    final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(IterableExtensions.<XAnnotation>head(annotations));
    Assert.assertNotNull(resolvedTypes.getActualType(IterableExtensions.<XAnnotation>head(annotations)));
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) Test(org.junit.Test)

Example 24 with IResolvedTypes

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

the class ErrorTest method testErrorModel_013.

@Test
public void testErrorModel_013() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Y {");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("static def <T> IExpectationSetters<T> expect(T value) {");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("}");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("HeaderAccess<?> unboundedMockHeaderAccess");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("def test() {");
    _builder.newLine();
    _builder.append("   ");
    _builder.append("val Object header = unboundedMockHeaderAccess.header");
    _builder.newLine();
    _builder.append("   ");
    _builder.append("val IExpectationSetters<Object> exp1 = expect(header)");
    _builder.newLine();
    _builder.append("   ");
    _builder.append("val IExpectationSetters<Object> exp2 = expect(unboundedMockHeaderAccess.getHeader())");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.append("interface IExpectationSetters<T> {}");
    _builder.newLine();
    _builder.append("interface HeaderAccess<T> {");
    _builder.newLine();
    _builder.append("   ");
    _builder.append("def  getHeader();");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFile file = this.processWithoutException(_builder);
    final XtendTypeDeclaration headerAccess = IterableExtensions.<XtendTypeDeclaration>last(file.getXtendTypes());
    XtendMember _head = IterableExtensions.<XtendMember>head(headerAccess.getMembers());
    final XtendFunction function = ((XtendFunction) _head);
    EObject _head_1 = IterableExtensions.<EObject>head(this.associations.getJvmElements(function));
    final JvmOperation operation = ((JvmOperation) _head_1);
    final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(operation);
    Assert.assertNotNull(resolvedTypes.getActualType(operation));
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) Test(org.junit.Test)

Example 25 with IResolvedTypes

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

the class ErrorTest method testErrorModel_017.

@Test
public void testErrorModel_017() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("import static extension Throwables.*");
    _builder.newLine();
    _builder.append("import java.net.URI");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class Throwables {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def static <T> T propagate(()=>T proc, (Exception)=>Exception handler) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("try {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("proc.apply");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("} catch(Exception e) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw handler.apply(e)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.append("class Client {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("valuri = [| new URI(\'\')].propagate [ new IllegalArgumentException(it) ]");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFile file = this.processWithoutException(_builder);
    final XtendTypeDeclaration client = IterableExtensions.<XtendTypeDeclaration>last(file.getXtendTypes());
    XtendMember _head = IterableExtensions.<XtendMember>head(client.getMembers());
    final XtendField field = ((XtendField) _head);
    XExpression _initialValue = field.getInitialValue();
    final XMemberFeatureCall initializer = ((XMemberFeatureCall) _initialValue);
    XExpression _head_1 = IterableExtensions.<XExpression>head(initializer.getMemberCallArguments());
    final XClosure closure = ((XClosure) _head_1);
    final JvmFormalParameter implicit = IterableExtensions.<JvmFormalParameter>head(closure.getImplicitFormalParameters());
    Assert.assertNotNull(implicit);
    final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(implicit);
    Assert.assertNotNull(resolvedTypes.getActualType(implicit));
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) XClosure(org.eclipse.xtext.xbase.XClosure) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) XMemberFeatureCall(org.eclipse.xtext.xbase.XMemberFeatureCall) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XExpression(org.eclipse.xtext.xbase.XExpression) XtendField(org.eclipse.xtend.core.xtend.XtendField) Test(org.junit.Test)

Aggregations

IResolvedTypes (org.eclipse.xtext.xbase.typesystem.IResolvedTypes)32 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)25 Test (org.junit.Test)25 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)21 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)19 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)14 XExpression (org.eclipse.xtext.xbase.XExpression)14 XAnnotation (org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation)11 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)9 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)7 EObject (org.eclipse.emf.ecore.EObject)6 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)6 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)5 XtendField (org.eclipse.xtend.core.xtend.XtendField)4 AnonymousClass (org.eclipse.xtend.core.xtend.AnonymousClass)3 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)3 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)3 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)3 XClosure (org.eclipse.xtext.xbase.XClosure)3 XMemberFeatureCall (org.eclipse.xtext.xbase.XMemberFeatureCall)3