org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference Java Examples

The following examples show how to use org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference. 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: AbstractResolvedOperation.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public List<JvmOperation> getOverriddenAndImplementedMethodCandidates() {
	if (overrideCandidates != null)
		return overrideCandidates;
	// here we are only interested in the raw type thus the declarator is not substituted
	// the found operation will be put in the right context by clients, e.g. #getOverriddenAndImplementedMethods
	ParameterizedTypeReference currentDeclarator = getContextType().getOwner().newParameterizedTypeReference(getDeclaration().getDeclaringType());
	List<LightweightTypeReference> superTypes = currentDeclarator.getSuperTypes();
	List<JvmOperation> result = Lists.newArrayListWithCapacity(5);
	for(LightweightTypeReference superType: superTypes) {
		if (superType.getType() instanceof JvmDeclaredType) {
			JvmDeclaredType declaredSuperType = (JvmDeclaredType) superType.getType();
			if (declaredSuperType != null) {
				Iterable<JvmFeature> equallyNamedFeatures = declaredSuperType.findAllFeaturesByName(getDeclaration().getSimpleName());
				for(JvmFeature equallyNamedFeature: equallyNamedFeatures) {
					if (equallyNamedFeature instanceof JvmOperation) {
						result.add((JvmOperation) equallyNamedFeature);
					}
				}
			}
		}
	}
	return overrideCandidates = Collections.unmodifiableList(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_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 #3
Source File: CollectionLiteralsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates a map type reference that comes as close as possible / necessary to its expected type.
 */
protected LightweightTypeReference createMapTypeReference(JvmGenericType mapType, LightweightTypeReference pairType, LightweightTypeReference expectation, ITypeReferenceOwner owner) {
	List<LightweightTypeReference> leftAndRight = pairType.getTypeArguments();
	
	LightweightTypeReference left = leftAndRight.get(0).getInvariantBoundSubstitute();
	LightweightTypeReference right = leftAndRight.get(1).getInvariantBoundSubstitute();
	
	LightweightTypeReference mapExpectation = getMapExpectation(expectation);
	if (mapExpectation != null) {
		List<LightweightTypeReference> typeArguments = expectation.getTypeArguments();
		left = doNormalizeElementType(left, typeArguments.get(0));
		right = doNormalizeElementType(right, typeArguments.get(1));
	}
	ParameterizedTypeReference result = owner.newParameterizedTypeReference(mapType);
	result.addTypeArgument(left.copyInto(owner));
	result.addTypeArgument(right.copyInto(owner));
	if (mapExpectation != null && !expectation.isAssignableFrom(result)) {
		// expectation does not match the computed type, but looks good according to the element types:
		// use expected type
		if (matchesExpectation(left, mapExpectation.getTypeArguments().get(0)) && matchesExpectation(right, mapExpectation.getTypeArguments().get(1))) {
			return expectation;
		}
	}
	return result;
}
 
Example #4
Source File: TypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected LightweightTypeReference doGetTypeParametersForSuperType(
		final Multimap<JvmType, LightweightTypeReference> all, JvmGenericType rawType, ITypeReferenceOwner owner,
		List<LightweightTypeReference> types) {
	// if we do not declare any parameters it is safe to return the first candidate
	if (!hasTypeParameters(rawType)) {
		return getFirstForRawType(all, rawType); 
	}
	ParameterizedTypeReference result = owner.newParameterizedTypeReference(rawType);
	pushRequestedTypes(types);
	if (!enhanceSuperType(Lists.newArrayList(all.get(rawType)), result)) {
		return null;
	}
	FunctionTypeReference resultAsFunctionType = result.getAsFunctionTypeReference();
	if (resultAsFunctionType != null)
		return resultAsFunctionType;
	return result;
}
 
Example #5
Source File: CollectionLiteralsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates a list of collection type references from the element types of a collection literal.
 */
protected List<LightweightTypeReference> computeCollectionTypeCandidates(XCollectionLiteral literal, JvmGenericType collectionType, LightweightTypeReference elementTypeExpectation, ITypeComputationState state) {
	List<XExpression> elements = literal.getElements();
	if(!elements.isEmpty()) {
		List<LightweightTypeReference> elementTypes = Lists.newArrayListWithCapacity(elements.size());
		for(XExpression element: elements) {
			ITypeComputationResult elementType = computeTypes(element, elementTypeExpectation, state);
			LightweightTypeReference actualType = elementType.getActualExpressionType();
			if(actualType != null && !actualType.isAny()) {
				ParameterizedTypeReference collectionTypeCandidate = state.getReferenceOwner().newParameterizedTypeReference(collectionType);
				collectionTypeCandidate.addTypeArgument(actualType.getWrapperTypeIfPrimitive());
				elementTypes.add(collectionTypeCandidate);
			}
		}
		return elementTypes;
	}
	return Collections.emptyList();
}
 
Example #6
Source File: AbstractOverloadedStaticMethodTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void linksTo(final String invocation, final String method) {
  try {
    final XtendFile file = this.file(this.inMethodBody(invocation), false);
    XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes());
    final XtendClass c = ((XtendClass) _head);
    XtendMember _head_1 = IterableExtensions.<XtendMember>head(c.getMembers());
    final XtendFunction m = ((XtendFunction) _head_1);
    XExpression _expression = m.getExpression();
    final XBlockExpression body = ((XBlockExpression) _expression);
    XExpression _last = IterableExtensions.<XExpression>last(body.getExpressions());
    final XAbstractFeatureCall featureCall = ((XAbstractFeatureCall) _last);
    JvmIdentifiableElement _feature = featureCall.getFeature();
    final JvmOperation operation = ((JvmOperation) _feature);
    final StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(this.services, file);
    final ParameterizedTypeReference declaration = owner.newParameterizedTypeReference(operation.getDeclaringType());
    final BottomResolvedOperation resolved = new BottomResolvedOperation(operation, declaration, this.overrideTester);
    Assert.assertEquals(method, resolved.getSimpleSignature());
    Assert.assertTrue(IterableExtensions.join(file.eResource().getErrors(), "\n"), file.eResource().getErrors().isEmpty());
    Assert.assertNull(featureCall.getImplicitReceiver());
    Assert.assertNull(featureCall.getImplicitFirstArgument());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #7
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 #8
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void _computeTypes(XTypeLiteral object, ITypeComputationState state) {
	JvmType type = object.getType();
	if (type == null) {
		return;
	}
	checkTypeParameterNotAllowedAsLiteral(object, type, state);
	ITypeReferenceOwner owner = state.getReferenceOwner();
	LightweightTypeReference clazz = owner.newParameterizedTypeReference(type);
	for (int i = 0; i < object.getArrayDimensions().size(); i++) {
		clazz = owner.newArrayTypeReference(clazz);
	}
	if (object.getArrayDimensions().isEmpty()) {
		if (clazz.isPrimitiveVoid()) {
			clazz = state.getReferenceOwner().newReferenceTo(Void.class);
		} else {
			clazz = clazz.getWrapperTypeIfPrimitive();
		}
	}
	LightweightTypeReference result = owner.newReferenceTo(Class.class);
	if (result instanceof ParameterizedTypeReference) {
		ParameterizedTypeReference parameterizedTypeReference = (ParameterizedTypeReference) result;
		parameterizedTypeReference.addTypeArgument(clazz);
	}
	state.acceptActualType(result);
}
 
Example #9
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 #10
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 #11
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 #12
Source File: ClosureTypeComputerUnitTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testPrepareComputation_05() throws Exception {
	JvmType iterableType = getTypeForName(Iterable.class, state);
	JvmType elementType = getTypeForName(String.class, state);
	ParameterizedTypeReference iterableTypeReference = state.getReferenceOwner().newParameterizedTypeReference(iterableType);
	iterableTypeReference.addTypeArgument(state.getReferenceOwner().newParameterizedTypeReference(elementType));
	assertTrue(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 #13
Source File: XtendReentrantTypeResolver.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
/* @Nullable */
protected JvmTypeReference doGetTypeReference(XComputedTypeReferenceImplCustom context) {
	final ITypeReferenceOwner owner = resolvedTypes.getReferenceOwner();
	LightweightTypeReference hashMapReference = owner.newReferenceTo(HashMap.class, new TypeReferenceInitializer<ParameterizedTypeReference>() {
		@Override
		public LightweightTypeReference enhance(ParameterizedTypeReference ref) {
			ref.addTypeArgument(owner.newReferenceTo(ArrayList.class, new TypeReferenceInitializer<ParameterizedTypeReference>(){
				@Override
				public LightweightTypeReference enhance(ParameterizedTypeReference keyType) {
					keyType.addTypeArgument(owner.newWildcardExtendsObject());
					return keyType;
				}
			}));
			ref.addTypeArgument(owner.toLightweightTypeReference(createOperation.getReturnType()));
			return ref;
		}
	});
	return toJavaCompliantTypeReference(hashMapReference, session);
}
 
Example #14
Source File: ClosureTypeComputerUnitTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testPrepareComputation_14() throws Exception {
	JvmType iterableType = getTypeForName(Iterable.class, state);
	JvmType elementType = getTypeForName(String.class, state);
	ParameterizedTypeReference iterableTypeReference = state.getReferenceOwner().newParameterizedTypeReference(iterableType);
	WildcardTypeReference wildcard = state.getReferenceOwner().newWildcardTypeReference();
	wildcard.addUpperBound(state.getReferenceOwner().newParameterizedTypeReference(elementType));
	iterableTypeReference.addTypeArgument(wildcard);
	assertTrue(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 #15
Source File: CreateMemberQuickfixes.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected LightweightTypeReference getReceiverType(XAbstractFeatureCall featureCall) {
	XExpression actualReceiver = featureCall.getActualReceiver();
	ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, featureCall);
	if (actualReceiver == null) {
		JvmDeclaredType callersType = getCallersType(featureCall);
		if (callersType != null)
			return owner.newParameterizedTypeReference(callersType);
	} else if (actualReceiver instanceof XAbstractFeatureCall && ((XAbstractFeatureCall) actualReceiver).isTypeLiteral()) {
		JvmType type = (JvmType) ((XAbstractFeatureCall) actualReceiver).getFeature();
		ParameterizedTypeReference reference = owner.newParameterizedTypeReference(type);
		return reference;
	} else {
		LightweightTypeReference typeRef = typeResolver.resolveTypes(featureCall).getActualType(actualReceiver);
		if(typeRef != null && typeRef.getType() instanceof JvmDeclaredType)
			return typeRef;
	}
	return null;
}
 
Example #16
Source File: ClosureTypeComputerUnitTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testPrepareComputation_03() throws Exception {
	JvmType iterableType = getTypeForName(CharSequenceIterable.class, state);
	JvmTypeParameter typeParameter = createTypeParameter("STRING", 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("CharSequenceIterable<Unbound[C]>", getEquivalentSimpleName(closureType));
	assertEquals("()=>Iterator<Unbound[C]>", closureType.getSimpleName());
}
 
Example #17
Source File: RawTypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected int doIsConformant(ParameterizedTypeReference left, FunctionTypeReference right, int flags) {
	if ((flags & ALLOW_FUNCTION_CONVERSION) == 0)
		return doIsConformant(left, (ParameterizedTypeReference) right, flags);
	// TODO don't convert if not necessary:
	// right.getType == left.getType -> doIsConformant optimized to function types -> declarator == procedure, ignore return type etc
	
	// next: get function type kind and arity of right and check against left if it is a function type
	// afterwards, do optimized check of left against the argument types of right
	FunctionTypeReference convertedLeft = left.getAsFunctionTypeReference();
	if (convertedLeft != null) {
		return doIsConformant(convertedLeft, right, flags);
	}
	if (right.isFunctionType()) {
		// todo optimize tryConvertToFunctionTypeReference
		convertedLeft = left.tryConvertToFunctionTypeReference(false);
		if (convertedLeft != null) {
			int result = doIsConformant(convertedLeft, right, flags);
			if ((result & SUCCESS) != 0) {
				return result | DEMAND_CONVERSION;
			}
		}
	}
	return doIsConformant(left, (ParameterizedTypeReference) right, flags);
}
 
Example #18
Source File: RawTypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected int doIsConformant(FunctionTypeReference left, ParameterizedTypeReference right, int flags) {
	if ((flags & ALLOW_FUNCTION_CONVERSION) == 0)
		return doIsConformant((ParameterizedTypeReference) left, right, flags);
	FunctionTypeReference convertedRight = right.getAsFunctionTypeReference();
	if (convertedRight != null) {
		return doIsConformant(left, convertedRight, flags);
	}
	if (left.isFunctionType()) {
		convertedRight = right.tryConvertToFunctionTypeReference(false);
		if (convertedRight != null) {
			int result = doIsConformant(left, convertedRight, flags);
			if ((result & SUCCESS) != 0) {
				return result | DEMAND_CONVERSION;
			}
		}
	}
	return doIsConformant((ParameterizedTypeReference) left, right, flags);
}
 
Example #19
Source File: TypeParameterByConstraintSubstitutor.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected LightweightTypeReference visitTypeArgument(LightweightTypeReference reference, ConstraintVisitingInfo visiting, boolean lowerBound) {
	if (lowerBound && (reference instanceof ParameterizedTypeReference)) {
		if (reference.getType() instanceof JvmTypeParameter) {
			JvmTypeParameter typeParameter = (JvmTypeParameter) reference.getType();
			// don't recurse into lower bounds of wildcards, e.g. constraint bound of 
			// C extends Comparable<? super C> 
			// is not 
			// Comparable<? super Object> 
			// but 
			// Comparable<?>
			if (!visiting.canVisit(typeParameter)) {
				return getOwner().newWildcardExtendsObject();
			}
		}
	}
	return super.visitTypeArgument(reference, visiting, lowerBound);
}
 
Example #20
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 #21
Source File: ClosureTypeComputerUnitTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testHintsAfterPrepareComputation_01() throws Exception {
	JvmType iterableType = getTypeForName(Iterable.class, state);
	JvmTypeParameter typeParameter = createTypeParameter("ELEMENT", state);
	ParameterizedTypeReference iterableTypeReference = state.getReferenceOwner().newParameterizedTypeReference(iterableType);
	UnboundTypeReference elementReference = (UnboundTypeReference) createTypeReference(typeParameter, state);
	iterableTypeReference.addTypeArgument(elementReference);

	assertTrue(state.getResolvedTypes().getHints(elementReference.getHandle()).isEmpty());
	
	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());
	
	assertEquals(1, state.getResolvedTypes().getHints(elementReference.getHandle()).size());
	UnboundTypeReference closureTypeArgument = (UnboundTypeReference) closureType.getTypeArguments().get(0);
	assertEquals(1, state.getResolvedTypes().getHints(closureTypeArgument.getHandle()).size());
}
 
Example #22
Source File: UnboundTypeParameterPreservingSubstitutor.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
/* @Nullable */
protected LightweightTypeReference getBoundTypeArgument(ParameterizedTypeReference reference, JvmTypeParameter type, Set<JvmTypeParameter> visiting) {
	LightweightMergedBoundTypeArgument boundTypeArgument = getTypeParameterMapping().get(type);
	if (boundTypeArgument != null) {
		LightweightTypeReference boundReference = boundTypeArgument.getTypeReference();
		if (boundReference != null && reference != boundReference) {
			if (boundReference instanceof UnboundTypeReference)
				return boundReference.copyInto(getOwner());
			JvmType boundType = boundReference.getType();
			if (boundType != type) {
				if (visiting.add(type)) {
					try {
						LightweightTypeReference result = boundReference.accept(this, visiting);
						return result;
					} finally {
						visiting.remove(type);
					}
				} else {
					return reference;
				}
			} 
		}
	}
	return null;
}
 
Example #23
Source File: TypeLiteralHelper.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected LightweightTypeReference getAsClassLiteral(final JvmIdentifiableElement feature) {
	if (feature instanceof JvmType) {
		final ITypeReferenceOwner owner = state.getReferenceOwner();
		return owner.newReferenceTo(Class.class, new TypeReferenceInitializer<ParameterizedTypeReference>() {
			@Override
			public LightweightTypeReference enhance(ParameterizedTypeReference reference) {
				LightweightTypeReference argumentType = owner.newParameterizedTypeReference((JvmType) feature);
				if (argumentType.isPrimitiveVoid()) {
					argumentType = owner.newReferenceTo(Void.class);
				} else {
					argumentType = argumentType.getWrapperTypeIfPrimitive();
				}
				reference.addTypeArgument(argumentType);
				return reference;
			}
		});
	}
	throw new IllegalArgumentException(String.valueOf(feature));
}
 
Example #24
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 #25
Source File: TypeParameterByUnboundSubstitutor.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected LightweightTypeReference getUnmappedSubstitute(ParameterizedTypeReference reference, JvmTypeParameter type, ConstraintVisitingInfo visiting) {
	List<JvmTypeParameter> declaredTypeParameters = getOwner().getDeclaredTypeParameters();
	if (declaredTypeParameters.contains(type)) {
		return reference.copyInto(getOwner());
	}
	LightweightTypeReference result = createUnboundTypeReference(type);
	if (result == null) {
		result = new TypeParameterByConstraintSubstitutor(getTypeParameterMapping(), getOwner()).substitute(type);
	}
	return result;
}
 
Example #26
Source File: ClosureTypeComputerUnitTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testPrepareComputation_02() throws Exception {
	JvmType iterableType = getTypeForName(StringIterable.class, state);
	ParameterizedTypeReference iterableTypeReference = state.getReferenceOwner().newParameterizedTypeReference(iterableType);
	assertTrue(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("StringIterable", getEquivalentSimpleName(closureType));
	assertEquals("()=>Iterator<String>", closureType.getSimpleName());
}
 
Example #27
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 #28
Source File: LightweightTypeReferenceSerializerTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testListLowerBound() {
  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.setLowerBound(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<? super java.lang.CharSequence>");
}
 
Example #29
Source File: TypeParameterSubstitutor.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param type the type of the reference. May be used by subtypes. 
 */
protected LightweightTypeReference enhanceParameterizedTypeReference(ParameterizedTypeReference origin, JvmType type, ParameterizedTypeReference result, Visiting visiting) {
	for(LightweightTypeReference argument: origin.getTypeArguments()) {
		result.addTypeArgument(visitTypeArgument(argument, visiting));
	}
	return result;
}
 
Example #30
Source File: LightweightTypeReferenceSerializerTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testListLowerAndUpperBound() {
  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.setLowerBound(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<? super java.lang.CharSequence>");
}