Search in sources :

Example 31 with JvmGenericType

use of org.eclipse.xtext.common.types.JvmGenericType in project xtext-xtend by eclipse.

the class JvmModelTests method testAnonymousClass_03.

@Test
public void testAnonymousClass_03() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("def <T> foo() {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("new Iterable<T>() {");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append("override iterator() {}");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final JvmOperation operation = this._iXtendJvmAssociations.getDirectlyInferredOperation(this.function(_builder.toString()));
        this.typeResolver.resolveTypes(operation.eResource());
        Assert.assertEquals(1, operation.getLocalClasses().size());
        final JvmGenericType anonymous = IterableExtensions.<JvmGenericType>head(operation.getLocalClasses());
        Assert.assertTrue(anonymous.isFinal());
        Assert.assertFalse(anonymous.isStatic());
        Assert.assertTrue(anonymous.isLocal());
        Assert.assertTrue(anonymous.isAnonymous());
        Assert.assertEquals(0, anonymous.getTypeParameters().size());
        Assert.assertEquals(JvmVisibility.DEFAULT, anonymous.getVisibility());
        Assert.assertEquals(2, anonymous.getSuperTypes().size());
        Assert.assertEquals("java.lang.Iterable<T>", IterableExtensions.<JvmTypeReference>last(anonymous.getSuperTypes()).getQualifiedName());
        Assert.assertEquals(2, anonymous.getMembers().size());
        final JvmMember constructor = IterableExtensions.<JvmMember>last(anonymous.getMembers());
        Assert.assertTrue((constructor instanceof JvmConstructor));
        Assert.assertEquals(0, ((JvmConstructor) constructor).getTypeParameters().size());
        final JvmMember overriding = IterableExtensions.<JvmMember>head(anonymous.getMembers());
        Assert.assertTrue((overriding instanceof JvmOperation));
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) JvmMember(org.eclipse.xtext.common.types.JvmMember) Test(org.junit.Test)

Example 32 with JvmGenericType

use of org.eclipse.xtext.common.types.JvmGenericType in project xtext-xtend by eclipse.

the class JvmModelTests method testJvmTypeParameter_07.

@Test
public void testJvmTypeParameter_07() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("class Foo {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("def <T> String foo() {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(this.file(_builder.toString(), false, false).getXtendTypes());
        final JvmGenericType clazz = this._iXtendJvmAssociations.getInferredType(((XtendClass) _head));
        final JvmOperation member = IterableExtensions.<JvmOperation>head(Iterables.<JvmOperation>filter(clazz.getMembers(), JvmOperation.class));
        EList<JvmTypeParameter> _typeParameters = member.getTypeParameters();
        String _plus = ("" + _typeParameters);
        Assert.assertEquals(_plus, 1, member.getTypeParameters().size());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) Test(org.junit.Test)

Example 33 with JvmGenericType

use of org.eclipse.xtext.common.types.JvmGenericType in project xtext-xtend by eclipse.

the class TestCaseCompiler method compile.

public void compile(String qualifiedName) throws IOException {
    ResourceSet set = injector.getInstance(ResourceSet.class);
    final String from = "src/" + qualifiedName.replace('.', '/') + ".xtend";
    final String to = "src-gen/" + qualifiedName.replace('.', '/') + ".java";
    Resource res = set.getResource(org.eclipse.emf.common.util.URI.createFileURI(from), true);
    EcoreUtil.resolveAll(res);
    if (!res.getErrors().isEmpty())
        throw new RuntimeException(res.getErrors().toString());
    final File file = new File(to);
    createFolders(file);
    FileWriter writer = new FileWriter(file);
    IXtendJvmAssociations associations = injector.getInstance(IXtendJvmAssociations.class);
    JvmModelGenerator generator = injector.getInstance(JvmModelGenerator.class);
    XtendFile xtendFile = (XtendFile) res.getContents().get(0);
    JvmGenericType inferredType = associations.getInferredType((XtendClass) xtendFile.getXtendTypes().get(0));
    GeneratorConfig config = injector.getInstance(IGeneratorConfigProvider.class).get(inferredType);
    CharSequence javaCode = generator.generateType(inferredType, config);
    writer.append(javaCode);
    writer.close();
    System.out.println("compiled " + from + " to " + to);
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmModelGenerator(org.eclipse.xtext.xbase.compiler.JvmModelGenerator) FileWriter(java.io.FileWriter) Resource(org.eclipse.emf.ecore.resource.Resource) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) GeneratorConfig(org.eclipse.xtext.xbase.compiler.GeneratorConfig) IGeneratorConfigProvider(org.eclipse.xtext.xbase.compiler.IGeneratorConfigProvider) IXtendJvmAssociations(org.eclipse.xtend.core.jvmmodel.IXtendJvmAssociations) File(java.io.File) XtendFile(org.eclipse.xtend.core.xtend.XtendFile)

Example 34 with JvmGenericType

use of org.eclipse.xtext.common.types.JvmGenericType in project xtext-xtend by eclipse.

the class DispatchHelperTest method testIgnoreVoidInParameterTypeInferrence_02.

@Test
public void testIgnoreVoidInParameterTypeInferrence_02() throws Exception {
    XtendClass clazz = clazz("class X {\n" + " def dispatch foo(Number n, Void v) {null}" + " def dispatch foo(Void ignore, Object o) {null}" + "}");
    JvmGenericType type = associations.getInferredType(clazz);
    JvmOperation dispatchMethod = dispatchHelper.getDispatcherOperation(type, new DispatchHelper.DispatchSignature("foo", 2));
    JvmFormalParameter firstParameter = dispatchMethod.getParameters().get(0);
    assertEquals("java.lang.Number", firstParameter.getParameterType().getIdentifier());
    JvmFormalParameter secondParameter = dispatchMethod.getParameters().get(1);
    assertEquals("java.lang.Object", secondParameter.getParameterType().getIdentifier());
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) DispatchSignature(org.eclipse.xtend.core.jvmmodel.DispatchHelper.DispatchSignature) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) DispatchHelper(org.eclipse.xtend.core.jvmmodel.DispatchHelper) Test(org.junit.Test)

Example 35 with JvmGenericType

use of org.eclipse.xtext.common.types.JvmGenericType in project xtext-xtend by eclipse.

the class DispatchHelperTest method testVisibility_00.

@Test
public void testVisibility_00() throws Exception {
    XtendClass superClazz = clazz("class Super {\n" + "  def private dispatch foo(Object x) {} \n" + "}");
    Resource subResource = superClazz.eResource().getResourceSet().createResource(URI.createURI("Sub.xtend", true));
    subResource.load(new StringInputStream("class Sub extends Super {\n" + "  def dispatch foo(String x) {}\n" + "  def dispatch foo(Number x) {}\n" + "}"), null);
    JvmGenericType type = (JvmGenericType) subResource.getContents().get(1);
    ListMultimap<DispatchSignature, JvmOperation> multimap = dispatchHelper.getDeclaredOrEnhancedDispatchMethods(type);
    List<JvmOperation> list = multimap.get(new DispatchHelper.DispatchSignature("foo", 1));
    assertEquals(2, list.size());
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) DispatchSignature(org.eclipse.xtend.core.jvmmodel.DispatchHelper.DispatchSignature) StringInputStream(org.eclipse.xtext.util.StringInputStream) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) Resource(org.eclipse.emf.ecore.resource.Resource) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) DispatchSignature(org.eclipse.xtend.core.jvmmodel.DispatchHelper.DispatchSignature) DispatchHelper(org.eclipse.xtend.core.jvmmodel.DispatchHelper) Test(org.junit.Test)

Aggregations

JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)155 Test (org.junit.Test)106 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)71 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)64 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)56 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)33 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)30 EObject (org.eclipse.emf.ecore.EObject)23 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)23 JvmMember (org.eclipse.xtext.common.types.JvmMember)21 JvmConstructor (org.eclipse.xtext.common.types.JvmConstructor)19 JvmField (org.eclipse.xtext.common.types.JvmField)19 JvmType (org.eclipse.xtext.common.types.JvmType)13 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)11 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)11 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)11 DispatchHelper (org.eclipse.xtend.core.jvmmodel.DispatchHelper)10 Resource (org.eclipse.emf.ecore.resource.Resource)9 Check (org.eclipse.xtext.validation.Check)9 DispatchSignature (org.eclipse.xtend.core.jvmmodel.DispatchHelper.DispatchSignature)8