Example #1
0
 public JvmWildcardTypeReference wildCardExtends(JvmTypeReference clone) {
   JvmWildcardTypeReference result = factory.createJvmWildcardTypeReference();
   JvmUpperBound upperBound = factory.createJvmUpperBound();
   upperBound.setTypeReference(clone);
   result.getConstraints().add(upperBound);
   return result;
 }
Example #2
0
 public JvmParameterizedTypeReference createTypeRef(JvmType type, JvmTypeReference... typeArgs) {
   List<JvmTypeReference> typeReferences = Collections.emptyList();
   if (typeArgs != null && typeArgs.length > 0) {
     typeReferences = newArrayListWithCapacity(typeArgs.length);
     for (int i = 0; i < typeArgs.length; i++) {
       JvmTypeReference jvmTypeReference = typeArgs[i];
       typeReferences.add(EcoreUtil2.clone(jvmTypeReference));
     }
   }
   if (type instanceof JvmGenericType) {
     List<JvmTypeParameter> list = ((JvmGenericType) type).getTypeParameters();
     if (!typeReferences.isEmpty() && list.size() != typeReferences.size()) {
       throw new IllegalArgumentException(
           "The type "
               + type.getIdentifier()
               + " expects "
               + list.size()
               + " type arguments, but was "
               + typeReferences.size()
               + ". Either pass zero arguments (raw type) or the correct number.");
     }
     // Raw type -> create type references to type param
     if (typeReferences.isEmpty() && !list.isEmpty()) {
       typeReferences = newArrayListWithCapacity(list.size());
       for (JvmTypeParameter typeParameter : list) {
         typeReferences.add(createTypeRef(typeParameter));
       }
     }
   }
   JvmParameterizedTypeReference reference = factory.createJvmParameterizedTypeReference();
   reference.setType(type);
   if (!typeReferences.isEmpty()) reference.getArguments().addAll(typeReferences);
   return reference;
 }
  protected JvmGenericType transform(XtendClass source, boolean prelinkingPhase) {
    JvmGenericType inferredJvmType = typesFactory.createJvmGenericType();
    source.eResource().getContents().add(inferredJvmType);
    associator.associatePrimary(source, inferredJvmType);
    inferredJvmType.setPackageName(source.getPackageName());
    inferredJvmType.setSimpleName(source.getName());
    inferredJvmType.setVisibility(JvmVisibility.PUBLIC);
    if (!prelinkingPhase) {
      JvmAnnotationType annotation =
          (JvmAnnotationType) typeReferences.findDeclaredType(SuppressWarnings.class, source);
      if (annotation != null) {
        JvmAnnotationReference suppressWarnings = typesFactory.createJvmAnnotationReference();
        suppressWarnings.setAnnotation(annotation);
        JvmStringAnnotationValue annotationValue = typesFactory.createJvmStringAnnotationValue();
        annotationValue.getValues().add("all");
        suppressWarnings.getValues().add(annotationValue);
        inferredJvmType.getAnnotations().add(suppressWarnings);
      }
      addDefaultConstructor(source, inferredJvmType);
      if (source.getExtends() == null) {
        JvmTypeReference typeRefToObject = typeReferences.getTypeForName(Object.class, source);
        if (typeRefToObject != null) inferredJvmType.getSuperTypes().add(typeRefToObject);
      } else {
        inferredJvmType.getSuperTypes().add(cloneWithProxies(source.getExtends()));
      }
      for (JvmTypeReference intf : source.getImplements()) {
        inferredJvmType.getSuperTypes().add(cloneWithProxies(intf));
      }
      copyAndFixTypeParameters(source.getTypeParameters(), inferredJvmType);
      for (XtendMember member : source.getMembers()) {
        if (member instanceof XtendField
            || (member instanceof XtendFunction && ((XtendFunction) member).getName() != null)
            || member instanceof XtendConstructor) {
          transform(member, inferredJvmType);
        }
      }
      appendSyntheticDispatchMethods(source, inferredJvmType);
      computeInferredReturnTypes(inferredJvmType);
      jvmTypesBuilder.translateAnnotationsTo(source.getAnnotations(), inferredJvmType);
      jvmTypesBuilder.setDocumentation(inferredJvmType, jvmTypesBuilder.getDocumentation(source));

      nameClashResolver.resolveNameClashes(inferredJvmType);
    }
    return inferredJvmType;
  }
 @Override
 public JvmTypeReference toTypeReference() {
   TypesFactory typesFactory = getTypesFactory();
   JvmWildcardTypeReference result = typesFactory.createJvmWildcardTypeReference();
   if (upperBounds != null && !upperBounds.isEmpty()) {
     for (LightweightTypeReference typeArgument : upperBounds) {
       JvmUpperBound constraint = typesFactory.createJvmUpperBound();
       constraint.setTypeReference(typeArgument.getWrapperTypeIfPrimitive().toTypeReference());
       result.getConstraints().add(constraint);
     }
   }
   if (lowerBound != null) {
     JvmLowerBound constraint = typesFactory.createJvmLowerBound();
     constraint.setTypeReference(lowerBound.getWrapperTypeIfPrimitive().toTypeReference());
     result.getConstraints().add(constraint);
   }
   return result;
 }
 /**
  * @return a {@link JvmOperation} with common denominator argument types of all given operations
  */
 protected JvmOperation deriveGenericDispatchOperationSignature(
     List<JvmOperation> sortedOperations, JvmGenericType target) {
   if (sortedOperations.isEmpty()) return null;
   final Iterator<JvmOperation> iterator = sortedOperations.iterator();
   JvmOperation first = iterator.next();
   JvmOperation result = typesFactory.createJvmOperation();
   target.getMembers().add(result);
   for (int i = 0; i < first.getParameters().size(); i++) {
     JvmFormalParameter parameter = typesFactory.createJvmFormalParameter();
     result.getParameters().add(parameter);
     parameter.setParameterType(getTypeProxy(parameter));
     JvmFormalParameter parameter2 = first.getParameters().get(i);
     parameter.setName(parameter2.getName());
   }
   jvmTypesBuilder.setBody(result, compileStrategies.forDispatcher(result, sortedOperations));
   JvmVisibility commonVisibility = null;
   boolean isFirst = true;
   boolean allStatic = true;
   for (JvmOperation jvmOperation : sortedOperations) {
     Iterable<XtendFunction> xtendFunctions =
         filter(associations.getSourceElements(jvmOperation), XtendFunction.class);
     for (XtendFunction func : xtendFunctions) {
       JvmVisibility xtendVisibility =
           func.eIsSet(Xtend2Package.Literals.XTEND_FUNCTION__VISIBILITY)
               ? func.getVisibility()
               : null;
       if (isFirst) {
         commonVisibility = xtendVisibility;
         isFirst = false;
       } else if (commonVisibility != xtendVisibility) {
         commonVisibility = null;
       }
       associator.associate(func, result);
       if (!func.isStatic()) allStatic = false;
     }
     for (JvmTypeReference declaredException : jvmOperation.getExceptions())
       result.getExceptions().add(cloneWithProxies(declaredException));
   }
   if (commonVisibility == null) result.setVisibility(JvmVisibility.PUBLIC);
   else result.setVisibility(commonVisibility);
   result.setStatic(allStatic);
   return result;
 }
 protected JvmTypeReference getTypeProxy(EObject pointer) {
   JvmParameterizedTypeReference typeReference =
       typesFactory.createJvmParameterizedTypeReference();
   final Resource eResource = pointer.eResource();
   String fragment = eResource.getURIFragment(pointer);
   URI uri = eResource.getURI();
   uri = uri.appendFragment(Xtend2Resource.FRAGMENT_PREFIX + fragment);
   ((InternalEObject) typeReference).eSetProxyURI(uri);
   return typeReference;
 }
 @Override
 public JvmTypeReference toJavaCompliantTypeReference(IVisibilityHelper helper) {
   TypesFactory typesFactory = getTypesFactory();
   JvmWildcardTypeReference result = typesFactory.createJvmWildcardTypeReference();
   if (upperBounds != null && !upperBounds.isEmpty()) {
     List<LightweightTypeReference> nonInterfaceTypes = getNonInterfaceTypes(upperBounds);
     JvmTypeReference upperBound =
         toJavaCompliantTypeReference(
             nonInterfaceTypes != null ? nonInterfaceTypes : upperBounds, helper);
     JvmUpperBound constraint = typesFactory.createJvmUpperBound();
     constraint.setTypeReference(upperBound);
     result.getConstraints().add(constraint);
   }
   if (lowerBound != null) {
     JvmLowerBound constraint = typesFactory.createJvmLowerBound();
     constraint.setTypeReference(lowerBound.toJavaCompliantTypeReference());
     result.getConstraints().add(constraint);
   }
   return result;
 }
Example #8
0
 public JvmMultiTypeReference createMultiTypeReference(
     EObject context, JvmTypeReference... references) {
   JvmMultiTypeReference result = factory.createJvmMultiTypeReference();
   if (references != null && references.length != 0) {
     for (JvmTypeReference reference : references) {
       result.getReferences().add(createDelegateTypeReference(reference));
     }
   }
   result.setType(findDeclaredType(Object.class, context));
   return result;
 }
 protected void transform(XtendConstructor source, JvmGenericType container) {
   JvmConstructor constructor = typesFactory.createJvmConstructor();
   container.getMembers().add(constructor);
   associator.associatePrimary(source, constructor);
   JvmVisibility visibility = source.getVisibility();
   constructor.setSimpleName(container.getSimpleName());
   constructor.setVisibility(visibility);
   for (XtendParameter parameter : source.getParameters()) {
     JvmFormalParameter jvmParam = typesFactory.createJvmFormalParameter();
     jvmParam.setName(parameter.getName());
     jvmParam.setParameterType(cloneWithProxies(parameter.getParameterType()));
     constructor.getParameters().add(jvmParam);
     associator.associate(parameter, jvmParam);
   }
   copyAndFixTypeParameters(source.getTypeParameters(), constructor);
   for (JvmTypeReference exception : source.getExceptions()) {
     constructor.getExceptions().add(cloneWithProxies(exception));
   }
   jvmTypesBuilder.translateAnnotationsTo(
       source.getAnnotationInfo().getAnnotations(), constructor);
   associator.associateLogicalContainer(source.getExpression(), constructor);
   jvmTypesBuilder.setDocumentation(constructor, jvmTypesBuilder.getDocumentation(source));
 }
 protected void transform(XtendField source, JvmGenericType container) {
   if ((source.isExtension() || source.getName() != null) && source.getType() != null) {
     JvmField field = typesFactory.createJvmField();
     field.setSimpleName(computeFieldName(source, container));
     container.getMembers().add(field);
     associator.associatePrimary(source, field);
     field.setVisibility(source.getVisibility());
     field.setStatic(source.isStatic());
     field.setType(cloneWithProxies(source.getType()));
     jvmTypesBuilder.translateAnnotationsTo(source.getAnnotationInfo().getAnnotations(), field);
     jvmTypesBuilder.setDocumentation(field, jvmTypesBuilder.getDocumentation(source));
     jvmTypesBuilder.setInitializer(field, source.getInitialValue());
   }
 }
 protected void addDefaultConstructor(XtendClass source, JvmGenericType target) {
   boolean declaredConstructor = false;
   for (XtendMember member : source.getMembers()) {
     if (member instanceof XtendConstructor) {
       declaredConstructor = true;
       break;
     }
   }
   if (!declaredConstructor) {
     JvmConstructor constructor = typesFactory.createJvmConstructor();
     target.getMembers().add(constructor);
     associator.associatePrimary(source, constructor);
     constructor.setSimpleName(source.getName());
     constructor.setVisibility(JvmVisibility.PUBLIC);
   }
 }
 protected void copyAndFixTypeParameters(
     List<JvmTypeParameter> typeParameters, JvmTypeParameterDeclarator target) {
   for (JvmTypeParameter typeParameter : typeParameters) {
     final JvmTypeParameter clonedTypeParameter = cloneWithProxies(typeParameter);
     target.getTypeParameters().add(clonedTypeParameter);
     boolean upperBoundSeen = false;
     for (JvmTypeConstraint constraint : clonedTypeParameter.getConstraints()) {
       if (constraint instanceof JvmUpperBound) {
         upperBoundSeen = true;
         break;
       }
     }
     if (!upperBoundSeen) {
       JvmUpperBound upperBound = typesFactory.createJvmUpperBound();
       upperBound.setTypeReference(typeReferences.getTypeForName(Object.class, typeParameter));
       clonedTypeParameter.getConstraints().add(upperBound);
     }
     associator.associate(typeParameter, clonedTypeParameter);
   }
 }
Example #13
0
 public JvmDelegateTypeReference createDelegateTypeReference(JvmTypeReference typeRef) {
   JvmDelegateTypeReference delegate = factory.createJvmDelegateTypeReference();
   delegate.setDelegate(typeRef);
   return delegate;
 }
  protected void transform(XtendFunction source, JvmGenericType container) {
    JvmOperation operation = typesFactory.createJvmOperation();
    container.getMembers().add(operation);
    associator.associatePrimary(source, operation);
    String sourceName = source.getName();
    JvmVisibility visibility = source.getVisibility();
    if (source.isDispatch()) {
      if (!source.eIsSet(Xtend2Package.Literals.XTEND_FUNCTION__VISIBILITY))
        visibility = JvmVisibility.PROTECTED;
      sourceName = "_" + sourceName;
    }
    operation.setSimpleName(sourceName);
    operation.setVisibility(visibility);
    operation.setStatic(source.isStatic());
    for (XtendParameter parameter : source.getParameters()) {
      JvmFormalParameter jvmParam = typesFactory.createJvmFormalParameter();
      jvmParam.setName(parameter.getName());
      jvmParam.setParameterType(cloneWithProxies(parameter.getParameterType()));
      operation.getParameters().add(jvmParam);
      associator.associate(parameter, jvmParam);
      jvmTypesBuilder.translateAnnotationsTo(parameter.getAnnotations(), jvmParam);
    }
    JvmTypeReference returnType = null;
    if (source.getReturnType() != null) {
      returnType = cloneWithProxies(source.getReturnType());
    } else {
      returnType = getTypeProxy(operation);
    }
    operation.setReturnType(returnType);
    copyAndFixTypeParameters(source.getTypeParameters(), operation);
    for (JvmTypeReference exception : source.getExceptions()) {
      operation.getExceptions().add(cloneWithProxies(exception));
    }
    jvmTypesBuilder.translateAnnotationsTo(source.getAnnotationInfo().getAnnotations(), operation);
    CreateExtensionInfo createExtensionInfo = source.getCreateExtensionInfo();
    if (createExtensionInfo != null) {
      JvmTypeReference arrayList =
          typeReferences.getTypeForName(ArrayList.class, container, typeReferences.wildCard());
      JvmTypeReference hashMap =
          typeReferences.getTypeForName(
              HashMap.class, container, arrayList, cloneWithProxies(returnType));

      JvmField cacheVar =
          jvmTypesBuilder.toField(
              source, CREATE_CHACHE_VARIABLE_PREFIX + source.getName(), hashMap);
      cacheVar.setFinal(true);
      jvmTypesBuilder.setInitializer(cacheVar, compileStrategies.forCacheVariable(container));
      container.getMembers().add(cacheVar);

      JvmOperation initializer = typesFactory.createJvmOperation();
      container.getMembers().add(initializer);
      initializer.setSimpleName(CREATE_INITIALIZER_PREFIX + source.getName());
      initializer.setVisibility(JvmVisibility.PRIVATE);
      initializer.setReturnType(typeReferences.getTypeForName(Void.TYPE, source));
      for (JvmTypeReference exception : source.getExceptions()) {
        initializer.getExceptions().add(cloneWithProxies(exception));
      }

      jvmTypesBuilder.setBody(
          operation, compileStrategies.forCacheMethod(createExtensionInfo, cacheVar, initializer));

      // the first parameter is the created object
      JvmFormalParameter jvmParam = typesFactory.createJvmFormalParameter();
      jvmParam.setName(createExtensionInfo.getName());
      jvmParam.setParameterType(getTypeProxy(createExtensionInfo.getCreateExpression()));
      initializer.getParameters().add(jvmParam);
      associator.associate(createExtensionInfo, jvmParam);

      // add all others
      for (XtendParameter parameter : source.getParameters()) {
        jvmParam = typesFactory.createJvmFormalParameter();
        jvmParam.setName(parameter.getName());
        jvmParam.setParameterType(cloneWithProxies(parameter.getParameterType()));
        initializer.getParameters().add(jvmParam);
        associator.associate(parameter, jvmParam);
      }
      associator.associate(source, initializer);
      associator.associateLogicalContainer(createExtensionInfo.getCreateExpression(), operation);
      associator.associateLogicalContainer(source.getExpression(), initializer);
    } else {
      associator.associateLogicalContainer(source.getExpression(), operation);
    }
    jvmTypesBuilder.setDocumentation(operation, jvmTypesBuilder.getDocumentation(source));
  }
Example #15
0
 public JvmAnyTypeReference createAnyTypeReference(EObject context) {
   JvmAnyTypeReference result = factory.createJvmAnyTypeReference();
   result.setType(findDeclaredType(Object.class, context));
   return result;
 }
Example #16
0
 public JvmGenericArrayTypeReference createArrayType(JvmTypeReference componentType) {
   JvmGenericArrayTypeReference result = factory.createJvmGenericArrayTypeReference();
   result.setComponentType(EcoreUtil2.cloneIfContained(componentType));
   return result;
 }
Example #17
0
 public JvmWildcardTypeReference wildCard() {
   JvmWildcardTypeReference result = factory.createJvmWildcardTypeReference();
   return result;
 }
 @Override
 public boolean createEObjectDescriptions(
     EObject eObject, IAcceptor<IEObjectDescription> acceptor) {
   if (eObject instanceof EClass) {
     QualifiedName name = nameProvider.getFullyQualifiedName(eObject);
     if (name != null) {
       URI uri = eObject.eResource().getURI();
       EClass eClass = ecoreFactory.createEClass();
       proxyTool.installProxyURI(uri, eClass, name);
       acceptor.accept(EObjectDescription.create(name, eClass));
     }
     return false;
   } else if (eObject instanceof GenClass) {
     QualifiedName name = nameProvider.getFullyQualifiedName(eObject);
     if (name != null) {
       URI uri = eObject.eResource().getURI();
       GenClass genClass = genFactory.createGenClass();
       proxyTool.installProxyURI(uri, genClass, name);
       acceptor.accept(EObjectDescription.create(name, genClass));
     }
     return false;
   } else if (eObject instanceof GenDataType) {
     QualifiedName name = nameProvider.getFullyQualifiedName(eObject);
     if (name != null) {
       URI uri = eObject.eResource().getURI();
       GenDataType genDataType =
           eObject instanceof GenEnum
               ? genFactory.createGenEnum()
               : genFactory.createGenDataType();
       proxyTool.installProxyURI(uri, genDataType, name);
       acceptor.accept(EObjectDescription.create(name, genDataType));
     }
     return false;
   } else if (eObject instanceof JvmGenericType) {
     QualifiedName name = nameProvider.getFullyQualifiedName(eObject);
     if (name != null) {
       URI uri = eObject.eResource().getURI();
       JvmGenericType jvmGenericType = typesFactory.createJvmGenericType();
       proxyTool.installProxyURI(uri, jvmGenericType, name);
       acceptor.accept(EObjectDescription.create(name, jvmGenericType));
     }
     return false;
   } else if (eObject instanceof JvmEnumerationType) {
     QualifiedName name = nameProvider.getFullyQualifiedName(eObject);
     if (name != null) {
       URI uri = eObject.eResource().getURI();
       JvmEnumerationType jvmEnumerationType = typesFactory.createJvmEnumerationType();
       proxyTool.installProxyURI(uri, jvmEnumerationType, name);
       acceptor.accept(EObjectDescription.create(name, jvmEnumerationType));
     }
     return false;
   } else if (eObject instanceof XAnnotationDirective) {
     QualifiedName name = nameProvider.getFullyQualifiedName(eObject);
     if (name != null) {
       acceptor.accept(EObjectDescription.create(name, eObject));
     }
     return false;
   } else if (eObject instanceof EPackage) {
     QualifiedName name = nameProvider.getFullyQualifiedName(eObject);
     if (name != null) {
       URI uri = eObject.eResource().getURI();
       EPackage ePackage = ecoreFactory.createEPackage();
       proxyTool.installProxyURI(uri, ePackage, name);
       acceptor.accept(
           EObjectDescription.create(name, ePackage, Collections.singletonMap("nsURI", "true")));
     }
     return true;
   } else if (eObject instanceof XPackage
       || eObject instanceof GenModel
       || eObject instanceof GenPackage) {
     return true;
   } else {
     return false;
   }
 }