Search in sources :

Example 1 with JvmExecutable

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

the class InferredJvmModelTest method testDispatchFunction_00.

@Test
public void testDispatchFunction_00() throws Exception {
    XtendFile xtendFile = file("class Foo { def dispatch foo(Object x) {null} def dispatch foo(String x) {null}}");
    JvmGenericType inferredType = getInferredType(xtendFile);
    // one main dispatch
    Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
    JvmOperation dispatch = findByNameAndFirstParameterType(operations, "foo", Object.class);
    assertEquals("java.lang.Object", dispatch.getReturnType().getIdentifier());
    // two internal case methods
    findByNameAndFirstParameterType(operations, "_foo", Object.class);
    findByNameAndFirstParameterType(operations, "_foo", String.class);
    for (JvmMember member : inferredType.getMembers()) {
        if (member instanceof JvmExecutable) {
            if (member.getSimpleName().startsWith("_"))
                assertEquals(JvmVisibility.PROTECTED, member.getVisibility());
            else
                assertEquals(JvmVisibility.PUBLIC, member.getVisibility());
        } else {
            assertEquals(JvmVisibility.PRIVATE, member.getVisibility());
        }
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmMember(org.eclipse.xtext.common.types.JvmMember) Test(org.junit.Test)

Example 2 with JvmExecutable

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

the class XtendValidator method doCheckDuplicateExecutables.

protected <Executable extends IResolvedExecutable> void doCheckDuplicateExecutables(JvmGenericType inferredType, List<Executable> declaredOperations, Function<String, List<Executable>> bySignature, Set<EObject> flaggedOperations) {
    Set<Executable> processed = Sets.newHashSet();
    for (Executable declaredExecutable : declaredOperations) {
        if (!processed.contains(declaredExecutable)) {
            List<Executable> sameErasure = bySignature.apply(declaredExecutable.getResolvedErasureSignature());
            if (sameErasure.size() > 1) {
                Multimap<String, Executable> perSignature = HashMultimap.create(sameErasure.size(), 2);
                outer: for (Executable executable : sameErasure) {
                    for (LightweightTypeReference parameterType : executable.getResolvedParameterTypes()) {
                        if (parameterType.isUnknown())
                            continue outer;
                    }
                    perSignature.put(executable.getResolvedSignature(), executable);
                }
                if (perSignature.size() > 1) {
                    for (Collection<Executable> sameSignature : perSignature.asMap().values()) {
                        for (Executable operationWithSameSignature : sameSignature) {
                            JvmExecutable executable = operationWithSameSignature.getDeclaration();
                            EObject otherSource = associations.getPrimarySourceElement(executable);
                            if (flaggedOperations.add(otherSource)) {
                                if (sameSignature.size() > 1) {
                                    error("Duplicate " + typeLabel(executable) + " " + operationWithSameSignature.getSimpleSignature() + " in type " + inferredType.getSimpleName(), otherSource, nameFeature(otherSource), DUPLICATE_METHOD);
                                } else {
                                    error("The " + typeLabel(executable) + " " + operationWithSameSignature.getSimpleSignature() + " has the same erasure " + operationWithSameSignature.getResolvedErasureSignature() + " as another " + typeLabel(executable) + " in type " + inferredType.getSimpleName(), otherSource, nameFeature(otherSource), DUPLICATE_METHOD);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) EObject(org.eclipse.emf.ecore.EObject) RichString(org.eclipse.xtend.core.xtend.RichString) IResolvedExecutable(org.eclipse.xtext.xbase.typesystem.override.IResolvedExecutable) JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) XtendExecutable(org.eclipse.xtend.core.xtend.XtendExecutable)

Example 3 with JvmExecutable

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

the class XtendGenerator method compileLocalTypeStubs.

public void compileLocalTypeStubs(final JvmFeature feature, final ITreeAppendable appendable, final GeneratorConfig config) {
    final Function1<JvmGenericType, Boolean> _function = (JvmGenericType it) -> {
        boolean _isAnonymous = it.isAnonymous();
        return Boolean.valueOf((!_isAnonymous));
    };
    final Consumer<JvmGenericType> _function_1 = (JvmGenericType it) -> {
        appendable.newLine();
        EObject _head = IterableExtensions.<EObject>head(this.getSourceElements(it));
        final AnonymousClass anonymousClass = ((AnonymousClass) _head);
        final ITreeAppendable childAppendable = appendable.trace(anonymousClass);
        childAppendable.append("abstract class ");
        this._treeAppendableUtil.traceSignificant(childAppendable, anonymousClass).append(it.getSimpleName());
        boolean _isEmpty = it.getTypeParameters().isEmpty();
        if (_isEmpty) {
            childAppendable.append(" ");
        }
        this.generateExtendsClause(it, childAppendable, null);
        childAppendable.append("{").increaseIndentation();
        boolean _needSyntheticThisVariable = this.needSyntheticThisVariable(anonymousClass, it);
        if (_needSyntheticThisVariable) {
            Pair<String, JvmGenericType> _mappedTo = Pair.<String, JvmGenericType>of("this", it);
            String _simpleName = it.getSimpleName();
            String _plus = ("_this" + _simpleName);
            final String thisName = childAppendable.declareSyntheticVariable(_mappedTo, _plus);
            childAppendable.newLine().append("final ").append(it.getSimpleName()).append(" ").append(thisName).append(" = this;").newLine();
        }
        final Procedure1<LoopParams> _function_2 = (LoopParams it_1) -> {
            final Function1<ITreeAppendable, ITreeAppendable> _function_3 = (ITreeAppendable it_2) -> {
                return it_2.newLine();
            };
            it_1.setSeparator(_function_3);
        };
        final Procedure1<JvmMember> _function_3 = (JvmMember it_1) -> {
            final ITreeAppendable memberAppendable = this._treeAppendableUtil.traceWithComments(childAppendable, it_1);
            memberAppendable.openScope();
            if ((it_1 instanceof JvmOperation)) {
                final ITreeAppendable tracedAppendable = childAppendable.trace(it_1);
                tracedAppendable.newLine();
                this.generateJavaDoc(it_1, tracedAppendable, config);
                this.generateVisibilityModifier(it_1, tracedAppendable);
                tracedAppendable.append("abstract ");
                this.generateTypeParameterDeclaration(((JvmTypeParameterDeclarator) it_1), tracedAppendable, null);
                JvmTypeReference _returnType = ((JvmOperation) it_1).getReturnType();
                boolean _tripleEquals = (_returnType == null);
                if (_tripleEquals) {
                    tracedAppendable.append("void");
                } else {
                    this._errorSafeExtensions.serializeSafely(((JvmOperation) it_1).getReturnType(), "Object", tracedAppendable);
                }
                tracedAppendable.append(" ");
                this._treeAppendableUtil.traceSignificant(tracedAppendable, it_1).append(((JvmOperation) it_1).getSimpleName());
                tracedAppendable.append("(");
                this.generateParameters(((JvmExecutable) it_1), tracedAppendable, null);
                tracedAppendable.append(")");
                this.generateThrowsClause(((JvmExecutable) it_1), tracedAppendable, null);
                tracedAppendable.append(";");
            } else {
                if ((it_1 instanceof JvmField)) {
                    final ITreeAppendable tracedAppendable_1 = childAppendable.trace(it_1);
                    tracedAppendable_1.newLine();
                    this.generateJavaDoc(it_1, tracedAppendable_1, config);
                    this.generateAnnotations(((JvmField) it_1).getAnnotations(), tracedAppendable_1, true, config);
                    if ((((JvmField) it_1).isFinal() && ((JvmField) it_1).isStatic())) {
                        tracedAppendable_1.append("final ");
                    }
                    boolean _isStatic = ((JvmField) it_1).isStatic();
                    if (_isStatic) {
                        tracedAppendable_1.append("static ");
                    }
                    boolean _isTransient = ((JvmField) it_1).isTransient();
                    if (_isTransient) {
                        tracedAppendable_1.append("transient ");
                    }
                    boolean _isVolatile = ((JvmField) it_1).isVolatile();
                    if (_isVolatile) {
                        tracedAppendable_1.append("volatile ");
                    }
                    this._errorSafeExtensions.serializeSafely(((JvmField) it_1).getType(), "Object", tracedAppendable_1);
                    tracedAppendable_1.append(" ");
                    this._treeAppendableUtil.traceSignificant(tracedAppendable_1, it_1).append(((JvmField) it_1).getSimpleName());
                    if ((((JvmField) it_1).isFinal() && ((JvmField) it_1).isStatic())) {
                        Object _constantValue = ((JvmField) it_1).getConstantValue();
                        boolean _tripleNotEquals = (_constantValue != null);
                        if (_tripleNotEquals) {
                            tracedAppendable_1.append(" = ");
                            this.generateJavaConstant(((JvmField) it_1).getConstantValue(), tracedAppendable_1);
                        } else {
                            this.generateInitialization(((JvmField) it_1), tracedAppendable_1, config);
                        }
                    }
                    tracedAppendable_1.append(";");
                } else {
                    this.generateMember(it_1, memberAppendable, config);
                }
            }
            memberAppendable.closeScope();
        };
        this._loopExtensions.<JvmMember>forEach(childAppendable, this.getAddedDeclarations(it, anonymousClass), _function_2, _function_3);
        childAppendable.decreaseIndentation().newLine().append("}");
        appendable.newLine();
    };
    IterableExtensions.<JvmGenericType>filter(feature.getLocalClasses(), _function).forEach(_function_1);
}
Also used : JvmTypeParameterDeclarator(org.eclipse.xtext.common.types.JvmTypeParameterDeclarator) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) ITreeAppendable(org.eclipse.xtext.xbase.compiler.output.ITreeAppendable) LoopParams(org.eclipse.xtext.xbase.compiler.LoopParams) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) EObject(org.eclipse.emf.ecore.EObject) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) JvmMember(org.eclipse.xtext.common.types.JvmMember) EObject(org.eclipse.emf.ecore.EObject) JvmField(org.eclipse.xtext.common.types.JvmField) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 4 with JvmExecutable

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

the class MemberFromSuperImplementor method initializeExecutableBuilder.

protected void initializeExecutableBuilder(final AbstractExecutableBuilder builder, final JvmDeclaredType overrider, final IResolvedExecutable overridden) {
    final JvmExecutable executable = overridden.getDeclaration();
    builder.setContext(overrider);
    builder.setVisibility(overridden.getDeclaration().getVisibility());
    final Procedure2<LightweightTypeReference, Integer> _function = (LightweightTypeReference it, Integer index) -> {
        final JvmFormalParameter declaredParameter = executable.getParameters().get((index).intValue());
        final AbstractParameterBuilder parameterBuilder = builder.newParameterBuilder();
        parameterBuilder.setName(declaredParameter.getSimpleName());
        parameterBuilder.setType(it);
        JvmAnnotationReference _findAnnotation = this.annotationLookup.findAnnotation(declaredParameter, Extension.class);
        boolean _tripleNotEquals = (_findAnnotation != null);
        parameterBuilder.setExtensionFlag(_tripleNotEquals);
    };
    IterableExtensions.<LightweightTypeReference>forEach(overridden.getResolvedParameterTypes(), _function);
    builder.setVarArgsFlag(executable.isVarArgs());
    builder.setExceptions(overridden.getResolvedExceptions());
}
Also used : Extension(org.eclipse.xtext.xbase.lib.Extension) JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) AbstractParameterBuilder(org.eclipse.xtend.ide.codebuilder.AbstractParameterBuilder) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference)

Example 5 with JvmExecutable

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

the class ImplementMemberFromSuperAssist method createOverrideMethodProposal.

protected ICompletionProposal createOverrideMethodProposal(XtendTypeDeclaration model, IResolvedExecutable overrideable, final ContentAssistContext context, IProposalConflictHelper conflictHelper) {
    IXtextDocument document = context.getDocument();
    XtextResource resource = (XtextResource) model.eResource();
    int offset = context.getReplaceRegion().getOffset();
    int currentIndentation = appendableFactory.getIndentationLevelAtOffset(offset, document, resource);
    final int indentationLevel = currentIndentation == 0 ? 1 : currentIndentation;
    ReplacingAppendable appendable = appendableFactory.create(document, resource, offset, context.getReplaceRegion().getLength(), new OptionalParameters() {

        {
            ensureEmptyLinesAround = true;
            baseIndentationLevel = indentationLevel;
        }
    });
    final String simpleName;
    JvmExecutable declaration = overrideable.getDeclaration();
    if (overrideable instanceof IResolvedOperation) {
        implementor.appendOverrideFunction(model, (IResolvedOperation) overrideable, appendable);
        simpleName = overrideable.getDeclaration().getSimpleName();
    } else if (model instanceof XtendClass) {
        implementor.appendConstructorFromSuper((XtendClass) model, (IResolvedConstructor) overrideable, appendable);
        simpleName = "new";
    } else {
        return null;
    }
    String code = appendable.getCode();
    if (!isValidProposal(code.trim(), context, conflictHelper) && !isValidProposal(simpleName, context, conflictHelper))
        return null;
    ImageDescriptor imageDescriptor = images.forOperation(declaration.getVisibility(), adornments.getOverrideAdornment(declaration));
    ImportOrganizingProposal completionProposal = createCompletionProposal(appendable, context.getReplaceRegion(), getLabel(overrideable), imageHelper.getImage(imageDescriptor));
    Matcher matcher = bodyExpressionPattern.matcher(code);
    if (matcher.find()) {
        int bodyExpressionLength = matcher.end(1) - matcher.start(1);
        int bodyExpressionStart = matcher.start(1) + appendable.getTotalOffset() - completionProposal.getReplacementOffset();
        if (bodyExpressionLength == 0) {
            completionProposal.setCursorPosition(bodyExpressionStart);
        } else {
            completionProposal.setSelectionStart(completionProposal.getReplacementOffset() + bodyExpressionStart);
            completionProposal.setSelectionLength(bodyExpressionLength);
            completionProposal.setAutoInsertable(false);
            completionProposal.setCursorPosition(bodyExpressionStart + bodyExpressionLength);
            completionProposal.setSimpleLinkedMode(context.getViewer(), '\t');
        }
    }
    completionProposal.setPriority(getPriority(model, declaration, context));
    completionProposal.setMatcher(new PrefixMatcher() {

        @Override
        public boolean isCandidateMatchingPrefix(String name, String prefix) {
            PrefixMatcher delegate = context.getMatcher();
            boolean result = delegate.isCandidateMatchingPrefix(simpleName, prefix);
            return result;
        }
    });
    return completionProposal;
}
Also used : PrefixMatcher(org.eclipse.xtext.ui.editor.contentassist.PrefixMatcher) PrefixMatcher(org.eclipse.xtext.ui.editor.contentassist.PrefixMatcher) Matcher(java.util.regex.Matcher) XtextResource(org.eclipse.xtext.resource.XtextResource) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) StyledString(org.eclipse.jface.viewers.StyledString) IResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.IResolvedOperation) IResolvedConstructor(org.eclipse.xtext.xbase.typesystem.override.IResolvedConstructor) OptionalParameters(org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters) JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) ImageDescriptor(org.eclipse.jface.resource.ImageDescriptor) ImportOrganizingProposal(org.eclipse.xtext.xbase.ui.contentassist.ImportOrganizingProposal) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Aggregations

JvmExecutable (org.eclipse.xtext.common.types.JvmExecutable)8 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)3 JvmMember (org.eclipse.xtext.common.types.JvmMember)3 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)3 EObject (org.eclipse.emf.ecore.EObject)2 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)2 JvmField (org.eclipse.xtext.common.types.JvmField)2 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)2 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)2 XMemberFeatureCall (org.eclipse.xtext.xbase.XMemberFeatureCall)2 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)2 Matcher (java.util.regex.Matcher)1 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)1 StyledString (org.eclipse.jface.viewers.StyledString)1 AnonymousClass (org.eclipse.xtend.core.xtend.AnonymousClass)1 RichString (org.eclipse.xtend.core.xtend.RichString)1 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)1 XtendExecutable (org.eclipse.xtend.core.xtend.XtendExecutable)1 AbstractParameterBuilder (org.eclipse.xtend.ide.codebuilder.AbstractParameterBuilder)1 JvmAnnotationReference (org.eclipse.xtext.common.types.JvmAnnotationReference)1