Search in sources :

Example 31 with JvmConstructor

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

the class XtendQuickfixProvider method addConstuctorFromSuper.

@Fix(IssueCodes.MISSING_CONSTRUCTOR)
public void addConstuctorFromSuper(final Issue issue, IssueResolutionAcceptor acceptor) {
    if (issue.getData() != null) {
        for (int i = 0; i < issue.getData().length; i += 2) {
            final URI constructorURI = URI.createURI(issue.getData()[i]);
            String javaSignature = issue.getData()[i + 1];
            String xtendSignature = "new" + javaSignature.substring(javaSignature.indexOf('('));
            acceptor.accept(issue, "Add constructor " + xtendSignature, "Add constructor " + xtendSignature, "fix_indent.gif", new ISemanticModification() {

                @Override
                public void apply(EObject element, IModificationContext context) throws Exception {
                    XtendClass clazz = (XtendClass) element;
                    JvmGenericType inferredType = associations.getInferredType(clazz);
                    ResolvedFeatures features = overrideHelper.getResolvedFeatures(inferredType);
                    ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) clazz.eResource(), insertionOffsets.getNewConstructorInsertOffset(null, clazz), 0, new OptionalParameters() {

                        {
                            ensureEmptyLinesAround = true;
                            baseIndentationLevel = 1;
                        }
                    });
                    EObject constructor = clazz.eResource().getResourceSet().getEObject(constructorURI, true);
                    if (constructor instanceof JvmConstructor) {
                        superMemberImplementor.appendConstructorFromSuper(clazz, new ResolvedConstructor((JvmConstructor) constructor, features.getType()), appendable);
                    }
                    appendable.commitChanges();
                }
            });
        }
    }
}
Also used : ResolvedFeatures(org.eclipse.xtext.xbase.typesystem.override.ResolvedFeatures) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) XtextResource(org.eclipse.xtext.resource.XtextResource) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) URI(org.eclipse.emf.common.util.URI) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) ResolvedConstructor(org.eclipse.xtext.xbase.typesystem.override.ResolvedConstructor) OptionalParameters(org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 32 with JvmConstructor

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

the class SuperMemberImplementorTest method checkImplementConstructor.

protected void checkImplementConstructor(final String firstParamType, String implementCode) {
    StringBuilderBasedAppendable appendable = new StringBuilderBasedAppendable();
    JvmConstructor constructor = Iterables.find(superClass.getDeclaredConstructors(), new Predicate<JvmConstructor>() {

        @Override
        public boolean apply(JvmConstructor c) {
            if (firstParamType == null)
                return c.getParameters().isEmpty();
            if (c.getParameters().size() >= 1) {
                return firstParamType.equals(c.getParameters().get(0).getParameterType().getSimpleName());
            }
            return false;
        }
    });
    LightweightTypeReference contextType = getContextType();
    ResolvedConstructor resolvedConstructor = new ResolvedConstructor(constructor, contextType);
    implementor.appendConstructorFromSuper(xtendClass, resolvedConstructor, appendable);
    String code = appendable.toString();
    if (!equalsIgnoreWhitespace(implementCode, code))
        assertEquals(implementCode, code);
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) StringBuilderBasedAppendable(org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable) ResolvedConstructor(org.eclipse.xtext.xbase.typesystem.override.ResolvedConstructor)

Example 33 with JvmConstructor

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

the class FindReferencesTest method testFindReferencesToAnonymousExplicitSuperConstructor.

@Test
public void testFindReferencesToAnonymousExplicitSuperConstructor() throws Exception {
    XtendClass classFoo = (XtendClass) testHelper.xtendFile("Foo", "class Foo { new() {} }").getXtendTypes().get(0);
    XtendClass classBar = (XtendClass) testHelper.xtendFile("Bar", "class Bar { val foo = new Foo{} }").getXtendTypes().get(0);
    waitForBuild();
    XtendField fieldFoo = (XtendField) classBar.getMembers().get(0);
    JvmConstructor inferredConstructor = associations.getInferredConstructor((XtendConstructor) classFoo.getMembers().get(0));
    final MockAcceptor mockAcceptor = new MockAcceptor();
    mockAcceptor.expect(((AnonymousClass) fieldFoo.getInitialValue()).getConstructorCall(), inferredConstructor, XCONSTRUCTOR_CALL__CONSTRUCTOR);
    findReferencesTester.checkFindReferences(inferredConstructor, "Java References to Foo (/test.project/src/Foo.xtend)", mockAcceptor);
}
Also used : XtendClass(org.eclipse.xtend.core.xtend.XtendClass) MockAcceptor(org.eclipse.xtend.ide.tests.findrefs.FindReferencesTestUtil.MockAcceptor) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) XtendField(org.eclipse.xtend.core.xtend.XtendField) Test(org.junit.Test)

Example 34 with JvmConstructor

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

the class JvmModelTests method testAnonymousClass_02.

@Test
public void testAnonymousClass_02() {
    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()));
        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(JvmVisibility.DEFAULT, anonymous.getVisibility());
        Assert.assertEquals(2, anonymous.getSuperTypes().size());
        Assert.assertEquals("java.lang.Runnable", 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).getParameters().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 35 with JvmConstructor

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

the class JvmModelTests method testJvmTypeParameter_04.

@Test
public void testJvmTypeParameter_04() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("class Foo {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("new <T>() {");
        _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 JvmConstructor member = IterableExtensions.<JvmConstructor>head(Iterables.<JvmConstructor>filter(clazz.getMembers(), JvmConstructor.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 : 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) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) 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