protected boolean areArgumentTypesValid(JvmExecutable exectuable, List<XExpression> arguments) {
   int numberOfParameters = exectuable.getParameters().size();
   int parametersToCheck = exectuable.isVarArgs() ? numberOfParameters - 1 : numberOfParameters;
   for (int i = 0; i < parametersToCheck; i++) {
     JvmTypeReference parameterType = exectuable.getParameters().get(i).getParameterType();
     XExpression argument = arguments.get(i);
     JvmTypeReference argumentType = getTypeProvider().getType(argument, true);
     if (parameterType == null) return true;
     if (!isCompatibleArgument(parameterType, argumentType)) return false;
   }
   if (exectuable.isVarArgs()) {
     int lastParamIndex = numberOfParameters - 1;
     JvmTypeReference lastParameterType =
         exectuable.getParameters().get(lastParamIndex).getParameterType();
     if (!(lastParameterType.getType() instanceof JvmArrayType))
       throw new IllegalStateException("Unexpected var arg type: " + lastParameterType);
     JvmTypeReference varArgType = ((JvmArrayType) lastParameterType.getType()).getComponentType();
     if (arguments.size() == numberOfParameters) {
       XExpression lastArgument = arguments.get(lastParamIndex);
       JvmTypeReference lastArgumentType = getTypeProvider().getType(lastArgument, true);
       if (isCompatibleArgument(lastParameterType, lastArgumentType)) return true;
       if (!isCompatibleArgument(varArgType, lastArgumentType)) return false;
     } else {
       for (int i = lastParamIndex; i < arguments.size(); i++) {
         XExpression argumentExpression = arguments.get(i);
         JvmTypeReference argumentType = getTypeProvider().getType(argumentExpression, true);
         if (!isCompatibleArgument(varArgType, argumentType)) return false;
       }
     }
   }
   return true;
 }
Esempio n. 2
0
 @Override
 public JvmTypeReference getExtendedClass() {
   for (JvmTypeReference candidate : getSuperTypes()) {
     if (candidate.getType() instanceof JvmGenericType
         && !((JvmGenericType) candidate.getType()).isInterface()) return candidate;
   }
   return null;
 }
 protected boolean isCompatibleArgument(
     JvmTypeReference declaredType, JvmTypeReference actualType) {
   if (actualType == null) return true;
   if (actualType.getType() instanceof JvmTypeParameter) {
     JvmTypeParameter type = (JvmTypeParameter) actualType.getType();
     if (type.getConstraints().isEmpty()) return true;
     for (JvmTypeConstraint constraint : type.getConstraints()) {
       if (isCompatibleArgument(declaredType, constraint.getTypeReference())) return true;
     }
     return false;
   }
   return conformance.isConformant(declaredType, actualType, true);
 }
 public Iterable<? extends JvmFeature> getFeaturesForType(JvmTypeReference declType) {
   if (declType instanceof JvmAnyTypeReference || declType instanceof JvmMultiTypeReference) {
     return Collections.emptyList();
   }
   if (declType != null && declType.getType() instanceof JvmDeclaredType) {
     return filter(
         filter(((JvmDeclaredType) declType.getType()).getMembers(), JvmFeature.class),
         new Predicate<JvmFeature>() {
           public boolean apply(JvmFeature input) {
             return !(input instanceof JvmConstructor);
           }
         });
   }
   return emptySet();
 }
 protected String getRawTypeIdentifier(JvmTypeReference reference) {
   if (reference instanceof JvmParameterizedTypeReference) {
     JvmType typeOrProxy =
         (JvmType)
             reference.eGet(TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, false);
     if (typeOrProxy.eIsProxy()) {
       URI uri = ((InternalEObject) typeOrProxy).eProxyURI();
       if (URIHelperConstants.PROTOCOL.equals(uri.scheme())) {
         if (URIHelperConstants.PRIMITIVES.regionMatches(
             1, uri.segment(0), 0, URIHelperConstants.PRIMITIVES.length() - 1)) {
           String fragment = uri.fragment();
           return fragment;
         } else if (URIHelperConstants.OBJECTS.regionMatches(
             1, uri.segment(0), 0, URIHelperConstants.OBJECTS.length() - 2)) {
           String fragment = uri.fragment();
           if (fragment.lastIndexOf('/') == -1) return fragment;
         }
       }
     }
   }
   RawTypeReferenceComputer strategy = new RawTypeReferenceComputer(TypesFactory.eINSTANCE);
   JvmTypeReference result = strategy.getRawTypeReference(reference, eResource());
   if (result == null) return null;
   JvmType rawResult = result.getType();
   return rawResult == null ? null : rawResult.getIdentifier();
 }
Esempio n. 6
0
 protected void createInheritedFeatureNodes(
     IOutlineNode parentNode,
     JvmDeclaredType baseType,
     Set<JvmFeature> processedFeatures,
     int inheritanceDepth,
     JvmTypeReference superType) {
   if (superType.getType() instanceof JvmDeclaredType) {
     JvmDeclaredType superClass = ((JvmGenericType) superType.getType());
     EObject xtendSuperClass = associations.getPrimarySourceElement(superType.getType());
     createFeatureNodesForType(
         parentNode,
         (XtendTypeDeclaration) xtendSuperClass,
         superClass,
         baseType,
         processedFeatures,
         inheritanceDepth + 1);
   }
 }
Esempio n. 7
0
 public JvmTypeReference getArgument(JvmTypeReference left, int index) {
   if (left.getType() instanceof JvmGenericType) {
     List<JvmTypeParameter> typeParameters = ((JvmGenericType) left.getType()).getTypeParameters();
     if (typeParameters.size() <= index) {
       throw new IllegalArgumentException(
           "The type "
               + left.getType().getIdentifier()
               + " cannot be parameterized with more than "
               + typeParameters.size()
               + " type arguments.");
     }
     if (left instanceof JvmParameterizedTypeReference) {
       List<JvmTypeReference> arguments = ((JvmParameterizedTypeReference) left).getArguments();
       if (arguments.size() == typeParameters.size()) {
         return arguments.get(index);
       }
     }
     final JvmTypeParameter jvmTypeParameter = typeParameters.get(index);
     return createTypeRef(jvmTypeParameter);
   }
   throw new IllegalArgumentException(left.getType().getIdentifier() + " is not generic.");
 }
Esempio n. 8
0
 public void serialize(
     final JvmTypeReference type,
     EObject context,
     IAppendable appendable,
     boolean withoutConstraints,
     boolean paramsToWildcard,
     boolean paramsToObject,
     boolean allowPrimitives) {
   IAppendable tracedAppendable = appendable;
   boolean tracing = false;
   if (appendable instanceof ITreeAppendable && type.eResource() == context.eResource()) {
     tracedAppendable = ((ITreeAppendable) appendable).trace(type);
     tracing = true;
   }
   if (type instanceof JvmWildcardTypeReference) {
     JvmWildcardTypeReference wildcard = (JvmWildcardTypeReference) type;
     if (!withoutConstraints) {
       tracedAppendable.append("?");
     }
     if (!wildcard.getConstraints().isEmpty()) {
       for (JvmTypeConstraint constraint : wildcard.getConstraints()) {
         if (constraint instanceof JvmLowerBound) {
           if (!withoutConstraints) tracedAppendable.append(" super ");
           serialize(
               constraint.getTypeReference(),
               context,
               tracedAppendable,
               withoutConstraints,
               paramsToWildcard,
               paramsToObject,
               false);
           return;
         }
       }
       boolean first = true;
       for (JvmTypeConstraint constraint : wildcard.getConstraints()) {
         if (constraint instanceof JvmUpperBound) {
           if (first) {
             if (!withoutConstraints) tracedAppendable.append(" extends ");
             first = false;
           } else {
             if (withoutConstraints)
               throw new IllegalStateException(
                   "cannot have two upperbounds if type should be printed without constraints");
             tracedAppendable.append(" & ");
           }
           serialize(
               constraint.getTypeReference(),
               context,
               tracedAppendable,
               withoutConstraints,
               paramsToWildcard,
               paramsToObject,
               false);
         }
       }
     } else if (withoutConstraints) {
       tracedAppendable.append("Object");
     }
   } else if (type instanceof JvmGenericArrayTypeReference) {
     serialize(
         ((JvmGenericArrayTypeReference) type).getComponentType(),
         context,
         tracedAppendable,
         withoutConstraints,
         paramsToWildcard,
         paramsToObject,
         true);
     tracedAppendable.append("[]");
   } else if (type instanceof JvmParameterizedTypeReference) {
     JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) type;
     if ((paramsToWildcard || paramsToObject)
         && parameterized.getType() instanceof JvmTypeParameter) {
       JvmTypeParameter parameter = (JvmTypeParameter) parameterized.getType();
       if (!isLocalTypeParameter(context, parameter)) {
         if (paramsToWildcard) tracedAppendable.append("?");
         else tracedAppendable.append("Object");
         return;
       }
     }
     JvmType jvmType =
         allowPrimitives ? type.getType() : primitives.asWrapperTypeIfPrimitive(type).getType();
     if (tracing) {
       ITextRegion region =
           locationProvider.getFullTextRegion(
               type, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, 0);
       if (region instanceof ITextRegionWithLineInformation) {
         ((ITreeAppendable) tracedAppendable)
             .trace(new LocationData((ITextRegionWithLineInformation) region, null))
             .append(jvmType);
       } else {
         tracedAppendable.append(jvmType);
       }
     } else {
       tracedAppendable.append(jvmType);
     }
     if (!parameterized.getArguments().isEmpty()) {
       tracedAppendable.append("<");
       for (int i = 0; i < parameterized.getArguments().size(); i++) {
         if (i != 0) {
           tracedAppendable.append(",");
         }
         serialize(
             parameterized.getArguments().get(i),
             context,
             tracedAppendable,
             withoutConstraints,
             paramsToWildcard,
             paramsToObject,
             false);
       }
       tracedAppendable.append(">");
     }
   } else if (type instanceof JvmAnyTypeReference) {
     tracedAppendable.append("Object");
   } else if (type instanceof JvmMultiTypeReference) {
     serialize(
         resolveMultiType(type, context),
         context,
         tracedAppendable,
         withoutConstraints,
         paramsToWildcard,
         paramsToObject,
         allowPrimitives);
   } else if (type instanceof JvmDelegateTypeReference) {
     JvmTypeReference delegate = ((JvmDelegateTypeReference) type).getDelegate();
     if (delegate != null)
       serialize(
           delegate,
           context,
           tracedAppendable,
           withoutConstraints,
           paramsToWildcard,
           paramsToObject,
           allowPrimitives);
     else tracedAppendable.append("Object");
   } else if (type instanceof JvmSpecializedTypeReference) {
     serialize(
         ((JvmSpecializedTypeReference) type).getEquivalent(),
         context,
         tracedAppendable,
         withoutConstraints,
         paramsToWildcard,
         paramsToObject,
         allowPrimitives);
   } else if (type instanceof JvmUnknownTypeReference) {
     if (type.eIsSet(TypesPackage.Literals.JVM_UNKNOWN_TYPE_REFERENCE__QUALIFIED_NAME)) {
       tracedAppendable.append(type.getQualifiedName());
     } else {
       tracedAppendable.append("Object");
     }
   } else {
     throw new IllegalArgumentException(String.valueOf(type));
   }
 }
Esempio n. 9
0
 public boolean isArray(JvmTypeReference type) {
   if (isNullOrProxy(type)) return false;
   return type instanceof JvmGenericArrayTypeReference || type.getType() instanceof JvmArrayType;
 }
Esempio n. 10
0
 public boolean isNullOrProxy(final JvmTypeReference reference) {
   return reference == null || reference.getType() == null || reference.getType().eIsProxy();
 }
Esempio n. 11
0
 public boolean is(final JvmTypeReference reference, final Class<?> clazz) {
   if (isNullOrProxy(reference)) return false;
   final boolean equals = clazz.getCanonicalName().equals(reference.getType().getIdentifier());
   return equals;
 }
Esempio n. 12
0
 public void generateBodyAnnotations(final XPackage pack) {
   final HashSet<ETypedElement> processed = CollectionLiterals.<ETypedElement>newHashSet();
   EList<XClassifier> _classifiers = pack.getClassifiers();
   for (final XClassifier xClassifier : _classifiers) {
     if ((xClassifier instanceof XDataType)) {
       final XDataType xDataType = ((XDataType) xClassifier);
       XDataTypeMapping _mapping = this.mappings.getMapping(xDataType);
       final EDataType eDataType = _mapping.getEDataType();
       final XBlockExpression createBody = xDataType.getCreateBody();
       XDataTypeMapping _mapping_1 = this.mappings.getMapping(xDataType);
       final JvmOperation creator = _mapping_1.getCreator();
       boolean _and = false;
       boolean _notEquals = (!Objects.equal(createBody, null));
       if (!_notEquals) {
         _and = false;
       } else {
         boolean _notEquals_1 = (!Objects.equal(creator, null));
         _and = (_notEquals && _notEquals_1);
       }
       if (_and) {
         final XcoreAppendable appendable = this.createAppendable();
         EList<JvmFormalParameter> _parameters = creator.getParameters();
         JvmFormalParameter _get = _parameters.get(0);
         appendable.declareVariable(_get, "it");
         JvmTypeReference _returnType = creator.getReturnType();
         Set<JvmTypeReference> _emptySet = Collections.<JvmTypeReference>emptySet();
         this.compiler.compile(createBody, appendable, _returnType, _emptySet);
         String _string = appendable.toString();
         String _extractBody = this.extractBody(_string);
         EcoreUtil.setAnnotation(eDataType, GenModelPackage.eNS_URI, "create", _extractBody);
       }
       final XBlockExpression convertBody = xDataType.getConvertBody();
       XDataTypeMapping _mapping_2 = this.mappings.getMapping(xDataType);
       final JvmOperation converter = _mapping_2.getConverter();
       boolean _and_1 = false;
       boolean _notEquals_2 = (!Objects.equal(convertBody, null));
       if (!_notEquals_2) {
         _and_1 = false;
       } else {
         boolean _notEquals_3 = (!Objects.equal(converter, null));
         _and_1 = (_notEquals_2 && _notEquals_3);
       }
       if (_and_1) {
         final XcoreAppendable appendable_1 = this.createAppendable();
         EList<JvmFormalParameter> _parameters_1 = converter.getParameters();
         JvmFormalParameter _get_1 = _parameters_1.get(0);
         appendable_1.declareVariable(_get_1, "it");
         JvmTypeReference _returnType_1 = converter.getReturnType();
         Set<JvmTypeReference> _emptySet_1 = Collections.<JvmTypeReference>emptySet();
         this.compiler.compile(convertBody, appendable_1, _returnType_1, _emptySet_1);
         String _string_1 = appendable_1.toString();
         String _extractBody_1 = this.extractBody(_string_1);
         EcoreUtil.setAnnotation(eDataType, GenModelPackage.eNS_URI, "convert", _extractBody_1);
       }
     } else {
       final XClass xClass = ((XClass) xClassifier);
       XClassMapping _mapping_3 = this.mappings.getMapping(xClass);
       final EClass eClass = _mapping_3.getEClass();
       EList<EStructuralFeature> _eAllStructuralFeatures = eClass.getEAllStructuralFeatures();
       for (final EStructuralFeature eStructuralFeature : _eAllStructuralFeatures) {
         boolean _add = processed.add(eStructuralFeature);
         if (_add) {
           final XStructuralFeature xFeature = this.mappings.getXFeature(eStructuralFeature);
           boolean _notEquals_4 = (!Objects.equal(xFeature, null));
           if (_notEquals_4) {
             final XBlockExpression getBody = xFeature.getGetBody();
             boolean _notEquals_5 = (!Objects.equal(getBody, null));
             if (_notEquals_5) {
               XFeatureMapping _mapping_4 = this.mappings.getMapping(xFeature);
               final JvmOperation getter = _mapping_4.getGetter();
               final XcoreAppendable appendable_2 = this.createAppendable();
               JvmTypeReference _returnType_2 = getter.getReturnType();
               Set<JvmTypeReference> _emptySet_2 = Collections.<JvmTypeReference>emptySet();
               this.compiler.compile(getBody, appendable_2, _returnType_2, _emptySet_2);
               String _string_2 = appendable_2.toString();
               String _extractBody_2 = this.extractBody(_string_2);
               EcoreUtil.setAnnotation(
                   eStructuralFeature, GenModelPackage.eNS_URI, "get", _extractBody_2);
             }
           }
         }
       }
       EList<EOperation> _eAllOperations = eClass.getEAllOperations();
       for (final EOperation eOperation : _eAllOperations) {
         boolean _add_1 = processed.add(eOperation);
         if (_add_1) {
           final XOperation xOperation = this.mappings.getXOperation(eOperation);
           boolean _notEquals_6 = (!Objects.equal(xOperation, null));
           if (_notEquals_6) {
             final XBlockExpression body = xOperation.getBody();
             boolean _notEquals_7 = (!Objects.equal(body, null));
             if (_notEquals_7) {
               XOperationMapping _mapping_5 = this.mappings.getMapping(xOperation);
               final JvmOperation jvmOperation = _mapping_5.getJvmOperation();
               boolean _notEquals_8 = (!Objects.equal(jvmOperation, null));
               if (_notEquals_8) {
                 final XcoreAppendable appendable_3 = this.createAppendable();
                 JvmDeclaredType _declaringType = jvmOperation.getDeclaringType();
                 appendable_3.declareVariable(_declaringType, "this");
                 JvmDeclaredType _declaringType_1 = jvmOperation.getDeclaringType();
                 EList<JvmTypeReference> _superTypes = _declaringType_1.getSuperTypes();
                 final JvmTypeReference superType =
                     IterableExtensions.<JvmTypeReference>head(_superTypes);
                 boolean _notEquals_9 = (!Objects.equal(superType, null));
                 if (_notEquals_9) {
                   JvmType _type = superType.getType();
                   appendable_3.declareVariable(_type, "super");
                 }
                 EList<JvmFormalParameter> _parameters_2 = jvmOperation.getParameters();
                 for (final JvmFormalParameter parameter : _parameters_2) {
                   String _name = parameter.getName();
                   appendable_3.declareVariable(parameter, _name);
                 }
                 JvmTypeReference _returnType_3 = jvmOperation.getReturnType();
                 EList<JvmTypeReference> _exceptions = jvmOperation.getExceptions();
                 HashSet<JvmTypeReference> _hashSet = new HashSet<JvmTypeReference>(_exceptions);
                 this.compiler.compile(body, appendable_3, _returnType_3, _hashSet);
                 String _string_3 = appendable_3.toString();
                 String _extractBody_3 = this.extractBody(_string_3);
                 EcoreUtil.setAnnotation(
                     eOperation, GenModelPackage.eNS_URI, "body", _extractBody_3);
               }
             }
           }
         }
       }
     }
   }
 }
 protected JvmType getRawType(JvmTypeReference reference) {
   RawTypeReferenceComputer strategy = new RawTypeReferenceComputer(TypesFactory.eINSTANCE);
   JvmTypeReference result = strategy.getRawTypeReference(reference, eResource());
   return result == null ? null : result.getType();
 }
Esempio n. 14
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("}");
  }
Esempio n. 15
0
 protected boolean identifierStartWith(JvmTypeReference typeReference, String prefix) {
   String identifier = typeReference.getType().getIdentifier();
   if (identifier != null) return identifier.startsWith(prefix);
   return false;
 }