Search in sources :

Example 71 with JvmTypeReference

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

the class XtendValidator method hasCycleInHierarchy.

protected boolean hasCycleInHierarchy(JvmGenericType type, Set<JvmGenericType> processedSuperTypes) {
    JvmDeclaredType container = type;
    do {
        if (processedSuperTypes.contains(container))
            return true;
        container = container.getDeclaringType();
    } while (container != null);
    processedSuperTypes.add(type);
    for (JvmTypeReference superTypeRef : type.getSuperTypes()) {
        if (superTypeRef.getType() instanceof JvmGenericType) {
            if (hasCycleInHierarchy((JvmGenericType) superTypeRef.getType(), processedSuperTypes))
                return true;
        }
    }
    processedSuperTypes.remove(type);
    return false;
}
Also used : JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType)

Example 72 with JvmTypeReference

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

the class XtendValidator method checkExceptions.

private void checkExceptions(EObject context, List<JvmTypeReference> exceptions, EReference reference) {
    Set<String> declaredExceptionNames = Sets.newHashSet();
    JvmTypeReference throwableType = getServices().getTypeReferences().getTypeForName(Throwable.class, context);
    if (throwableType == null) {
        return;
    }
    ITypeReferenceOwner owner = new StandardTypeReferenceOwner(getServices(), context);
    LightweightTypeReference throwableReference = owner.toLightweightTypeReference(throwableType);
    for (int i = 0; i < exceptions.size(); i++) {
        JvmTypeReference exception = exceptions.get(i);
        // throwables may not carry generics thus the raw comparison is sufficient
        if (exception.getType() != null && !exception.getType().eIsProxy()) {
            if (!throwableReference.isAssignableFrom(exception.getType()))
                error("No exception of type " + exception.getSimpleName() + " can be thrown; an exception type must be a subclass of Throwable", reference, i, EXCEPTION_NOT_THROWABLE);
            if (!declaredExceptionNames.add(exception.getQualifiedName()))
                error("Exception " + exception.getSimpleName() + " is declared twice", reference, i, EXCEPTION_DECLARED_TWICE);
        }
    }
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) RichString(org.eclipse.xtend.core.xtend.RichString) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 73 with JvmTypeReference

use of org.eclipse.xtext.common.types.JvmTypeReference 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 74 with JvmTypeReference

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

the class XtendFormatter method _format.

protected void _format(final XtendParameter param, @Extension final IFormattableDocument format) {
    final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
        it.oneSpace();
    };
    format.append(this.textRegionExtensions.regionFor(param).keyword("extension"), _function);
    this.formatAnnotations(param, format, XbaseFormatterPreferenceKeys.newLineAfterParameterAnnotations);
    format.<JvmTypeReference>format(param.getParameterType());
    final ISemanticRegion nameNode = this.textRegionExtensions.regionFor(param).feature(XtendPackage.Literals.XTEND_PARAMETER__NAME);
    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
        it.oneSpace();
    };
    format.prepend(nameNode, _function_1);
}
Also used : IHiddenRegionFormatter(org.eclipse.xtext.formatting2.IHiddenRegionFormatter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)

Example 75 with JvmTypeReference

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

the class AnonymousClassUtil method isSameSignature.

protected boolean isSameSignature(JvmConstructor constructor, JvmConstructor superConstructor) {
    if (constructor.getParameters().size() != superConstructor.getParameters().size())
        return false;
    for (int i = 0; i < constructor.getParameters().size(); ++i) {
        JvmTypeReference paramType = constructor.getParameters().get(i).getParameterType();
        JvmTypeReference superParamType = superConstructor.getParameters().get(i).getParameterType();
        if (!paramType.getIdentifier().equals(superParamType.getIdentifier()))
            return false;
    }
    return true;
}
Also used : JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference)

Aggregations

JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)130 Test (org.junit.Test)58 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)46 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)38 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)36 JvmType (org.eclipse.xtext.common.types.JvmType)28 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)23 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)18 XExpression (org.eclipse.xtext.xbase.XExpression)18 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)17 EObject (org.eclipse.emf.ecore.EObject)15 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)12 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)12 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)12 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)12 AnonymousClass (org.eclipse.xtend.core.xtend.AnonymousClass)8 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)8 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)8 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)8 JvmParameterizedTypeReference (org.eclipse.xtext.common.types.JvmParameterizedTypeReference)8