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

The following examples show how to use org.eclipse.xtend.lib.macro.declaration.ClassDeclaration. 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: TypeAdapterImplProcessor.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
private ArrayList<FieldDeclaration> getTargetFields(final TypeReference targetType, @Extension final TransformationContext context) {
  final Type objectType = context.newTypeReference(Object.class).getType();
  final ArrayList<FieldDeclaration> targetFields = CollectionLiterals.<FieldDeclaration>newArrayList();
  TypeReference typeRef = targetType;
  while ((!Objects.equal(typeRef.getType(), objectType))) {
    {
      Type _type = typeRef.getType();
      final ClassDeclaration clazz = ((ClassDeclaration) _type);
      final Function1<FieldDeclaration, Boolean> _function = (FieldDeclaration it) -> {
        boolean _isStatic = it.isStatic();
        return Boolean.valueOf((!_isStatic));
      };
      Iterable<? extends FieldDeclaration> _filter = IterableExtensions.filter(clazz.getDeclaredFields(), _function);
      Iterables.<FieldDeclaration>addAll(targetFields, _filter);
      typeRef = clazz.getExtendedClass();
    }
  }
  return targetFields;
}
 
Example #2
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 #3
Source File: AccessObjectProcessor.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doRegisterGlobals(final List<? extends ClassDeclaration> annotatedSourceElements, @Extension final RegisterGlobalsContext ctx) {
  final Consumer<ClassDeclaration> _function = (ClassDeclaration it) -> {
    String _qualifiedName = it.getQualifiedName();
    int _length = it.getQualifiedName().length();
    int _length_1 = it.getSimpleName().length();
    int _minus = (_length - _length_1);
    final String pkg = _qualifiedName.substring(0, _minus);
    String _simpleName = it.getSimpleName();
    final String PVersionName = ((pkg + "P") + _simpleName);
    String _simpleName_1 = it.getSimpleName();
    final String GVersionName = ((pkg + "G") + _simpleName_1);
    ctx.registerClass(PVersionName);
    ctx.registerClass(GVersionName);
  };
  annotatedSourceElements.forEach(_function);
}
 
Example #4
Source File: DeclarationsTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testNestedClass() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("package p");
  _builder.newLine();
  _builder.newLine();
  _builder.append("class Outer {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("static class Inner {}");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
    Assert.assertEquals("p", it.getPackageName());
    TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
    final ClassDeclaration outer = ((ClassDeclaration) _head);
    Assert.assertEquals("p.Outer", outer.getQualifiedName());
    final ClassDeclaration inner = IterableExtensions.head(outer.getDeclaredClasses());
    Assert.assertEquals("Inner", inner.getSimpleName());
    Assert.assertEquals("p.Outer.Inner", inner.getQualifiedName());
    Assert.assertNotNull(it.getTypeLookup().findClass("p.Outer.Inner"));
    Assert.assertNotNull(it.getTypeLookup().findTypeGlobally("p.Outer.Inner"));
  };
  this.asCompilationUnit(this.validFile(_builder), _function);
}
 
Example #5
Source File: ExternalizedProcessor.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doGenerateCode(final List<? extends ClassDeclaration> annotatedSourceElements, @Extension final CodeGenerationContext context) {
  for (final ClassDeclaration clazz : annotatedSourceElements) {
    {
      final Path filePath = clazz.getCompilationUnit().getFilePath();
      Path _targetFolder = context.getTargetFolder(filePath);
      String _replace = clazz.getQualifiedName().replace(".", "/");
      String _plus = (_replace + ".properties");
      final Path file = _targetFolder.append(_plus);
      StringConcatenation _builder = new StringConcatenation();
      {
        Iterable<? extends FieldDeclaration> _declaredFields = clazz.getDeclaredFields();
        for(final FieldDeclaration field : _declaredFields) {
          String _simpleName = field.getSimpleName();
          _builder.append(_simpleName);
          _builder.append(" = ");
          String _initializerAsString = this.getInitializerAsString(field);
          _builder.append(_initializerAsString);
          _builder.newLineIfNotEmpty();
        }
      }
      context.setContents(file, _builder);
    }
  }
}
 
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: EqualsHashCodeProcessor.java    From xtext-lib with Eclipse Public License 2.0 6 votes vote down vote up
public boolean hasSuperEquals(final ClassDeclaration cls) {
  boolean _xblockexpression = false;
  {
    Type _type = cls.getExtendedClass().getType();
    final ClassDeclaration superClass = ((ClassDeclaration) _type);
    boolean _xifexpression = false;
    boolean _equals = this.context.newTypeReference(superClass).equals(this.context.getObject());
    if (_equals) {
      _xifexpression = false;
    } else {
      boolean _xifexpression_1 = false;
      boolean _hasEquals = this.hasEquals(superClass);
      if (_hasEquals) {
        _xifexpression_1 = true;
      } else {
        _xifexpression_1 = this.hasSuperEquals(superClass);
      }
      _xifexpression = _xifexpression_1;
    }
    _xblockexpression = _xifexpression;
  }
  return _xblockexpression;
}
 
Example #8
Source File: EqualsHashCodeProcessor.java    From xtext-lib with Eclipse Public License 2.0 6 votes vote down vote up
public boolean hasSuperHashCode(final ClassDeclaration cls) {
  boolean _xblockexpression = false;
  {
    Type _type = cls.getExtendedClass().getType();
    final ClassDeclaration superClass = ((ClassDeclaration) _type);
    boolean _xifexpression = false;
    boolean _equals = this.context.newTypeReference(superClass).equals(this.context.getObject());
    if (_equals) {
      _xifexpression = false;
    } else {
      boolean _xifexpression_1 = false;
      boolean _hasHashCode = this.hasHashCode(superClass);
      if (_hasHashCode) {
        _xifexpression_1 = true;
      } else {
        _xifexpression_1 = this.hasSuperHashCode(superClass);
      }
      _xifexpression = _xifexpression_1;
    }
    _xblockexpression = _xifexpression;
  }
  return _xblockexpression;
}
 
Example #9
Source File: FileProcessor.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doGenerateCode(final ClassDeclaration annotatedClass, @Extension final CodeGenerationContext context) {
  final Path path = annotatedClass.getCompilationUnit().getFilePath();
  final Path result = context.getTargetFolder(path).append("out.txt");
  final String[] segments = context.getContents(context.getProjectFolder(path).append("res/template.txt")).toString().split(",");
  StringConcatenation _builder = new StringConcatenation();
  {
    boolean _hasElements = false;
    for(final String seg : segments) {
      if (!_hasElements) {
        _hasElements = true;
      } else {
        _builder.appendImmediate("|", "");
      }
      _builder.append(seg);
    }
  }
  context.setContents(result, _builder);
}
 
Example #10
Source File: __GeneratedSourceModifingAnnotationProcessor.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doGenerateCode(final ClassDeclaration annotatedClass, @Extension final CodeGenerationContext context) {
  final Path targetFolder = context.getTargetFolder(annotatedClass.getCompilationUnit().getFilePath());
  String _lastSegment = targetFolder.getLastSegment();
  boolean _equals = Objects.equal(_lastSegment, "xtend-gen");
  Assert.assertTrue(_equals);
}
 
Example #11
Source File: TemplateProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doGenerateCode(final ClassDeclaration annotatedClass, @Extension final CodeGenerationContext context) {
  synchronized (TemplateProcessor.LOCK) {
    final String propertyContents = this.generatePropertiesFile(annotatedClass, context);
    this.generateMessagesClass(propertyContents, annotatedClass, context);
  }
}
 
Example #12
Source File: FinalFieldsConstructorProcessor.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
public ResolvedConstructor getSuperConstructor(final TypeDeclaration it) {
  if ((it instanceof ClassDeclaration)) {
    if ((Objects.equal(((ClassDeclaration)it).getExtendedClass(), this.context.getObject()) || (((ClassDeclaration)it).getExtendedClass() == null))) {
      return null;
    }
    return IterableExtensions.head(((ClassDeclaration)it).getExtendedClass().getDeclaredResolvedConstructors());
  } else {
    return null;
  }
}
 
Example #13
Source File: EmfAdaptableProcessor.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public String getAdapterClassName(final ClassDeclaration declaration) {
  String _qualifiedName = declaration.getQualifiedName();
  String _plus = (_qualifiedName + ".");
  String _simpleName = declaration.getSimpleName();
  String _plus_1 = (_plus + _simpleName);
  return (_plus_1 + "Adapter");
}
 
Example #14
Source File: ToStringProcessor.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
public ToStringConfiguration getToStringConfig(final ClassDeclaration it) {
  ToStringConfiguration _xblockexpression = null;
  {
    final AnnotationReference anno = it.findAnnotation(this.context.findTypeGlobally(ToString.class));
    ToStringConfiguration _xifexpression = null;
    if ((anno == null)) {
      _xifexpression = null;
    } else {
      _xifexpression = new ToStringConfiguration(anno);
    }
    _xblockexpression = _xifexpression;
  }
  return _xblockexpression;
}
 
Example #15
Source File: TemplateProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doGenerateCode(final List<? extends ClassDeclaration> annotatedSourceElements, @Extension final CodeGenerationContext context) {
  synchronized (TemplateProcessor.LOCK) {
    this.buildFileMaps(annotatedSourceElements, context);
    for (final ClassDeclaration annotatedClass : annotatedSourceElements) {
      {
        this.actualPropertyContents = this.propertyContentMap.get(this.getMessagesProperties(annotatedClass));
        this.doGenerateCode(annotatedClass, context);
      }
    }
    this.saveFileMaps(annotatedSourceElements, context);
  }
}
 
Example #16
Source File: TypeLookupImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ClassDeclaration findSourceClass(final String qualifiedName) {
  final XtendTypeDeclarationImpl<? extends XtendTypeDeclaration> type = this.findSourceType(qualifiedName);
  XtendTypeDeclarationImpl<? extends XtendTypeDeclaration> _switchResult = null;
  boolean _matched = false;
  if (type instanceof ClassDeclaration) {
    _matched=true;
    _switchResult = ((XtendTypeDeclarationImpl<? extends XtendTypeDeclaration>)type);
  }
  return ((ClassDeclaration)_switchResult);
}
 
Example #17
Source File: TemplateProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void buildFileMaps(final List<? extends ClassDeclaration> annotatedSourceElements, @Extension final CodeGenerationContext context) {
  HashMap<Path, String> _hashMap = new HashMap<Path, String>();
  this.propertyContentMap = _hashMap;
  for (final ClassDeclaration annotatedClass : annotatedSourceElements) {
    {
      final Path propertyFile = this.getMessagesProperties(annotatedClass);
      boolean _containsKey = this.propertyContentMap.containsKey(propertyFile);
      boolean _not = (!_containsKey);
      if (_not) {
        boolean _exists = context.exists(propertyFile);
        if (_exists) {
          String propertyContents = context.getContents(propertyFile).toString();
          int _length = propertyContents.length();
          boolean _greaterThan = (_length > 0);
          if (_greaterThan) {
            final boolean endsWithWindowsLineSeparator = propertyContents.endsWith("\r\n");
            final boolean endsWithUnixLineSeparator = propertyContents.endsWith("\n");
            if (((!endsWithWindowsLineSeparator) && (!endsWithUnixLineSeparator))) {
              String _propertyContents = propertyContents;
              String _lineSeparator = System.lineSeparator();
              propertyContents = (_propertyContents + _lineSeparator);
            }
          }
          this.propertyContentMap.put(propertyFile, propertyContents);
        } else {
          this.propertyContentMap.put(propertyFile, "");
        }
      }
    }
  }
}
 
Example #18
Source File: TemplateProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void saveFileMaps(final List<? extends ClassDeclaration> annotatedSourceElements, @Extension final CodeGenerationContext context) {
  for (final ClassDeclaration annotatedClass : annotatedSourceElements) {
    {
      this.actualPropertyContents = this.propertyContentMap.get(this.getMessagesProperties(annotatedClass));
      Path _messagesProperties = this.getMessagesProperties(annotatedClass);
      context.setContents(_messagesProperties, this.actualPropertyContents);
    }
  }
}
 
Example #19
Source File: _TESTDATA_InternalClassProcessor.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doGenerateCode(final ClassDeclaration annotatedClass, @Extension final CodeGenerationContext context) {
  final Path tF = context.getTargetFolder(annotatedClass.getCompilationUnit().getFilePath());
  Path _append = tF.append("/Test.txt");
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("Hello");
  _builder.newLine();
  context.setContents(_append, _builder);
}
 
Example #20
Source File: DeclarationsTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testOverriddenMethodFromSource() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("package p");
  _builder.newLine();
  _builder.newLine();
  _builder.append("abstract class C extends java.util.AbstractList<String> implements I {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("override add(String s);");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  _builder.append("interface I {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("def boolean add(String s)");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
    Assert.assertEquals("p", it.getPackageName());
    TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
    final ClassDeclaration clazz = ((ClassDeclaration) _head);
    Assert.assertEquals("p.C", clazz.getQualifiedName());
    final MethodDeclaration add = IterableExtensions.head(clazz.getDeclaredMethods());
    final Iterable<? extends MethodDeclaration> allOverridden = add.getOverriddenOrImplementedMethods();
    Assert.assertEquals(2, IterableExtensions.size(allOverridden));
    final MethodDeclaration listAdd = IterableExtensions.head(allOverridden);
    Assert.assertEquals("add", listAdd.getSimpleName());
    Assert.assertEquals("E", IterableExtensions.head(listAdd.getParameters()).getType().getSimpleName());
    final MethodDeclaration intfAdd = IterableExtensions.last(allOverridden);
    Assert.assertEquals("add", intfAdd.getSimpleName());
    Assert.assertEquals("String", IterableExtensions.head(intfAdd.getParameters()).getType().getSimpleName());
  };
  this.asCompilationUnit(this.validFile(_builder), _function);
}
 
Example #21
Source File: DeclarationsTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example #22
Source File: DeclarationsTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example #23
Source File: TemplateProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private String generatePropertiesFile(final ClassDeclaration annotatedClass, @Extension final CodeGenerationContext context) {
  final String label = this.replaceNewlines(this.getLabel(annotatedClass, context));
  final String description = this.replaceNewlines(this.getDescription(annotatedClass, context));
  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;
}
 
Example #24
Source File: XtendFieldDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public ClassDeclaration getDeclaringType() {
  TypeDeclaration _declaringType = super.getDeclaringType();
  return ((ClassDeclaration) _declaringType);
}
 
Example #25
Source File: DeclarationsTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@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);
}
 
Example #26
Source File: MutableJvmEnumerationTypeDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Iterable<? extends MutableClassDeclaration> getDeclaredClasses() {
  Iterable<? extends ClassDeclaration> _declaredClasses = super.getDeclaredClasses();
  return ((Iterable<? extends MutableClassDeclaration>) _declaredClasses);
}
 
Example #27
Source File: XtendTypeDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Iterable<? extends ClassDeclaration> getDeclaredClasses() {
  return Iterables.<ClassDeclaration>filter(this.getDeclaredMembers(), ClassDeclaration.class);
}
 
Example #28
Source File: TypeAdapterImplProcessor.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void doRegisterGlobals(final ClassDeclaration annotatedClass, @Extension final RegisterGlobalsContext context) {
  String _qualifiedName = annotatedClass.getQualifiedName();
  String _plus = (_qualifiedName + ".Factory");
  context.registerClass(_plus);
}
 
Example #29
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 #30
Source File: MutableJvmInterfaceDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Iterable<? extends MutableClassDeclaration> getDeclaredClasses() {
  Iterable<? extends ClassDeclaration> _declaredClasses = super.getDeclaredClasses();
  return ((Iterable<? extends MutableClassDeclaration>) _declaredClasses);
}