Java Code Examples for org.eclipse.xtext.common.types.JvmTypeConstraint#getTypeReference()

The following examples show how to use org.eclipse.xtext.common.types.JvmTypeConstraint#getTypeReference() . 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: 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 2
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 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: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkTypeParameterConstraintIsValid(JvmTypeParameter typeParameter) {
	if(!typeParameter.getConstraints().isEmpty()) {
		for(JvmTypeConstraint constraint: typeParameter.getConstraints()) {
			JvmTypeReference typeReference = constraint.getTypeReference();
			if(typeReference instanceof JvmGenericArrayTypeReference)
				error(String.format("The array type %s cannot be used as a type parameter bound", typeReference.getSimpleName()),
						typeReference, null, INVALID_TYPE_PARAMETER_BOUNDS);
			else if (typeReference.getType() instanceof JvmTypeParameter && typeParameter.getConstraints().size() > 1)
				error(String.format("The type parameter %s cannot be used as a type parameter bound with additional bounds", typeReference.getSimpleName()),
						typeReference, null, INVALID_TYPE_PARAMETER_BOUNDS);
		}
	}
}
 
Example 5
Source File: AbstractLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void initializeConstraintMapping(JvmTypeParameter typeParameter, UnboundTypeParameterPreservingSubstitutor substitutor, UnboundTypeReference typeReference) {
	if (!typeReference.internalIsResolved()) {
		List<JvmTypeConstraint> constraints = typeParameter.getConstraints();
		for(JvmTypeConstraint constraint: constraints) {
			JvmTypeReference constraintReference = constraint.getTypeReference();
			if (constraintReference != null) {
				LightweightTypeReference substitute = substitutor.substitute(constraintReference);
				if (!substitute.isType(Object.class) && !substitute.isPrimitiveVoid()) {
					typeReference.acceptHint(substitute, BoundTypeArgumentSource.CONSTRAINT, constraint, VarianceInfo.OUT, VarianceInfo.OUT);
				}
			}
		}
	}
}
 
Example 6
Source File: ParameterizedTypeReference.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isRawType(JvmTypeParameter current, RecursionGuard<JvmTypeParameter> guard) {
	if (guard.tryNext(current)) {
		List<JvmTypeConstraint> constraints = current.getConstraints();
		for(int i = 0, size = constraints.size(); i < size; i++) {
			JvmTypeConstraint constraint = constraints.get(i);
			if (constraint.eClass() == TypesPackage.Literals.JVM_UPPER_BOUND && constraint.getTypeReference() != null) {
				JvmTypeReference superType = constraint.getTypeReference();
				JvmType rawSuperType = superType.getType();
				if (rawSuperType != null) {
					if (rawSuperType.eClass() == TypesPackage.Literals.JVM_TYPE_PARAMETER) {
						if (isRawType((JvmTypeParameter) rawSuperType, guard)) {
							return true;
						}
					}
					if (rawSuperType.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) {
						if (!((JvmGenericType) rawSuperType).getTypeParameters().isEmpty()) {
							if (superType.eClass() == TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE) {
								JvmParameterizedTypeReference casted = (JvmParameterizedTypeReference) superType;
								if (casted.getArguments().isEmpty()) {
									return true;
								}
							}
						}
					}
				}
			}
		}
	}
	return false;
}
 
Example 7
Source File: ParameterizedTypeReference.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private Primitive getPrimitiveKind(JvmTypeParameter type, /* @Nullable */ RecursionGuard<JvmTypeParameter> guard) {
	if (type.eIsProxy())
		return null;
	for(JvmTypeConstraint constraint: type.getConstraints()) {
		if (constraint.eClass() == TypesPackage.Literals.JVM_UPPER_BOUND) {
			JvmTypeReference upperBound = constraint.getTypeReference();
			if (upperBound != null) {
				JvmType upperBoundType = upperBound.getType();
				if (upperBoundType.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) {
					return getPrimitiveKind((JvmGenericType) upperBoundType);
				}
				if (type == upperBoundType) {
					return null;
				}
				// guard against recursive deps
				if (upperBoundType.eClass() == TypesPackage.Literals.JVM_TYPE_PARAMETER) {
					JvmTypeParameter upperBoundTypeParameter = (JvmTypeParameter) upperBoundType;
					if (guard == null) {
						guard = new RecursionGuard<JvmTypeParameter>();
						guard.tryNext(type);
					}
					if (guard.tryNext(upperBoundTypeParameter)) {
						return getPrimitiveKind(upperBoundTypeParameter, guard);
					}
					return null;
				}
			}
		}
	}
	return null;
}
 
Example 8
Source File: ParameterizedTypeReference.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isWrapper(JvmTypeParameter typeParameter, /* @Nullable */ RecursionGuard<JvmTypeParameter> stack) {
	for(JvmTypeConstraint constraint: typeParameter.getConstraints()) {
		if (constraint.eClass() == TypesPackage.Literals.JVM_UPPER_BOUND) {
			JvmTypeReference upperBound = constraint.getTypeReference();
			if (upperBound != null) {
				JvmType upperBoundType = upperBound.getType();
				if (upperBoundType == null) {
					return false;
				}
				if (upperBoundType.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) {
					return isWrapper((JvmGenericType)upperBoundType);
				}
				// guard against recursive deps
				if (upperBoundType.eClass() == TypesPackage.Literals.JVM_TYPE_PARAMETER) {
					if (typeParameter == upperBoundType || stack != null && !stack.tryNext((JvmTypeParameter) upperBoundType)) {
						return false;
					}
					if (stack == null) {
						stack = new RecursionGuard<JvmTypeParameter>();
						stack.tryNext(typeParameter);
						stack.tryNext((JvmTypeParameter) upperBoundType);
					}
					return isWrapper((JvmTypeParameter) upperBoundType, stack);
				}
			}
		}
	}
	return false;
}
 
Example 9
Source File: DeferredTypeParameterHintCollector.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected LightweightTypeReference getStricterConstraint(final UnboundTypeReference typeParameter, LightweightTypeReference hint) {
	final JvmTypeParameter parameter = typeParameter.getTypeParameter();
	List<JvmTypeConstraint> constraints = parameter.getConstraints();
	for(JvmTypeConstraint constraint: constraints) {
		JvmTypeReference constraintReference = constraint.getTypeReference();
		if (constraintReference != null) {
			final boolean[] recursive = new boolean[] { false };
			LightweightTypeReferenceFactory factory = new LightweightTypeReferenceFactory(hint.getOwner()) {
				@Override
				public LightweightTypeReference doVisitParameterizedTypeReference(JvmParameterizedTypeReference reference) {
					JvmType type = reference.getType();
					if (type == parameter) {// recursively bound
						recursive[0] = true;
					}
					return super.doVisitParameterizedTypeReference(reference);
				}
			};
			LightweightTypeReference lightweightReference = factory.toLightweightReference(constraintReference);
			if (!recursive[0]) {
				if (hint.isAssignableFrom(lightweightReference)) {
					hint = lightweightReference;	
				} else if (hint.isResolved() && !lightweightReference.getRawTypeReference().isAssignableFrom(hint, TypeConformanceComputationArgument.RAW)) {
					return null;
				}
			}
		}
	}
	return hint;
}