use of org.eclipse.xtend.lib.macro.declaration.MutableEnumerationValueDeclaration 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);
}
use of org.eclipse.xtend.lib.macro.declaration.MutableEnumerationValueDeclaration in project xtext-xtend by eclipse.
the class CheckMutableEnumerationTypeDeclarationProcessor method doTransform.
@Override
public void doTransform(final List<? extends MutableEnumerationTypeDeclaration> annotatedTargetElements, @Extension final TransformationContext context) {
for (final MutableEnumerationTypeDeclaration enumeration : annotatedTargetElements) {
{
final Procedure1<String> _function = (String identifier) -> {
final Procedure1<MutableEnumerationValueDeclaration> _function_1 = (MutableEnumerationValueDeclaration it) -> {
};
enumeration.addValue(identifier, _function_1).remove();
};
MutableAssert.assertValidJavaIdentifier("name", _function);
final Procedure0 _function_1 = () -> {
enumeration.addValue("foo", null);
};
MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "initializer cannot be null", _function_1);
final Procedure0 _function_2 = () -> {
final Procedure1<MutableEnumerationValueDeclaration> _function_3 = (MutableEnumerationValueDeclaration it) -> {
};
enumeration.addValue("A", _function_3).setVisibility(Visibility.PRIVATE);
};
MutableAssert.<UnsupportedOperationException>assertThrowable(UnsupportedOperationException.class, "It is not possible to change visibility of enumeration value.", _function_2);
}
}
}
use of org.eclipse.xtend.lib.macro.declaration.MutableEnumerationValueDeclaration in project xtext-xtend by eclipse.
the class MutableJvmEnumerationTypeDeclarationImpl method addValue.
@Override
public MutableEnumerationValueDeclaration addValue(final String name, final Procedure1<MutableEnumerationValueDeclaration> initializer) {
this.checkMutable();
ConditionUtils.checkJavaIdentifier(name, "name");
Preconditions.checkArgument((initializer != null), "initializer cannot be null");
final JvmEnumerationLiteral jvmLiteral = TypesFactory.eINSTANCE.createJvmEnumerationLiteral();
jvmLiteral.setSimpleName(name);
jvmLiteral.setVisibility(JvmVisibility.PUBLIC);
this.getDelegate().getMembers().add(jvmLiteral);
MemberDeclaration _memberDeclaration = this.getCompilationUnit().toMemberDeclaration(jvmLiteral);
final MutableEnumerationValueDeclaration mutableEnumerationValueDeclaration = ((MutableEnumerationValueDeclaration) _memberDeclaration);
initializer.apply(mutableEnumerationValueDeclaration);
return mutableEnumerationValueDeclaration;
}
use of org.eclipse.xtend.lib.macro.declaration.MutableEnumerationValueDeclaration 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);
}
Aggregations