use of org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner in project xtext-xtend by eclipse.
the class DeferredTypeParameterHintCollectorTest method in.
public Map<JvmTypeParameter, LightweightMergedBoundTypeArgument> in(final String typeParameters, final String expectedType, final String actualType) {
final JvmOperation operation = this.operation(typeParameters, expectedType, actualType);
ITypeReferenceOwner _owner = this.getOwner();
final DeferredTypeParameterHintCollector collector = new DeferredTypeParameterHintCollector(_owner);
final DefaultReentrantTypeResolver resolver = this.resolverProvider.get();
resolver.initializeFrom(EcoreUtil.getRootContainer(operation));
ITypeReferenceOwner _owner_1 = this.getOwner();
PublicResolvedTypes _publicResolvedTypes = new PublicResolvedTypes(resolver);
final MockTypeParameterSubstitutor substitutor = new MockTypeParameterSubstitutor(_owner_1, _publicResolvedTypes);
final LightweightTypeReference hasUnbounds = substitutor.substitute(this.toLightweightTypeReference(IterableExtensions.<JvmFormalParameter>head(operation.getParameters()).getParameterType()));
final LightweightTypeReference isActual = this.toLightweightTypeReference(IterableExtensions.<JvmFormalParameter>last(operation.getParameters()).getParameterType());
collector.processPairedReferences(hasUnbounds, isActual);
return substitutor.getTypeParameterMapping();
}
use of org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner in project xtext-xtend by eclipse.
the class SuperMemberImplementorTest method getContextType.
protected LightweightTypeReference getContextType() {
JvmGenericType inferredType = associations.getInferredType(xtendClass);
ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, inferredType);
ParameterizedTypeReference contextType = owner.newParameterizedTypeReference(inferredType);
for (JvmTypeParameter typeParamter : inferredType.getTypeParameters()) {
contextType.addTypeArgument(owner.newParameterizedTypeReference(typeParamter));
}
return contextType;
}
use of org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner in project xtext-xtend by eclipse.
the class ReplacingAppendableTest method insertListField.
protected XtextDocument insertListField(final String model, final String fieldName) throws Exception {
final int cursorPosition = model.indexOf('|');
String actualModel = model.replace("|", " ");
final XtendFile file = testHelper.xtendFile("Foo", actualModel);
final XtextDocument document = documentProvider.get();
document.set(actualModel);
XtextResource xtextResource = (XtextResource) file.eResource();
document.setInput(xtextResource);
final EObject context = eObjectAtOffsetHelper.resolveElementAt(xtextResource, cursorPosition);
document.modify(new IUnitOfWork.Void<XtextResource>() {
@Override
public void process(XtextResource state) throws Exception {
ReplacingAppendable a = appendableFactory.create(document, state, cursorPosition, 1);
ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, context);
LightweightTypeReference typeRef = owner.toLightweightTypeReference(services.getTypeReferences().getTypeForName(List.class, context, typesFactory.createJvmWildcardTypeReference()));
a.append(typeRef);
a.append(" ").append(fieldName);
a.commitChanges();
}
});
return document;
}
use of org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner in project xtext-xtend by eclipse.
the class CacheMethodCompileStrategy method apply.
@Override
public void apply(ITreeAppendable appendable) {
JvmOperation cacheMethod = (JvmOperation) logicalContainerProvider.getLogicalContainer(createExtensionInfo.getCreateExpression());
JvmDeclaredType containerType = cacheMethod.getDeclaringType();
IResolvedTypes resolvedTypes = typeResolver.resolveTypes(containerType);
final ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, containerType);
LightweightTypeReference listType = owner.newReferenceTo(ArrayList.class, new TypeReferenceInitializer<ParameterizedTypeReference>() {
@Override
public LightweightTypeReference enhance(ParameterizedTypeReference reference) {
reference.addTypeArgument(owner.newWildcardTypeReference());
return reference;
}
});
String cacheVarName = cacheField.getSimpleName();
String cacheKeyVarName = appendable.declareSyntheticVariable("CacheKey", "_cacheKey");
appendable.append("final ").append(listType).append(" ").append(cacheKeyVarName).append(" = ").append(CollectionLiterals.class).append(".newArrayList(");
List<JvmFormalParameter> list = cacheMethod.getParameters();
for (Iterator<JvmFormalParameter> iterator = list.iterator(); iterator.hasNext(); ) {
JvmFormalParameter jvmFormalParameter = iterator.next();
appendable.append(getVarName(jvmFormalParameter));
if (iterator.hasNext()) {
appendable.append(", ");
}
}
appendable.append(");");
// declare result variable
LightweightTypeReference returnType = resolvedTypes.getActualType(initializerMethod.getParameters().get(0));
if (returnType != null) {
appendable.newLine().append("final ").append(returnType);
} else {
appendable.newLine().append("final Object");
}
String resultVarName = "_result";
appendable.append(" ").append(resultVarName).append(";");
// open synchronize block
appendable.newLine().append("synchronized (").append(cacheVarName).append(") {");
appendable.increaseIndentation();
// if the cache contains the key return the previously created object.
appendable.newLine().append("if (").append(cacheVarName).append(".containsKey(").append(cacheKeyVarName).append(")) {");
appendable.increaseIndentation();
appendable.newLine().append("return ").append(cacheVarName).append(".get(").append(cacheKeyVarName).append(");");
appendable.decreaseIndentation().newLine().append("}");
// execute the creation
compiler.toJavaStatement(createExtensionInfo.getCreateExpression(), appendable, true);
appendable.newLine();
appendable.append(resultVarName).append(" = ");
compiler.toJavaExpression(createExtensionInfo.getCreateExpression(), appendable);
appendable.append(";");
// store the newly created object in the cache
appendable.newLine().append(cacheVarName).append(".put(").append(cacheKeyVarName).append(", ");
LightweightTypeReference fieldType = resolvedTypes.getActualType(cacheField);
LightweightTypeReference declaredResultType = fieldType.getTypeArguments().get(1);
boolean castRequired = false;
if (!declaredResultType.isAssignableFrom(returnType)) {
castRequired = true;
appendable.append("(").append(declaredResultType).append(")");
}
appendable.append(resultVarName).append(");");
// close synchronize block
appendable.decreaseIndentation();
appendable.newLine().append("}");
appendable.newLine().append(initializerMethod.getSimpleName()).append("(").append(resultVarName);
for (JvmFormalParameter parameter : cacheMethod.getParameters()) {
appendable.append(", ").append(parameter.getName());
}
appendable.append(");");
// return the result
appendable.newLine().append("return ");
if (castRequired) {
appendable.append("(").append(declaredResultType).append(")");
}
appendable.append(resultVarName).append(";");
}
use of org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner in project xtext-xtend by eclipse.
the class XtendValidator method checkExceptions.
private void checkExceptions(EObject context, List<JvmTypeReference> exceptions, EReference reference) {
Set<String> declaredExceptionNames = Sets.newHashSet();
JvmTypeReference throwableType = getServices().getTypeReferences().getTypeForName(Throwable.class, context);
if (throwableType == null) {
return;
}
ITypeReferenceOwner owner = new StandardTypeReferenceOwner(getServices(), context);
LightweightTypeReference throwableReference = owner.toLightweightTypeReference(throwableType);
for (int i = 0; i < exceptions.size(); i++) {
JvmTypeReference exception = exceptions.get(i);
// throwables may not carry generics thus the raw comparison is sufficient
if (exception.getType() != null && !exception.getType().eIsProxy()) {
if (!throwableReference.isAssignableFrom(exception.getType()))
error("No exception of type " + exception.getSimpleName() + " can be thrown; an exception type must be a subclass of Throwable", reference, i, EXCEPTION_NOT_THROWABLE);
if (!declaredExceptionNames.add(exception.getQualifiedName()))
error("Exception " + exception.getSimpleName() + " is declared twice", reference, i, EXCEPTION_DECLARED_TWICE);
}
}
}
Aggregations