Java Code Examples for org.eclipse.xtext.common.types.access.IJvmTypeProvider#findTypeByName()

The following examples show how to use org.eclipse.xtext.common.types.access.IJvmTypeProvider#findTypeByName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeReferences.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * looks up a JVMType corresponding to the given {@link Class}. This method ignores any Jvm types created in non-
 * {@link TypeResource} in the given context's resourceSet, but goes straight to the Java-layer, using a
 * {@link IJvmTypeProvider}.
 * 
 * @return the JvmType with the same qualified name as the given {@link Class} object, or null if no such JvmType
 *         could be found using the context's resourceSet.
 */
public JvmType findDeclaredType(String typeName, Notifier context) {
	if (typeName == null)
		throw new NullPointerException("typeName");
	if (context == null)
		throw new NullPointerException("context");
	ResourceSet resourceSet = EcoreUtil2.getResourceSet(context);
	if (resourceSet == null)
		return null;
	// make sure a type provider is configured in the resource set. 
	IJvmTypeProvider typeProvider = typeProviderFactory.findOrCreateTypeProvider(resourceSet);
	try {
		final JvmType result = typeProvider.findTypeByName(typeName);
		return result;
	} catch (RuntimeException e) {
		operationCanceledManager.propagateAsErrorIfCancelException(e);
		log.info("Couldn't find JvmType for name '" + typeName + "' in context " + context, e);
		return null;
	}
}
 
Example 2
Source File: JavaLinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testNavigateToXtendClass() 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 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;
	JvmTypeReference superType = declaredJavaType.getSuperTypes().get(0);
	JvmType xtendType = superType.getType();
	assertNotNull(xtendType);
	assertEquals("test.XtendClass", xtendType.getQualifiedName());
	Resource xtendResource = xtendType.eResource();
	assertEquals("platform:/resource" + xtendFile.getFullPath(), xtendResource.getURI().toString());
}
 
Example 3
Source File: JavaLinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testGetXtendClassDirectly() 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 xtendType = jvmTypeProvider.findTypeByName("test.XtendClass");
	assertNotNull(xtendType);
	assertEquals("test.XtendClass", xtendType.getQualifiedName());
	Resource xtendResource = xtendType.eResource();
	assertEquals("platform:/resource" + xtendFile.getFullPath(), xtendResource.getURI().toString());
}
 
Example 4
Source File: JavaLinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@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());
}
 
Example 5
Source File: AnnotationLookup.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private JvmAnnotationType findAnnotationType(Class<? extends Annotation> type, Notifier context) {
	ResourceSet resourceSet = EcoreUtil2.getResourceSet(context);
	if (resourceSet == null)
		return null;
	IJvmTypeProvider typeProvider = typeProviderFactory.findOrCreateTypeProvider(resourceSet);
	try {
		final JvmType result = typeProvider.findTypeByName(type.getName());
		if (result instanceof JvmAnnotationType)
			return (JvmAnnotationType) result;
		return null;
	} catch (RuntimeException e) {
		return null;
	}
}
 
Example 6
Source File: ContentAssistTestLanguageProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example 7
Source File: Java8TypeProviderTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example 8
Source File: JavaLinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example 9
Source File: AbstractBuilder.java    From sarl with Apache License 2.0 5 votes vote down vote up
private JvmTypeReference innerFindType(Notifier context, String typeName) {
	final IJvmTypeProvider provider = getTypeResolutionContext();
	JvmType type = null;
	if (provider != null) {
		type = provider.findTypeByName(typeName);
	}
	TypeReferences typeRefs = getTypeReferences();
	if (type == null) {
		type = typeRefs.findDeclaredType(typeName, context);
	}
	if (type == null) {
		return null;
	}
	return typeRefs.createTypeRef(type);
}
 
Example 10
Source File: JvmTypeReferences.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public JvmParameterizedTypeReferenceConstructor(String qualifiedName, TypesFactory typesFactory, IJvmTypeProvider typesProvider, JvmParameterizedTypeReferenceConstructor parent) {
	this(typesProvider.findTypeByName(qualifiedName),typesFactory,typesProvider,parent);
}
 
Example 11
Source File: JvmTypeReferences.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public JvmParameterizedTypeReferenceConstructor(String qualifiedName, TypesFactory typesFactory, IJvmTypeProvider typesProvider, JvmParameterizedTypeReferenceConstructor parent) {
	this(typesProvider.findTypeByName(qualifiedName),typesFactory,typesProvider,parent);
}
 
Example 12
Source File: SARLLabelProvider.java    From sarl with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("checkstyle:npathcomplexity")
@Override
public ImageDescriptor getImageDescriptorForQualifiedName(String qualifiedName, Notifier context,
		IJvmTypeProvider typeProvider) {
	JvmType type = null;
	if (typeProvider != null) {
		type = typeProvider.findTypeByName(qualifiedName);
	}
	if (type == null && context != null) {
		type = this.services.getTypeReferences().findDeclaredType(qualifiedName, context);
	}
	int adornments = this.adornments.get(type);
	JvmVisibility visibility = JvmVisibility.DEFAULT;
	if (type != null) {
		if (type.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) {
			final JvmGenericType gtype = (JvmGenericType) type;
			visibility = gtype.getVisibility();
			final int ecoreCode = this.inheritanceHelper.getSarlElementEcoreType(gtype);
			switch (ecoreCode) {
			case SarlPackage.SARL_AGENT:
				return this.images.forAgent(visibility, this.adornments.get(gtype));
			case SarlPackage.SARL_BEHAVIOR:
				return this.images.forBehavior(visibility, this.adornments.get(gtype));
			case SarlPackage.SARL_CAPACITY:
				// Remove the "abstract" ornment because capacities are always abstract.
				adornments = (adornments & JavaElementImageDescriptor.ABSTRACT) ^ adornments;
				return this.images.forCapacity(visibility, adornments);
			case SarlPackage.SARL_EVENT:
				return this.images.forEvent(visibility, this.adornments.get(gtype));
			case SarlPackage.SARL_SKILL:
				return this.images.forSkill(visibility, this.adornments.get(gtype));
			default:
				if (gtype.isInterface()) {
					return this.images.forInterface(visibility, this.adornments.get(gtype));
				}
			}
		} else if (type.eClass() == TypesPackage.Literals.JVM_ENUMERATION_TYPE) {
			final JvmEnumerationType etype = (JvmEnumerationType) type;
			visibility = etype.getVisibility();
			return this.images.forEnum(visibility, adornments);
		} else if (type.eClass() == TypesPackage.Literals.JVM_ANNOTATION_TYPE) {
			final JvmAnnotationType atype = (JvmAnnotationType) type;
			visibility = atype.getVisibility();
			return this.images.forAnnotation(visibility, adornments);
		} else {
			visibility = JvmVisibility.DEFAULT;
		}
	}
	// Default icon is the class icon.
	return this.images.forClass(visibility, adornments);
}