use of org.eclipse.xtend.lib.macro.declaration.ClassDeclaration in project xtext-xtend by eclipse.
the class DeclarationsTest method testAnnotation2.
@Test
public void testAnnotation2() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("class MyClass {");
_builder.newLine();
_builder.append("\t");
_builder.append("@com.google.inject.Inject() MyClass foo");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
final ClassDeclaration sourceClazz = ((ClassDeclaration) _head);
final MutableClassDeclaration javaClass = it.getTypeLookup().findClass("MyClass");
Assert.assertEquals(javaClass.getQualifiedName(), sourceClazz.getQualifiedName());
final FieldDeclaration field = IterableExtensions.head(sourceClazz.getDeclaredFields());
Assert.assertEquals(Boolean.FALSE, IterableExtensions.head(field.getAnnotations()).getValue("optional"));
final MutableFieldDeclaration javaField = IterableExtensions.head(javaClass.getDeclaredFields());
Object _value = IterableExtensions.head(javaField.getAnnotations()).getValue("optional");
Assert.assertFalse((((Boolean) _value)).booleanValue());
};
this.asCompilationUnit(this.validFile(_builder), _function);
}
use of org.eclipse.xtend.lib.macro.declaration.ClassDeclaration in project xtext-xtend by eclipse.
the class DeclarationsTest method testRemove.
@Test
public void testRemove() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("class C {");
_builder.newLine();
_builder.append("\t");
_builder.append("def void m() {}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
final ClassDeclaration c = ((ClassDeclaration) _head);
final MutableClassDeclaration mutable = it.getTypeLookup().findClass(c.getQualifiedName());
final Consumer<MutableMemberDeclaration> _function_1 = (MutableMemberDeclaration it_1) -> {
it_1.remove();
};
mutable.getDeclaredMembers().forEach(_function_1);
Assert.assertTrue(IterableExtensions.isEmpty(mutable.getDeclaredMembers()));
};
this.asCompilationUnit(this.validFile(_builder), _function);
}
use of org.eclipse.xtend.lib.macro.declaration.ClassDeclaration in project xtext-xtend by eclipse.
the class DeclarationsTest method testAnnotation.
@Test
public void testAnnotation() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("@SuppressWarnings(\"unused\")");
_builder.newLine();
_builder.append("class MyClass {");
_builder.newLine();
_builder.append("\t");
_builder.append("@com.google.inject.Inject(optional=true) MyClass foo");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
Assert.assertNull(it.getPackageName());
TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
final ClassDeclaration clazz = ((ClassDeclaration) _head);
Assert.assertEquals("MyClass", clazz.getQualifiedName());
final AnnotationReference suppressWarning = IterableExtensions.head(clazz.getAnnotations());
final AnnotationTypeDeclaration supressWarningsDeclaration = suppressWarning.getAnnotationTypeDeclaration();
Assert.assertEquals("java.lang.SuppressWarnings", supressWarningsDeclaration.getQualifiedName());
Assert.assertEquals("unused", suppressWarning.getStringArrayValue("value")[0]);
Assert.assertEquals(2, IterableExtensions.size(supressWarningsDeclaration.getAnnotations()));
final AnnotationTypeElementDeclaration valueProperty = IterableExtensions.<AnnotationTypeElementDeclaration>head(Iterables.<AnnotationTypeElementDeclaration>filter(supressWarningsDeclaration.getDeclaredMembers(), AnnotationTypeElementDeclaration.class));
Assert.assertEquals("String[]", valueProperty.getType().toString());
Assert.assertEquals("value", valueProperty.getSimpleName());
MemberDeclaration _head_1 = IterableExtensions.head(clazz.getDeclaredMembers());
final FieldDeclaration field = ((FieldDeclaration) _head_1);
final AnnotationReference inject = IterableExtensions.head(field.getAnnotations());
Object _value = inject.getValue("optional");
Assert.assertTrue((((Boolean) _value)).booleanValue());
};
this.asCompilationUnit(this.validFile(_builder), _function);
}
use of org.eclipse.xtend.lib.macro.declaration.ClassDeclaration in project xtext-xtend by eclipse.
the class DeclarationsTest method testSimpleClassWithField.
@Test
public void testSimpleClassWithField() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package foo");
_builder.newLine();
_builder.newLine();
_builder.append("class MyClass extends Object implements java.io.Serializable {");
_builder.newLine();
_builder.append("\t");
_builder.append("MyClass foo");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
Assert.assertEquals("foo", it.getPackageName());
TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
final ClassDeclaration clazz = ((ClassDeclaration) _head);
Assert.assertEquals("foo.MyClass", clazz.getQualifiedName());
Assert.assertEquals("Object", clazz.getExtendedClass().toString());
Assert.assertEquals("Serializable", IterableExtensions.head(clazz.getImplementedInterfaces()).toString());
MemberDeclaration _head_1 = IterableExtensions.head(clazz.getDeclaredMembers());
final FieldDeclaration field = ((FieldDeclaration) _head_1);
Assert.assertEquals("foo", field.getSimpleName());
Assert.assertSame(it.getTypeLookup().findClass("foo.MyClass"), field.getType().getType());
};
this.asCompilationUnit(this.validFile(_builder), _function);
}
use of org.eclipse.xtend.lib.macro.declaration.ClassDeclaration in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testRemove.
@Test
public void testRemove() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package myannotation");
_builder.newLine();
_builder.newLine();
_builder.append("import com.google.common.base.Preconditions");
_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.MutableNamedElement");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableTypeDeclaration");
_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 TransformationParticipant<MutableNamedElement> {");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(List<? extends MutableNamedElement> annotatedTargetElements,");
_builder.newLine();
_builder.append("\t\t");
_builder.append("extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (annotatedTargetElement : annotatedTargetElements) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("remove(annotatedTargetElement, context)");
_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 dispatch void remove(MutableNamedElement annotatedTargetElement, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val sourceElement = annotatedTargetElement.primarySourceElement");
_builder.newLine();
_builder.append("\t\t");
_builder.append("Preconditions.checkState(sourceElement != null,");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("\'");
_builder.append("\'\'a source element should not be null before removing, but: ");
_builder.append("�", "\t\t\t");
_builder.append("sourceElement");
_builder.append("�", "\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("Preconditions.checkState(sourceElement.primaryGeneratedJavaElement == annotatedTargetElement,");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("\'");
_builder.append("\'\'expected: ");
_builder.append("�", "\t\t\t");
_builder.append("annotatedTargetElement");
_builder.append("�", "\t\t\t");
_builder.append(", but: ");
_builder.append("�", "\t\t\t");
_builder.append("sourceElement.primaryGeneratedJavaElement");
_builder.append("�", "\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedTargetElement.remove");
_builder.newLine();
_builder.newLine();
_builder.append("\t\t");
_builder.append("Preconditions.checkState(annotatedTargetElement.primarySourceElement == null,");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("\'");
_builder.append("\'\'a source element should be null after removing, but: ");
_builder.append("�", "\t\t\t");
_builder.append("annotatedTargetElement.primarySourceElement");
_builder.append("�", "\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("Preconditions.checkState(sourceElement.primaryGeneratedJavaElement == null,");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("\'");
_builder.append("\'\'a target element should be null after removing, but: ");
_builder.append("�", "\t\t\t");
_builder.append("sourceElement.primaryGeneratedJavaElement");
_builder.append("�", "\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("\t\t");
_builder.append("try {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("annotatedTargetElement.remove");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("Preconditions.checkState(false, \'");
_builder.append("\'\'");
_builder.append("�", "\t\t\t");
_builder.append("IllegalArgumentException");
_builder.append("�", "\t\t\t");
_builder.append(" is expected\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("} catch (IllegalStateException e) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("Preconditions.checkState(e.message.startsWith(\"This element has already been removed: \"),");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("\'");
_builder.append("\'\'Wrong error message: ");
_builder.append("�", "\t\t\t\t");
_builder.append("e.message");
_builder.append("�", "\t\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("def dispatch void remove(MutableTypeDeclaration annotatedTargetElement, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("try {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("annotatedTargetElement.remove");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("Preconditions.checkState(false, \'");
_builder.append("\'\'");
_builder.append("�", "\t\t\t");
_builder.append("UnsupportedOperationException");
_builder.append("�", "\t\t\t");
_builder.append(" is expected\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("} catch (UnsupportedOperationException e) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("Preconditions.checkState(e.message == \"The type cannot be removed.\", \'\'");
_builder.append("\'Wrong error message: ");
_builder.append("�", "\t\t\t");
_builder.append("e.message");
_builder.append("�", "\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("}");
_builder.newLine();
_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("class UserClass {");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("String fieldToRemove");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("new(String arg) {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("new(@MyAnnotation Integer argToRemove) {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def methodToRemove() {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("static class NestedClass {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("static interface NestedInteface {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("static annotation NestedAnnotation {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("static enum NestedEnum {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("enum UserEnum {");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("interface UserInterface {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def String methodToRemove();");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("annotation UserAnnotation {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("String fieldToRemove");
_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.UserClass");
Assert.assertNotNull(clazz);
Assert.assertEquals(5, IterableExtensions.size(clazz.getDeclaredMembers()));
final MutableConstructorDeclaration constructor = IterableExtensions.head(clazz.getDeclaredConstructors());
Assert.assertNotNull(clazz);
Assert.assertEquals(0, IterableExtensions.size(constructor.getParameters()));
Assert.assertEquals(1, IterableExtensions.size(Iterables.<ClassDeclaration>filter(clazz.getDeclaredMembers(), ClassDeclaration.class)));
Assert.assertEquals(1, IterableExtensions.size(Iterables.<InterfaceDeclaration>filter(clazz.getDeclaredMembers(), InterfaceDeclaration.class)));
Assert.assertEquals(1, IterableExtensions.size(Iterables.<EnumerationTypeDeclaration>filter(clazz.getDeclaredMembers(), EnumerationTypeDeclaration.class)));
Assert.assertEquals(1, IterableExtensions.size(Iterables.<AnnotationTypeDeclaration>filter(clazz.getDeclaredMembers(), AnnotationTypeDeclaration.class)));
final MutableEnumerationTypeDeclaration enum_ = it.getTypeLookup().findEnumerationType("myusercode.UserEnum");
Assert.assertNotNull(enum_);
final MutableInterfaceDeclaration interface_ = it.getTypeLookup().findInterface("myusercode.UserInterface");
Assert.assertNotNull(interface_);
Assert.assertEquals(0, IterableExtensions.size(interface_.getDeclaredMembers()));
final MutableAnnotationTypeDeclaration annotation = it.getTypeLookup().findAnnotationType("myusercode.UserAnnotation");
Assert.assertNotNull(annotation);
Assert.assertEquals(0, IterableExtensions.size(annotation.getDeclaredMembers()));
Element _primarySourceElement = it.getTracability().getPrimarySourceElement(clazz);
final MethodDeclaration removedMethod = ((ClassDeclaration) _primarySourceElement).findDeclaredMethod("methodToRemove");
Expression _body = removedMethod.getBody();
final XExpression expression = ((ExpressionImpl) _body).getDelegate();
Assert.assertNull(this.logicalContainerProvider.getLogicalContainer(expression));
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Aggregations