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

The following examples show how to use org.eclipse.xtext.common.types.JvmTypeConstraint. 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: XtendJvmModelInferrer.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void fixTypeParameters(JvmTypeParameterDeclarator target) {
	for (JvmTypeParameter typeParameter : target.getTypeParameters()) {
		boolean upperBoundSeen = false;
		for (JvmTypeConstraint constraint : typeParameter.getConstraints()) {
			if (constraint instanceof JvmUpperBound) {
				upperBoundSeen = true;
				break;
			}
		}
		if (!upperBoundSeen) {
			JvmUpperBound upperBound = typesFactory.createJvmUpperBound();
			upperBound.setTypeReference(typeReferences.getTypeForName(Object.class, target));
			typeParameter.getConstraints().add(upperBound);
		}
	}
}
 
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 given type parameter. The simple names of
 * the type bounds are used. The string representation includes the bounds, except for
 * the upper bound {@link Object}. 
 */
protected String getTypeParameterAsString(JvmTypeParameter typeParameter) {
	StringBuilder b = new StringBuilder();
	b.append(typeParameter.getName());
	ITypeReferenceOwner referenceOwner = getState().getReferenceOwner();
	if(!typeParameter.getConstraints().isEmpty()) {
		boolean firstUpperBound = true;
		for(int j=0; j<typeParameter.getConstraints().size(); ++j) {
			JvmTypeConstraint constraint = typeParameter.getConstraints().get(j);
			LightweightTypeReference typeRef = referenceOwner.toLightweightTypeReference(constraint.getTypeReference());
			if(constraint instanceof JvmUpperBound) {
				if(typeRef.isType(Object.class))
					continue;
				if (firstUpperBound) {
					b.append(" extends ");
					firstUpperBound = false;
				} else {
					b.append(" & ");
				}
			} else 
				b.append(" super ");
			b.append(typeRef.getHumanReadableName());
		}
	}
	return b.toString();
}
 
Example #3
Source File: NameConcatHelper.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
static String computeFor(JvmWildcardTypeReference typeReference, char innerClassDelimiter, NameType nameType) {
	if (typeReference.eIsSet(TypesPackage.Literals.JVM_CONSTRAINT_OWNER__CONSTRAINTS)) {
		if (typeReference.getConstraints().size() == 1) {
			JvmTypeConstraint onlyConstraint = typeReference.getConstraints().get(0);
			if (nameType != NameType.ID && nameType != NameType.TO_STRING) {
				JvmTypeReference reference = onlyConstraint.getTypeReference();
				if (reference == null || (onlyConstraint instanceof JvmUpperBound && 
						Object.class.getCanonicalName().equals(onlyConstraint.getTypeReference().getIdentifier()))) {
					return "?";
				}
			} else if (nameType == NameType.ID && onlyConstraint.getTypeReference() == null) {
				return "?";
			}
		}
		StringBuilder mutableResult = new StringBuilder(64);
		mutableResult.append("? ");
		appendConstraintsName(mutableResult, typeReference.getConstraints(), innerClassDelimiter, nameType);
		return mutableResult.toString();
	}
	return "?";
}
 
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_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 #5
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_T_01() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmTypeParameter typeVariable = type.getTypeParameters().get(1);
	assertEquals("T", 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("S", upperBound.getTypeReference().getIdentifier());
	JvmTypeParameter s = type.getTypeParameters().get(0);
	assertSame(s, upperBound.getTypeReference().getType());
}
 
Example #6
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Java 5 does not allow forward references in type parameters, so we have to validate this, too
 */
public void doCheckTypeParameterForwardReference(List<JvmTypeParameter> sourceTypeParameters) {
	if (sourceTypeParameters.size() >= 1) {
		final Set<JvmTypeParameter> allowed = newHashSet();
		for(int i = 0; i < sourceTypeParameters.size(); i++) {
			JvmTypeParameter current = sourceTypeParameters.get(i);
			for(JvmTypeConstraint constraint: current.getConstraints()) {
				JvmTypeReference constraintRef = constraint.getTypeReference();
				if (constraintRef != null) {
					JvmType constraintType = constraintRef.getType();
					if (constraintType.eClass() == TypesPackage.Literals.JVM_TYPE_PARAMETER) {
						EObject sourceElement = associations.getPrimarySourceElement(constraintType);
						if (sourceElement!=null && sourceElement.eContainer() == current.eContainer() && !allowed.contains(sourceElement)) {
							error("Illegal forward reference to type parameter " + ((JvmTypeParameter)constraintType).getSimpleName(), 
									constraintRef, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, -1, TYPE_PARAMETER_FORWARD_REFERENCE);
						}
					}
				}
			}
			allowed.add(current);
		}
	}
}
 
Example #7
Source File: ClassParsingTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void classGeneric_XextendsNumber() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"package io.sarl.lang.tests.test",
			"class C1<X extends Number> {",
			"	var x : X",
			"	def setX(param : X) { this.x = param }",
			"	def getX : X { this.x }",
			"}"));
	assertEquals("io.sarl.lang.tests.test", mas.getPackage());
	SarlClass clazz = (SarlClass) mas.getXtendTypes().get(0);
	assertNotNull(clazz);
	//
	assertEquals("C1", clazz.getName());
	//
	assertEquals(1, clazz.getTypeParameters().size());
	//
	JvmTypeParameter parameter = clazz.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 #8
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_Z_01() {
	String typeName = ParameterizedTypes.Inner.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmTypeParameter typeParameterZ = type.getTypeParameters().get(2);
	assertEquals("Z", typeParameterZ.getIdentifier());
	assertSame(type, typeParameterZ.getDeclarator());
	assertEquals(1, typeParameterZ.getConstraints().size());
	JvmTypeConstraint typeConstraint = typeParameterZ.getConstraints().get(0);
	assertTrue(typeConstraint instanceof JvmUpperBound);
	JvmUpperBound upperBound = (JvmUpperBound) typeConstraint;
	assertNotNull(upperBound.getTypeReference());
	assertEquals("java.util.List<W>", upperBound.getTypeReference().getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) upperBound.getTypeReference();
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	assertEquals("W", typeArgument.getIdentifier());
	JvmTypeParameter w = ((JvmTypeParameterDeclarator) type.getDeclaringType()).getTypeParameters().get(4);
	assertSame(w, typeArgument.getType());
}
 
Example #9
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 #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_X_01() {
	String typeName = ParameterizedTypes.Inner.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmTypeParameter typeParameterX = type.getTypeParameters().get(0);
	assertEquals("X", typeParameterX.getIdentifier());
	assertSame(type, typeParameterX.getDeclarator());
	assertEquals(1, typeParameterX.getConstraints().size());
	JvmTypeConstraint typeConstraint = typeParameterX.getConstraints().get(0);
	assertTrue(typeConstraint instanceof JvmUpperBound);
	JvmUpperBound upperBound = (JvmUpperBound) typeConstraint;
	assertNotNull(upperBound.getTypeReference());
	assertEquals("W", upperBound.getTypeReference().getIdentifier());
	JvmType upperBoundType = upperBound.getTypeReference().getType();
	assertTrue(upperBoundType instanceof JvmTypeParameter);
	JvmTypeParameter typeParameterW = (JvmTypeParameter) upperBoundType;
	assertSame(type.getDeclaringType(), typeParameterW.getDeclarator());
}
 
Example #11
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_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 #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_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 #13
Source File: JvmTypeReferencesValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkNotMultipleBounds(JvmWildcardTypeReference typeRef) {
	List<JvmTypeConstraint> constraints = typeRef.getConstraints();
	if (constraints.size() >= 2) {
		int upperBounds = 0;
		int lowerBounds = 0;
		for(int i = 0; i < constraints.size(); i++) {
			JvmTypeConstraint constraint = constraints.get(i);
			if (constraint.eClass() == TypesPackage.Literals.JVM_UPPER_BOUND) {
				upperBounds++;
				if (upperBounds > 1) {
					error("Invalid type constraint. Cannot use multiple upper bounds in wildcards.", 
							typeRef, TypesPackage.Literals.JVM_CONSTRAINT_OWNER__CONSTRAINTS, i, IssueCodes.INVALID_WILDCARD_CONSTRAINTS);
					return;
				}
			} else {
				lowerBounds++;
				if (lowerBounds > 1) {
					error("Invalid type constraint. Cannot use multiple lower bounds in wildcards.", 
							typeRef, TypesPackage.Literals.JVM_CONSTRAINT_OWNER__CONSTRAINTS, i, IssueCodes.INVALID_WILDCARD_CONSTRAINTS);
				}
			}
		}
	}
}
 
Example #14
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_T_01() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmTypeParameter typeVariable = type.getTypeParameters().get(1);
	assertEquals("T", 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("S", upperBound.getTypeReference().getIdentifier());
	JvmTypeParameter s = type.getTypeParameters().get(0);
	assertSame(s, upperBound.getTypeReference().getType());
}
 
Example #15
Source File: RawTypeHelper.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected List<JvmType> getRawTypesFromConstraints(ITypeReferenceOwner owner, JvmTypeParameter typeParameter, ResourceSet resourceSet) {
	if (visited.add(typeParameter)) {
		List<JvmTypeConstraint> constraints = typeParameter.getConstraints();
		if (!constraints.isEmpty()) {
			List<JvmType> result = Lists.newArrayList();
			for(JvmTypeConstraint constraint: constraints) {
				if (constraint instanceof JvmUpperBound && constraint.getTypeReference() != null) {
					result.addAll(owner.toLightweightTypeReference(constraint.getTypeReference()).accept(this, resourceSet));
				}
			}
			if (!result.isEmpty())
				return result;
		}
	}
	return createObjectReference(resourceSet);
}
 
Example #16
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 #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_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 #18
Source File: TypeParameterByConstraintSubstitutor.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
/* @Nullable */
protected LightweightTypeReference getDeclaredUpperBound(JvmTypeParameter typeParameter, ConstraintVisitingInfo visiting) {
	if (!typeParameter.getConstraints().isEmpty() &&
				((DeclaredConstraintVisitingInfo)visiting).tryVisitDeclaredUpperBoundsOf(typeParameter)) {
		try {
			JvmTypeConstraint constraint = typeParameter.getConstraints().get(0);
			if (constraint instanceof JvmUpperBound) {
				LightweightTypeReference reference = getOwner().toLightweightTypeReference(constraint.getTypeReference());
				if (visiting.getCurrentDeclarator() != reference.getType()) {
					return reference.accept(this, visiting);
				}
				WildcardTypeReference result = getOwner().newWildcardTypeReference();
				result.addUpperBound(getObjectReference());
				return result;
			}
		} finally {
			((DeclaredConstraintVisitingInfo)visiting).didVisitDeclaredUpperBoundsOf(typeParameter);
		}
	}
	return null;
}
 
Example #19
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 #20
Source File: AbstractTypeProviderTest.java    From xtext-extras with 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 #21
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_X_01() {
	String typeName = ParameterizedTypes.Inner.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmTypeParameter typeParameterX = type.getTypeParameters().get(0);
	assertEquals("X", typeParameterX.getIdentifier());
	assertSame(type, typeParameterX.getDeclarator());
	assertEquals(1, typeParameterX.getConstraints().size());
	JvmTypeConstraint typeConstraint = typeParameterX.getConstraints().get(0);
	assertTrue(typeConstraint instanceof JvmUpperBound);
	JvmUpperBound upperBound = (JvmUpperBound) typeConstraint;
	assertNotNull(upperBound.getTypeReference());
	assertEquals("W", upperBound.getTypeReference().getIdentifier());
	JvmType upperBoundType = upperBound.getTypeReference().getType();
	assertTrue(upperBoundType instanceof JvmTypeParameter);
	JvmTypeParameter typeParameterW = (JvmTypeParameter) upperBoundType;
	assertSame(type.getDeclaringType(), typeParameterW.getDeclarator());
}
 
Example #22
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_Inner_Z_01() {
	String typeName = ParameterizedTypes.Inner.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmTypeParameter typeParameterZ = type.getTypeParameters().get(2);
	assertEquals("Z", typeParameterZ.getIdentifier());
	assertSame(type, typeParameterZ.getDeclarator());
	assertEquals(1, typeParameterZ.getConstraints().size());
	JvmTypeConstraint typeConstraint = typeParameterZ.getConstraints().get(0);
	assertTrue(typeConstraint instanceof JvmUpperBound);
	JvmUpperBound upperBound = (JvmUpperBound) typeConstraint;
	assertNotNull(upperBound.getTypeReference());
	assertEquals("java.util.List<W>", upperBound.getTypeReference().getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) upperBound.getTypeReference();
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	assertEquals("W", typeArgument.getIdentifier());
	JvmTypeParameter w = ((JvmTypeParameterDeclarator) type.getDeclaringType()).getTypeParameters().get(4);
	assertSame(w, typeArgument.getType());
}
 
Example #23
Source File: AgentParsingTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
public void functionGeneric_XextendsNumber_sarlNotation() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"package io.sarl.lang.tests.test",
			"agent A1 {",
			"	def setX(param : X) : void with X extends Number { 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());
	assertEquals(1, parameter.getConstraints().size());
	//
	JvmTypeConstraint constraint = parameter.getConstraints().get(0);
	assertEquals("java.lang.Number", constraint.getTypeReference().getIdentifier());
	assertTrue(constraint.getIdentifier().startsWith("extends"));
}
 
Example #24
Source File: AbstractTypeProviderTest.java    From xtext-eclipse 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 #25
Source File: AbstractTypeProviderTest.java    From xtext-extras with 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 #26
Source File: AgentParsingTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
public void functionGeneric_XextendsNumber_javaNotation() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"package io.sarl.lang.tests.test",
			"agent A1 {",
			"	def <X extends Number> setX(param : X) : void { 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());
	assertEquals(1, parameter.getConstraints().size());
	//
	JvmTypeConstraint constraint = parameter.getConstraints().get(0);
	assertEquals("java.lang.Number", constraint.getTypeReference().getIdentifier());
	assertTrue(constraint.getIdentifier().startsWith("extends"));
}
 
Example #27
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_T_01() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmTypeParameter typeVariable = type.getTypeParameters().get(1);
	assertEquals("T", 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("S", upperBound.getTypeReference().getIdentifier());
	JvmTypeParameter s = type.getTypeParameters().get(0);
	assertSame(s, upperBound.getTypeReference().getType());
}
 
Example #28
Source File: AbstractTypeProviderTest.java    From xtext-extras 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 #29
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_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 #30
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());
}