Search in sources :

Example 1 with AnnotationReference

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

the class ProjectTemplateProcessor method generatePropertiesFile.

private String generatePropertiesFile(final ClassDeclaration annotatedClass, @Extension final CodeGenerationContext context) {
    final AnnotationReference annotation = annotatedClass.findAnnotation(context.findTypeGlobally(ProjectTemplate.class));
    final String label = this.replaceNewlines(annotation.getStringValue("label"));
    final String description = this.replaceNewlines(annotation.getStringValue("description"));
    String _simpleName = annotatedClass.getSimpleName();
    final String labelLineStart = (_simpleName + "_Label=");
    final String labelLine = (labelLineStart + label);
    boolean _contains = this.actualPropertyContents.contains(labelLineStart);
    if (_contains) {
        this.actualPropertyContents = this.actualPropertyContents.replaceFirst((("(?m)^" + labelLineStart) + ".*$"), labelLine);
    } else {
        String _actualPropertyContents = this.actualPropertyContents;
        String _lineSeparator = System.lineSeparator();
        String _plus = (labelLine + _lineSeparator);
        this.actualPropertyContents = (_actualPropertyContents + _plus);
    }
    String _simpleName_1 = annotatedClass.getSimpleName();
    final String descriptionLineStart = (_simpleName_1 + "_Description=");
    final String descriptionLine = (descriptionLineStart + description);
    boolean _contains_1 = this.actualPropertyContents.contains(descriptionLineStart);
    if (_contains_1) {
        this.actualPropertyContents = this.actualPropertyContents.replaceFirst((("(?m)^" + descriptionLineStart) + ".*$"), descriptionLine);
    } else {
        String _actualPropertyContents_1 = this.actualPropertyContents;
        String _lineSeparator_1 = System.lineSeparator();
        String _plus_1 = (descriptionLine + _lineSeparator_1);
        this.actualPropertyContents = (_actualPropertyContents_1 + _plus_1);
    }
    this.propertyContentMap.put(this.getMessagesProperties(annotatedClass), this.actualPropertyContents);
    return this.actualPropertyContents;
}
Also used : AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) ProjectTemplate(org.eclipse.xtext.ui.wizard.template.ProjectTemplate) AbstractProjectTemplate(org.eclipse.xtext.ui.wizard.template.AbstractProjectTemplate)

Example 2 with AnnotationReference

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

the class EqualsHashCodeProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration it, @Extension final TransformationContext context) {
    AnnotationReference _findAnnotation = it.findAnnotation(context.findTypeGlobally(Data.class));
    boolean _tripleNotEquals = (_findAnnotation != null);
    if (_tripleNotEquals) {
        return;
    }
    @Extension final EqualsHashCodeProcessor.Util util = new EqualsHashCodeProcessor.Util(context);
    boolean _hasEquals = util.hasEquals(it);
    if (_hasEquals) {
        final AnnotationReference annotation = it.findAnnotation(context.findTypeGlobally(EqualsHashCode.class));
        context.addWarning(annotation, "equals is already defined, this annotation has no effect");
    } else {
        boolean _hasHashCode = util.hasHashCode(it);
        if (_hasHashCode) {
            context.addWarning(it, "hashCode is already defined, this annotation has no effect");
        } else {
            final Function1<MutableFieldDeclaration, Boolean> _function = (MutableFieldDeclaration it_1) -> {
                return Boolean.valueOf((((!it_1.isStatic()) && (!it_1.isTransient())) && context.isThePrimaryGeneratedJavaElement(it_1)));
            };
            final Iterable<? extends MutableFieldDeclaration> fields = IterableExtensions.filter(it.getDeclaredFields(), _function);
            util.addEquals(it, fields, util.hasSuperEquals(it));
            util.addHashCode(it, fields, util.hasSuperHashCode(it));
        }
    }
}
Also used : Extension(org.eclipse.xtext.xbase.lib.Extension) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) Data(org.eclipse.xtend.lib.annotations.Data) EqualsHashCode(org.eclipse.xtend.lib.annotations.EqualsHashCode)

Example 3 with AnnotationReference

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

the class AbstractReusableActiveAnnotationTests method testSetEmptyListAsAnnotationValue.

@Test
public void testSetEmptyListAsAnnotationValue() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _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.ClassDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(MyAnnotationProcessor)");
    _builder.newLine();
    _builder.append("annotation MyAnnotation {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("annotation Values {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("int[] intValue");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("long[] longValue");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("short[] shortValue");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("double[] doubleValue");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("float[] floatValue");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("byte[] byteValue");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("boolean[] booleanValue");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("char[] charValue");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String[] stringValue");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("Class<?>[] classValue");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("Enum1[] enumValue");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("Annotation2[] annotationValue");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("enum Enum1 {}");
    _builder.newLine();
    _builder.append("annotation Annotation2 {}");
    _builder.newLine();
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyAnnotationProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration it, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("removeAnnotation(annotations.head)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("addAnnotation(Values.newAnnotationReference [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setIntValue(\'intValue\', emptyList)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setLongValue(\'longValue\', emptyList)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setShortValue(\'shortValue\', emptyList)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setDoubleValue(\'doubleValue\', emptyList)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setFloatValue(\'floatValue\', emptyList)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setByteValue(\'byteValue\', emptyList)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setBooleanValue(\'booleanValue\', emptyList)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setCharValue(\'charValue\', emptyList)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setStringValue(\'stringValue\', emptyList)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setClassValue(\'classValue\', emptyList)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setEnumValue(\'enumValue\', emptyList)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setAnnotationValue(\'annotationValue\', emptyList)");
    _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.*");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("class Foo {");
    _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 foo = it.getTypeLookup().findClass("myusercode.Foo");
        final Type annotationType = it.getTypeLookup().findTypeGlobally("myannotation.Values");
        final AnnotationReference values = foo.findAnnotation(annotationType);
        Assert.assertEquals(0, ((List<Integer>) Conversions.doWrapArray(values.getIntArrayValue("intValue"))).size());
        Assert.assertEquals(0, ((List<Long>) Conversions.doWrapArray(values.getLongArrayValue("longValue"))).size());
        Assert.assertEquals(0, ((List<Short>) Conversions.doWrapArray(values.getShortArrayValue("shortValue"))).size());
        Assert.assertEquals(0, ((List<Double>) Conversions.doWrapArray(values.getDoubleArrayValue("doubleValue"))).size());
        Assert.assertEquals(0, ((List<Float>) Conversions.doWrapArray(values.getFloatArrayValue("floatValue"))).size());
        Assert.assertEquals(0, ((List<Byte>) Conversions.doWrapArray(values.getByteArrayValue("byteValue"))).size());
        Assert.assertEquals(0, ((List<Boolean>) Conversions.doWrapArray(values.getBooleanArrayValue("booleanValue"))).size());
        Assert.assertEquals(0, ((List<Character>) Conversions.doWrapArray(values.getCharArrayValue("charValue"))).size());
        Assert.assertEquals(0, ((List<String>) Conversions.doWrapArray(values.getStringArrayValue("stringValue"))).size());
        Assert.assertEquals(0, ((List<TypeReference>) Conversions.doWrapArray(values.getClassArrayValue("classValue"))).size());
        Assert.assertEquals(0, ((List<EnumerationValueDeclaration>) Conversions.doWrapArray(values.getEnumArrayValue("enumValue"))).size());
        Assert.assertEquals(0, ((List<AnnotationReference>) Conversions.doWrapArray(values.getAnnotationArrayValue("annotationValue"))).size());
    };
    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) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) List(java.util.List) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 4 with AnnotationReference

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

the class AbstractReusableActiveAnnotationTests method testAddAnnotationValue.

@Test
public void testAddAnnotationValue() {
    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.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationParticipant");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableAnnotationTarget");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(AddAnnotationValueProcessor)");
    _builder.newLine();
    _builder.append("annotation AddAnnotationValue { }");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class AddAnnotationValueProcessor implements TransformationParticipant<MutableAnnotationTarget> {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(List<? extends MutableAnnotationTarget> annotationTargets, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotationTargets.forEach [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("addAnnotation(");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("MyAnnotation.findTypeGlobally.newAnnotationReference [");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'value\', #[\'foo\', \'bar\', \'baz\'] as String[])");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'singleValue\', \'foo\')");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'booleans\', #[true, false, true] as boolean[])");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'singleBoolean\', true)");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'numbers\', #[1, 2, 3] as int[])");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'singleNumber\', 1)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.append("annotation MyAnnotation {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String[] value");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String singleValue");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("boolean[] booleans");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("boolean singleBoolean");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("int[] numbers");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("int singleNumber");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/AddAnnotationValue.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@myannotation.AddAnnotationValue");
    _builder_1.newLine();
    _builder_1.append("class MyClass {}");
    _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");
        final AnnotationReference annotation = clazz.findAnnotation(it.getTypeReferenceProvider().newTypeReference("myannotation.MyAnnotation").getType());
        Object _value = annotation.getValue("value");
        Assert.assertArrayEquals(((String[]) ((String[]) Conversions.unwrapArray(Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList("foo", "bar", "baz")), String.class))), ((String[]) _value));
        Assert.assertEquals("foo", annotation.getValue("singleValue"));
        Object _value_1 = annotation.getValue("booleans");
        final boolean[] booleans = ((boolean[]) _value_1);
        Assert.assertTrue(booleans[2]);
        Assert.assertEquals(Boolean.valueOf(true), annotation.getValue("singleBoolean"));
        Object _value_2 = annotation.getValue("numbers");
        Assert.assertArrayEquals(new int[] { 1, 2, 3 }, ((int[]) _value_2));
        Assert.assertEquals(Integer.valueOf(1), annotation.getValue("singleNumber"));
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : 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) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 5 with AnnotationReference

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

the class AbstractReusableActiveAnnotationTests method testTracing4.

@Test
public void testTracing4() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.AbstractClassProcessor");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(MyAnnoProcessor)");
    _builder.newLine();
    _builder.append("annotation MyAnno {}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyAnnoProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.addMethod(\"foo\") [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("primarySourceElement = cls.annotations.head");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("body = [\"return;\"]");
    _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("myannotation/MyAnno.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.MyAnno");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnno");
    _builder_1.newLine();
    _builder_1.append("class Client extends Object{}");
    _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 cls = it.getTypeLookup().findClass("myusercode.Client");
        final MutableMethodDeclaration fooMethod = cls.findDeclaredMethod("foo");
        final Element anno = it.getTracability().getPrimarySourceElement(IterableExtensions.head(cls.getAnnotations()));
        Assert.assertTrue((anno instanceof AnnotationReference));
        Assert.assertEquals(anno, it.getTracability().getPrimarySourceElement(((Element) fooMethod)));
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) Element(org.eclipse.xtend.lib.macro.declaration.Element) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Aggregations

AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)33 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)19 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)17 Test (org.junit.Test)17 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)16 EObject (org.eclipse.emf.ecore.EObject)11 EnumerationValueDeclaration (org.eclipse.xtend.lib.macro.declaration.EnumerationValueDeclaration)7 JvmAnnotationReference (org.eclipse.xtext.common.types.JvmAnnotationReference)7 List (java.util.List)5 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)5 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)5 Data (org.eclipse.xtend.lib.annotations.Data)4 MutableEnumerationValueDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableEnumerationValueDeclaration)4 Extension (org.eclipse.xtext.xbase.lib.Extension)4 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)4 AccessibleObject (java.lang.reflect.AccessibleObject)3 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)3 Type (org.eclipse.xtend.lib.macro.declaration.Type)3 AnnotationReferenceBuildContext (org.eclipse.xtend.lib.macro.services.AnnotationReferenceBuildContext)3 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)3