use of org.eclipse.xtext.xbase.typesystem.IResolvedTypes in project xtext-xtend by eclipse.
the class ErrorTest method testErrorModel_006.
@Test
public void testErrorModel_006() throws Exception {
StringConcatenation _builder = new StringConcatenation();
_builder.append("@Data class A {}");
_builder.newLine();
_builder.append("@");
_builder.newLine();
final XtendFile file = this.processWithoutException(_builder);
final XtendTypeDeclaration typeDeclaration = IterableExtensions.<XtendTypeDeclaration>last(file.getXtendTypes());
final EList<XAnnotation> annotations = typeDeclaration.getAnnotations();
final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(IterableExtensions.<XAnnotation>head(annotations));
Assert.assertNotNull(resolvedTypes.getActualType(IterableExtensions.<XAnnotation>head(annotations)));
}
use of org.eclipse.xtext.xbase.typesystem.IResolvedTypes in project xtext-xtend by eclipse.
the class ErrorTest method testErrorModel_012.
@Test
public void testErrorModel_012() throws Exception {
StringConcatenation _builder = new StringConcatenation();
_builder.append("class Y {");
_builder.newLine();
_builder.append(" ");
_builder.append("static def <T> IExpectationSetters<T> expect(T value) {");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("HeaderAccess<?> unboundedMockHeaderAccess");
_builder.newLine();
_builder.append(" ");
_builder.append("def test() {");
_builder.newLine();
_builder.append(" ");
_builder.append("val Object header = unboundedMockHeaderAccess.header");
_builder.newLine();
_builder.append(" ");
_builder.append("val IExpectationSettersObject> exp1 = expect(header)");
_builder.newLine();
_builder.append(" ");
_builder.append("val IExpectationSetters<Object> exp2 = expect(unboundedMockHeaderAccess.getHeader())");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.append("interface HeaderAccess<T> {");
_builder.newLine();
_builder.append(" ");
_builder.append("def T getHeader();");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.append("interface IExpectationSetters<T> {}");
_builder.newLine();
final XtendFile file = this.processWithoutException(_builder);
final XtendTypeDeclaration y = IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes());
XtendMember _get = y.getMembers().get(3);
final XtendField exp1 = ((XtendField) _get);
final XExpression initializer = exp1.getInitialValue();
final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(initializer);
Assert.assertNotNull(resolvedTypes.getActualType(initializer));
}
use of org.eclipse.xtext.xbase.typesystem.IResolvedTypes 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.IResolvedTypes in project xtext-xtend by eclipse.
the class AnonymousClassTypeTest method testAnonymousWithAdditionalMember.
@Test
public void testAnonymousWithAdditionalMember() {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("def foo() {");
_builder.newLine();
_builder.append("\t");
_builder.append("val foo = new Runnable() {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("int bar");
_builder.newLine();
_builder.append("\t\t");
_builder.append("override run() {}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("foo");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final XtendFunction function = this.function(_builder.toString());
final JvmOperation operation = this._iXtendJvmAssociations.getDirectlyInferredOperation(function);
final IResolvedTypes resolvedTypes = this._iBatchTypeResolver.resolveTypes(operation.eResource());
Assert.assertEquals("Runnable", resolvedTypes.getActualType(operation).toString());
XExpression _expression = function.getExpression();
final XExpression variable = IterableExtensions.<XExpression>last(((XBlockExpression) _expression).getExpressions());
final LightweightTypeReference variableType = resolvedTypes.getActualType(variable);
Assert.assertEquals("__Foo_1", variableType.toString());
Assert.assertTrue(variableType.isSubtypeOf(Runnable.class));
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.xbase.typesystem.IResolvedTypes in project xtext-xtend by eclipse.
the class AnonymousClassTypeTest method testPlainAnonymous.
@Test
public void testPlainAnonymous() {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("def foo() {");
_builder.newLine();
_builder.append("\t");
_builder.append("new Runnable() {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("override run() {}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final JvmOperation operation = this._iXtendJvmAssociations.getDirectlyInferredOperation(this.function(_builder.toString()));
final IResolvedTypes resolvedTypes = this._iBatchTypeResolver.resolveTypes(operation.eResource());
Assert.assertEquals("Runnable", resolvedTypes.getActualType(operation).toString());
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
Aggregations