Search in sources :

Example 26 with JvmDeclaredType

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

the class DispatchHelper method getAllDispatchCases.

/**
 * Return all the cases that are associated with the given dispatch operation.
 */
public List<JvmOperation> getAllDispatchCases(JvmOperation dispatcherOperation) {
    DispatchSignature dispatchSignature = new DispatchSignature(dispatcherOperation.getSimpleName(), dispatcherOperation.getParameters().size());
    JvmDeclaredType type = dispatcherOperation.getDeclaringType();
    ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, type);
    ContextualVisibilityHelper contextualVisibilityHelper = new ContextualVisibilityHelper(visibilityHelper, owner.newParameterizedTypeReference(type));
    return getAllDispatchMethods(dispatchSignature, type, contextualVisibilityHelper);
}
Also used : ContextualVisibilityHelper(org.eclipse.xtext.xbase.typesystem.util.ContextualVisibilityHelper) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 27 with JvmDeclaredType

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

the class XtendJvmModelInferrer method infer.

@Override
public void infer(/* @Nullable */
EObject object, final /* @NonNull */
IJvmDeclaredTypeAcceptor acceptor, boolean preIndexingPhase) {
    if (!(object instanceof XtendFile))
        return;
    final Set<JvmDeclaredType> types = new LinkedHashSet<JvmDeclaredType>();
    final IJvmDeclaredTypeAcceptor wrapper = new IJvmDeclaredTypeAcceptor() {

        @SuppressWarnings("deprecation")
        @Override
        public <T extends JvmDeclaredType> IPostIndexingInitializing<T> accept(T type) {
            types.add(type);
            return acceptor.accept(type);
        }

        @Override
        public <T extends JvmDeclaredType> void accept(T type, Procedure1<? super T> lateInitialization) {
            types.add(type);
            acceptor.accept(type, lateInitialization);
        }
    };
    final XtendFile xtendFile = (XtendFile) object;
    generatorConfig = generatorConfigProvider.get(xtendFile);
    final List<Runnable> doLater = newArrayList();
    for (final XtendTypeDeclaration declaration : xtendFile.getXtendTypes()) {
        inferTypeSceleton(declaration, wrapper, preIndexingPhase, xtendFile, doLater, null);
    }
    ActiveAnnotationContexts contexts = null;
    BatchLinkableResource resource = (BatchLinkableResource) xtendFile.eResource();
    try {
        compilerPhases.setIndexing(xtendFile, true);
        try {
            contexts = contextProvider.computeContext(xtendFile);
        } catch (Throwable t) {
            operationCanceledManager.propagateAsErrorIfCancelException(t);
            logger.error("Couldn't create annotation contexts", t);
            return;
        }
        try {
            contexts.before(ActiveAnnotationContexts.AnnotationCallback.INDEXING);
            for (ActiveAnnotationContext ctx : contexts.getContexts().values()) {
                try {
                    annotationProcessor.indexingPhase(ctx, wrapper, CancelIndicator.NullImpl);
                } catch (Throwable t) {
                    operationCanceledManager.propagateAsErrorIfCancelException(t);
                    ctx.handleProcessingError(xtendFile.eResource(), t);
                }
            }
        } finally {
            contexts.after(ActiveAnnotationContexts.AnnotationCallback.INDEXING);
        }
    } finally {
        compilerPhases.setIndexing(xtendFile, false);
        resource.getCache().clear(resource);
    }
    if (!preIndexingPhase) {
        final ActiveAnnotationContexts finalContexts = contexts;
        Runnable lateInit = new Runnable() {

            @Override
            public void run() {
                for (Runnable runnable : doLater) {
                    runnable.run();
                }
                try {
                    finalContexts.before(ActiveAnnotationContexts.AnnotationCallback.INFERENCE);
                    for (ActiveAnnotationContext ctx : finalContexts.getContexts().values()) {
                        try {
                            annotationProcessor.inferencePhase(ctx, CancelIndicator.NullImpl);
                        } catch (Throwable t) {
                            operationCanceledManager.propagateAsErrorIfCancelException(t);
                            ctx.handleProcessingError(xtendFile.eResource(), t);
                        }
                    }
                } finally {
                    finalContexts.after(ActiveAnnotationContexts.AnnotationCallback.INFERENCE);
                }
            }
        };
        resource.addJvmMemberInitializer(lateInit);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) LinkedHashSet(java.util.LinkedHashSet) BatchLinkableResource(org.eclipse.xtext.xbase.resource.BatchLinkableResource) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) ActiveAnnotationContexts(org.eclipse.xtend.core.macro.ActiveAnnotationContexts) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) ActiveAnnotationContext(org.eclipse.xtend.core.macro.ActiveAnnotationContext) IJvmDeclaredTypeAcceptor(org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor)

Example 28 with JvmDeclaredType

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

the class XtendJvmModelInferrer method initialize.

protected void initialize(XtendClass source, JvmGenericType inferredJvmType) {
    inferredJvmType.setVisibility(source.getVisibility());
    boolean isStatic = source.isStatic() && !isTopLevel(source);
    if (!isStatic) {
        JvmDeclaredType declaringType = inferredJvmType.getDeclaringType();
        if (declaringType instanceof JvmGenericType) {
            if (((JvmGenericType) declaringType).isInterface())
                isStatic = true;
        } else if (declaringType instanceof JvmAnnotationType) {
            isStatic = true;
        }
    }
    inferredJvmType.setStatic(isStatic);
    inferredJvmType.setAbstract(source.isAbstract());
    inferredJvmType.setStrictFloatingPoint(source.isStrictFloatingPoint());
    if (!inferredJvmType.isAbstract()) {
        inferredJvmType.setFinal(source.isFinal());
    }
    translateAnnotationsTo(source.getAnnotations(), inferredJvmType);
    JvmTypeReference extendsClause = source.getExtends();
    if (extendsClause == null || extendsClause.getType() == null) {
        JvmTypeReference typeRefToObject = typeReferences.getTypeForName(Object.class, source);
        if (typeRefToObject != null)
            inferredJvmType.getSuperTypes().add(typeRefToObject);
    } else {
        inferredJvmType.getSuperTypes().add(jvmTypesBuilder.cloneWithProxies(extendsClause));
    }
    for (JvmTypeReference intf : source.getImplements()) {
        inferredJvmType.getSuperTypes().add(jvmTypesBuilder.cloneWithProxies(intf));
    }
    fixTypeParameters(inferredJvmType);
    addDefaultConstructor(source, inferredJvmType);
    for (XtendMember member : source.getMembers()) {
        if (member instanceof XtendField || (member instanceof XtendFunction && ((XtendFunction) member).getName() != null) || member instanceof XtendConstructor) {
            transform(member, inferredJvmType, true);
        }
    }
    appendSyntheticDispatchMethods(source, inferredJvmType);
    jvmTypesBuilder.copyDocumentationTo(source, inferredJvmType);
    nameClashResolver.resolveNameClashes(inferredJvmType);
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) XtendConstructor(org.eclipse.xtend.core.xtend.XtendConstructor) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) XtendField(org.eclipse.xtend.core.xtend.XtendField)

Example 29 with JvmDeclaredType

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

the class XtendJvmModelInferrer method inferTypeSceleton.

protected void inferTypeSceleton(final XtendTypeDeclaration declaration, final IJvmDeclaredTypeAcceptor acceptor, boolean preIndexingPhase, XtendFile xtendFile, List<Runnable> doLater, JvmDeclaredType containerSceleton) {
    JvmDeclaredType inferredSceleton = doInferTypeSceleton(declaration, acceptor, preIndexingPhase, xtendFile, doLater);
    if (inferredSceleton != null) {
        setNameAndAssociate(xtendFile, declaration, inferredSceleton);
        if (containerSceleton != null)
            containerSceleton.getMembers().add(inferredSceleton);
        acceptor.accept(inferredSceleton);
        for (XtendMember member : declaration.getMembers()) {
            if (member instanceof XtendTypeDeclaration)
                inferTypeSceleton((XtendTypeDeclaration) member, acceptor, preIndexingPhase, xtendFile, doLater, inferredSceleton);
        }
    }
}
Also used : XtendMember(org.eclipse.xtend.core.xtend.XtendMember) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType)

Example 30 with JvmDeclaredType

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

the class XtendCompilerTest method compileAllClassesWithTheSameFileHeader.

@Test
public void compileAllClassesWithTheSameFileHeader() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("/**");
        _builder.newLine();
        _builder.append(" ");
        _builder.append("* Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others.");
        _builder.newLine();
        _builder.append(" ");
        _builder.append("* All rights reserved. This program and the accompanying materials");
        _builder.newLine();
        _builder.append(" ");
        _builder.append("* are made available under the terms of the Eclipse Public License v1.0");
        _builder.newLine();
        _builder.append(" ");
        _builder.append("* which accompanies this distribution, and is available at");
        _builder.newLine();
        _builder.append(" ");
        _builder.append("* http://www.eclipse.org/legal/epl-v10.html");
        _builder.newLine();
        _builder.append(" ");
        _builder.append("*/");
        _builder.newLine();
        _builder.append("package foo");
        _builder.newLine();
        _builder.newLine();
        _builder.append("class bar {");
        _builder.newLine();
        _builder.append("    ");
        _builder.append("String name = \'foobar\'");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        _builder.newLine();
        _builder.append("class baz {");
        _builder.newLine();
        _builder.append("    ");
        _builder.append("String name = \'foobaz\'");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final String input = _builder.toString();
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("/**");
        _builder_1.newLine();
        _builder_1.append(" ");
        _builder_1.append("* Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others.");
        _builder_1.newLine();
        _builder_1.append(" ");
        _builder_1.append("* All rights reserved. This program and the accompanying materials");
        _builder_1.newLine();
        _builder_1.append(" ");
        _builder_1.append("* are made available under the terms of the Eclipse Public License v1.0");
        _builder_1.newLine();
        _builder_1.append(" ");
        _builder_1.append("* which accompanies this distribution, and is available at");
        _builder_1.newLine();
        _builder_1.append(" ");
        _builder_1.append("* http://www.eclipse.org/legal/epl-v10.html");
        _builder_1.newLine();
        _builder_1.append(" ");
        _builder_1.append("*/");
        _builder_1.newLine();
        _builder_1.append("package foo;");
        _builder_1.newLine();
        _builder_1.newLine();
        _builder_1.append("@SuppressWarnings(\"all\")");
        _builder_1.newLine();
        _builder_1.append("public class bar {");
        _builder_1.newLine();
        _builder_1.append("  ");
        _builder_1.append("private String name = \"foobar\";");
        _builder_1.newLine();
        _builder_1.append("}");
        _builder_1.newLine();
        final String expectedBarClass = _builder_1.toString();
        StringConcatenation _builder_2 = new StringConcatenation();
        _builder_2.append("/**");
        _builder_2.newLine();
        _builder_2.append(" ");
        _builder_2.append("* Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others.");
        _builder_2.newLine();
        _builder_2.append(" ");
        _builder_2.append("* All rights reserved. This program and the accompanying materials");
        _builder_2.newLine();
        _builder_2.append(" ");
        _builder_2.append("* are made available under the terms of the Eclipse Public License v1.0");
        _builder_2.newLine();
        _builder_2.append(" ");
        _builder_2.append("* which accompanies this distribution, and is available at");
        _builder_2.newLine();
        _builder_2.append(" ");
        _builder_2.append("* http://www.eclipse.org/legal/epl-v10.html");
        _builder_2.newLine();
        _builder_2.append(" ");
        _builder_2.append("*/");
        _builder_2.newLine();
        _builder_2.append("package foo;");
        _builder_2.newLine();
        _builder_2.newLine();
        _builder_2.append("@SuppressWarnings(\"all\")");
        _builder_2.newLine();
        _builder_2.append("public class baz {");
        _builder_2.newLine();
        _builder_2.append("  ");
        _builder_2.append("private String name = \"foobaz\";");
        _builder_2.newLine();
        _builder_2.append("}");
        _builder_2.newLine();
        final String expectedBazClass = _builder_2.toString();
        final XtendFile file = this.file(input.toString(), true);
        final JvmDeclaredType barType = IterableExtensions.<JvmDeclaredType>head(Iterables.<JvmDeclaredType>filter(file.eResource().getContents(), JvmDeclaredType.class));
        final JvmDeclaredType bazType = IterableExtensions.<JvmDeclaredType>last(Iterables.<JvmDeclaredType>filter(file.eResource().getContents(), JvmDeclaredType.class));
        final GeneratorConfig generatorConfig = this.generatorConfigProvider.get(barType);
        CharSequence barJavaCode = this.generator.generateType(barType, generatorConfig);
        barJavaCode = this.postProcessor.postProcess(null, barJavaCode);
        CharSequence bazJavaCode = this.generator.generateType(bazType, generatorConfig);
        bazJavaCode = this.postProcessor.postProcess(null, bazJavaCode);
        XtendCompilerTest.assertEquals(expectedBarClass.toString(), barJavaCode.toString());
        XtendCompilerTest.assertEquals(expectedBazClass.toString(), bazJavaCode.toString());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) GeneratorConfig(org.eclipse.xtext.xbase.compiler.GeneratorConfig) AbstractXtendCompilerTest(org.eclipse.xtend.core.tests.compiler.AbstractXtendCompilerTest) Test(org.junit.Test)

Aggregations

JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)74 EObject (org.eclipse.emf.ecore.EObject)21 JvmType (org.eclipse.xtext.common.types.JvmType)18 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)12 JvmMember (org.eclipse.xtext.common.types.JvmMember)12 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)10 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)8 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)8 JvmConstructor (org.eclipse.xtext.common.types.JvmConstructor)8 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)8 Test (org.junit.Test)8 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)7 Resource (org.eclipse.emf.ecore.resource.Resource)5 AnonymousClass (org.eclipse.xtend.core.xtend.AnonymousClass)5 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)5 JvmAnnotationType (org.eclipse.xtext.common.types.JvmAnnotationType)5 XExpression (org.eclipse.xtext.xbase.XExpression)5 List (java.util.List)4 EList (org.eclipse.emf.common.util.EList)4 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)4