Java Code Examples for org.eclipse.xtext.common.types.JvmGenericType

The following examples show how to use org.eclipse.xtext.common.types.JvmGenericType. These examples are extracted from open source projects. 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 Project: xtext-extras   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMethods_publicStrictFpMethod_01() {
	String typeName = Methods.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation method = getMethodFromType(type, Methods.class, "publicStrictFpMethod()");
	assertSame(type, method.getDeclaringType());
	assertFalse(method.isAbstract());
	assertFalse(method.isFinal());
	assertFalse(method.isStatic());
	assertFalse(method.isSynchronized());
	assertTrue(method.isStrictFloatingPoint());
	assertFalse(method.isNative());
	assertEquals(JvmVisibility.PUBLIC, method.getVisibility());
	JvmType methodType = method.getReturnType().getType();
	assertEquals("void", methodType.getIdentifier());
}
 
Example #2
Source Project: xtext-extras   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_Inner_05() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation methodV = getMethodFromType(type, ParameterizedTypes.Inner.class, "methodVArray_01()");
	JvmTypeReference listT = methodV.getReturnType();
	assertEquals("java.util.List<? extends V>[]", listT.getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) ((JvmGenericArrayTypeReference) listT)
			.getComponentType();
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	assertTrue(typeArgument instanceof JvmWildcardTypeReference);
	JvmWildcardTypeReference wildcardTypeArgument = (JvmWildcardTypeReference) typeArgument;
	assertEquals("? extends V", wildcardTypeArgument.getIdentifier());
	assertEquals(1, wildcardTypeArgument.getConstraints().size());
	JvmUpperBound upperBound = (JvmUpperBound) wildcardTypeArgument.getConstraints().get(0);
	JvmTypeParameter v = type.getTypeParameters().get(3);
	assertSame(v, upperBound.getTypeReference().getType());
}
 
Example #3
Source Project: xtext-extras   Author: eclipse   File: JvmTypesBuilderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testStringAnnotation() {
  try {
    final XAnnotationsFactory f = XAnnotationsFactory.eINSTANCE;
    final XExpression e = this.expression("\'Foo\'");
    final XAnnotation anno = f.createXAnnotation();
    JvmType _findDeclaredType = this.references.findDeclaredType(Inject.class, e);
    anno.setAnnotationType(((JvmAnnotationType) _findDeclaredType));
    anno.setValue(e);
    final JvmGenericType type = this.typesFactory.createJvmGenericType();
    this._jvmTypesBuilder.addAnnotation(type, anno);
    Assert.assertEquals(anno.getAnnotationType(), IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getAnnotation());
    JvmAnnotationValue _head = IterableExtensions.<JvmAnnotationValue>head(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues());
    EObject _head_1 = IterableExtensions.<EObject>head(((JvmCustomAnnotationValue) _head).getValues());
    Assert.assertTrue((_head_1 instanceof XStringLiteral));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #4
Source Project: xtext-xtend   Author: eclipse   File: XtendCompiler.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected boolean isVariableDeclarationRequired(XExpression expr, ITreeAppendable b, boolean recursive) {
	boolean result = super.isVariableDeclarationRequired(expr, b, recursive);
	if (result && expr instanceof XConstructorCall) {
		EObject container = expr.eContainer();
		if (container instanceof AnonymousClass) {
			AnonymousClass anonymousClass = (AnonymousClass) container;
			result = isVariableDeclarationRequired(anonymousClass, b, recursive);
			if (result) {
				JvmConstructor constructor = anonymousClass.getConstructorCall().getConstructor();
				JvmDeclaredType type = constructor.getDeclaringType();
				if (((JvmGenericType) type).isAnonymous()) {
					return false;
				}
			}
		}
	}
	return result;
}
 
Example #5
Source Project: xtext-eclipse   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_Inner_06() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation methodV = getMethodFromType(type, ParameterizedTypes.Inner.class, "methodVArray_02()");
	JvmTypeReference listV = methodV.getReturnType();
	assertEquals("java.util.List<? extends V[]>", listV.getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) listV;
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	assertTrue(typeArgument instanceof JvmWildcardTypeReference);
	JvmWildcardTypeReference wildcardTypeArgument = (JvmWildcardTypeReference) typeArgument;
	assertEquals("? extends V[]", wildcardTypeArgument.getIdentifier());
	assertEquals(1, wildcardTypeArgument.getConstraints().size());
	JvmUpperBound upperBound = (JvmUpperBound) wildcardTypeArgument.getConstraints().get(0);
	JvmType upperBoundType = upperBound.getTypeReference().getType();
	assertTrue(upperBoundType instanceof JvmArrayType);
	assertTrue(((JvmArrayType) upperBoundType).getComponentType() instanceof JvmTypeParameter);
	JvmTypeParameter v = type.getTypeParameters().get(3);
	assertSame(v, ((JvmArrayType) upperBoundType).getComponentType());
}
 
Example #6
Source Project: xtext-xtend   Author: eclipse   File: InferredJvmModelTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testInferredFunction_02() throws Exception {
	XtendFile xtendFile = file("class Foo { def create result: newArrayList(s) newList(String s) {} }");
	JvmGenericType inferredType = getInferredType(xtendFile);
	XtendClass xtendClass = (XtendClass) xtendFile.getXtendTypes().get(0);
	EList<JvmMember> jvmMembers = inferredType.getMembers();
	assertEquals(4, jvmMembers.size());
	JvmMember jvmMember = jvmMembers.get(1);
	assertTrue(jvmMember instanceof JvmOperation);
	XtendFunction xtendFunction = (XtendFunction) xtendClass.getMembers().get(0);
	assertEquals(xtendFunction.getName(), jvmMember.getSimpleName());
	assertEquals(JvmVisibility.PUBLIC, jvmMember.getVisibility());
	assertEquals("java.util.ArrayList<java.lang.String>", ((JvmOperation) jvmMember).getReturnType().getIdentifier());
	
	JvmField cacheVar = (JvmField) jvmMembers.get(2);
	assertEquals("_createCache_" + xtendFunction.getName(), cacheVar.getSimpleName());
	assertEquals(JvmVisibility.PRIVATE, cacheVar.getVisibility());
	assertEquals("java.util.HashMap<java.util.ArrayList<? extends java.lang.Object>, java.util.ArrayList<java.lang.String>>", cacheVar.getType().getIdentifier());
	
	JvmOperation privateInitializer = (JvmOperation) jvmMembers.get(3);
	assertEquals("_init_"+xtendFunction.getName(), privateInitializer.getSimpleName());
	assertEquals(JvmVisibility.PRIVATE, privateInitializer.getVisibility());
	assertEquals("java.util.ArrayList<java.lang.String>", privateInitializer.getParameters().get(0).getParameterType().getIdentifier());
	assertEquals("java.lang.String", privateInitializer.getParameters().get(1).getParameterType().getIdentifier());
}
 
Example #7
Source Project: xtext-xtend   Author: eclipse   File: XtendValidator.java    License: Eclipse Public License 2.0 6 votes vote down vote up
protected boolean hasCycleInHierarchy(JvmGenericType type, Set<JvmGenericType> processedSuperTypes) {
	JvmDeclaredType container = type;
	do {
		if (processedSuperTypes.contains(container))
			return true;
		container = container.getDeclaringType();
	} while (container != null);
	processedSuperTypes.add(type);
	for (JvmTypeReference superTypeRef : type.getSuperTypes()) {
		if (superTypeRef.getType() instanceof JvmGenericType) {
			if (hasCycleInHierarchy((JvmGenericType) superTypeRef.getType(), processedSuperTypes))
				return true;
		}
	}
	processedSuperTypes.remove(type);
	return false;
}
 
Example #8
Source Project: xtext-extras   Author: eclipse   File: ResourceDescriptionProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testStubGeneration_01() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("public class MyTest {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("public String helloWorld() {");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("return \"Hello\";");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("}");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final Procedure1<IResourceDescription> _function = (IResourceDescription it) -> {
    Assert.assertEquals("MyTest", IterableExtensions.<IEObjectDescription>head(it.getExportedObjects()).getQualifiedName().toString());
    EObject _eObjectOrProxy = IterableExtensions.<IEObjectDescription>head(it.getExportedObjects()).getEObjectOrProxy();
    Assert.assertFalse(((JvmGenericType) _eObjectOrProxy).isInterface());
    Assert.assertEquals(1, IterableExtensions.size(it.getExportedObjects()));
  };
  this.resultsIn(_builder, _function);
}
 
Example #9
Source Project: xtext-eclipse   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMemberCount_07() {
	String typeName = StaticNestedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	int methodCount = StaticNestedTypes.class.getDeclaredMethods().length;
	assertEquals(1, methodCount);
	int constructorCount = StaticNestedTypes.class.getDeclaredConstructors().length;
	assertEquals(1, constructorCount); // default constructor
	int nestedTypesCount = StaticNestedTypes.class.getClasses().length;
	assertEquals(1, nestedTypesCount);
	assertEquals(methodCount + constructorCount + nestedTypesCount, type.getMembers().size());
	diagnose(type);
	Resource resource = type.eResource();
	getAndResolveAllFragments(resource);
	recomputeAndCheckIdentifiers(resource);
}
 
Example #10
Source Project: sarl   Author: sarl   File: SARLJvmModelInferrer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected JvmOperation deriveGenericDispatchOperationSignature(
		Iterable<JvmOperation> localOperations, JvmGenericType target) {
	final JvmOperation dispatcher = super.deriveGenericDispatchOperationSignature(localOperations, target);
	//
	// Fixing the behavior for determining the visibility of the dispatcher since
	// it does not fit the SARL requirements.
	//
	JvmVisibility higherVisibility = JvmVisibility.PRIVATE;
	for (final JvmOperation jvmOperation : localOperations) {
		final Iterable<XtendFunction> xtendFunctions = Iterables.filter(
				this.sarlAssociations.getSourceElements(jvmOperation), XtendFunction.class);
		for (final XtendFunction func : xtendFunctions) {
			JvmVisibility visibility = func.getVisibility();
			if (visibility == null) {
				visibility = this.defaultVisibilityProvider.getDefaultJvmVisibility(func);
			}
			if (this.visibilityComparator.compare(visibility, higherVisibility) > 0) {
				higherVisibility = visibility;
			}
		}
	}
	dispatcher.setVisibility(higherVisibility);

	return dispatcher;
}
 
Example #11
Source Project: xtext-xtend   Author: eclipse   File: InferredJvmModelTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testDispatchFunction_01() throws Exception {
	XtendFile xtendFile = file("class Foo { def dispatch foo(Object x, String y) {null} def dispatch foo(String x) {null}}");
	JvmGenericType inferredType = getInferredType(xtendFile);
	
	// two dispatch methods
	Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
	JvmOperation dispatch = findByNameAndFirstParameterType(operations, "foo", Object.class);
	assertEquals("java.lang.Object", dispatch.getReturnType().getIdentifier());
	assertEquals(2, dispatch.getParameters().size());
	
	dispatch = findByNameAndFirstParameterType(operations, "foo", String.class);
	assertEquals("java.lang.Object", dispatch.getReturnType().getIdentifier());
	assertEquals(1, dispatch.getParameters().size());
	
	// two internal case methods
	findByNameAndFirstParameterType(operations, "_foo", Object.class);
	findByNameAndFirstParameterType(operations, "_foo", String.class);
}
 
Example #12
Source Project: xtext-xtend   Author: eclipse   File: XtendEObjectAtOffsetHelper.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected EObject resolveCrossReferencedElement(INode node) {
	EObject referencedElement = super.resolveCrossReferencedElement(node);
	EObject referenceOwner = NodeModelUtils.findActualSemanticObjectFor(node);
	if(referenceOwner instanceof XConstructorCall) {
		if (referenceOwner.eContainer() instanceof AnonymousClass) {
			AnonymousClass anon = (AnonymousClass) referenceOwner.eContainer();
			JvmGenericType superType = anonymousClassUtil.getSuperType(anon);
			if(superType != null) {
				if (referencedElement instanceof JvmGenericType)  
					return superType;
				else if(referencedElement instanceof JvmConstructor) {
					if(superType.isInterface())
						return superType;
					JvmConstructor superConstructor = anonymousClassUtil.getSuperTypeConstructor(anon);
					if(superConstructor != null)
						return superConstructor;
				}
			}
		}
	}
	return referencedElement;
}
 
Example #13
Source Project: xtext-eclipse   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
protected void doTestInnerType_WrappedIterator_02(JvmGenericType wrappedIterator) {
	assertEquals(3, Iterables.size(wrappedIterator.getDeclaredConstructors()));
	JvmConstructor constructor = (JvmConstructor) Iterables.find(wrappedIterator.getMembers(),
			new Predicate<JvmMember>() {
				@Override
				public boolean apply(JvmMember input) {
					return (input instanceof JvmConstructor) && input.getSimpleName().equals("WrappedIterator")
							&& ((JvmConstructor) input).getParameters().size() == 3;
				}
			});
	assertNotNull(constructor);
	JvmFormalParameter firstParameter = constructor.getParameters().get(0);
	assertEquals(1, firstParameter.getAnnotations().size());
	assertEquals("java.lang.String", firstParameter.getParameterType().getIdentifier());
	assertEquals(TestAnnotationWithDefaults.class.getName(),
			firstParameter.getAnnotations().get(0).getAnnotation().getQualifiedName());
	JvmFormalParameter secondParameter = constructor.getParameters().get(1);
	assertEquals(0, secondParameter.getAnnotations().size());
	assertEquals("int", secondParameter.getParameterType().getIdentifier());
	JvmFormalParameter thirdParameter = constructor.getParameters().get(2);
	assertEquals(1, thirdParameter.getAnnotations().size());
	assertEquals("java.util.Iterator<V>", thirdParameter.getParameterType().getIdentifier());
	assertEquals(TestAnnotation.NestedAnnotation.class.getName(),
			thirdParameter.getAnnotations().get(0).getAnnotation().getQualifiedName());
}
 
Example #14
Source Project: xtext-extras   Author: eclipse   File: CollectionLiteralsTypeComputer.java    License: Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates a map type reference that comes as close as possible / necessary to its expected type.
 */
protected LightweightTypeReference createMapTypeReference(JvmGenericType mapType, LightweightTypeReference pairType, LightweightTypeReference expectation, ITypeReferenceOwner owner) {
	List<LightweightTypeReference> leftAndRight = pairType.getTypeArguments();
	
	LightweightTypeReference left = leftAndRight.get(0).getInvariantBoundSubstitute();
	LightweightTypeReference right = leftAndRight.get(1).getInvariantBoundSubstitute();
	
	LightweightTypeReference mapExpectation = getMapExpectation(expectation);
	if (mapExpectation != null) {
		List<LightweightTypeReference> typeArguments = expectation.getTypeArguments();
		left = doNormalizeElementType(left, typeArguments.get(0));
		right = doNormalizeElementType(right, typeArguments.get(1));
	}
	ParameterizedTypeReference result = owner.newParameterizedTypeReference(mapType);
	result.addTypeArgument(left.copyInto(owner));
	result.addTypeArgument(right.copyInto(owner));
	if (mapExpectation != null && !expectation.isAssignableFrom(result)) {
		// expectation does not match the computed type, but looks good according to the element types:
		// use expected type
		if (matchesExpectation(left, mapExpectation.getTypeArguments().get(0)) && matchesExpectation(right, mapExpectation.getTypeArguments().get(1))) {
			return expectation;
		}
	}
	return result;
}
 
Example #15
Source Project: xtext-eclipse   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMemberCount_12() {
	String typeName = Fields.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	int constructorCount = Fields.class.getDeclaredConstructors().length;
	assertEquals(1, constructorCount); // default constructor
	int fieldCount = Fields.class.getDeclaredFields().length;
	assertEquals(7, fieldCount);
	int nestedCount = Fields.class.getDeclaredClasses().length;
	assertEquals(1, nestedCount);
	assertEquals(nestedCount + constructorCount + fieldCount, type.getMembers().size());
	diagnose(type);
	Resource resource = type.eResource();
	getAndResolveAllFragments(resource);
	recomputeAndCheckIdentifiers(resource);
}
 
Example #16
Source Project: xtext-eclipse   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testFindTypeByName_javaUtilList_01() {
	String typeName = List.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	assertNotNull(type);
	assertEquals(typeName, type.getIdentifier());
	assertEquals(1, type.getTypeParameters().size());
	JvmTypeParameter typeVariable = type.getTypeParameters().get(0);
	assertEquals("E", typeVariable.getName());
	assertEquals(1, typeVariable.getConstraints().size());
	JvmTypeConstraint typeConstraint = typeVariable.getConstraints().get(0);
	assertTrue(typeConstraint instanceof JvmUpperBound);
	JvmUpperBound upperBound = (JvmUpperBound) typeConstraint;
	assertSame(typeVariable, upperBound.getOwner());
	assertNotNull(upperBound.getTypeReference());
	assertFalse(upperBound.getTypeReference().getType().eIsProxy());
	assertEquals(Object.class.getName(), upperBound.getTypeReference().getIdentifier());
	diagnose(type);
	Resource resource = type.eResource();
	getAndResolveAllFragments(resource);
	recomputeAndCheckIdentifiers(resource);
}
 
Example #17
Source Project: xtext-extras   Author: eclipse   File: JvmTypesBuilderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testIntegerAnnotation() {
  try {
    final XAnnotationsFactory f = XAnnotationsFactory.eINSTANCE;
    final XExpression e = this.expression("\'Foo\'");
    final XAnnotation anno = f.createXAnnotation();
    JvmType _findDeclaredType = this.references.findDeclaredType(TestAnnotation3.class, e);
    final JvmAnnotationType annotatiomType = ((JvmAnnotationType) _findDeclaredType);
    anno.setAnnotationType(annotatiomType);
    final XAnnotationElementValuePair pair = f.createXAnnotationElementValuePair();
    pair.setElement(IterableExtensions.<JvmOperation>head(annotatiomType.getDeclaredOperations()));
    pair.setValue(this.expression("10"));
    EList<XAnnotationElementValuePair> _elementValuePairs = anno.getElementValuePairs();
    this._jvmTypesBuilder.<XAnnotationElementValuePair>operator_add(_elementValuePairs, pair);
    final JvmGenericType type = this.typesFactory.createJvmGenericType();
    this._jvmTypesBuilder.addAnnotation(type, anno);
    Assert.assertEquals(anno.getAnnotationType(), IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getAnnotation());
    Assert.assertEquals(1, IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues().size());
    JvmAnnotationValue _head = IterableExtensions.<JvmAnnotationValue>head(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues());
    final JvmCustomAnnotationValue value = ((JvmCustomAnnotationValue) _head);
    EObject _head_1 = IterableExtensions.<EObject>head(value.getValues());
    Assert.assertTrue((_head_1 instanceof XNumberLiteral));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #18
Source Project: xtext-extras   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_W_01() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmTypeParameter typeParameterW = type.getTypeParameters().get(4);
	assertEquals("W", typeParameterW.getIdentifier());
	assertSame(type, typeParameterW.getDeclarator());
	assertEquals(2, typeParameterW.getConstraints().size());
	JvmTypeConstraint firstTypeConstraint = typeParameterW.getConstraints().get(0);
	assertTrue(firstTypeConstraint instanceof JvmUpperBound);
	JvmUpperBound firstUpperBound = (JvmUpperBound) firstTypeConstraint;
	assertNotNull(firstUpperBound.getTypeReference());
	assertFalse(firstUpperBound.getTypeReference().toString(), firstUpperBound.getTypeReference().eIsProxy());
	assertEquals("java.lang.Comparable<S>", firstUpperBound.getTypeReference().getIdentifier());
	JvmParameterizedTypeReference comparableType = (JvmParameterizedTypeReference) firstUpperBound
			.getTypeReference();
	assertEquals(1, comparableType.getArguments().size());
	JvmTypeReference typeArgument = comparableType.getArguments().get(0);
	assertEquals("S", typeArgument.getIdentifier());
	JvmTypeParameter s = type.getTypeParameters().get(0);
	assertSame(s, typeArgument.getType());
}
 
Example #19
Source Project: xtext-extras   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_04() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation methodY = getMethodFromType(type, ParameterizedTypes.class, "methodY(Y)");
	assertEquals(1, methodY.getParameters().size());
	JvmType parameterType = methodY.getParameters().get(0).getParameterType().getType();
	assertFalse(parameterType.eIsProxy());
	assertEquals("Y", parameterType.getIdentifier());
	assertTrue(parameterType instanceof JvmTypeParameter);
	assertSame(methodY, ((JvmTypeParameter) parameterType).getDeclarator());
	JvmTypeParameter y = (JvmTypeParameter) parameterType;
	assertEquals(1, y.getConstraints().size());
	JvmUpperBound upperBound = (JvmUpperBound) y.getConstraints().get(0);
	JvmTypeParameter t = type.getTypeParameters().get(1);
	assertSame(t, upperBound.getTypeReference().getType());
}
 
Example #20
Source Project: xtext-extras   Author: eclipse   File: OverrideProposalUtil.java    License: Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isCandidate(LightweightTypeReference type, IResolvedExecutable executable,
		IVisibilityHelper visibilityHelper) {
	JvmDeclaredType declaringType = executable.getDeclaration().getDeclaringType();
	if (type.getType() != declaringType && isVisible(executable, visibilityHelper)) {
		JvmExecutable rawExecutable = executable.getDeclaration();
		if (rawExecutable instanceof JvmOperation) {
			JvmOperation operation = (JvmOperation) rawExecutable;
			if (operation.isFinal() || operation.isStatic()) {
				return false;
			} else {
				if (type.getType() instanceof JvmGenericType && ((JvmGenericType) type.getType()).isInterface()) {
					return  declaringType instanceof JvmGenericType
							&& ((JvmGenericType) declaringType).isInterface() && !operation.isAbstract();
				} else {
					return true;
				}
			}
		} else {
			return true;
		}
	}
	return false;
}
 
Example #21
Source Project: xtext-extras   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_U_01() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmTypeParameter typeVariable = type.getTypeParameters().get(2);
	assertEquals("U", typeVariable.getIdentifier());
	assertSame(type, typeVariable.getDeclarator());
	assertEquals(1, typeVariable.getConstraints().size());
	JvmTypeConstraint typeConstraint = typeVariable.getConstraints().get(0);
	assertTrue(typeConstraint instanceof JvmUpperBound);
	JvmUpperBound upperBound = (JvmUpperBound) typeConstraint;
	assertNotNull(upperBound.getTypeReference());
	assertFalse(upperBound.getTypeReference().toString(), upperBound.getTypeReference().eIsProxy());
	assertEquals("java.util.List<S>", upperBound.getTypeReference().getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) upperBound.getTypeReference();
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	JvmTypeParameter s = type.getTypeParameters().get(0);
	assertSame(s, typeArgument.getType());
}
 
Example #22
Source Project: xtext-extras   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_Inner_05() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation methodV = getMethodFromType(type, ParameterizedTypes.Inner.class, "methodVArray_01()");
	JvmTypeReference listT = methodV.getReturnType();
	assertEquals("java.util.List<? extends V>[]", listT.getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) ((JvmGenericArrayTypeReference) listT)
			.getComponentType();
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	assertTrue(typeArgument instanceof JvmWildcardTypeReference);
	JvmWildcardTypeReference wildcardTypeArgument = (JvmWildcardTypeReference) typeArgument;
	assertEquals("? extends V", wildcardTypeArgument.getIdentifier());
	assertEquals(1, wildcardTypeArgument.getConstraints().size());
	JvmUpperBound upperBound = (JvmUpperBound) wildcardTypeArgument.getConstraints().get(0);
	JvmTypeParameter v = type.getTypeParameters().get(3);
	assertSame(v, upperBound.getTypeReference().getType());
}
 
Example #23
Source Project: xtext-extras   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_W_02() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmTypeParameter typeParameterW = type.getTypeParameters().get(4);
	assertEquals("W", typeParameterW.getIdentifier());
	assertSame(type, typeParameterW.getDeclarator());
	assertEquals(2, typeParameterW.getConstraints().size());

	JvmTypeConstraint secondTypeConstraint = typeParameterW.getConstraints().get(1);
	assertTrue(secondTypeConstraint instanceof JvmUpperBound);
	JvmUpperBound secondUpperBound = (JvmUpperBound) secondTypeConstraint;
	assertNotNull(secondUpperBound.getTypeReference());
	assertFalse(secondUpperBound.getTypeReference().toString(), secondUpperBound.getTypeReference().eIsProxy());
	assertEquals("java.io.Serializable", secondUpperBound.getTypeReference().getIdentifier());
}
 
Example #24
Source Project: xtext-extras   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMemberCount_03() {
	String typeName = InitializerWithoutConstructor.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	int methodCount = InitializerWithoutConstructor.class.getDeclaredMethods().length;
	assertEquals(0, methodCount);
	int constructorCount = InitializerWithoutConstructor.class.getDeclaredConstructors().length;
	assertEquals(1, constructorCount); // default constructor
	assertEquals(methodCount + constructorCount, type.getMembers().size());
	diagnose(type);
	Resource resource = type.eResource();
	getAndResolveAllFragments(resource);
	recomputeAndCheckIdentifiers(resource);
}
 
Example #25
Source Project: xtext-extras   Author: eclipse   File: ReusedTypeProviderTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
@Override
public void testFindTypeByName_AbstractMultimap_02() {
	String typeName = "com.google.common.collect.AbstractMultimap";
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation containsValue = (JvmOperation) Iterables
			.getOnlyElement(type.findAllFeaturesByName("containsValue"));
	Assert.assertNotNull(containsValue);
	JvmFormalParameter firstParam = containsValue.getParameters().get(0);
	Assert.assertEquals(0, firstParam.getAnnotations().size());
}
 
Example #26
Source Project: xtext-extras   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
protected void assertMembers(String typeName, Set<String> memberNames) {
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	List<JvmMember> members = type.getMembers();
	assertEquals(memberNames.size(), members.size());
	for (org.eclipse.xtext.common.types.JvmMember member : members) {
		assertTrue(member.getIdentifier(), member instanceof JvmOperation);
		JvmOperation op = (JvmOperation) member;
		assertTrue(op.getSimpleName(), memberNames.remove(op.getSimpleName()));
	}
	assertTrue(memberNames.isEmpty());
}
 
Example #27
Source Project: xtext-extras   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFindTypeByName_javaLangCharSequence_03() {
	String typeName = CharSequence.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation length = (JvmOperation) type.eResource().getEObject("java.lang.CharSequence.length()");
	assertNotNull(length);
	JvmType returnType = length.getReturnType().getType();
	assertNotNull(returnType);
	assertFalse(returnType.eIsProxy());
	assertEquals("int", returnType.getIdentifier());
}
 
Example #28
Source Project: xtext-xtend   Author: eclipse   File: InferredJvmModelTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testDispatchFunction_02() throws Exception {
	XtendFile xtendFile = file("class Foo { def dispatch foo(Object x) {'foo'} def dispatch foo(String x) {'bar'}}");
	JvmGenericType inferredType = getInferredType(xtendFile);
	
	// one main dispatch
	Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
	JvmOperation dispatch = findByNameAndFirstParameterType(operations, "foo", Object.class);
	assertEquals(String.class.getName(), dispatch.getReturnType().getIdentifier());
	
	// two internal case methods
	findByNameAndFirstParameterType(operations, "_foo", Object.class);
	findByNameAndFirstParameterType(operations, "_foo", String.class);
}
 
Example #29
Source Project: sarl   Author: sarl   File: InheritanceHelper.java    License: Apache License 2.0 5 votes vote down vote up
/** Extract and replies the Ecore type, provided by {@link SarlPackage} for the given JvmElement.
 *
 * @param type the JVM type to test.
 * @return the code of the type, see {@link SarlPackage}; or {@code 0} if the code is unknown.
 * @since 0.6
 */
public int getSarlElementEcoreType(JvmGenericType type) {
	final JvmAnnotationReference annotationRef = this.annotationUtil.findAnnotation(type, SarlElementType.class.getName());
	if (annotationRef != null) {
		final Integer intValue = this.annotationUtil.findIntValue(annotationRef);
		if (intValue != null) {
			return intValue.intValue();
		}
	}
	return 0;
}
 
Example #30
Source Project: sarl   Author: sarl   File: SarlJvmModelAssociations.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public SarlSpace getSarlSpace(JvmGenericType jvmType) {
	final EObject primarySourceElement = getPrimarySourceElement(jvmType);
	if (primarySourceElement instanceof SarlSpace) {
		return (SarlSpace) primarySourceElement;
	}
	return null;
}