Search in sources :

Example 1 with IJvmTypeProvider

use of org.eclipse.xtext.common.types.access.IJvmTypeProvider 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 2 with IJvmTypeProvider

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

the class JavaLinkingTest method testNavigateToXtendClassArray.

@Test
public void testNavigateToXtendClassArray() throws Exception {
    IFile xtendFile = testHelper.createFile("test/XtendClass", "package test\nclass XtendClass {}");
    IFile javaFile = testHelper.createFileImpl(WorkbenchTestHelper.TESTPROJECT_NAME + "/src/test/JavaClass.java", "package test;\npublic class JavaClass extends java.util.ArrayList<XtendClass[]> {}");
    waitForBuild();
    assertNumberOfMarkers(xtendFile, 0);
    assertNumberOfMarkers(javaFile, 0);
    ResourceSet resourceSet = resourceSetProvider.get(testHelper.getProject());
    IJvmTypeProvider jvmTypeProvider = typeProviderFactory.findOrCreateTypeProvider(resourceSet);
    JvmType javaType = jvmTypeProvider.findTypeByName("test.JavaClass");
    assertNotNull(javaType);
    assertTrue(javaType instanceof JvmDeclaredType);
    JvmDeclaredType declaredJavaType = (JvmDeclaredType) javaType;
    JvmParameterizedTypeReference arrayList = (JvmParameterizedTypeReference) declaredJavaType.getSuperTypes().get(0);
    JvmTypeReference arrayReference = arrayList.getArguments().get(0);
    assertTrue(arrayReference instanceof JvmGenericArrayTypeReference);
    JvmType arrayType = arrayReference.getType();
    assertTrue(arrayType instanceof JvmArrayType);
    JvmType xtendType = ((JvmArrayType) arrayType).getComponentType();
    assertNotNull(xtendType);
    assertEquals("test.XtendClass", xtendType.getQualifiedName());
    Resource xtendResource = xtendType.eResource();
    assertEquals("platform:/resource" + xtendFile.getFullPath(), xtendResource.getURI().toString());
}
Also used : JvmGenericArrayTypeReference(org.eclipse.xtext.common.types.JvmGenericArrayTypeReference) IFile(org.eclipse.core.resources.IFile) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) Resource(org.eclipse.emf.ecore.resource.Resource) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) JvmType(org.eclipse.xtext.common.types.JvmType) JvmParameterizedTypeReference(org.eclipse.xtext.common.types.JvmParameterizedTypeReference) IJvmTypeProvider(org.eclipse.xtext.common.types.access.IJvmTypeProvider) JvmArrayType(org.eclipse.xtext.common.types.JvmArrayType) Test(org.junit.Test)

Example 3 with IJvmTypeProvider

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

the class JavaLinkingTest method testGetXtendClassAsArray.

@Test
public void testGetXtendClassAsArray() throws Exception {
    IFile xtendFile = testHelper.createFile("test/XtendClass", "package test\nclass XtendClass {}");
    waitForBuild();
    assertNumberOfMarkers(xtendFile, 0);
    ResourceSet resourceSet = resourceSetProvider.get(testHelper.getProject());
    IJvmTypeProvider jvmTypeProvider = typeProviderFactory.findOrCreateTypeProvider(resourceSet);
    JvmType xtendArrayType = jvmTypeProvider.findTypeByName("test.XtendClass[][]");
    assertNotNull(xtendArrayType);
    assertTrue(xtendArrayType instanceof JvmArrayType);
    assertEquals("test.XtendClass[][]", xtendArrayType.getQualifiedName());
    Resource xtendResource = xtendArrayType.eResource();
    assertEquals("platform:/resource" + xtendFile.getFullPath(), xtendResource.getURI().toString());
}
Also used : IFile(org.eclipse.core.resources.IFile) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) JvmType(org.eclipse.xtext.common.types.JvmType) IJvmTypeProvider(org.eclipse.xtext.common.types.access.IJvmTypeProvider) JvmArrayType(org.eclipse.xtext.common.types.JvmArrayType) Test(org.junit.Test)

Example 4 with IJvmTypeProvider

use of org.eclipse.xtext.common.types.access.IJvmTypeProvider in project xtext-eclipse by eclipse.

the class ContentAssistTestLanguageProposalProvider method completeReferenceHolder_SubtypeReference.

@Override
public void completeReferenceHolder_SubtypeReference(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    ResourceSet resourceSet = model.eResource().getResourceSet();
    IJvmTypeProvider typeProvider = typeProviderFactory.findOrCreateTypeProvider(resourceSet);
    JvmType superType = typeProvider.findTypeByName(Collection.class.getName());
    typesProposalProvider.createSubTypeProposals(superType, this, context, ContentAssistTestLanguagePackage.Literals.REFERENCE_HOLDER__SUBTYPE_REFERENCE, acceptor);
}
Also used : Collection(java.util.Collection) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) JvmType(org.eclipse.xtext.common.types.JvmType) IJvmTypeProvider(org.eclipse.xtext.common.types.access.IJvmTypeProvider)

Example 5 with IJvmTypeProvider

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

the class Java8TypeProviderTest method testDefaultJavaMethod.

@Test
public void testDefaultJavaMethod() {
    final IJvmTypeProvider typeProvider = this.typeProviderFactory.createTypeProvider();
    JvmType _findTypeByName = typeProvider.findTypeByName(JavaInterface.class.getCanonicalName());
    final JvmGenericType intf = ((JvmGenericType) _findTypeByName);
    this.doTestMethods(intf);
}
Also used : JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmType(org.eclipse.xtext.common.types.JvmType) IJvmTypeProvider(org.eclipse.xtext.common.types.access.IJvmTypeProvider) Test(org.junit.Test)

Aggregations

IJvmTypeProvider (org.eclipse.xtext.common.types.access.IJvmTypeProvider)8 JvmType (org.eclipse.xtext.common.types.JvmType)6 Resource (org.eclipse.emf.ecore.resource.Resource)5 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)5 Test (org.junit.Test)5 IFile (org.eclipse.core.resources.IFile)4 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)3 JvmArrayType (org.eclipse.xtext.common.types.JvmArrayType)2 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)2 IScope (org.eclipse.xtext.scoping.IScope)2 CharMatcher (com.google.common.base.CharMatcher)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 EList (org.eclipse.emf.common.util.EList)1 EClass (org.eclipse.emf.ecore.EClass)1 EObject (org.eclipse.emf.ecore.EObject)1 AccessRestriction (org.eclipse.jdt.internal.compiler.env.AccessRestriction)1 BasicSearchEngine (org.eclipse.jdt.internal.core.search.BasicSearchEngine)1