Java Code Examples for org.eclipse.xtext.common.types.JvmTypeParameter#getConstraints()

The following examples show how to use org.eclipse.xtext.common.types.JvmTypeParameter#getConstraints() . 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: 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 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: UIStrings.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public String toString(Iterable<? extends JvmTypeParameter> elements) {
	StringBuilder buffer = new StringBuilder();
	boolean needsSeparator = false;
	for (JvmTypeParameter type : elements) {
		if (needsSeparator)
			buffer.append(", ");
		needsSeparator = true;
		if (type != null) {
			buffer.append(type.getSimpleName());
			List<String> upper = newArrayList();
			String lower = null;
			for (JvmTypeConstraint constr : type.getConstraints()) {
				String simpleName = constr.getTypeReference().getSimpleName();
				if (constr instanceof JvmUpperBound) {
					upper.add(simpleName);
				} else {
					lower = simpleName;
				}
			}
			if(!upper.isEmpty()) {
				buffer.append(" extends ");
				buffer.append(Strings.concat(" & ", upper));
			}
			if(lower!=null) {
				buffer.append(" super ");
				buffer.append(lower);
			}
		} else
			buffer.append("[null]");
	}
	return buffer.toString();
}
 
Example 6
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 7
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 8
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 9
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 10
Source File: AbstractResolvedOperation.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public List<LightweightTypeReference> getResolvedTypeParameterConstraints(int idx) throws IndexOutOfBoundsException {
	JvmTypeParameter typeParameter = getResolvedTypeParameters().get(idx);
	List<JvmTypeConstraint> constraints = typeParameter.getConstraints();
	List<JvmTypeReference> constraintReferences = Lists.newArrayListWithCapacity(constraints.size());
	for(JvmTypeConstraint constraint: constraints) {
		constraintReferences.add(constraint.getTypeReference());
	}
	List<LightweightTypeReference> result = getResolvedReferences(constraintReferences);
	return result;
}
 
Example 11
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;
}
 
Example 12
Source File: XtypeFormatter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _format(final JvmTypeParameter ref, @Extension final IFormattableDocument document) {
  EList<JvmTypeConstraint> _constraints = ref.getConstraints();
  for (final JvmTypeConstraint c : _constraints) {
    {
      final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
        it.oneSpace();
      };
      document.<JvmTypeConstraint>prepend(c, _function);
      document.<JvmTypeConstraint>format(c);
    }
  }
}
 
Example 13
Source File: XbaseFormatter2.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _format(final JvmTypeParameter ref, final FormattableDocument document) {
  EList<JvmTypeConstraint> _constraints = ref.getConstraints();
  for (final JvmTypeConstraint c : _constraints) {
    {
      final Procedure1<FormattingDataInit> _function = (FormattingDataInit it) -> {
        it.oneSpace();
      };
      Function1<? super FormattableDocument, ? extends Iterable<FormattingData>> _prepend = this._formattingDataFactory.prepend(this._nodeModelAccess.nodeForEObject(c), _function);
      document.operator_add(_prepend);
      this.format(c, document);
    }
  }
}
 
Example 14
Source File: ReflectionTypeFactory.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected JvmTypeParameter createTypeParameter(TypeVariable<?> variable, JvmMember container) {
	JvmTypeParameter result = TypesFactory.eINSTANCE.createJvmTypeParameter();
	result.setName(variable.getName());
	Type[] bounds = variable.getBounds();
	if (bounds.length != 0) {
		InternalEList<JvmTypeConstraint> constraints = (InternalEList<JvmTypeConstraint>)result.getConstraints();
		for (Type bound : variable.getBounds()) {
			JvmUpperBound upperBound = TypesFactory.eINSTANCE.createJvmUpperBound();
			((JvmTypeConstraintImplCustom) upperBound).internalSetTypeReference(createTypeReference(bound));
			constraints.addUnique(upperBound);
		}
	}
	return result;
}
 
Example 15
Source File: HoverUiStrings.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String typeParameters(Iterable<? extends JvmTypeParameter> typeParams) {
	if (!isEmpty(typeParams)) {
		StringBuilder result = new StringBuilder("<");
		boolean needsSeparator = false;
		OUTER: for (JvmTypeParameter typeParam : typeParams) {
			if (needsSeparator)
				result.append(", ");
			needsSeparator = true;
			if(typeParam != null) {
				result.append(typeParam.getSimpleName());
				List<JvmTypeConstraint> constraints = typeParam.getConstraints();
				if (!constraints.isEmpty()) {
					if (constraints.size() == 1) {
						JvmType singleConstraint = constraints.get(0).getTypeReference().getType();
						if (Object.class.getName().equals(singleConstraint.getIdentifier())) {
							continue OUTER;
						}
					}
					result.append(" extends ");
					for(int i = 0; i < constraints.size(); i++) {
						if (i != 0) {
							result.append(" & ");
						}
						result.append(constraints.get(i).getTypeReference().getSimpleName());
					}
				}
			} else 
				result.append("[null]");
		}
		return result.append(">").toString();
	}
	return "";
}
 
Example 16
Source File: ParserTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testTypeParams_3() throws Exception {
	XtendFunction func = function("def <T extends CharSequence & java.io.Serializable> foo(T t) { t}");
	assertEquals(1, func.getTypeParameters().size());
	JvmTypeParameter tp = func.getTypeParameters().get(0);
	assertEquals("T", tp.getName());
	assertEquals(2, tp.getConstraints().size());
	for (JvmTypeConstraint constraint : tp.getConstraints()) {
		assertTrue(constraint instanceof JvmUpperBound);
	}
}