org.eclipse.xtext.common.types.JvmTypeParameter Java Examples

The following examples show how to use org.eclipse.xtext.common.types.JvmTypeParameter. 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: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_Inner_08() {
	String typeName = ParameterizedTypes.Inner.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation methodV = getMethodFromType(type, ParameterizedTypes.Inner.class, "methodZArray_02()");
	JvmTypeReference listZ = methodV.getReturnType();
	assertEquals("java.util.List<Z[]>[]", listZ.getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) ((JvmGenericArrayTypeReference) listZ)
			.getComponentType();
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	JvmType argumentType = typeArgument.getType();
	assertTrue(argumentType instanceof JvmArrayType);
	JvmComponentType componentType = ((JvmArrayType) argumentType).getComponentType();
	JvmTypeParameter z = type.getTypeParameters().get(2);
	assertSame(z, componentType);
}
 
Example #2
Source File: AbstractPendingLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns the unresolved string representation of the unresolved type parameters of the feature. The simple names of
 * the type bounds are used. The string representation includes the angle brackets. 
 */
protected String getFeatureTypeParametersAsString(boolean showBounds) {
	List<JvmTypeParameter> typeParameters = getDeclaredTypeParameters();
	if(!typeParameters.isEmpty()) {
		StringBuilder b = new StringBuilder();
		b.append("<");
		for(int i=0; i<typeParameters.size(); ++i) {
			JvmTypeParameter typeParameter = typeParameters.get(i);
			if(showBounds)
				b.append(getTypeParameterAsString(typeParameter));
			else 
				b.append(typeParameter.getSimpleName());
			if(i < typeParameters.size()-1)
				b.append(", ");
		}
		b.append(">");
		return b.toString();
	}
	return "";
}
 
Example #3
Source File: XtendInterfaceImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@SuppressWarnings("unchecked")
@Override
public void eSet(int featureID, Object newValue)
{
	switch (featureID)
	{
		case XtendPackage.XTEND_INTERFACE__EXTENDS:
			getExtends().clear();
			getExtends().addAll((Collection<? extends JvmTypeReference>)newValue);
			return;
		case XtendPackage.XTEND_INTERFACE__TYPE_PARAMETERS:
			getTypeParameters().clear();
			getTypeParameters().addAll((Collection<? extends JvmTypeParameter>)newValue);
			return;
	}
	super.eSet(featureID, newValue);
}
 
Example #4
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_03() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation methodT = getMethodFromType(type, ParameterizedTypes.class, "methodT(T)");
	JvmTypeReference listS = methodT.getReturnType();
	assertFalse(listS.toString(), listS.eIsProxy());
	assertEquals("java.util.List<? extends java.lang.Object & super T>", listS.getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) listS;
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	assertTrue(typeArgument instanceof JvmWildcardTypeReference);
	JvmWildcardTypeReference wildcardTypeArgument = (JvmWildcardTypeReference) typeArgument;
	assertEquals("? extends java.lang.Object & super T", wildcardTypeArgument.getIdentifier());
	assertEquals(2, wildcardTypeArgument.getConstraints().size());
	JvmUpperBound uperBound = (JvmUpperBound) wildcardTypeArgument.getConstraints().get(0);
	assertEquals("java.lang.Object", uperBound.getTypeReference().getIdentifier());
	JvmLowerBound lowerBound = (JvmLowerBound) wildcardTypeArgument.getConstraints().get(1);
	JvmTypeParameter t = type.getTypeParameters().get(1);
	assertSame(t, lowerBound.getTypeReference().getType());
}
 
Example #5
Source File: AbstractClassMirror.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String getFragment(EObject obj, IFragmentProvider.Fallback fallback) {
	if (obj instanceof JvmTypeParameter)
		return getFragment(obj.eContainer(), fallback) + "/" + ((JvmTypeParameter) obj).getName();
	if (obj instanceof JvmTypeReference || obj instanceof JvmFormalParameter)
		return fallback.getFragment(obj);
	if (obj instanceof JvmIdentifiableElement) {
		if (obj instanceof JvmArrayType) {
			if (obj.eContainer() instanceof JvmGenericArrayTypeReference)
				return fallback.getFragment(obj);
			if (obj.eContainer() instanceof JvmIdentifiableElement) {
				return getFragment(obj.eContainer(), fallback) + "[]";
			}
		}
		return ((JvmIdentifiableElement) obj).getIdentifier();
	}
	return fallback.getFragment(obj);
}
 
Example #6
Source File: AbstractTypeProviderTest.java    From xtext-extras with 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 #7
Source File: AbstractTypeProviderTest.java    From xtext-extras with 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 #8
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_03() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation methodT = getMethodFromType(type, ParameterizedTypes.class, "methodT(T)");
	JvmTypeReference listS = methodT.getReturnType();
	assertFalse(listS.toString(), listS.eIsProxy());
	assertEquals("java.util.List<? extends java.lang.Object & super T>", listS.getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) listS;
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	assertTrue(typeArgument instanceof JvmWildcardTypeReference);
	JvmWildcardTypeReference wildcardTypeArgument = (JvmWildcardTypeReference) typeArgument;
	assertEquals("? extends java.lang.Object & super T", wildcardTypeArgument.getIdentifier());
	assertEquals(2, wildcardTypeArgument.getConstraints().size());
	JvmUpperBound uperBound = (JvmUpperBound) wildcardTypeArgument.getConstraints().get(0);
	assertEquals("java.lang.Object", uperBound.getTypeReference().getIdentifier());
	JvmLowerBound lowerBound = (JvmLowerBound) wildcardTypeArgument.getConstraints().get(1);
	JvmTypeParameter t = type.getTypeParameters().get(1);
	assertSame(t, lowerBound.getTypeReference().getType());
}
 
Example #9
Source File: DeferredTypeParameterHintCollectorTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public void hasNoHintsFor(final Map<JvmTypeParameter, LightweightMergedBoundTypeArgument> mapping, final String typeParamName) {
  final Set<JvmTypeParameter> allKeys = mapping.keySet();
  for (final JvmTypeParameter key : allKeys) {
    String _simpleName = key.getSimpleName();
    boolean _equals = Objects.equal(_simpleName, typeParamName);
    if (_equals) {
      LightweightTypeReference _typeReference = mapping.get(key).getTypeReference();
      final UnboundTypeReference unbound = ((UnboundTypeReference) _typeReference);
      boolean _isEmpty = unbound.getAllHints().isEmpty();
      boolean _not = (!_isEmpty);
      if (_not) {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("Unexpected mapping for ");
        _builder.append(typeParamName);
        _builder.append(" in ");
        final Function1<JvmTypeParameter, String> _function = (JvmTypeParameter it) -> {
          return it.getSimpleName();
        };
        Iterable<String> _map = IterableExtensions.<JvmTypeParameter, String>map(mapping.keySet(), _function);
        _builder.append(_map);
        Assert.fail(_builder.toString());
      }
    }
  }
}
 
Example #10
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_Inner_07() {
	String typeName = ParameterizedTypes.Inner.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation methodV = getMethodFromType(type, ParameterizedTypes.Inner.class, "methodZArray_01()");
	JvmTypeReference listZ = methodV.getReturnType();
	assertEquals("java.util.List<Z[][]>", listZ.getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) listZ;
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	JvmType argumentType = typeArgument.getType();
	assertTrue(argumentType instanceof JvmArrayType);
	assertTrue(((JvmArrayType) argumentType).getComponentType() instanceof JvmArrayType);
	JvmComponentType componentType = ((JvmArrayType) ((JvmArrayType) argumentType).getComponentType())
			.getComponentType();
	JvmTypeParameter z = type.getTypeParameters().get(2);
	assertSame(z, componentType);
}
 
Example #11
Source File: AbstractLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public List<LightweightTypeReference> getTypeArguments() {
	if (typeArguments == null) {
		List<JvmTypeParameter> typeParameters = getDeclaredTypeParameters();
		if (typeParameters.isEmpty() || getTypeParameterMapping().isEmpty()) {
			typeArguments = Collections.emptyList();
		} else {
			List<LightweightTypeReference> result = Lists.newArrayListWithCapacity(typeParameters.size());
			for(JvmTypeParameter parameter: typeParameters) {
				LightweightMergedBoundTypeArgument typeArgument = getTypeParameterMapping().get(parameter);
				result.add(typeArgument.getTypeReference());
			}
			typeArguments = result;
		}
	}
	return typeArguments;
}
 
Example #12
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_Inner_Y_01() {
	String typeName = ParameterizedTypes.Inner.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmTypeParameter typeParameterY = type.getTypeParameters().get(1);
	assertEquals("Y", typeParameterY.getIdentifier());
	assertSame(type, typeParameterY.getDeclarator());
	assertEquals(1, typeParameterY.getConstraints().size());
	JvmTypeConstraint typeConstraint = typeParameterY.getConstraints().get(0);
	assertTrue(typeConstraint instanceof JvmUpperBound);
	JvmUpperBound upperBound = (JvmUpperBound) typeConstraint;
	assertNotNull(upperBound.getTypeReference());
	assertEquals("java.util.List<X>", upperBound.getTypeReference().getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) upperBound.getTypeReference();
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	assertEquals("X", typeArgument.getIdentifier());
	JvmTypeParameter x = type.getTypeParameters().get(0);
	assertSame(x, typeArgument.getType());
}
 
Example #13
Source File: XtendClassImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@SuppressWarnings("unchecked")
@Override
public void eSet(int featureID, Object newValue)
{
	switch (featureID)
	{
		case XtendPackage.XTEND_CLASS__EXTENDS:
			setExtends((JvmTypeReference)newValue);
			return;
		case XtendPackage.XTEND_CLASS__IMPLEMENTS:
			getImplements().clear();
			getImplements().addAll((Collection<? extends JvmTypeReference>)newValue);
			return;
		case XtendPackage.XTEND_CLASS__TYPE_PARAMETERS:
			getTypeParameters().clear();
			getTypeParameters().addAll((Collection<? extends JvmTypeParameter>)newValue);
			return;
	}
	super.eSet(featureID, newValue);
}
 
Example #14
Source File: InterfaceParsingTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void interfaceGeneric_XextendsNumber() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"package io.sarl.lang.tests.test",
			"interface I1<X extends Number> {",
			"	def setX(param : X)",
			"	def getX : X",
			"}"));
	assertEquals("io.sarl.lang.tests.test", mas.getPackage());
	SarlInterface interf = (SarlInterface) mas.getXtendTypes().get(0);
	assertNotNull(interf);
	//
	assertEquals("I1", interf.getName());
	//
	assertEquals(1, interf.getTypeParameters().size());
	//
	JvmTypeParameter parameter = interf.getTypeParameters().get(0);
	assertEquals("X", parameter.getName());
	assertEquals(1, parameter.getConstraints().size());
	//
	JvmTypeConstraint constraint = parameter.getConstraints().get(0);
	assertEquals("java.lang.Number", constraint.getTypeReference().getIdentifier());
	assertTrue(constraint.getIdentifier().startsWith("extends"));
}
 
Example #15
Source File: AgentParsingTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
public void functionGeneric_X_sarlNotation() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"package io.sarl.lang.tests.test",
			"agent A1 {",
			"	def setX(param : X) : void with X { var xxx : X }",
			"}"));
	assertEquals("io.sarl.lang.tests.test", mas.getPackage());
	SarlAgent agent = (SarlAgent) mas.getXtendTypes().get(0);
	assertNotNull(agent);
	//
	assertEquals("A1", agent.getName());
	assertEquals(1, agent.getMembers().size());
	//
	SarlAction action = (SarlAction) agent.getMembers().get(0);
	assertEquals("setX", action.getName());
	assertEquals(1, action.getTypeParameters().size());
	//
	JvmTypeParameter parameter = action.getTypeParameters().get(0);
	assertEquals("X", parameter.getName());
	assertNullOrEmpty(parameter.getConstraints());
}
 
Example #16
Source File: ArrayTypes.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * This handles bogus constraint definitions, e.g. 
 * <ul>
 * 	<li>{@code T extends String[]},</li>
 * 	<li>{@code T extends V[]}, or</li>
 * 	<li>{@code T extends V, V extends String[]}</li>
 * </ul>
 */
/* @Nullable */
private ArrayTypeReference doTryConvertToArray(ParameterizedTypeReference typeReference, RecursionGuard<JvmTypeParameter> recursionGuard) {
	JvmType type = typeReference.getType();
	if (recursionGuard.tryNext((JvmTypeParameter) type)) {
		List<LightweightTypeReference> superTypes = typeReference.getSuperTypes();
		for(int i = 0, size = superTypes.size(); i < size; i++) {
			LightweightTypeReference superType = superTypes.get(i);
			if (superType.isArray()) {
				return (ArrayTypeReference) superType;
			}
			ArrayTypeReference result = doTryConvertToArray(typeReference);
			if (result != null) {
				return result;
			} else {
				JvmType rawSuperType = superType.getType();
				if (rawSuperType != null && rawSuperType.eClass() == TypesPackage.Literals.JVM_TYPE_PARAMETER) {
					result = doTryConvertToArray((ParameterizedTypeReference) superType, recursionGuard);
					if (result != null)
						return result;
				}
			}
		}
	}
	return null;
}
 
Example #17
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with 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 #18
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with 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 #19
Source File: AbstractResolvedFeature.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isResolvedTypeParameter(LightweightTypeReference typeReference, JvmTypeParameter typeParameter) {
	List<LightweightTypeReference> typeArguments = typeReference.getTypeArguments();
	for(int i = 0, size = typeArguments.size(); i < size; i++) {
		LightweightTypeReference typeArgument = typeArguments.get(i);
		if (typeParameter.equals(typeArgument.getType()) || isResolvedTypeParameter(typeArgument, typeParameter)) {
			return true;
		}
	}
	LightweightTypeReference outer = typeReference.getOuter();
	if (outer != null) {
		if (isResolvedTypeParameter(outer, typeParameter)) {
			return true;
		}
	}
	return false;
}
 
Example #20
Source File: ElementOrComponentTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public LightweightTypeReference doVisitParameterizedTypeReference(ParameterizedTypeReference reference) {
	DeclaratorTypeArgumentCollector typeArgumentCollector = new ConstraintAwareTypeArgumentCollector(owner);
	Map<JvmTypeParameter, LightweightMergedBoundTypeArgument> typeParameterMapping = typeArgumentCollector.getTypeParameterMapping(reference);
	TypeParameterSubstitutor<?> substitutor = new UnboundTypeParameterPreservingSubstitutor(typeParameterMapping, owner);
	JvmGenericType iterable = (JvmGenericType) owner.getServices().getTypeReferences().findDeclaredType(Iterable.class, owner.getContextResourceSet());
	if (iterable == null) {
		return owner.newReferenceToObject();
	}
	LightweightTypeReference substituteMe = owner.newParameterizedTypeReference(iterable.getTypeParameters().get(0));
	LightweightTypeReference substitutedArgument = substitutor.substitute(substituteMe).getUpperBoundSubstitute();
	if (substitutedArgument.getType() instanceof JvmTypeParameter && 
			!(owner.getDeclaredTypeParameters().contains(substitutedArgument.getType()))) {
		return substitutedArgument.getRawTypeReference();
	}
	return substitutedArgument;
}
 
Example #21
Source File: ParserTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testTypeParams_1() throws Exception {
	XtendFunction func = function("def <T> foo(T t) {t}");
	assertEquals(1, func.getTypeParameters().size());
	JvmTypeParameter tp = func.getTypeParameters().get(0);
	assertEquals("T", tp.getName());
	assertEquals(0, tp.getConstraints().size());
}
 
Example #22
Source File: ActualTypeArgumentCollectorTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBestEffortMapping_04() {
  Map<JvmTypeParameter, List<LightweightBoundTypeArgument>> _mappedBy = this.mappedBy("T", "java.util.HashMap<java.util.ArrayList<T>, java.util.ArrayList<T>>", "java.util.Map<Iterable<String>, java.util.HashSet<Integer>>");
  Pair<String, VarianceInfo> _mappedTo = Pair.<String, VarianceInfo>of("String", VarianceInfo.INVARIANT);
  Triple<String, VarianceInfo, VarianceInfo> _mappedTo_1 = this.operator_mappedTo(_mappedTo, VarianceInfo.INVARIANT);
  Pair<String, VarianceInfo> _mappedTo_2 = Pair.<String, VarianceInfo>of("Integer", VarianceInfo.INVARIANT);
  Triple<String, VarianceInfo, VarianceInfo> _mappedTo_3 = this.operator_mappedTo(_mappedTo_2, VarianceInfo.INVARIANT);
  this.assertMapping(_mappedBy, "T", _mappedTo_1, _mappedTo_3);
}
 
Example #23
Source File: ActualTypeArgumentCollectorTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testCircularTypeParams_11() {
  Map<JvmTypeParameter, List<LightweightBoundTypeArgument>> _mappedBy = this.mappedBy("T extends (Object)=>T", "T", "Object");
  Pair<String, VarianceInfo> _mappedTo = Pair.<String, VarianceInfo>of("Object", VarianceInfo.OUT);
  Triple<String, VarianceInfo, VarianceInfo> _mappedTo_1 = this.operator_mappedTo(_mappedTo, VarianceInfo.OUT);
  this.assertMapping(_mappedBy, "T", _mappedTo_1);
}
 
Example #24
Source File: ActualTypeArgumentCollectorTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testCircularTypeParams_02() {
  Map<JvmTypeParameter, List<LightweightBoundTypeArgument>> _mappedBy = this.mappedBy("T extends Iterable<T>", "CharSequence", "String");
  Pair<String, VarianceInfo> _mappedTo = Pair.<String, VarianceInfo>of("Iterable<Object>", VarianceInfo.OUT);
  Triple<String, VarianceInfo, VarianceInfo> _mappedTo_1 = this.operator_mappedTo(_mappedTo, VarianceInfo.OUT);
  this.assertMapping(_mappedBy, "T", _mappedTo_1);
}
 
Example #25
Source File: TypeParameterScope.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected IEObjectDescription doGetSingleElement(QualifiedName name) {
	if (name.getSegmentCount() == 1) {
		String singleSegment = name.getFirstSegment();
		for(int i = 0; i < typeParameters.size(); i++) {
			List<JvmTypeParameter> chunk = typeParameters.get(i);
			for(int j = 0; j < chunk.size(); j++) {
				JvmTypeParameter candidate = chunk.get(j);
				if (singleSegment.equals(candidate.getSimpleName())) {
					return EObjectDescription.create(name, candidate);
				}	
			}
		}
	}
	return null;
}
 
Example #26
Source File: ActualTypeArgumentCollectorTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testDependentTypeParams_01() {
  Map<JvmTypeParameter, List<LightweightBoundTypeArgument>> _mappedBy = this.mappedBy("T extends CharSequence, T2 extends T", "java.util.Map<T, T2>", "java.util.Map<String, String>");
  Pair<String, VarianceInfo> _mappedTo = Pair.<String, VarianceInfo>of("String", VarianceInfo.INVARIANT);
  Triple<String, VarianceInfo, VarianceInfo> _mappedTo_1 = this.operator_mappedTo(_mappedTo, VarianceInfo.INVARIANT);
  Map<JvmTypeParameter, List<LightweightBoundTypeArgument>> _assertMapping = this.assertMapping(_mappedBy, "T", _mappedTo_1);
  Pair<String, VarianceInfo> _mappedTo_2 = Pair.<String, VarianceInfo>of("String", VarianceInfo.INVARIANT);
  Triple<String, VarianceInfo, VarianceInfo> _mappedTo_3 = this.operator_mappedTo(_mappedTo_2, VarianceInfo.INVARIANT);
  this.assertMapping(_assertMapping, "T2", _mappedTo_3);
}
 
Example #27
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_ParameterizedTypes_V_01() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmTypeParameter typeParameterV = type.getTypeParameters().get(3);
	assertEquals("V", typeParameterV.getIdentifier());
	assertSame(type, typeParameterV.getDeclarator());
	assertEquals(1, typeParameterV.getConstraints().size());
	JvmTypeConstraint typeConstraint = typeParameterV.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<java.util.List<? extends V>>", upperBound.getTypeReference().getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) upperBound.getTypeReference();
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	assertEquals("java.util.List<? extends V>", typeArgument.getIdentifier());
	JvmParameterizedTypeReference nestedListType = (JvmParameterizedTypeReference) typeArgument;
	assertEquals(1, nestedListType.getArguments().size());
	JvmTypeReference nestedArgument = nestedListType.getArguments().get(0);
	assertTrue(nestedArgument instanceof JvmWildcardTypeReference);
	assertEquals(1, ((JvmWildcardTypeReference) nestedArgument).getConstraints().size());
	JvmUpperBound nestedUpperBound = (JvmUpperBound) ((JvmWildcardTypeReference) nestedArgument).getConstraints()
			.get(0);
	assertSame(typeParameterV, nestedUpperBound.getTypeReference().getType());
}
 
Example #28
Source File: SkillParsingTest.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Test
@Tag("sarlParsing")
public void functionGeneric_XYextendsX_sarlNotation() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"package io.sarl.lang.tests.test",
			"capacity C1 { }",
			"skill S1 implements C1 {",
			"	def setX(param : X) : void with X, Y extends X { var xxx : X; var yyy : Y }",
			"}"));
	assertEquals("io.sarl.lang.tests.test", mas.getPackage());
	SarlSkill skill = (SarlSkill) mas.getXtendTypes().get(1);
	assertNotNull(skill);
	//
	assertEquals("S1", skill.getName());
	assertEquals(1, skill.getMembers().size());
	//
	SarlAction action = (SarlAction) skill.getMembers().get(0);
	assertEquals("setX", action.getName());
	assertEquals(2, action.getTypeParameters().size());
	//
	JvmTypeParameter parameter1 = action.getTypeParameters().get(0);
	assertEquals("X", parameter1.getName());
	assertNullOrEmpty(parameter1.getConstraints());
	//
	JvmTypeParameter parameter2 = action.getTypeParameters().get(1);
	assertEquals("Y", parameter2.getName());
	assertEquals(1, parameter2.getConstraints().size());
	//
	JvmTypeConstraint constraint = parameter2.getConstraints().get(0);
	assertEquals("X", constraint.getTypeReference().getIdentifier());
	assertTrue(constraint.getIdentifier().startsWith("extends"));
}
 
Example #29
Source File: XtendClassImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public EList<JvmTypeParameter> getTypeParameters()
{
	if (typeParameters == null)
	{
		typeParameters = new EObjectContainmentEList<JvmTypeParameter>(JvmTypeParameter.class, this, XtendPackage.XTEND_CLASS__TYPE_PARAMETERS);
	}
	return typeParameters;
}
 
Example #30
Source File: ReceiverFeatureScope.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected Map<JvmTypeParameter, LightweightMergedBoundTypeArgument> getReceiverTypeParameterMapping() {
	if (receiverTypeParameterMapping == null) {
		receiverTypeParameterMapping = Collections.emptyMap();
		if (receiverType != null) {
			receiverTypeParameterMapping = new DeclaratorTypeArgumentCollector().getTypeParameterMapping(receiverType);
		}
	}
	return receiverTypeParameterMapping;
}