예제 #1
0
 protected void doConversion(
     final JvmTypeReference left,
     final JvmTypeReference right,
     final ITreeAppendable appendable,
     XExpression context,
     final Later expression) {
   if (getPrimitives().isPrimitive(left) && !getPrimitives().isPrimitive(right)) {
     convertWrapperToPrimitive(
         right, getPrimitives().asPrimitiveIfWrapperType(right), context, appendable, expression);
   } else if (getPrimitives().isPrimitive(right) && !getPrimitives().isPrimitive(left)) {
     convertPrimitiveToWrapper(
         right, getPrimitives().asWrapperTypeIfPrimitive(right), context, appendable, expression);
   } else if (right instanceof JvmMultiTypeReference) {
     convertMultiType(left, (JvmMultiTypeReference) right, context, appendable, expression);
   } else if (right instanceof JvmDelegateTypeReference) {
     doConversion(
         left, ((JvmDelegateTypeReference) right).getDelegate(), appendable, context, expression);
   } else if (getTypeReferences().isArray(right) && isList(left)) {
     convertArrayToList(left, appendable, context, expression);
   } else if (isList(right) && getTypeReferences().isArray(left)) {
     convertListToArray(left, appendable, context, expression);
   } else if (isFunction(right)
       || (isFunction(left)
           && closures.findImplementingOperation(right, context.eResource()) != null)) {
     convertFunctionType(left, right, appendable, expression, context);
   } else if (isProcedure(right)
       || (isProcedure(left)
           && closures.findImplementingOperation(right, context.eResource()) != null)) {
     convertFunctionType(left, right, appendable, expression, context);
   } else {
     expression.exec(appendable);
   }
 }
예제 #2
0
  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("}");
  }