Search in sources :

Example 1 with ResolvedConstructor

use of org.eclipse.xtext.xbase.typesystem.override.ResolvedConstructor in project xtext-eclipse by eclipse.

the class XbaseDeclarativeHoverSignatureProvider method _signature.

protected String _signature(XConstructorCall constructorCall, boolean typeAtEnd) {
    if (typeAtEnd) {
        throw new UnsupportedOperationException();
    }
    IResolvedTypes resolvedTypes = typeResolver.resolveTypes(constructorCall);
    LightweightTypeReference createdType = resolvedTypes.getActualType(constructorCall);
    final List<LightweightTypeReference> typeArguments = resolvedTypes.getActualTypeArguments(constructorCall);
    final int typeArgumentCount = createdType.getTypeArguments().size();
    final int constructorTypeArgumentCount = typeArguments.size();
    final JvmConstructor constructor = constructorCall.getConstructor();
    ResolvedConstructor resolvedConstructor = new ResolvedConstructor(constructor, createdType) {

        @Override
        protected Map<JvmTypeParameter, LightweightMergedBoundTypeArgument> computeContextTypeParameterMapping() {
            Map<JvmTypeParameter, LightweightMergedBoundTypeArgument> result = super.computeContextTypeParameterMapping();
            if (typeArgumentCount == constructorTypeArgumentCount)
                return result;
            List<JvmTypeParameter> constructorTypeParameters = getDeclaration().getTypeParameters();
            for (int i = 0; i < constructorTypeParameters.size(); i++) {
                result.put(constructorTypeParameters.get(i), new LightweightMergedBoundTypeArgument(typeArguments.get(i), VarianceInfo.INVARIANT));
            }
            return result;
        }
    };
    StringBuilder result = new StringBuilder(250);
    if (typeArgumentCount != constructorTypeArgumentCount) {
        result.append("<");
        for (int i = 0; i < constructorTypeArgumentCount - typeArgumentCount; i++) {
            if (i != 0) {
                result.append(", ");
            }
            result.append(typeArguments.get(i).getHumanReadableName());
        }
        result.append("> ");
    }
    result.append(constructor.getDeclaringType().getSimpleName());
    if (typeArgumentCount != 0) {
        result.append("<");
        for (int i = constructorTypeArgumentCount - typeArgumentCount; i < constructorTypeArgumentCount; i++) {
            if (i != constructorTypeArgumentCount - typeArgumentCount) {
                result.append(", ");
            }
            result.append(typeArguments.get(i).getHumanReadableName());
        }
        result.append(">");
    }
    result.append('(');
    List<LightweightTypeReference> parameterTypes = resolvedConstructor.getResolvedParameterTypes();
    for (int i = 0; i < parameterTypes.size(); i++) {
        if (i != 0) {
            result.append(", ");
        }
        result.append(parameterTypes.get(i).getHumanReadableName());
        result.append(' ').append(constructor.getParameters().get(i).getSimpleName());
    }
    result.append(')');
    List<LightweightTypeReference> exceptions = resolvedConstructor.getResolvedExceptions();
    if (!exceptions.isEmpty()) {
        result.append(" throws ");
        for (int i = 0; i < exceptions.size(); i++) {
            if (i != 0) {
                result.append(", ");
            }
            result.append(exceptions.get(i).getHumanReadableName());
        }
    }
    return result.toString();
}
Also used : LightweightMergedBoundTypeArgument(org.eclipse.xtext.xbase.typesystem.references.LightweightMergedBoundTypeArgument) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) ResolvedConstructor(org.eclipse.xtext.xbase.typesystem.override.ResolvedConstructor)

Example 2 with ResolvedConstructor

use of org.eclipse.xtext.xbase.typesystem.override.ResolvedConstructor 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 3 with ResolvedConstructor

use of org.eclipse.xtext.xbase.typesystem.override.ResolvedConstructor 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)

Aggregations

JvmConstructor (org.eclipse.xtext.common.types.JvmConstructor)3 ResolvedConstructor (org.eclipse.xtext.xbase.typesystem.override.ResolvedConstructor)3 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)2 CoreException (org.eclipse.core.runtime.CoreException)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)1 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)1 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)1 ISemanticModification (org.eclipse.xtext.ui.editor.model.edit.ISemanticModification)1 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)1 StringBuilderBasedAppendable (org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable)1 IResolvedTypes (org.eclipse.xtext.xbase.typesystem.IResolvedTypes)1 ResolvedFeatures (org.eclipse.xtext.xbase.typesystem.override.ResolvedFeatures)1 LightweightMergedBoundTypeArgument (org.eclipse.xtext.xbase.typesystem.references.LightweightMergedBoundTypeArgument)1 ReplacingAppendable (org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable)1 OptionalParameters (org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters)1