use of org.eclipse.xtext.common.types.JvmTypeConstraint in project xtext-xtend by eclipse.
the class InferredJvmModelTest method testInferredTypeWithSelfReferringTypeParameter.
@Test
public void testInferredTypeWithSelfReferringTypeParameter() throws Exception {
XtendFile xtendFile = file("package foo class Foo <T extends Foo> {}");
JvmGenericType inferredType = getInferredType(xtendFile);
assertEquals(1, inferredType.getTypeParameters().size());
JvmTypeParameter typeParameter = inferredType.getTypeParameters().get(0);
assertEquals(1, typeParameter.getConstraints().size());
JvmTypeConstraint typeConstraint = typeParameter.getConstraints().get(0);
assertTrue(typeConstraint instanceof JvmUpperBound);
assertEquals(inferredType, ((JvmUpperBound) typeConstraint).getTypeReference().getType());
}
use of org.eclipse.xtext.common.types.JvmTypeConstraint in project xtext-xtend by eclipse.
the class ParserTest method testTypeParams_3.
@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);
}
}
use of org.eclipse.xtext.common.types.JvmTypeConstraint in project xtext-xtend by eclipse.
the class MemberFromSuperImplementor method appendOverrideFunction.
public void appendOverrideFunction(final XtendTypeDeclaration overrider, final IResolvedOperation overriddenOperation, final ISourceAppender appendable) {
final JvmDeclaredType inferredType = this.associations.getInferredType(overrider);
final AbstractMethodBuilder methodBuilder = this.codeBuilderFactory.createMethodBuilder(inferredType);
this.initializeExecutableBuilder(methodBuilder, inferredType, overriddenOperation);
methodBuilder.setOverrideFlag(true);
methodBuilder.setMethodName(overriddenOperation.getDeclaration().getSimpleName());
methodBuilder.setReturnType(overriddenOperation.getResolvedReturnType());
boolean _isSynchronized = overriddenOperation.getDeclaration().isSynchronized();
if (_isSynchronized) {
methodBuilder.setSynchronizedFlag(true);
}
boolean _isEmpty = overriddenOperation.getResolvedTypeParameters().isEmpty();
boolean _not = (!_isEmpty);
if (_not) {
final ArrayList<JvmTypeParameter> typeParameters = CollectionLiterals.<JvmTypeParameter>newArrayList();
final Procedure2<JvmTypeParameter, Integer> _function = (JvmTypeParameter typeParam, Integer idx) -> {
final JvmTypeParameter newTypeParam = this.typesFactory.createJvmTypeParameter();
newTypeParam.setName(typeParam.getName());
final Consumer<LightweightTypeReference> _function_1 = (LightweightTypeReference it) -> {
final JvmUpperBound upperBound = this.typesFactory.createJvmUpperBound();
upperBound.setTypeReference(it.toJavaCompliantTypeReference());
EList<JvmTypeConstraint> _constraints = newTypeParam.getConstraints();
_constraints.add(upperBound);
};
overriddenOperation.getResolvedTypeParameterConstraints((idx).intValue()).forEach(_function_1);
typeParameters.add(newTypeParam);
};
IterableExtensions.<JvmTypeParameter>forEach(overriddenOperation.getResolvedTypeParameters(), _function);
methodBuilder.setTypeParameters(typeParameters);
}
boolean _isAbstract = overriddenOperation.getDeclaration().isAbstract();
boolean _not_1 = (!_isAbstract);
if (_not_1) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("super.");
{
List<JvmTypeParameter> _resolvedTypeParameters = overriddenOperation.getResolvedTypeParameters();
boolean _hasElements = false;
for (final JvmTypeParameter typeParam : _resolvedTypeParameters) {
if (!_hasElements) {
_hasElements = true;
_builder.append("<");
} else {
_builder.appendImmediate(", ", "");
}
String _simpleName = typeParam.getSimpleName();
_builder.append(_simpleName);
}
if (_hasElements) {
_builder.append(">");
}
}
String _simpleName_1 = overriddenOperation.getDeclaration().getSimpleName();
_builder.append(_simpleName_1);
_builder.append("(");
final Function1<JvmFormalParameter, String> _function_1 = (JvmFormalParameter it) -> {
return it.getSimpleName();
};
String _join = IterableExtensions.join(ListExtensions.<JvmFormalParameter, String>map(overriddenOperation.getDeclaration().getParameters(), _function_1), ", ");
_builder.append(_join);
_builder.append(")");
final String body = _builder.toString();
final Procedure1<? super ISourceAppender> superTypeRef = this.getImplementedInterface(inferredType, overriddenOperation.getDeclaration().getDeclaringType());
final Procedure1<ISourceAppender> _function_2 = (ISourceAppender it) -> {
if (superTypeRef != null) {
superTypeRef.apply(it);
}
it.append(body);
};
methodBuilder.setBodyGenerator(_function_2);
}
boolean _isValid = methodBuilder.isValid();
if (_isValid) {
methodBuilder.build(appendable);
}
}
use of org.eclipse.xtext.common.types.JvmTypeConstraint in project xtext-xtend by eclipse.
the class XtendJvmModelInferrer method fixTypeParameters.
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);
}
}
}
Aggregations