Search in sources :

Example 6 with JvmConstructor

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

the class XtendCompiler method isVariableDeclarationRequired.

@Override
protected boolean isVariableDeclarationRequired(XExpression expr, ITreeAppendable b, boolean recursive) {
    boolean result = super.isVariableDeclarationRequired(expr, b, recursive);
    if (result && expr instanceof XConstructorCall) {
        EObject container = expr.eContainer();
        if (container instanceof AnonymousClass) {
            AnonymousClass anonymousClass = (AnonymousClass) container;
            result = isVariableDeclarationRequired(anonymousClass, b, recursive);
            if (result) {
                JvmConstructor constructor = anonymousClass.getConstructorCall().getConstructor();
                JvmDeclaredType type = constructor.getDeclaringType();
                if (((JvmGenericType) type).isAnonymous()) {
                    return false;
                }
            }
        }
    }
    return result;
}
Also used : AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) EObject(org.eclipse.emf.ecore.EObject) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) XConstructorCall(org.eclipse.xtext.xbase.XConstructorCall) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType)

Example 7 with JvmConstructor

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

the class XtendCompiler method _toJavaStatement.

@Override
protected void _toJavaStatement(final XConstructorCall expr, ITreeAppendable b, final boolean isReferenced) {
    for (XExpression arg : expr.getArguments()) {
        prepareExpression(arg, b);
    }
    if (!isReferenced) {
        b.newLine();
        constructorCallToJavaExpression(expr, b);
        if (expr.eContainer() instanceof AnonymousClass) {
            JvmConstructor constructor = expr.getConstructor();
            JvmDeclaredType declaringType = constructor.getDeclaringType();
            compileAnonymousClassBody((AnonymousClass) expr.eContainer(), declaringType, b);
        }
        b.append(";");
    } else if (isVariableDeclarationRequired(expr, b, true)) {
        Later later = new Later() {

            @Override
            public void exec(ITreeAppendable appendable) {
                constructorCallToJavaExpression(expr, appendable);
                if (expr.eContainer() instanceof AnonymousClass) {
                    JvmConstructor constructor = expr.getConstructor();
                    JvmDeclaredType declaringType = constructor.getDeclaringType();
                    compileAnonymousClassBody((AnonymousClass) expr.eContainer(), declaringType, appendable);
                }
            }
        };
        declareFreshLocalVariable(expr, b, later);
    }
}
Also used : Later(org.eclipse.xtext.xbase.compiler.Later) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) XExpression(org.eclipse.xtext.xbase.XExpression) ITreeAppendable(org.eclipse.xtext.xbase.compiler.output.ITreeAppendable)

Example 8 with JvmConstructor

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

the class AnonymousClassUtil method getSuperType.

/* @Nullable */
public JvmGenericType getSuperType(AnonymousClass anonymousClass) {
    JvmConstructor constructor = anonymousClass.getConstructorCall().getConstructor();
    if (constructor != null && !constructor.eIsProxy()) {
        JvmDeclaredType declaringType = constructor.getDeclaringType();
        JvmType superType = Iterables.getLast(declaringType.getSuperTypes()).getType();
        if (superType instanceof JvmGenericType)
            return (JvmGenericType) superType;
    }
    return null;
}
Also used : JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmType(org.eclipse.xtext.common.types.JvmType)

Example 9 with JvmConstructor

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

the class XtendJvmModelInferrer method addDefaultConstructor.

protected void addDefaultConstructor(XtendClass source, JvmGenericType target) {
    for (XtendMember member : source.getMembers()) {
        if (member instanceof XtendConstructor)
            return;
    }
    JvmConstructor constructor = typesFactory.createJvmConstructor();
    target.getMembers().add(constructor);
    associator.associate(source, constructor);
    constructor.setSimpleName(source.getName());
    constructor.setVisibility(JvmVisibility.PUBLIC);
    typeExtensions.setSynthetic(constructor, true);
}
Also used : XtendMember(org.eclipse.xtend.core.xtend.XtendMember) XtendConstructor(org.eclipse.xtend.core.xtend.XtendConstructor) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor)

Example 10 with JvmConstructor

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

the class JvmModelTests method testAnonymousClass_01.

@Test
public void testAnonymousClass_01() {
    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("int field");
        _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()));
        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.assertFalse(anonymous.isAnonymous());
        Assert.assertEquals(JvmVisibility.DEFAULT, anonymous.getVisibility());
        Assert.assertEquals(2, anonymous.getSuperTypes().size());
        Assert.assertEquals("java.lang.Runnable", IterableExtensions.<JvmTypeReference>last(anonymous.getSuperTypes()).getQualifiedName());
        Assert.assertEquals(3, anonymous.getMembers().size());
        final JvmMember constructor = IterableExtensions.<JvmMember>last(anonymous.getMembers());
        Assert.assertTrue((constructor instanceof JvmConstructor));
        Assert.assertEquals(0, ((JvmConstructor) constructor).getParameters().size());
        JvmMember _head = IterableExtensions.<JvmMember>head(anonymous.getMembers());
        Assert.assertTrue((_head instanceof JvmField));
        final JvmMember overriding = anonymous.getMembers().get(1);
        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) JvmField(org.eclipse.xtext.common.types.JvmField) Test(org.junit.Test)

Aggregations

JvmConstructor (org.eclipse.xtext.common.types.JvmConstructor)42 Test (org.junit.Test)21 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)19 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)16 EObject (org.eclipse.emf.ecore.EObject)13 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)10 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)8 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)8 JvmMember (org.eclipse.xtext.common.types.JvmMember)8 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)7 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)7 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)6 XConstructorCall (org.eclipse.xtext.xbase.XConstructorCall)6 XtextResource (org.eclipse.xtext.resource.XtextResource)5 AnonymousClass (org.eclipse.xtend.core.xtend.AnonymousClass)4 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)3 MockAcceptor (org.eclipse.xtend.ide.tests.findrefs.FindReferencesTestUtil.MockAcceptor)3 JvmField (org.eclipse.xtext.common.types.JvmField)3 URI (org.eclipse.emf.common.util.URI)2 JvmConstructorDeclarationImpl (org.eclipse.xtend.core.macro.declaration.JvmConstructorDeclarationImpl)2