protected SignatureHashBuilder appendType(JvmTypeReference ref) {
   if (ref != null && ref.getIdentifier() != null) {
     append(ref.getIdentifier());
   } else {
     append("*unresolved*");
   }
   return this;
 }
Exemple #2
0
 @Test
 public void testEmptyListAsAnnotationValueDefault() {
   try {
     StringConcatenation _builder = new StringConcatenation();
     _builder.append("annotation Foo {");
     _builder.newLine();
     _builder.append("\t");
     _builder.append("String[] bar = #[]");
     _builder.newLine();
     _builder.append("}");
     _builder.newLine();
     String _string = _builder.toString();
     XtendAnnotationType _annotationType = this.annotationType(_string);
     JvmAnnotationType _inferredAnnotationType =
         this._iXtendJvmAssociations.getInferredAnnotationType(_annotationType);
     EList<JvmMember> _members = _inferredAnnotationType.getMembers();
     JvmMember _head = IterableExtensions.<JvmMember>head(_members);
     final JvmOperation inferred = ((JvmOperation) _head);
     JvmTypeReference _returnType = inferred.getReturnType();
     String _identifier = _returnType.getIdentifier();
     Assert.assertEquals("java.lang.String[]", _identifier);
     JvmAnnotationValue _defaultValue = inferred.getDefaultValue();
     Assert.assertTrue((_defaultValue instanceof JvmStringAnnotationValue));
     JvmAnnotationValue _defaultValue_1 = inferred.getDefaultValue();
     EList<String> _values = ((JvmStringAnnotationValue) _defaultValue_1).getValues();
     boolean _isEmpty = _values.isEmpty();
     Assert.assertTrue(_isEmpty);
   } catch (Throwable _e) {
     throw Exceptions.sneakyThrow(_e);
   }
 }
Exemple #3
0
 @Test
 public void testInterfaceImplicitSuperType() {
   try {
     StringConcatenation _builder = new StringConcatenation();
     _builder.append("interface Foo {");
     _builder.newLine();
     _builder.append("}");
     _builder.newLine();
     String _string = _builder.toString();
     XtendInterface _interfaze = this.interfaze(_string);
     final JvmGenericType inferred = this._iXtendJvmAssociations.getInferredType(_interfaze);
     EList<JvmTypeReference> _superTypes = inferred.getSuperTypes();
     int _size = _superTypes.size();
     Assert.assertEquals(1, _size);
     EList<JvmTypeReference> _superTypes_1 = inferred.getSuperTypes();
     JvmTypeReference _head = IterableExtensions.<JvmTypeReference>head(_superTypes_1);
     String _identifier = _head.getIdentifier();
     Assert.assertEquals("java.lang.Object", _identifier);
   } catch (Throwable _e) {
     throw Exceptions.sneakyThrow(_e);
   }
 }
 protected void assertCommonSuperType(String expected, List<JvmTypeReference> refs) {
   JvmTypeReference type = getComputer().getCommonSuperType(refs);
   assertEquals(expected, type.getIdentifier());
 }
  protected void convertFunctionType(
      final JvmTypeReference expectedType,
      final JvmTypeReference functionType,
      final ITreeAppendable appendable,
      final Later expression,
      XExpression context) {
    //		JvmTypeReference resolvedLeft = closures.getResolvedExpectedType(expectedType,
    // functionType);
    if (expectedType.getIdentifier().equals(Object.class.getName())
        || EcoreUtil.equals(expectedType.getType(), functionType.getType())
        || ((expectedType instanceof JvmSynonymTypeReference)
            && Iterables.any(
                ((JvmSynonymTypeReference) expectedType).getReferences(),
                new Predicate<JvmTypeReference>() {
                  public boolean apply(@Nullable JvmTypeReference ref) {
                    return EcoreUtil.equals(ref.getType(), functionType.getType());
                  }
                }))) {
      // same raw type but different type parameters
      // at this point we know that we are compatible so we have to convince the Java compiler about
      // that ;-)
      if (!getTypeConformanceComputer().isConformant(expectedType, functionType)) {
        // insert a cast
        appendable.append("(");
        serialize(expectedType, context, appendable);
        appendable.append(")");
      }
      expression.exec(appendable);
      return;
    }
    JvmOperation operation = closures.findImplementingOperation(expectedType, context.eResource());
    if (operation == null) {
      throw new IllegalStateException(
          "expected type " + expectedType + " not mappable from " + functionType);
    }
    JvmType declaringType =
        (expectedType instanceof JvmParameterizedTypeReference)
            ? expectedType.getType()
            : operation.getDeclaringType();
    final JvmTypeReference typeReferenceWithPlaceHolder =
        getTypeReferences().createTypeRef(declaringType);
    ITypeArgumentContext typeArgumentContext =
        contextProvider.getTypeArgumentContext(
            new TypeArgumentContextProvider.AbstractRequest() {
              @Override
              public JvmTypeReference getExpectedType() {
                return functionType;
              }

              @Override
              public JvmTypeReference getDeclaredType() {
                return typeReferenceWithPlaceHolder;
              }

              @Override
              public String toString() {
                return "TypeConvertingCompiler.convertFunctionType [expected="
                    + functionType
                    + ",declared="
                    + typeReferenceWithPlaceHolder
                    + "]";
              }
            });
    JvmTypeReference resolvedExpectedType =
        typeArgumentContext.resolve(typeReferenceWithPlaceHolder);
    appendable.append("new ");
    serialize(resolvedExpectedType, context, appendable, true, false);
    appendable.append("() {");
    appendable.increaseIndentation().increaseIndentation();
    appendable.newLine().append("public ");
    serialize(
        typeArgumentContext.resolve(operation.getReturnType()), context, appendable, true, false);
    appendable.append(" ").append(operation.getSimpleName()).append("(");
    EList<JvmFormalParameter> params = operation.getParameters();
    for (Iterator<JvmFormalParameter> iterator = params.iterator(); iterator.hasNext(); ) {
      JvmFormalParameter p = iterator.next();
      final String name = p.getName();
      serialize(
          typeArgumentContext.resolve(p.getParameterType()), context, appendable, false, false);
      appendable.append(" ").append(name);
      if (iterator.hasNext()) appendable.append(",");
    }
    appendable.append(") {");
    appendable.increaseIndentation();
    if (!getTypeReferences().is(operation.getReturnType(), Void.TYPE))
      appendable.newLine().append("return ");
    else appendable.newLine();
    expression.exec(appendable);
    appendable.append(".");
    JvmOperation actualOperation =
        closures.findImplementingOperation(functionType, context.eResource());
    appendable.append(actualOperation.getSimpleName());
    appendable.append("(");
    for (Iterator<JvmFormalParameter> iterator = params.iterator(); iterator.hasNext(); ) {
      JvmFormalParameter p = iterator.next();
      final String name = p.getName();
      appendable.append(name);
      if (iterator.hasNext()) appendable.append(",");
    }
    appendable.append(");");
    appendable.decreaseIndentation();
    appendable.newLine().append("}");
    appendable.decreaseIndentation().decreaseIndentation();
    appendable.newLine().append("}");
  }