org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration Java Examples

The following examples show how to use org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProblemSupportTests.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testErrorOnDerivedElement() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("class MyClass {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("String foo = \'foo\'");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
    final MutableClassDeclaration derived = it.getTypeLookup().findClass("MyClass");
    it.getProblemSupport().addError(derived, "error on derived element");
    Assert.assertEquals("error on derived element", IterableExtensions.<Resource.Diagnostic>head(it.getXtendFile().eResource().getErrors()).getMessage());
    Resource.Diagnostic _head = IterableExtensions.<Resource.Diagnostic>head(it.getXtendFile().eResource().getErrors());
    Assert.assertEquals(IterableExtensions.<XtendTypeDeclaration>head(it.getXtendFile().getXtendTypes()), ((EObjectDiagnosticImpl) _head).getProblematicObject());
  };
  this.asCompilationUnit(this.validFile(_builder), _function);
}
 
Example #2
Source File: AddInterfaceWithDefaultProcessor.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
  super.doTransform(annotatedClass, context);
  Type _findTypeGlobally = context.findTypeGlobally("de.test.Test");
  final MutableInterfaceDeclaration ifType = ((MutableInterfaceDeclaration) _findTypeGlobally);
  final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
    StringConcatenationClient _client = new StringConcatenationClient() {
      @Override
      protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
        _builder.append("System.out.println(\"Hello World\");");
      }
    };
    it.setBody(_client);
    it.setDefault(true);
  };
  ifType.addMethod("sayHello", _function);
}
 
Example #3
Source File: AnnotationWithNestedEnumProcessor.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
  final MutableInterfaceDeclaration interfaceType = context.findInterface(this.getInterfaceName(annotatedClass));
  context.setPrimarySourceElement(interfaceType, annotatedClass);
  try {
    final EnumerationValueDeclaration value = annotatedClass.findAnnotation(context.findTypeGlobally(AnnotationWithNestedEnum.class)).getEnumValue("value");
    final Procedure1<AnnotationReferenceBuildContext> _function = (AnnotationReferenceBuildContext it) -> {
      it.setEnumValue("value", value);
    };
    final AnnotationReference ref = context.newAnnotationReference(AnnotationWithNestedEnum.class, _function);
    interfaceType.addAnnotation(ref);
  } catch (final Throwable _t) {
    if (_t instanceof Exception) {
      final Exception exc = (Exception)_t;
      String _message = exc.getMessage();
      String _plus = ("failed: " + _message);
      context.addError(annotatedClass, _plus);
    } else {
      throw Exceptions.sneakyThrow(_t);
    }
  }
}
 
Example #4
Source File: ToAnnoProcessor.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@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);
}
 
Example #5
Source File: DeclarationsTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@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);
}
 
Example #6
Source File: DeclarationsTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@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);
}
 
Example #7
Source File: FinalFieldsConstructorProcessor.java    From xtext-lib with Eclipse Public License 2.0 6 votes vote down vote up
public void addFinalFieldsConstructor(final MutableClassDeclaration it) {
  boolean _isEmpty = this.getFinalFieldsConstructorArgumentTypes(it).isEmpty();
  if (_isEmpty) {
    final AnnotationReference anno = it.findAnnotation(this.context.findTypeGlobally(FinalFieldsConstructor.class));
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("There are no final fields, this annotation has no effect");
    this.context.addWarning(anno, _builder.toString());
    return;
  }
  boolean _hasFinalFieldsConstructor = this.hasFinalFieldsConstructor(it);
  if (_hasFinalFieldsConstructor) {
    this.context.addError(it, this.getConstructorAlreadyExistsMessage(it));
    return;
  }
  final Procedure1<MutableConstructorDeclaration> _function = (MutableConstructorDeclaration it_1) -> {
    this.context.setPrimarySourceElement(it_1, this.context.getPrimarySourceElement(it_1.getDeclaringType()));
    this.makeFinalFieldsConstructor(it_1);
  };
  it.addConstructor(_function);
}
 
Example #8
Source File: LogProcessor.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doTransform(MutableClassDeclaration cls, TransformationContext context) {
	cls.addField("LOG", field -> {
		field.setStatic(true);
		field.setFinal(true);
		field.setType(context.newTypeReference(Logger.class));
		field.setInitializer(new StringConcatenationClient() {
			@Override
			protected void appendTo(TargetStringConcatenation target) {
				target.append(Logger.class);
				target.append(".getLogger(");
				target.append(cls.getSimpleName());
				target.append(".class)");
				target.newLineIfNotEmpty();
			}
		});
		context.setPrimarySourceElement(field, cls);
	});
}
 
Example #9
Source File: DataProcessor.java    From xtext-lib with Eclipse Public License 2.0 6 votes vote down vote up
public void addDataToString(final MutableClassDeclaration cls) {
  final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
    this.context.setPrimarySourceElement(it, this.context.getPrimarySourceElement(cls));
    it.setReturnType(this.context.getString());
    it.addAnnotation(this.context.newAnnotationReference(Override.class));
    it.addAnnotation(this.context.newAnnotationReference(Pure.class));
    StringConcatenationClient _client = new StringConcatenationClient() {
      @Override
      protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
        _builder.append("String result = new ");
        _builder.append(ToStringHelper.class);
        _builder.append("().toString(this);");
        _builder.newLineIfNotEmpty();
        _builder.append("return result;");
        _builder.newLine();
      }
    };
    it.setBody(_client);
  };
  cls.addMethod("toString", _function);
}
 
Example #10
Source File: AccessorsAnnotationTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testProperty() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("import org.eclipse.xtend.core.tests.macro.Accessors");
  _builder.newLine();
  _builder.newLine();
  _builder.append("class A {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("@Accessors String field");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("@Accessors val String finalField = \'foo\'");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final IAcceptor<XtendCompilerTester.CompilationResult> _function = (XtendCompilerTester.CompilationResult it) -> {
    final TransformationContext ctx = it.getTransformationContext();
    final MutableClassDeclaration classA = ctx.findClass("A");
    Assert.assertEquals(Visibility.PUBLIC, classA.findDeclaredMethod("getField").getVisibility());
    Assert.assertEquals(Visibility.PUBLIC, classA.findDeclaredMethod("setField", ctx.getString()).getVisibility());
    Assert.assertEquals(Visibility.PUBLIC, classA.findDeclaredMethod("getFinalField").getVisibility());
    Assert.assertNull(classA.findDeclaredMethod("setFinalField", ctx.getString()));
  };
  this._xtendCompilerTester.compile(_builder, _function);
}
 
Example #11
Source File: FileProcessor.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
  final Path path = annotatedClass.getCompilationUnit().getFilePath();
  final String contents = context.getContents(context.getProjectFolder(path).append("res/template.txt")).toString();
  final String[] segments = contents.trim().split(",");
  for (final String segment : segments) {
    final Procedure1<MutableFieldDeclaration> _function = (MutableFieldDeclaration it) -> {
      it.setType(context.getString());
    };
    annotatedClass.addField(segment, _function);
  }
}
 
Example #12
Source File: _TESTDATA_InternalClassProcessor.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
  String _qualifiedName = annotatedClass.getQualifiedName();
  String _plus = (_qualifiedName + ".InternalClass");
  MutableClassDeclaration _findClass = context.findClass(_plus);
  final Procedure1<MutableClassDeclaration> _function = (MutableClassDeclaration it) -> {
    final Procedure1<MutableFieldDeclaration> _function_1 = (MutableFieldDeclaration it_1) -> {
      it_1.setType(context.getString());
    };
    it.addField("myField", _function_1);
  };
  ObjectExtensions.<MutableClassDeclaration>operator_doubleArrow(_findClass, _function);
}
 
Example #13
Source File: AccessorsAnnotationTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testProperty2() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("import org.eclipse.xtend.core.tests.macro.Accessors");
  _builder.newLine();
  _builder.newLine();
  _builder.append("class A {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("@Accessors String field");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("private def String getField() {");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("return null");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("} ");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final IAcceptor<XtendCompilerTester.CompilationResult> _function = (XtendCompilerTester.CompilationResult it) -> {
    final TransformationContext ctx = it.getTransformationContext();
    final MutableClassDeclaration classA = ctx.findClass("A");
    Assert.assertEquals(Visibility.PRIVATE, classA.findDeclaredMethod("getField").getVisibility());
    Assert.assertEquals(Visibility.PUBLIC, classA.findDeclaredMethod("setField", ctx.getString()).getVisibility());
    Assert.assertEquals(2, IterableExtensions.size(classA.getDeclaredMethods()));
  };
  this._xtendCompilerTester.compile(_builder, _function);
}
 
Example #14
Source File: ImmutableProcessor.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public MutableMethodDeclaration tryAddMethod(final MutableClassDeclaration it, final String name, final Procedure1<? super MutableMethodDeclaration> initializer) {
  MutableMethodDeclaration _elvis = null;
  MutableMethodDeclaration _findDeclaredMethod = it.findDeclaredMethod(name);
  if (_findDeclaredMethod != null) {
    _elvis = _findDeclaredMethod;
  } else {
    MutableMethodDeclaration _addMethod = it.addMethod(name, ((Procedure1<MutableMethodDeclaration>)initializer));
    _elvis = _addMethod;
  }
  return _elvis;
}
 
Example #15
Source File: Bug464136Processor.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
  final Procedure0 _function = () -> {
    throw new LinkageError("Just a test :-/");
  };
  context.validateLater(_function);
}
 
Example #16
Source File: DeclarationsTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example #17
Source File: TypeLookupImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public MutableClassDeclaration findClass(final String qualifiedName) {
  final Type type = this.findType(qualifiedName);
  MutableClassDeclaration _switchResult = null;
  boolean _matched = false;
  if (type instanceof MutableClassDeclaration) {
    _matched=true;
    _switchResult = ((MutableClassDeclaration)type);
  }
  return _switchResult;
}
 
Example #18
Source File: ExternalizedTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testExtractAnnotation() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("package i18n");
  _builder.newLine();
  _builder.newLine();
  _builder.append("@Externalized");
  _builder.newLine();
  _builder.append("class MyMessages {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("val GREETING = \"Hello {0}\"");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("val DATE_MESSAGE = \"Today, is ${0,date}.\"");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final IAcceptor<XtendCompilerTester.CompilationResult> _function = (XtendCompilerTester.CompilationResult it) -> {
    @Extension
    final TransformationContext ctx = it.getTransformationContext();
    final MutableClassDeclaration clazz = ctx.findClass("i18n.MyMessages");
    Assert.assertEquals(2, IterableExtensions.size(clazz.getDeclaredMethods()));
    final Path path = it.getCompilationUnit().getFilePath();
    Path _targetFolder = ctx.getTargetFolder(path);
    String _replace = clazz.getQualifiedName().replace(".", "/");
    String _plus = (_replace + ".properties");
    final Path properties = _targetFolder.append(_plus);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("GREETING = Hello {0}");
    _builder_1.newLine();
    _builder_1.append("DATE_MESSAGE = Today, is ${0,date}.");
    _builder_1.newLine();
    Assert.assertEquals(_builder_1.toString(), ctx.getContents(properties).toString());
  };
  this.compilerTester.compile(_builder, _function);
}
 
Example #19
Source File: ExtractProcessor.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
  final MutableInterfaceDeclaration interfaceType = context.findInterface(this.getInterfaceName(annotatedClass));
  context.setPrimarySourceElement(interfaceType, annotatedClass);
  Iterable<? extends TypeReference> _implementedInterfaces = annotatedClass.getImplementedInterfaces();
  TypeReference _newTypeReference = context.newTypeReference(interfaceType);
  Iterable<TypeReference> _plus = Iterables.<TypeReference>concat(_implementedInterfaces, Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(_newTypeReference)));
  annotatedClass.setImplementedInterfaces(_plus);
  Iterable<? extends MutableMethodDeclaration> _declaredMethods = annotatedClass.getDeclaredMethods();
  for (final MutableMethodDeclaration method : _declaredMethods) {
    Visibility _visibility = method.getVisibility();
    boolean _equals = Objects.equal(_visibility, Visibility.PUBLIC);
    if (_equals) {
      final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
        it.setDocComment(method.getDocComment());
        it.setReturnType(method.getReturnType());
        Iterable<? extends MutableParameterDeclaration> _parameters = method.getParameters();
        for (final MutableParameterDeclaration p : _parameters) {
          it.addParameter(p.getSimpleName(), p.getType());
        }
        it.setExceptions(((TypeReference[])Conversions.unwrapArray(method.getExceptions(), TypeReference.class)));
        context.setPrimarySourceElement(it, method);
      };
      interfaceType.addMethod(method.getSimpleName(), _function);
    }
  }
}
 
Example #20
Source File: ExtractTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testExtractAnnotation() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("@extract.Extract");
  _builder.newLine();
  _builder.append("class MyClass {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("override doStuff(String myParam) throws IllegalArgumentException {");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("return myParam");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("}");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final IAcceptor<XtendCompilerTester.CompilationResult> _function = (XtendCompilerTester.CompilationResult it) -> {
    @Extension
    final TransformationContext ctx = it.getTransformationContext();
    final MutableInterfaceDeclaration interf = ctx.findInterface("MyClassInterface");
    final MutableClassDeclaration clazz = ctx.findClass("MyClass");
    Assert.assertEquals(IterableExtensions.head(clazz.getImplementedInterfaces()).getType(), interf);
    MutableMethodDeclaration _head = IterableExtensions.head(interf.getDeclaredMethods());
    final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it_1) -> {
      Assert.assertEquals("doStuff", it_1.getSimpleName());
      Assert.assertEquals(ctx.getString(), it_1.getReturnType());
      Assert.assertEquals(ctx.newTypeReference(IllegalArgumentException.class), IterableExtensions.head(it_1.getExceptions()));
    };
    ObjectExtensions.<MutableMethodDeclaration>operator_doubleArrow(_head, _function_1);
  };
  this.compilerTester.compile(_builder, _function);
}
 
Example #21
Source File: TypeAdapterImplProcessor.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
protected MutableMethodDeclaration generateFactory(final MutableClassDeclaration factory, final MutableClassDeclaration impl, final TypeReference targetType, @Extension final TransformationContext context) {
  MutableMethodDeclaration _xblockexpression = null;
  {
    TypeReference _newTypeReference = context.newTypeReference("com.google.gson.TypeAdapterFactory");
    factory.setImplementedInterfaces(Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(_newTypeReference)));
    final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
      final MutableTypeParameterDeclaration t = it.addTypeParameter("T");
      it.addParameter("gson", context.newTypeReference("com.google.gson.Gson"));
      it.addParameter("typeToken", context.newTypeReference("com.google.gson.reflect.TypeToken", context.newTypeReference(t)));
      it.setReturnType(context.newTypeReference("com.google.gson.TypeAdapter", context.newTypeReference(t)));
      StringConcatenationClient _client = new StringConcatenationClient() {
        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
          _builder.append("if (!");
          _builder.append(targetType);
          _builder.append(".class.isAssignableFrom(typeToken.getRawType())) {");
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          _builder.append("return null;");
          _builder.newLine();
          _builder.append("}");
          _builder.newLine();
          _builder.append("return (TypeAdapter<T>) new ");
          _builder.append(impl);
          _builder.append("(gson);");
          _builder.newLineIfNotEmpty();
        }
      };
      it.setBody(_client);
    };
    _xblockexpression = factory.addMethod("create", _function);
  }
  return _xblockexpression;
}
 
Example #22
Source File: ToStringProcessor.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
@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 ToStringProcessor.Util util = new ToStringProcessor.Util(context);
  final AnnotationReference annotation = it.findAnnotation(context.findTypeGlobally(ToString.class));
  final ToStringConfiguration configuration = new ToStringConfiguration(annotation);
  boolean _hasToString = util.hasToString(it);
  if (_hasToString) {
    context.addWarning(annotation, "toString is already defined, this annotation has no effect.");
  } else {
    TypeReference _extendedClass = it.getExtendedClass();
    TypeReference _object = context.getObject();
    boolean _notEquals = (!Objects.equal(_extendedClass, _object));
    if (_notEquals) {
      util.addReflectiveToString(it, configuration);
    } else {
      final Function1<MutableFieldDeclaration, Boolean> _function = (MutableFieldDeclaration it_1) -> {
        return Boolean.valueOf(((context.isThePrimaryGeneratedJavaElement(it_1) && (!it_1.isStatic())) && (!it_1.isTransient())));
      };
      util.addToString(it, IterableExtensions.filter(it.getDeclaredFields(), _function), configuration);
    }
  }
}
 
Example #23
Source File: TypeAdapterImplProcessor.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
  final AnnotationReference typeAdapterImplAnnotation = annotatedClass.findAnnotation(context.findTypeGlobally(TypeAdapterImpl.class));
  final TypeReference targetType = typeAdapterImplAnnotation.getClassValue("value");
  this.generateImpl(annotatedClass, targetType, context);
  String _qualifiedName = annotatedClass.getQualifiedName();
  String _plus = (_qualifiedName + ".Factory");
  this.generateFactory(context.findClass(_plus), annotatedClass, targetType, context);
}
 
Example #24
Source File: EqualsHashCodeProcessor.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
@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));
    }
  }
}
 
Example #25
Source File: FinalFieldsConstructorProcessor.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
protected void _transform(final MutableClassDeclaration it, @Extension final TransformationContext context) {
  AnnotationReference _findAnnotation = it.findAnnotation(context.findTypeGlobally(Data.class));
  boolean _tripleNotEquals = (_findAnnotation != null);
  if (_tripleNotEquals) {
    return;
  }
  AnnotationReference _findAnnotation_1 = it.findAnnotation(context.findTypeGlobally(Accessors.class));
  boolean _tripleNotEquals_1 = (_findAnnotation_1 != null);
  if (_tripleNotEquals_1) {
    return;
  }
  @Extension
  final FinalFieldsConstructorProcessor.Util util = new FinalFieldsConstructorProcessor.Util(context);
  util.addFinalFieldsConstructor(it);
}
 
Example #26
Source File: AccessorsProcessor.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
public void transform(final MutableMemberDeclaration it, final TransformationContext context) {
  if (it instanceof MutableClassDeclaration) {
    _transform((MutableClassDeclaration)it, context);
    return;
  } else if (it instanceof MutableFieldDeclaration) {
    _transform((MutableFieldDeclaration)it, context);
    return;
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, context).toString());
  }
}
 
Example #27
Source File: FinalFieldsConstructorProcessor.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
public void transform(final MutableTypeParameterDeclarator it, final TransformationContext context) {
  if (it instanceof MutableConstructorDeclaration) {
    _transform((MutableConstructorDeclaration)it, context);
    return;
  } else if (it instanceof MutableClassDeclaration) {
    _transform((MutableClassDeclaration)it, context);
    return;
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, context).toString());
  }
}
 
Example #28
Source File: JsonRpcDataProcessor.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
protected MutableMethodDeclaration generateToString(final MutableClassDeclaration impl, @Extension final TransformationContext context) {
  MutableMethodDeclaration _xblockexpression = null;
  {
    final ArrayList<FieldDeclaration> toStringFields = CollectionLiterals.<FieldDeclaration>newArrayList();
    ClassDeclaration c = impl;
    do {
      {
        Iterable<? extends FieldDeclaration> _declaredFields = c.getDeclaredFields();
        Iterables.<FieldDeclaration>addAll(toStringFields, _declaredFields);
        TypeReference _extendedClass = c.getExtendedClass();
        Type _type = null;
        if (_extendedClass!=null) {
          _type=_extendedClass.getType();
        }
        c = ((ClassDeclaration) _type);
      }
    } while(((c != null) && (!Objects.equal(c, context.getObject()))));
    final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
      it.setReturnType(context.getString());
      it.addAnnotation(context.newAnnotationReference(Override.class));
      it.addAnnotation(context.newAnnotationReference(Pure.class));
      final AccessorsProcessor.Util accessorsUtil = new AccessorsProcessor.Util(context);
      StringConcatenationClient _client = new StringConcatenationClient() {
        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
          _builder.append(ToStringBuilder.class);
          _builder.append(" b = new ");
          _builder.append(ToStringBuilder.class);
          _builder.append("(this);");
          _builder.newLineIfNotEmpty();
          {
            for(final FieldDeclaration field : toStringFields) {
              _builder.append("b.add(\"");
              String _simpleName = field.getSimpleName();
              _builder.append(_simpleName);
              _builder.append("\", ");
              {
                TypeDeclaration _declaringType = field.getDeclaringType();
                boolean _equals = Objects.equal(_declaringType, impl);
                if (_equals) {
                  _builder.append("this.");
                  String _simpleName_1 = field.getSimpleName();
                  _builder.append(_simpleName_1);
                } else {
                  String _getterName = accessorsUtil.getGetterName(field);
                  _builder.append(_getterName);
                  _builder.append("()");
                }
              }
              _builder.append(");");
              _builder.newLineIfNotEmpty();
            }
          }
          _builder.append("return b.toString();");
          _builder.newLine();
        }
      };
      it.setBody(_client);
    };
    _xblockexpression = impl.addMethod("toString", _function);
  }
  return _xblockexpression;
}
 
Example #29
Source File: JsonRpcDataProcessor.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, final TransformationContext context) {
  this.generateImpl(annotatedClass, context);
}
 
Example #30
Source File: JsonRpcDataTransformationContext.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
public MutableClassDeclaration findClass(final String arg0) {
  return this.delegate.findClass(arg0);
}