Java Code Examples for org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference#addTypeArgument()

The following examples show how to use org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference#addTypeArgument() . 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: CollectionLiteralsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates a collection type reference that comes as close as possible / necessary to its expected type.
 */
protected LightweightTypeReference createCollectionTypeReference(JvmGenericType collectionType, LightweightTypeReference elementType, LightweightTypeReference expectedType, ITypeReferenceOwner owner) {
	ParameterizedTypeReference result = new ParameterizedTypeReference(owner, collectionType);
	result.addTypeArgument(elementType);
	if (isIterableExpectation(expectedType) && !expectedType.isAssignableFrom(result)) {
		// avoid to assign a set literal to a list and viceversa:
		// at least the raw types must be assignable
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=498779
		if (expectedType.getRawTypeReference().isAssignableFrom(result.getRawTypeReference())) {
			LightweightTypeReference expectedElementType = getElementOrComponentType(expectedType, owner);
			if (matchesExpectation(elementType, expectedElementType)) {
				return expectedType;
			}
		}
	}
	return result;
}
 
Example 2
Source File: ClosureTypeComputerUnitTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testPrepareComputation_08() throws Exception {
	JvmType iterableType = getTypeForName(Iterable.class, state);
	JvmTypeParameter typeParameter = createTypeParameter("ELEMENT", state);
	ParameterizedTypeReference iterableTypeReference = state.getReferenceOwner().newParameterizedTypeReference(iterableType);
	iterableTypeReference.addTypeArgument(createTypeReference(typeParameter, state));
	assertFalse(iterableTypeReference.isResolved());
	
	TypeExpectation expectation = new TypeExpectation(iterableTypeReference, state, false);
	// closure indicates Function1 but Iterable is Function0 - type is Function1
	ClosureTypeComputer computer = new ClosureTypeComputer(closure("[]", getContextResourceSet()), expectation, state);
	computer.selectStrategy();
	LightweightTypeReference closureType = computer.getExpectedClosureType();
	assertEquals("java.lang.Iterable.iterator()", computer.getOperation().getIdentifier());
	assertEquals("Iterable<Unbound[T]>", getEquivalentSimpleName(closureType));
}
 
Example 3
Source File: ClosureTypeComputerUnitTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testPrepareComputation_16() throws Exception {
	JvmType iterableType = getTypeForName(Iterable.class, state);
	JvmTypeParameter typeParameter = createTypeParameter("ELEMENT", state);
	ParameterizedTypeReference iterableTypeReference = state.getReferenceOwner().newParameterizedTypeReference(iterableType);
	iterableTypeReference.addTypeArgument(createTypeReference(typeParameter, state));
	assertFalse(iterableTypeReference.isResolved());
	
	TypeExpectation expectation = new TypeExpectation(iterableTypeReference, state, false);
	// closure indicates Function1 but Iterable is Function0 - type is Function1
	ClosureTypeComputer computer = new ClosureTypeComputer(closure("[it|]", getContextResourceSet()), expectation, state);
	computer.selectStrategy();
	LightweightTypeReference closureType = computer.getExpectedClosureType();
	assertEquals("org.eclipse.xtext.xbase.lib.Procedures$Procedure1.apply(Param)", computer.getOperation().getIdentifier());
	assertEquals("Procedure1<Unbound[Param]>", getEquivalentSimpleName(closureType));
}
 
Example 4
Source File: ClosureTypeComputerUnitTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testPrepareComputation_11() throws Exception {
	JvmType listFunctionType = getTypeForName(ListFunction1.class, state);
	// type with illegal type parameters
	JvmTypeParameter singleTypeParameter = createTypeParameter("InAndOut", state);
	ParameterizedTypeReference listFunctionTypeReference = state.getReferenceOwner().newParameterizedTypeReference(listFunctionType);
	listFunctionTypeReference.addTypeArgument(createTypeReference(singleTypeParameter, state));
	assertFalse(listFunctionTypeReference.isResolved());
	
	TypeExpectation expectation = new TypeExpectation(listFunctionTypeReference, state, false);
	ClosureTypeComputer computer = new ClosureTypeComputer(closure("[]", getContextResourceSet()), expectation, state);
	computer.selectStrategy();
	LightweightTypeReference closureType = computer.getExpectedClosureType();
	assertEquals("org.eclipse.xtext.xbase.tests.typesystem.TypeResolutionTestData$ListFunction1.apply(java.util.List)", computer.getOperation().getIdentifier());
	assertEquals("ListFunction1<Unbound[T], Unbound[R]>", getEquivalentSimpleName(closureType));
	assertEquals("(List<Unbound[T]>)=>List<Unbound[R]>", closureType.getSimpleName());
}
 
Example 5
Source File: ClosureTypeComputerUnitTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testPrepareComputation_12() throws Exception {
	JvmType comparatorType = getTypeForName(Comparator.class, state);
	// type with illegal type parameters
	JvmTypeParameter typeParameter = createTypeParameter("COMPARABLE", state);
	ParameterizedTypeReference comparatorTypeReference = state.getReferenceOwner().newParameterizedTypeReference(comparatorType);
	comparatorTypeReference.addTypeArgument(createTypeReference(typeParameter, state));
	assertFalse(comparatorTypeReference.isResolved());
	
	TypeExpectation expectation = new TypeExpectation(comparatorTypeReference, state, false);
	ClosureTypeComputer computer = new ClosureTypeComputer(closure("[x, y|]", getContextResourceSet()), expectation, state);
	computer.selectStrategy();
	LightweightTypeReference closureType = computer.getExpectedClosureType();
	assertEquals("java.util.Comparator.compare(T,T)", computer.getOperation().getIdentifier());
	assertEquals("Comparator<Unbound[T]>", getEquivalentSimpleName(closureType));
	assertEquals("(Unbound[T], Unbound[T])=>int", closureType.getSimpleName());
}
 
Example 6
Source File: LightweightTypeReferenceSerializerTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testFunctionType4() {
  FunctionTypeReference _newFunctionTypeReference = this.getOwner().newFunctionTypeReference(this.type(Function3.class));
  final Procedure1<FunctionTypeReference> _function = (FunctionTypeReference it) -> {
    it.setReturnType(this.typeRef(Object.class));
    it.addTypeArgument(this.typeRef(Object.class));
    it.addParameterType(this.typeRef(String.class));
    it.addTypeArgument(this.typeRef(String.class));
    ParameterizedTypeReference _typeRef = this.typeRef(List.class);
    final Procedure1<ParameterizedTypeReference> _function_1 = (ParameterizedTypeReference it_1) -> {
      WildcardTypeReference _newWildcardTypeReference = it_1.getOwner().newWildcardTypeReference();
      final Procedure1<WildcardTypeReference> _function_2 = (WildcardTypeReference it_2) -> {
        it_2.setLowerBound(this.typeRef(CharSequence.class));
      };
      WildcardTypeReference _doubleArrow = ObjectExtensions.<WildcardTypeReference>operator_doubleArrow(_newWildcardTypeReference, _function_2);
      it_1.addTypeArgument(_doubleArrow);
    };
    final ParameterizedTypeReference listOfCharSequence = ObjectExtensions.<ParameterizedTypeReference>operator_doubleArrow(_typeRef, _function_1);
    it.addParameterType(listOfCharSequence);
    it.addTypeArgument(listOfCharSequence);
    final ArrayTypeReference arrayOfObject = it.getOwner().newArrayTypeReference(this.typeRef(Object.class));
    it.addParameterType(arrayOfObject);
    it.addTypeArgument(arrayOfObject);
  };
  this.assertInJava(this.assertInXtend(ObjectExtensions.<FunctionTypeReference>operator_doubleArrow(_newFunctionTypeReference, _function), "(java.lang.String, java.util.List<? super java.lang.CharSequence>, java.lang.Object[])=>java.lang.Object"), "org.eclipse.xtext.xbase.lib.Functions$Function3<java.lang.Object, java.lang.String, java.util.List<? super java.lang.CharSequence>, java.lang.Object[]>");
}
 
Example 7
Source File: ClosureTypeComputerUnitTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testPrepareComputation_01() throws Exception {
	JvmType iterableType = getTypeForName(Iterable.class, state);
	JvmTypeParameter typeParameter = createTypeParameter("ELEMENT", state);
	ParameterizedTypeReference iterableTypeReference = state.getReferenceOwner().newParameterizedTypeReference(iterableType);
	iterableTypeReference.addTypeArgument(createTypeReference(typeParameter, state));
	assertFalse(iterableTypeReference.isResolved());
	
	TypeExpectation expectation = new TypeExpectation(iterableTypeReference, state, false);
	ClosureTypeComputer computer = new ClosureTypeComputer(closure("[|]", getContextResourceSet()), expectation, state);
	computer.selectStrategy();
	LightweightTypeReference closureType = computer.getExpectedClosureType();
	assertEquals("java.lang.Iterable.iterator()", computer.getOperation().getIdentifier());
	assertEquals("Iterable<Unbound[T]>", getEquivalentSimpleName(closureType));
	assertEquals("()=>Iterator<Unbound[T]>", closureType.getSimpleName());
}
 
Example 8
Source File: ClosureTypeComputerUnitTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testHintsAfterPrepareComputation_03() throws Exception {
	JvmType iterableType = getTypeForName(Iterable.class, state);
	JvmType appendableType = getTypeForName(Appendable.class, state);
	JvmType charSequenceType = getTypeForName(CharSequence.class, state);
	ParameterizedTypeReference iterableTypeReference = state.getReferenceOwner().newParameterizedTypeReference(iterableType);
	WildcardTypeReference typeArgument = state.getReferenceOwner().newWildcardTypeReference();
	typeArgument.addUpperBound(state.getReferenceOwner().newParameterizedTypeReference(appendableType));
	typeArgument.addUpperBound(state.getReferenceOwner().newParameterizedTypeReference(charSequenceType));
	iterableTypeReference.addTypeArgument(typeArgument);
	
	TypeExpectation expectation = new TypeExpectation(iterableTypeReference, state, false);
	ClosureTypeComputer computer = new ClosureTypeComputer(closure("[|]", getContextResourceSet()), expectation, state);
	computer.selectStrategy();
	FunctionTypeReference closureType = computer.getExpectedClosureType();
	assertEquals("java.lang.Iterable.iterator()", computer.getOperation().getIdentifier());
	assertEquals("Iterable<Unbound[T]>", getEquivalentSimpleName(closureType));
	assertEquals("()=>Iterator<Unbound[T]>", closureType.getSimpleName());
	
	UnboundTypeReference closureTypeArgument = (UnboundTypeReference) closureType.getTypeArguments().get(0);
	List<LightweightBoundTypeArgument> hints = state.getResolvedTypes().getHints(closureTypeArgument.getHandle());
	assertEquals(hints.toString(), 2, hints.size());
}
 
Example 9
Source File: LightweightTypeReferenceSerializerTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testFunctionType7() {
  FunctionTypeReference _newFunctionTypeReference = this.getOwner().newFunctionTypeReference(this.type(Procedure3.class));
  final Procedure1<FunctionTypeReference> _function = (FunctionTypeReference it) -> {
    it.addParameterType(this.typeRef(String.class));
    it.addTypeArgument(this.typeRef(String.class));
    ParameterizedTypeReference _typeRef = this.typeRef(List.class);
    final Procedure1<ParameterizedTypeReference> _function_1 = (ParameterizedTypeReference it_1) -> {
      WildcardTypeReference _newWildcardTypeReference = it_1.getOwner().newWildcardTypeReference();
      final Procedure1<WildcardTypeReference> _function_2 = (WildcardTypeReference it_2) -> {
        it_2.setLowerBound(this.typeRef(CharSequence.class));
      };
      WildcardTypeReference _doubleArrow = ObjectExtensions.<WildcardTypeReference>operator_doubleArrow(_newWildcardTypeReference, _function_2);
      it_1.addTypeArgument(_doubleArrow);
    };
    final ParameterizedTypeReference listOfCharSequence = ObjectExtensions.<ParameterizedTypeReference>operator_doubleArrow(_typeRef, _function_1);
    it.addParameterType(listOfCharSequence);
    it.addTypeArgument(listOfCharSequence);
    final ArrayTypeReference arrayOfObject = it.getOwner().newArrayTypeReference(this.typeRef(Object.class));
    it.addParameterType(arrayOfObject);
    it.addTypeArgument(arrayOfObject);
  };
  this.assertInJava(this.assertInXtend(ObjectExtensions.<FunctionTypeReference>operator_doubleArrow(_newFunctionTypeReference, _function), "(java.lang.String, java.util.List<? super java.lang.CharSequence>, java.lang.Object[])=>void"), "org.eclipse.xtext.xbase.lib.Procedures$Procedure3<java.lang.String, java.util.List<? super java.lang.CharSequence>, java.lang.Object[]>");
}
 
Example 10
Source File: LightweightTypeReferenceSerializerTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testListUpperBounds() {
  ParameterizedTypeReference _typeRef = this.typeRef(List.class);
  final Procedure1<ParameterizedTypeReference> _function = (ParameterizedTypeReference it) -> {
    WildcardTypeReference _newWildcardTypeReference = it.getOwner().newWildcardTypeReference();
    final Procedure1<WildcardTypeReference> _function_1 = (WildcardTypeReference it_1) -> {
      it_1.addUpperBound(this.typeRef(CharSequence.class));
      it_1.addUpperBound(this.typeRef(Serializable.class));
    };
    WildcardTypeReference _doubleArrow = ObjectExtensions.<WildcardTypeReference>operator_doubleArrow(_newWildcardTypeReference, _function_1);
    it.addTypeArgument(_doubleArrow);
  };
  this.assertInXtendAndJava(ObjectExtensions.<ParameterizedTypeReference>operator_doubleArrow(_typeRef, _function), "java.util.List<? extends java.lang.CharSequence & java.io.Serializable>");
}
 
Example 11
Source File: CollectionLiteralsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void computeType(XSetLiteral literal, JvmGenericType setType, JvmGenericType mapType, ITypeExpectation expectation, ITypeComputationState state) {
	LightweightTypeReference elementTypeExpectation = null;
	LightweightTypeReference expectedType = expectation.getExpectedType();
	if(expectedType != null) {
		elementTypeExpectation = getElementOrComponentType(expectedType, state);
	}
	List<LightweightTypeReference> setTypeCandidates = computeCollectionTypeCandidates(literal, setType, elementTypeExpectation, state);
	LightweightTypeReference commonSetType = getCommonSuperType(setTypeCandidates, state);
	ITypeReferenceOwner owner = state.getReferenceOwner();
	if(commonSetType != null) {
		LightweightTypeReference commonElementType = commonSetType.getTypeArguments().get(0).getInvariantBoundSubstitute();
		if(isMapLiteral(expectedType, commonElementType)) {
			LightweightTypeReference mapTypeReference = createMapTypeReference(mapType, commonElementType, expectedType, owner);
			expectation.acceptActualType(mapTypeReference, ConformanceFlags.UNCHECKED);
			commonElementType = createNormalizedPairType(commonElementType, mapTypeReference, owner);
			refineElementTypeExpectation(literal, commonElementType, state);
		} else {
			commonElementType = normalizeElementType(commonElementType, expectedType, owner);
			if (expectedType != null) {
				commonSetType = createCollectionTypeReference(setType, commonElementType, expectedType, owner);
			}
			expectation.acceptActualType(commonSetType, ConformanceFlags.UNCHECKED);
			refineElementTypeExpectation(literal, commonElementType, state);
		}
	} else {
		if(isMapExpectation(expectedType)) {
			ParameterizedTypeReference unboundCollectionType = owner.newParameterizedTypeReference(mapType);
			unboundCollectionType.addTypeArgument(expectation.createUnboundTypeReference(literal, mapType.getTypeParameters().get(0)));
			unboundCollectionType.addTypeArgument(expectation.createUnboundTypeReference(literal, mapType.getTypeParameters().get(1)));
			expectation.acceptActualType(unboundCollectionType, ConformanceFlags.UNCHECKED);
		} else {
			setUnboundCollectionType(literal, setType, expectation, elementTypeExpectation, state);
		}
	}
}
 
Example 12
Source File: LightweightTypeReferenceSerializerTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testListWildcard() {
  ParameterizedTypeReference _typeRef = this.typeRef(List.class);
  final Procedure1<ParameterizedTypeReference> _function = (ParameterizedTypeReference it) -> {
    it.addTypeArgument(it.getOwner().newWildcardTypeReference());
  };
  this.assertInXtendAndJava(ObjectExtensions.<ParameterizedTypeReference>operator_doubleArrow(_typeRef, _function), "java.util.List<?>");
}
 
Example 13
Source File: TypeParameterSubstitutor.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected LightweightTypeReference doVisitInnerTypeReference(InnerTypeReference reference, Visiting visiting) {
	if (reference.isResolved() && reference.isOwnedBy(getOwner()))
		return reference;
	
	LightweightTypeReference outer = reference.getOuter().accept(this, visiting);
	ParameterizedTypeReference result = getOwner().newParameterizedTypeReference(outer, reference.getType());
	for(LightweightTypeReference argument: reference.getTypeArguments()) {
		result.addTypeArgument(visitTypeArgument(argument, visiting));
	}
	return result;
}
 
Example 14
Source File: CustomTypeParameterSubstitutor.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected LightweightTypeReference enhanceParameterizedTypeReference(ParameterizedTypeReference origin, JvmType type, ParameterizedTypeReference result, ConstraintVisitingInfo visiting) {
	for(int i = 0; i < origin.getTypeArguments().size(); i++) {
		LightweightTypeReference argument = origin.getTypeArguments().get(i);
		visiting.pushInfo(type instanceof JvmTypeParameterDeclarator ? (JvmTypeParameterDeclarator) type : null, i);
		LightweightTypeReference visitedArgument = visitTypeArgument(argument, visiting);
		result.addTypeArgument(visitedArgument);
	}
	return result;
}
 
Example 15
Source File: ResolvedFeature.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected LightweightTypeReference getDeclaredType(JvmIdentifiableElement feature) {
	if (feature instanceof JvmConstructor) {
		return getState().getReferenceOwner().newReferenceTo(Void.TYPE);
	}
	/*
	 * The actual result type is Class<? extends |X|> where |X| is the erasure of 
	 * the static type of the expression on which getClass is called. For example, 
	 * no cast is required in this code fragment:
	 *   Number n = 0;
	 *   Class<? extends Number> c = n.getClass();
	 */
	if (feature instanceof JvmOperation && feature.getSimpleName().equals("getClass")) {
		JvmOperation getClassOperation = (JvmOperation) feature;
		if (getClassOperation.getParameters().isEmpty() && "java.lang.Object".equals(getClassOperation.getDeclaringType().getIdentifier())) {
			LightweightTypeReference receiverType = getReceiverType();
			if (receiverType == null) {
				throw new IllegalStateException("Cannot determine type of receiver "+ getReceiver());
			}
			List<JvmType> rawTypes = receiverType.getRawTypes();
			if (rawTypes.isEmpty()) {
				return super.getDeclaredType(feature);
			}
			ITypeReferenceOwner owner = receiverType.getOwner();
			ParameterizedTypeReference result = owner.newParameterizedTypeReference(((JvmOperation) feature).getReturnType().getType());
			WildcardTypeReference wildcard = owner.newWildcardTypeReference();
			wildcard.addUpperBound(owner.toPlainTypeReference(rawTypes.get(0)));
			result.addTypeArgument(wildcard);
			return result;
		}
	}
	return super.getDeclaredType(feature);
}
 
Example 16
Source File: FeatureLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected LightweightTypeReference getDeclaredType(JvmIdentifiableElement feature) {
	if (feature instanceof JvmConstructor) {
		return getState().getReferenceOwner().toLightweightTypeReference(getState().getTypeReferences().getTypeForName(Void.TYPE, feature));
	}
	/*
	 * The actual result type is Class<? extends |X|> where |X| is the erasure of 
	 * the static type of the expression on which getClass is called. For example, 
	 * no cast is required in this code fragment:
	 *   Number n = 0;
	 *   Class<? extends Number> c = n.getClass();
	 */
	if (isGetClass(feature)) {
		LightweightTypeReference receiverType = getReceiverType();
		if (receiverType == null) {
			throw new IllegalStateException("Cannot determine the receiver's type");
		}
		List<JvmType> rawTypes = receiverType.getRawTypes();
		if (rawTypes.isEmpty()) {
			return super.getDeclaredType(feature);
		}
		ITypeReferenceOwner owner = receiverType.getOwner();
		ParameterizedTypeReference result = owner.newParameterizedTypeReference(((JvmOperation) feature).getReturnType().getType());
		WildcardTypeReference wildcard = owner.newWildcardTypeReference();
		wildcard.addUpperBound(owner.toPlainTypeReference(rawTypes.get(0)));
		result.addTypeArgument(wildcard);
		return result;
	}
	return super.getDeclaredType(feature);
}
 
Example 17
Source File: LightweightTypeReferenceSerializerTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testListUpperBound() {
  ParameterizedTypeReference _typeRef = this.typeRef(List.class);
  final Procedure1<ParameterizedTypeReference> _function = (ParameterizedTypeReference it) -> {
    WildcardTypeReference _newWildcardTypeReference = it.getOwner().newWildcardTypeReference();
    final Procedure1<WildcardTypeReference> _function_1 = (WildcardTypeReference it_1) -> {
      it_1.addUpperBound(this.typeRef(CharSequence.class));
    };
    WildcardTypeReference _doubleArrow = ObjectExtensions.<WildcardTypeReference>operator_doubleArrow(_newWildcardTypeReference, _function_1);
    it.addTypeArgument(_doubleArrow);
  };
  this.assertInXtendAndJava(ObjectExtensions.<ParameterizedTypeReference>operator_doubleArrow(_typeRef, _function), "java.util.List<? extends java.lang.CharSequence>");
}
 
Example 18
Source File: LightweightTypeReferenceSerializerTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testListUpperBoundObject() {
  ParameterizedTypeReference _typeRef = this.typeRef(List.class);
  final Procedure1<ParameterizedTypeReference> _function = (ParameterizedTypeReference it) -> {
    WildcardTypeReference _newWildcardTypeReference = it.getOwner().newWildcardTypeReference();
    final Procedure1<WildcardTypeReference> _function_1 = (WildcardTypeReference it_1) -> {
      it_1.addUpperBound(this.typeRef(Object.class));
    };
    WildcardTypeReference _doubleArrow = ObjectExtensions.<WildcardTypeReference>operator_doubleArrow(_newWildcardTypeReference, _function_1);
    it.addTypeArgument(_doubleArrow);
  };
  this.assertInXtendAndJava(ObjectExtensions.<ParameterizedTypeReference>operator_doubleArrow(_typeRef, _function), "java.util.List<?>");
}
 
Example 19
Source File: CollectionLiteralsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void setUnboundCollectionType(XCollectionLiteral literal, JvmGenericType collectionType, ITypeExpectation expectation,
		LightweightTypeReference elementTypeExpectation, ITypeComputationState state) {
	ParameterizedTypeReference unboundCollectionType = state.getReferenceOwner().newParameterizedTypeReference(collectionType);
	if (elementTypeExpectation != null) {
		unboundCollectionType.addTypeArgument(elementTypeExpectation);
	} else {
		UnboundTypeReference unboundTypeArgument = expectation.createUnboundTypeReference(literal, collectionType.getTypeParameters().get(0));
		unboundCollectionType.addTypeArgument(unboundTypeArgument);
	}
	expectation.acceptActualType(unboundCollectionType, ConformanceFlags.UNCHECKED);
}
 
Example 20
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected LightweightTypeReference getAndEnhanceIterableOrArrayFromComponent(LightweightTypeReference parameterType, JvmGenericType iterableType,
		final CompoundTypeReference compoundResult) {
	if (parameterType.isUnknown()) {
		compoundResult.addComponent(parameterType);
		return parameterType;
	}
	ITypeReferenceOwner owner = compoundResult.getOwner();
	LightweightTypeReference iterableOrArray = null;
	LightweightTypeReference addAsArrayComponentAndIterable = null;
	if (parameterType.isPrimitive()) {
		iterableOrArray = owner.newArrayTypeReference(parameterType);
		compoundResult.addComponent(iterableOrArray);
		addAsArrayComponentAndIterable = parameterType.getWrapperTypeIfPrimitive();
	} else if (parameterType.isAny()) {
		addAsArrayComponentAndIterable = parameterType.getOwner().newReferenceToObject();
	} else {
		addAsArrayComponentAndIterable = parameterType;
	}
	if (iterableType != null) {
		ParameterizedTypeReference reference = owner.newParameterizedTypeReference(iterableType);
		WildcardTypeReference wildcard = owner.newWildcardTypeReference();
		wildcard.addUpperBound(addAsArrayComponentAndIterable);
		reference.addTypeArgument(wildcard);
		compoundResult.addComponent(reference);
		if (iterableOrArray == null) {
			iterableOrArray = reference;
			LightweightTypeReference potentialPrimitive = addAsArrayComponentAndIterable.getPrimitiveIfWrapperType();
			if (potentialPrimitive != addAsArrayComponentAndIterable) {
				compoundResult.addComponent(owner.newArrayTypeReference(potentialPrimitive));
			}
		}
		compoundResult.addComponent(owner.newArrayTypeReference(addAsArrayComponentAndIterable));
	} else if (iterableOrArray == null) { // no JRE on the CP
		if (addAsArrayComponentAndIterable != null) {
			iterableOrArray = owner.newArrayTypeReference(addAsArrayComponentAndIterable);
			compoundResult.addComponent(iterableOrArray);
		} else {
			compoundResult.addComponent(parameterType);
			return parameterType;
		}
	}
	return iterableOrArray;
}