use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration in project xtext-xtend by eclipse.
the class ImmutableProcessor method doTransform.
@Override
public void doTransform(final List<? extends MutableClassDeclaration> annotatedTargetElements, @Extension final TransformationContext context) {
for (final MutableClassDeclaration clazz : annotatedTargetElements) {
{
final Iterable<? extends MutableFieldDeclaration> fields = clazz.getDeclaredFields();
final Procedure1<MutableConstructorDeclaration> _function = (MutableConstructorDeclaration it) -> {
for (final MutableFieldDeclaration f : fields) {
{
f.markAsInitializedBy(it);
it.addParameter(f.getSimpleName(), f.getType());
}
}
final CompilationStrategy _function_1 = (CompilationStrategy.CompilationContext it_1) -> {
StringConcatenation _builder = new StringConcatenation();
{
for (final MutableFieldDeclaration f_1 : fields) {
_builder.append("this.");
String _simpleName = f_1.getSimpleName();
_builder.append(_simpleName);
_builder.append(" = ");
String _simpleName_1 = f_1.getSimpleName();
_builder.append(_simpleName_1);
_builder.append(";");
_builder.newLineIfNotEmpty();
}
}
return _builder;
};
it.setBody(_function_1);
};
clazz.addConstructor(_function);
for (final MutableFieldDeclaration f : fields) {
{
f.setFinal(true);
String _firstUpper = StringExtensions.toFirstUpper(f.getSimpleName());
final String getterName = ("get" + _firstUpper);
final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it) -> {
f.markAsRead();
it.setReturnType(f.getType());
final CompilationStrategy _function_2 = (CompilationStrategy.CompilationContext it_1) -> {
StringConcatenation _builder = new StringConcatenation();
_builder.append("return ");
String _simpleName = f.getSimpleName();
_builder.append(_simpleName);
_builder.append(";");
_builder.newLineIfNotEmpty();
return _builder;
};
it.setBody(_function_2);
};
this.tryAddMethod(clazz, getterName, _function_1);
}
}
final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it) -> {
it.setReturnType(context.getPrimitiveInt());
it.addAnnotation(context.newAnnotationReference(Override.class));
final CompilationStrategy _function_2 = (CompilationStrategy.CompilationContext it_1) -> {
StringConcatenation _builder = new StringConcatenation();
_builder.append("final int prime = 31;");
_builder.newLine();
{
boolean _extendsSomethingWithProperHashCode = this.getExtendsSomethingWithProperHashCode(clazz);
if (_extendsSomethingWithProperHashCode) {
_builder.append("int result = super.hashCode();");
_builder.newLine();
} else {
_builder.append("int result = 1;");
_builder.newLine();
}
}
{
for (final MutableFieldDeclaration f_1 : fields) {
{
TypeReference _type = f_1.getType();
TypeReference _primitiveBoolean = context.getPrimitiveBoolean();
boolean _equals = Objects.equal(_type, _primitiveBoolean);
if (_equals) {
_builder.append("result = prime * result + (");
String _simpleName = f_1.getSimpleName();
_builder.append(_simpleName);
_builder.append(" ? 1231 : 1237);");
_builder.newLineIfNotEmpty();
} else {
TypeReference _primitiveInt = context.getPrimitiveInt();
TypeReference _primitiveChar = context.getPrimitiveChar();
TypeReference _primitiveByte = context.getPrimitiveByte();
TypeReference _primitiveShort = context.getPrimitiveShort();
boolean _contains = Collections.<TypeReference>unmodifiableSet(CollectionLiterals.<TypeReference>newHashSet(_primitiveInt, _primitiveChar, _primitiveByte, _primitiveShort)).contains(f_1.getType());
if (_contains) {
_builder.append("result = prime * result + ");
String _simpleName_1 = f_1.getSimpleName();
_builder.append(_simpleName_1);
_builder.append(";");
_builder.newLineIfNotEmpty();
} else {
TypeReference _primitiveLong = context.getPrimitiveLong();
TypeReference _type_1 = f_1.getType();
boolean _equals_1 = Objects.equal(_primitiveLong, _type_1);
if (_equals_1) {
_builder.append("result = prime * result + (int) (");
String _simpleName_2 = f_1.getSimpleName();
_builder.append(_simpleName_2);
_builder.append(" ^ (");
String _simpleName_3 = f_1.getSimpleName();
_builder.append(_simpleName_3);
_builder.append(" >>> 32));");
_builder.newLineIfNotEmpty();
} else {
TypeReference _primitiveFloat = context.getPrimitiveFloat();
TypeReference _type_2 = f_1.getType();
boolean _equals_2 = Objects.equal(_primitiveFloat, _type_2);
if (_equals_2) {
_builder.append("result = prime * result + Float.floatToIntBits(");
String _simpleName_4 = f_1.getSimpleName();
_builder.append(_simpleName_4);
_builder.append(");");
_builder.newLineIfNotEmpty();
} else {
TypeReference _primitiveDouble = context.getPrimitiveDouble();
TypeReference _type_3 = f_1.getType();
boolean _equals_3 = Objects.equal(_primitiveDouble, _type_3);
if (_equals_3) {
_builder.append("result = prime * result + (int) (Double.doubleToLongBits(");
String _simpleName_5 = f_1.getSimpleName();
_builder.append(_simpleName_5);
_builder.append(") ^ (Double.doubleToLongBits(");
String _simpleName_6 = f_1.getSimpleName();
_builder.append(_simpleName_6);
_builder.append(") >>> 32));");
_builder.newLineIfNotEmpty();
} else {
_builder.append("result = prime * result + ((");
String _simpleName_7 = f_1.getSimpleName();
_builder.append(_simpleName_7);
_builder.append("== null) ? 0 : ");
String _simpleName_8 = f_1.getSimpleName();
_builder.append(_simpleName_8);
_builder.append(".hashCode());");
_builder.newLineIfNotEmpty();
}
}
}
}
}
}
}
}
_builder.append("return result;");
_builder.newLine();
return _builder;
};
it.setBody(_function_2);
};
this.tryAddMethod(clazz, "hashCode", _function_1);
}
}
}
use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration in project xtext-xtend by eclipse.
the class DeclarationsTest method testMutableClassDeclaration.
@Test
public void testMutableClassDeclaration() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package foo");
_builder.newLine();
_builder.newLine();
_builder.append("class MyClass<T extends CharSequence> {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("String myField");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("new(String initial) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("this.myField = initial");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("def <T2 extends CharSequence> MyClass myMethod(T2 a, T b) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("myField = myField + a + b");
_builder.newLine();
_builder.append("\t\t");
_builder.append("return this");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
final MutableClassDeclaration genClazz = it.getTypeLookup().findClass("foo.MyClass");
final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it_1) -> {
CompilationUnit _compilationUnit = genClazz.getCompilationUnit();
it_1.setReturnType(((CompilationUnitImpl) _compilationUnit).getTypeReferenceProvider().getString());
it_1.setVisibility(Visibility.PRIVATE);
final CompilationStrategy _function_2 = (CompilationStrategy.CompilationContext it_2) -> {
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("return \"foo\";");
_builder_1.newLine();
return _builder_1;
};
it_1.setBody(_function_2);
};
genClazz.addMethod("newMethod", _function_1);
final MutableMethodDeclaration mutableMethod = genClazz.findDeclaredMethod("newMethod");
Assert.assertSame(mutableMethod, ((Object[]) Conversions.unwrapArray(genClazz.getDeclaredMethods(), Object.class))[1]);
Assert.assertEquals("String", mutableMethod.getReturnType().toString());
Assert.assertEquals(Visibility.PRIVATE, mutableMethod.getVisibility());
};
this.asCompilationUnit(this.validFile(_builder), _function);
}
use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration in project xtext-xtend by eclipse.
the class DeclarationsTest method testAnnotationReferenceValues.
@Test
public void testAnnotationReferenceValues() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package foo");
_builder.newLine();
_builder.append("@test.Annotation(");
_builder.newLine();
_builder.append("\t");
_builder.append("intValue = 2 / 2 + 2 * 3 - 4 % 1,");
_builder.newLine();
_builder.append("\t");
_builder.append("longValue = 42 + 4 + 6 * 42 - 4 / 45,");
_builder.newLine();
_builder.append("\t");
_builder.append("stringValue = \'foo\' + \'baz\',");
_builder.newLine();
_builder.append("\t");
_builder.append("booleanArrayValue = #[true, false],");
_builder.newLine();
_builder.append("\t");
_builder.append("intArrayValue = #[ -1, 34 + 45, 2 - 6 ],");
_builder.newLine();
_builder.append("\t");
_builder.append("longArrayValue = #[42, 5 * -3],");
_builder.newLine();
_builder.append("\t");
_builder.append("stringArrayValue = #[\'foo\', \'bla\' + \'buzz\'],");
_builder.newLine();
_builder.append("\t");
_builder.append("typeValue = String,");
_builder.newLine();
_builder.append("\t");
_builder.append("typeArrayValue = #[String, Integer],");
_builder.newLine();
_builder.append("\t");
_builder.append("annotation2Value = @test.Annotation2(\'foo\' + \'wuppa\'),");
_builder.newLine();
_builder.append("\t");
_builder.append("annotation2ArrayValue = #[@test.Annotation2, @test.Annotation2(\'foo\'+\'wuppa\')]");
_builder.newLine();
_builder.append("\t");
_builder.append(") class Bar {");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
final MutableClassDeclaration baseClass = it.getTypeLookup().findClass("foo.Bar");
final AnnotationReference annoRef = IterableExtensions.head(baseClass.getAnnotations());
Assert.assertEquals(Integer.valueOf((((2 / 2) + (2 * 3)) - (4 % 1))), annoRef.getValue("intValue"));
Assert.assertEquals(Integer.valueOf((((42 + 4) + (6 * 42)) - (4 / 45))), annoRef.getValue("longValue"));
Assert.assertEquals("foobaz", annoRef.getValue("stringValue"));
Object _value = annoRef.getValue("booleanArrayValue");
final boolean[] bools = ((boolean[]) _value);
Assert.assertTrue(bools[0]);
Assert.assertFalse(bools[1]);
Object _value_1 = annoRef.getValue("intArrayValue");
Assert.assertArrayEquals(new int[] { (-1), (34 + 45), (2 - 6) }, ((int[]) _value_1));
Object _value_2 = annoRef.getValue("typeArrayValue");
final TypeReference[] type = ((TypeReference[]) _value_2);
Assert.assertEquals(it.getTypeReferenceProvider().newTypeReference(Integer.class), type[1]);
Object _value_3 = annoRef.getValue("annotation2Value");
final AnnotationReference anno = ((AnnotationReference) _value_3);
Assert.assertEquals("foowuppa", anno.getValue("value"));
Object _value_4 = annoRef.getValue("annotation2ArrayValue");
final AnnotationReference[] annoArray = ((AnnotationReference[]) _value_4);
Assert.assertEquals("HUBBA BUBBA!", annoArray[0].getValue("value"));
};
this.asCompilationUnit(this.validFile(_builder), _function);
}
use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration in project xtext-xtend by eclipse.
the class DeclarationsTest method testIsAssignable.
@Test
public void testIsAssignable() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package foo");
_builder.newLine();
_builder.newLine();
_builder.append("class BaseClass implements InterfaceA {");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.append("class SubType extends BaseClass implements InterfaceA {");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.append("interface InterfaceA {}");
_builder.newLine();
_builder.append("interface InterfaceB {}");
_builder.newLine();
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
TypeDeclaration _get = ((TypeDeclaration[]) Conversions.unwrapArray(it.getSourceTypeDeclarations(), TypeDeclaration.class))[0];
final ClassDeclaration baseClass = ((ClassDeclaration) _get);
final MutableClassDeclaration subClass = it.getTypeLookup().findClass("foo.SubType");
TypeDeclaration _get_1 = ((TypeDeclaration[]) Conversions.unwrapArray(it.getSourceTypeDeclarations(), TypeDeclaration.class))[2];
final InterfaceDeclaration interfaceA = ((InterfaceDeclaration) _get_1);
TypeDeclaration _get_2 = ((TypeDeclaration[]) Conversions.unwrapArray(it.getSourceTypeDeclarations(), TypeDeclaration.class))[3];
final InterfaceDeclaration interfaceB = ((InterfaceDeclaration) _get_2);
final Type object = it.getTypeReferenceProvider().getObject().getType();
Assert.assertTrue(object.isAssignableFrom(baseClass));
Assert.assertTrue(object.isAssignableFrom(subClass));
Assert.assertTrue(object.isAssignableFrom(interfaceA));
Assert.assertTrue(object.isAssignableFrom(interfaceB));
Assert.assertTrue(baseClass.isAssignableFrom(baseClass));
Assert.assertTrue(baseClass.isAssignableFrom(subClass));
Assert.assertFalse(baseClass.isAssignableFrom(interfaceB));
Assert.assertFalse(baseClass.isAssignableFrom(interfaceA));
Assert.assertFalse(baseClass.isAssignableFrom(object));
Assert.assertTrue(interfaceA.isAssignableFrom(baseClass));
Assert.assertTrue(interfaceA.isAssignableFrom(subClass));
Assert.assertTrue(interfaceA.isAssignableFrom(interfaceA));
Assert.assertFalse(interfaceA.isAssignableFrom(interfaceB));
Assert.assertFalse(interfaceA.isAssignableFrom(object));
};
this.asCompilationUnit(this.validFile(_builder), _function);
}
use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration in project xtext-xtend by eclipse.
the class DeclarationsTest method testSetImplementedInterfaces.
@Test
public void testSetImplementedInterfaces() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("class BaseClass {}");
_builder.newLine();
_builder.append("interface Interface {}");
_builder.newLine();
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
final MutableClassDeclaration baseClass = it.getTypeLookup().findClass("BaseClass");
final MutableInterfaceDeclaration interf = it.getTypeLookup().findInterface("Interface");
final TypeReference objectType = baseClass.getExtendedClass();
Assert.assertEquals("Object", objectType.getSimpleName());
Assert.assertTrue(IterableExtensions.isEmpty(baseClass.getImplementedInterfaces()));
final TypeReference superType = it.getTypeReferenceProvider().newTypeReference(AccessibleObject.class);
baseClass.setExtendedClass(superType);
Assert.assertEquals("AccessibleObject", baseClass.getExtendedClass().getSimpleName());
Assert.assertTrue(IterableExtensions.isEmpty(baseClass.getImplementedInterfaces()));
baseClass.setExtendedClass(null);
Assert.assertEquals("Object", baseClass.getExtendedClass().getSimpleName());
Assert.assertTrue(IterableExtensions.isEmpty(baseClass.getImplementedInterfaces()));
TypeReference _newTypeReference = it.getTypeReferenceProvider().newTypeReference(interf);
baseClass.setImplementedInterfaces(Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(_newTypeReference)));
Assert.assertEquals("Interface", IterableExtensions.head(baseClass.getImplementedInterfaces()).getSimpleName());
baseClass.setImplementedInterfaces(Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList()));
Assert.assertTrue(IterableExtensions.isEmpty(baseClass.getImplementedInterfaces()));
};
this.asCompilationUnit(this.validFile(_builder), _function);
}
Aggregations