Search in sources :

Example 11 with Type

use of org.eclipse.xtend.lib.macro.declaration.Type in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testBug441081.

@Test
public void testBug441081() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package bug441081");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.AbstractClassProcessor");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("interface GenericInterface {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def <T> T m()");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(Bug441081Processor)");
    _builder.newLine();
    _builder.append("annotation Bug441081 {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class Bug441081Processor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.implementedInterfaces = annotatedClass.implementedInterfaces + #[findTypeGlobally(GenericInterface).newTypeReference]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.addMethod(\"m\") [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("returnType = addTypeParameter(\"T\", object).newTypeReference");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("body = \'");
    _builder.append("\'\'return null;\'");
    _builder.append("\'\'");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("primarySourceElement = annotatedClass");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("bug441081/Bug441081.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("import bug441081.Bug441081");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@Bug441081");
    _builder_1.newLine();
    _builder_1.append("class Bug441081Client {");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    Pair<String, String> _mappedTo_1 = Pair.<String, String>of("Bug441081Client.xtend", _builder_1.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration c = it.getTypeLookup().findClass("Bug441081Client");
        Type _type = c.findDeclaredMethod("m").getReturnType().getType();
        final TypeParameterDeclaration typeParam = ((TypeParameterDeclaration) _type);
        Assert.assertFalse(IterableExtensions.isEmpty(typeParam.getUpperBounds()));
        this.validator.assertNoIssues(it.getXtendFile());
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) Type(org.eclipse.xtend.lib.macro.declaration.Type) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) TypeParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.TypeParameterDeclaration) MutableTypeParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 12 with Type

use of org.eclipse.xtend.lib.macro.declaration.Type in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testAccessAndModifyEnumerationValueDeclaration.

@Test
public void testAccessAndModifyEnumerationValueDeclaration() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import java.util.List");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.CodeGenerationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.CodeGenerationParticipant");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.RegisterGlobalsContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.RegisterGlobalsParticipant");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationParticipant");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.EnumerationTypeDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.EnumerationValueDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.TypeDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.Visibility");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import static com.google.common.base.Preconditions.*");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(MyAnnotationProcessor)");
    _builder.newLine();
    _builder.append("annotation MyAnnotation {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyAnnotationProcessor implements RegisterGlobalsParticipant<EnumerationTypeDeclaration>, TransformationParticipant<MutableEnumerationTypeDeclaration>, CodeGenerationParticipant<EnumerationTypeDeclaration> {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doGenerateCode(List<? extends EnumerationTypeDeclaration> annotatedSourceElements,");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("extension CodeGenerationContext context) {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doRegisterGlobals(List<? extends EnumerationTypeDeclaration> annotatedSourceElements,");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("RegisterGlobalsContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("for (enumeration : annotatedSourceElements) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("enumeration.checkState");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def checkState(EnumerationTypeDeclaration enumeration) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val values = enumeration.declaredValues");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("checkState(values.size == 3, \"enumeration.declaredValues.size != 3\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("enumeration.findDeclaredValue(values.get(0).simpleName).checkState(\"A\", enumeration)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("enumeration.findDeclaredValue(values.get(1).simpleName).checkState(\"B\", enumeration)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("enumeration.findDeclaredValue(values.get(2).simpleName).checkState(\"C\", enumeration)");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def checkState(EnumerationValueDeclaration value, String expectedSimpleName, TypeDeclaration expectedType) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("checkState(value.simpleName == expectedSimpleName, \"value.simpleName != expectedSimpleName\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("checkState(value.declaringType == expectedType, \"value.declaringType != expectedType\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("checkState(value.visibility == Visibility.PUBLIC, \"value.visibility != Visibility.PUBLIC\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("checkState(value.annotations.size == 0, \"value.annotations.size != 0\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("checkState(value.findAnnotation(null) == null, \"value.findAnnotation(null) != null\")");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(List<? extends MutableEnumerationTypeDeclaration> annotatedTargetElements,");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("for (enumeration : annotatedTargetElements) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("enumeration.checkState");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("for (value : enumeration.declaredValues) {");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("checkState(value.annotations.size == 0, value.annotations.size != 0)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("value.addAnnotation(Deprecated.newAnnotationReference)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("checkState(value.annotations.size == 1, value.annotations.size != 1)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("enumeration.addValue(\"D\") [");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("addAnnotation(Deprecated.newAnnotationReference)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/MyAnnotation.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.MyAnnotation");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("enum UserCode {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("A, ");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("B,");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("C");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableEnumerationTypeDeclaration enumerationType = it.getTypeLookup().findEnumerationType("myusercode.UserCode");
        Assert.assertEquals(4, IterableExtensions.size(enumerationType.getDeclaredValues()));
        final Type deprecatedAnnotationType = it.getTypeReferenceProvider().newTypeReference(Deprecated.class).getType();
        Iterable<? extends MutableEnumerationValueDeclaration> _declaredValues = enumerationType.getDeclaredValues();
        for (final MutableEnumerationValueDeclaration value : _declaredValues) {
            {
                Assert.assertEquals(1, IterableExtensions.size(value.getAnnotations()));
                Assert.assertNotNull(value.findAnnotation(deprecatedAnnotationType));
            }
        }
        Assert.assertEquals("D", IterableExtensions.last(enumerationType.getDeclaredValues()).getSimpleName());
        Assert.assertNotNull(enumerationType.findDeclaredValue("D"));
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : MutableEnumerationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) Type(org.eclipse.xtend.lib.macro.declaration.Type) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableEnumerationValueDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableEnumerationValueDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Test(org.junit.Test)

Example 13 with Type

use of org.eclipse.xtend.lib.macro.declaration.Type in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testAnnotationValueSetting_1.

@Test
public void testAnnotationValueSetting_1() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import java.util.List");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.*");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.*");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(ConfigurableAnnotationProcessor)");
    _builder.newLine();
    _builder.append("annotation ConfigurableAnnotation {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("BlackOrWhite color");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("BlackOrWhite[] colors");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("Class<?> type");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("Class<?>[] types");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("SomeAnnotation annotation");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("SomeAnnotation[] annotations");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("annotation SomeAnnotation {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("boolean value");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("enum BlackOrWhite {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("BLACK, WHITE");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class ConfigurableAnnotationProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val anno = annotatedClass.annotations.head");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val someAnnotationType = findTypeGlobally(SomeAnnotation)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val enumType = findTypeGlobally(\'myannotation.BlackOrWhite\') as EnumerationTypeDeclaration");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val white = enumType.findDeclaredValue(\'WHITE\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val black = enumType.findDeclaredValue(\'BLACK\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val existingValue = anno.getValue(\'color\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (existingValue != white)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"color\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annoWithColor = annotatedClass.addAnnotation(newAnnotationReference(anno) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setEnumValue(\'color\', black)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(anno)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val existingColorsValue = annoWithColor.getValue(\'colors\') as Object[]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (existingColorsValue.get(0) != white && existingColorsValue.get(1) != black && existingColorsValue.length != 2)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"colors\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annoWithColors = annotatedClass.addAnnotation(newAnnotationReference(annoWithColor) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setEnumValue(\'colors\', black, white)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(annoWithColor)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val existingType = annoWithColors.getValue(\'type\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (existingType != string)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"type\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annoWithType = annotatedClass.addAnnotation(newAnnotationReference(annoWithColors) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setClassValue(\'type\', annotatedClass.newTypeReference)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(annoWithColors)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val existingTypes = annoWithType.getValue(\'types\') as Object[]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (existingTypes.get(0) != primitiveInt && existingTypes.get(1) != annotatedClass.newTypeReference && existingTypes.length != 2)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"types\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annoWithTypes = annotatedClass.addAnnotation(newAnnotationReference(annoWithType) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setClassValue(\'types\', primitiveBoolean)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(annoWithType)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annotationReference = annoWithTypes.getAnnotationValue(\'annotation\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (someAnnotationType != annotationReference.annotationTypeDeclaration)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"someAnnotationType != annotationReference.annotationTypeDeclaration\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annoWithAnnotation = annotatedClass.addAnnotation(newAnnotationReference(annoWithTypes) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setAnnotationValue(\'annotation\', ");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("newAnnotationReference(someAnnotationType) [");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("setBooleanValue(\'value\', false)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append(")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(annoWithTypes)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annotationReferences = annoWithAnnotation.getAnnotationArrayValue(\'annotations\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (annotationReferences.size != 2)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"annotationReferences.size != 2\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotationReferences.forEach [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("if (someAnnotationType != annotationTypeDeclaration)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("throw new AssertionError(\"someAnnotationType != annotationTypeDeclaration\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.addAnnotation(newAnnotationReference(annoWithAnnotation) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setAnnotationValue(\'annotations\', ");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("newAnnotationReference(someAnnotationType) [");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("setBooleanValue(\'value\', false)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("],");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("newAnnotationReference(someAnnotationType) [");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("setBooleanValue(\'value\', false)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append(")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(annoWithAnnotation)");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/ConfigurableAnnotation.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.*");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@ConfigurableAnnotation(");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("color=BlackOrWhite.WHITE, ");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("colors=#[BlackOrWhite.WHITE, BlackOrWhite.BLACK], ");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("type = String, ");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("types=#[Integer, MyClass],");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("annotation=@SomeAnnotation(true),");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("annotations=#[@SomeAnnotation(true), @SomeAnnotation(true)]");
    _builder_1.newLine();
    _builder_1.append(")");
    _builder_1.newLine();
    _builder_1.append("class MyClass {");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration clazz = it.getTypeLookup().findClass("myusercode.MyClass");
        Type _findTypeGlobally = it.getTypeLookup().findTypeGlobally("myannotation.BlackOrWhite");
        final EnumerationTypeDeclaration colorEnum = ((EnumerationTypeDeclaration) _findTypeGlobally);
        final Type annotationType = it.getTypeLookup().findTypeGlobally("myannotation.ConfigurableAnnotation");
        final AnnotationReference annotation = clazz.findAnnotation(annotationType);
        Assert.assertEquals(colorEnum.findDeclaredValue("BLACK"), annotation.getValue("color"));
        Object _value = annotation.getValue("colors");
        final Object[] colors = ((Object[]) _value);
        Assert.assertEquals(2, colors.length);
        Assert.assertEquals(colorEnum.findDeclaredValue("BLACK"), colors[0]);
        Assert.assertEquals(colorEnum.findDeclaredValue("WHITE"), colors[1]);
        Assert.assertEquals(it.getTypeReferenceProvider().newTypeReference(clazz), annotation.getValue("type"));
        final TypeReference[] types = annotation.getClassArrayValue("types");
        Assert.assertEquals(1, types.length);
        Assert.assertEquals(it.getTypeReferenceProvider().getPrimitiveBoolean(), types[0]);
        final Type someAnnotationType = it.getTypeLookup().findTypeGlobally("myannotation.SomeAnnotation");
        final AnnotationReference annotationValue = annotation.getAnnotationValue("annotation");
        Assert.assertNotNull(annotationValue);
        Assert.assertEquals(someAnnotationType, annotationValue.getAnnotationTypeDeclaration());
        Assert.assertFalse(annotationValue.getBooleanValue("value"));
        final AnnotationReference[] annotationsValue = annotation.getAnnotationArrayValue("annotations");
        Assert.assertNotNull(annotationsValue);
        Assert.assertEquals(2, ((List<AnnotationReference>) Conversions.doWrapArray(annotationsValue)).size());
        final Consumer<AnnotationReference> _function_1 = (AnnotationReference it_1) -> {
            Assert.assertEquals(someAnnotationType, annotationValue.getAnnotationTypeDeclaration());
            Assert.assertFalse(annotationValue.getBooleanValue("value"));
        };
        ((List<AnnotationReference>) Conversions.doWrapArray(annotationsValue)).forEach(_function_1);
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) Type(org.eclipse.xtend.lib.macro.declaration.Type) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) Consumer(java.util.function.Consumer) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) EObject(org.eclipse.emf.ecore.EObject) List(java.util.List) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) EnumerationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.EnumerationTypeDeclaration) MutableEnumerationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration) Test(org.junit.Test)

Example 14 with Type

use of org.eclipse.xtend.lib.macro.declaration.Type in project xtext-xtend by eclipse.

the class AccessObjectProcessorTest method testWithPackage.

@Test
public void testWithPackage() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package my.pack");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.core.tests.macro.Accessors");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@org.eclipse.xtend.core.tests.macro.AccessObjectAnn");
    _builder.newLine();
    _builder.append("class A {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String field");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final IAcceptor<XtendCompilerTester.CompilationResult> _function = (XtendCompilerTester.CompilationResult it) -> {
        final TransformationContext ctx = it.getTransformationContext();
        final MutableClassDeclaration classA = ctx.findClass("my.pack.A");
        Assert.assertNotNull(classA.findDeclaredMethod("getField"));
        final MutableClassDeclaration classPA = ctx.findClass("my.pack.PA");
        final Function1<TypeReference, Boolean> _function_1 = (TypeReference it_1) -> {
            Type _type = it_1.getType();
            Type _type_1 = ctx.newTypeReference(Serializable.class).getType();
            return Boolean.valueOf(Objects.equal(_type, _type_1));
        };
        Assert.assertNotNull(IterableExtensions.findFirst(classPA.getImplementedInterfaces(), _function_1));
        final MutableClassDeclaration classGA = ctx.findClass("my.pack.GA");
        final Function1<TypeReference, Boolean> _function_2 = (TypeReference it_1) -> {
            Type _type = it_1.getType();
            Type _type_1 = ctx.newTypeReference(Serializable.class).getType();
            return Boolean.valueOf(Objects.equal(_type, _type_1));
        };
        Assert.assertNotNull(IterableExtensions.findFirst(classGA.getImplementedInterfaces(), _function_2));
    };
    this._xtendCompilerTester.compile(_builder, _function);
}
Also used : Type(org.eclipse.xtend.lib.macro.declaration.Type) Serializable(java.io.Serializable) XtendCompilerTester(org.eclipse.xtend.core.compiler.batch.XtendCompilerTester) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) TransformationContext(org.eclipse.xtend.lib.macro.TransformationContext) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) AbstractActiveAnnotationTest(org.eclipse.xtend.core.tests.macro.AbstractActiveAnnotationTest) Test(org.junit.Test)

Example 15 with Type

use of org.eclipse.xtend.lib.macro.declaration.Type in project xtext-xtend by eclipse.

the class ToAnnoProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    super.doTransform(annotatedClass, context);
    Type _findTypeGlobally = context.findTypeGlobally(this.generatedAnnotationName(annotatedClass));
    final MutableAnnotationTypeDeclaration annotationType = ((MutableAnnotationTypeDeclaration) _findTypeGlobally);
    final Procedure1<MutableAnnotationTypeElementDeclaration> _function = (MutableAnnotationTypeElementDeclaration it) -> {
        it.setDocComment("Best building strategy game ever");
        it.setType(context.newTypeReference(Integer.TYPE));
        boolean _booleanValue = annotatedClass.findAnnotation(context.findTypeGlobally(ToAnno.class)).getBooleanValue("defaultValue");
        if (_booleanValue) {
            StringConcatenationClient _client = new StringConcatenationClient() {

                @Override
                protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                    _builder.append("1602");
                }
            };
            it.setDefaultValueExpression(_client);
        }
    };
    annotationType.addAnnotationTypeElement("anno", _function);
}
Also used : Type(org.eclipse.xtend.lib.macro.declaration.Type) MutableAnnotationTypeElementDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableAnnotationTypeElementDeclaration) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) ToAnno(org.eclipse.xtend.core.tests.macro.ToAnno) MutableAnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableAnnotationTypeDeclaration)

Aggregations

Type (org.eclipse.xtend.lib.macro.declaration.Type)23 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)16 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)11 Test (org.junit.Test)11 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)10 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)10 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)9 JvmType (org.eclipse.xtext.common.types.JvmType)7 AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)5 MutableEnumerationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration)5 List (java.util.List)4 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)4 MutableInterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration)3 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)3 Serializable (java.io.Serializable)2 Consumer (java.util.function.Consumer)2 EObject (org.eclipse.emf.ecore.EObject)2 XtendCompilerTester (org.eclipse.xtend.core.compiler.batch.XtendCompilerTester)2 AbstractActiveAnnotationTest (org.eclipse.xtend.core.tests.macro.AbstractActiveAnnotationTest)2 TransformationContext (org.eclipse.xtend.lib.macro.TransformationContext)2