Search in sources :

Example 46 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("* This program and the accompanying materials are made available under the");
        _builder.newLine();
        _builder.append(" ");
        _builder.append("* terms of the Eclipse Public License 2.0 which is available at");
        _builder.newLine();
        _builder.append(" ");
        _builder.append("* http://www.eclipse.org/legal/epl-2.0.");
        _builder.newLine();
        _builder.append(" ");
        _builder.append("* ");
        _builder.newLine();
        _builder.append(" ");
        _builder.append("* SPDX-License-Identifier: EPL-2.0");
        _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("* This program and the accompanying materials are made available under the");
        _builder_1.newLine();
        _builder_1.append(" ");
        _builder_1.append("* terms of the Eclipse Public License 2.0 which is available at");
        _builder_1.newLine();
        _builder_1.append(" ");
        _builder_1.append("* http://www.eclipse.org/legal/epl-2.0.");
        _builder_1.newLine();
        _builder_1.append(" ");
        _builder_1.append("* ");
        _builder_1.newLine();
        _builder_1.append(" ");
        _builder_1.append("* SPDX-License-Identifier: EPL-2.0");
        _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("* This program and the accompanying materials are made available under the");
        _builder_2.newLine();
        _builder_2.append(" ");
        _builder_2.append("* terms of the Eclipse Public License 2.0 which is available at");
        _builder_2.newLine();
        _builder_2.append(" ");
        _builder_2.append("* http://www.eclipse.org/legal/epl-2.0.");
        _builder_2.newLine();
        _builder_2.append(" ");
        _builder_2.append("* ");
        _builder_2.newLine();
        _builder_2.append(" ");
        _builder_2.append("* SPDX-License-Identifier: EPL-2.0");
        _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) Test(org.junit.Test)

Example 47 with JvmDeclaredType

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

the class XtendCompilerErrorHandlingTest method assertCompilesTo.

public void assertCompilesTo(final CharSequence input, final CharSequence expected, final boolean shouldBeSyntacticallyValid) {
    try {
        final XtendFile file = this.file(input.toString(), false, shouldBeSyntacticallyValid);
        final Resource resource = file.eResource();
        try {
            this.issueProviderFactory.attachData(resource);
            final JvmDeclaredType inferredType = IterableExtensions.<JvmDeclaredType>head(Iterables.<JvmDeclaredType>filter(resource.getContents(), JvmDeclaredType.class));
            CharSequence javaCode = this.generator.generateType(inferredType, this.generatorConfigProvider.get(inferredType));
            javaCode = this.postProcessor.postProcess(null, javaCode);
            Assert.assertEquals(expected.toString(), javaCode.toString());
        } finally {
            this.issueProviderFactory.detachData(resource);
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) Resource(org.eclipse.emf.ecore.resource.Resource) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType)

Example 48 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 49 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 50 with JvmDeclaredType

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

the class XtendValidator method checkDuplicateAndOverriddenFunctions.

@Check
public void checkDuplicateAndOverriddenFunctions(XtendTypeDeclaration xtendType) {
    final JvmDeclaredType inferredType = associations.getInferredType(xtendType);
    if (inferredType instanceof JvmGenericType) {
        JavaVersion targetVersion = getGeneratorConfig(xtendType).getJavaSourceVersion();
        ResolvedFeatures resolvedFeatures = overrideHelper.getResolvedFeatures(inferredType, targetVersion);
        Set<EObject> flaggedOperations = Sets.newHashSet();
        doCheckDuplicateExecutables((JvmGenericType) inferredType, resolvedFeatures, flaggedOperations);
        doCheckOverriddenMethods(xtendType, (JvmGenericType) inferredType, resolvedFeatures, flaggedOperations);
        doCheckFunctionOverrides(resolvedFeatures, flaggedOperations);
    }
}
Also used : ResolvedFeatures(org.eclipse.xtext.xbase.typesystem.override.ResolvedFeatures) EObject(org.eclipse.emf.ecore.EObject) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JavaVersion(org.eclipse.xtext.util.JavaVersion) Check(org.eclipse.xtext.validation.Check)

Aggregations

JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)138 Test (org.junit.Test)41 EObject (org.eclipse.emf.ecore.EObject)26 JvmType (org.eclipse.xtext.common.types.JvmType)24 JvmMember (org.eclipse.xtext.common.types.JvmMember)21 JvmConstructor (org.eclipse.xtext.common.types.JvmConstructor)18 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)18 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)16 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)16 JvmAnnotationReference (org.eclipse.xtext.common.types.JvmAnnotationReference)15 JvmAnnotationType (org.eclipse.xtext.common.types.JvmAnnotationType)13 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)11 JvmAnnotationTarget (org.eclipse.xtext.common.types.JvmAnnotationTarget)10 TestAnnotation (org.eclipse.xtext.common.types.testSetups.TestAnnotation)10 Resource (org.eclipse.emf.ecore.resource.Resource)8 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)8 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)7 JvmArrayType (org.eclipse.xtext.common.types.JvmArrayType)7 JvmParameterizedTypeReference (org.eclipse.xtext.common.types.JvmParameterizedTypeReference)7 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)6