示例#1
0
 private Class<?>[] getParamTypes(JvmExecutable exe) {
   List<JvmFormalParameter> parameters = exe.getParameters();
   List<Class<?>> result = Lists.newArrayList();
   for (JvmFormalParameter p : parameters) {
     result.add(getRawType(p.getParameterType().getType()));
   }
   return result.toArray(new Class<?>[result.size()]);
 }
示例#2
0
 protected IScope createLocalVarScopeForJvmOperation(JvmOperation context, IScope parentScope) {
   EList<JvmFormalParameter> parameters = context.getParameters();
   List<LocalVarDescription> descriptions = newArrayList();
   for (JvmFormalParameter p : parameters) {
     if (p.getName() != null)
       descriptions.add(new LocalVarDescription(QualifiedName.create(p.getName()), p));
   }
   return new JvmFeatureScope(parentScope, "operation " + context.getSimpleName(), descriptions);
 }
示例#3
0
 protected IScope createLocalVarScopeForClosure(XClosure closure, IScope parentScope) {
   List<IValidatedEObjectDescription> descriptions = Lists.newArrayList();
   EList<JvmFormalParameter> params = closure.getFormalParameters();
   for (JvmFormalParameter p : params) {
     if (p.getName() != null) {
       IValidatedEObjectDescription desc = createLocalVarDescription(p);
       descriptions.add(desc);
     }
   }
   return new JvmFeatureScope(parentScope, "XClosure", descriptions);
 }
 protected SignatureHashBuilder appendSignature(JvmConstructor operation) {
   appendVisibility(operation.getVisibility()).appendTypeParameters(operation).append("(");
   for (JvmFormalParameter p : operation.getParameters()) {
     appendType(p.getParameterType()).append(" ");
   }
   append(") ");
   for (JvmTypeReference ex : operation.getExceptions()) {
     appendType(ex).append(" ");
   }
   return this;
 }
 protected String _case(
     JvmOperation input,
     XUnaryOperation context,
     EReference reference,
     JvmFeatureDescription jvmFeatureDescription) {
   if (input.getParameters().size() != 1) return INVALID_NUMBER_OF_ARGUMENTS;
   if (context.getOperand() != null) {
     JvmTypeReference operandType = getTypeProvider().getType(context.getOperand(), true);
     final JvmFormalParameter param = input.getParameters().get(0);
     if (!conformance.isConformant(param.getParameterType(), operandType, true))
       return INVALID_ARGUMENT_TYPES;
   }
   return null;
 }
 protected String _case(
     JvmOperation input,
     XAssignment context,
     EReference ref,
     JvmFeatureDescription jvmFeatureDescription) {
   final int irrelevantArguments = jvmFeatureDescription.getNumberOfIrrelevantArguments();
   if (input.getParameters().size() != (1 + irrelevantArguments))
     return INVALID_NUMBER_OF_ARGUMENTS;
   if (context.getValue() != null) {
     JvmTypeReference type = getTypeProvider().getType(context.getValue(), true);
     final JvmFormalParameter valueParam = input.getParameters().get(0 + irrelevantArguments);
     if (!isCompatibleArgument(valueParam.getParameterType(), type)) return INVALID_ARGUMENT_TYPES;
   }
   return null;
 }
 protected void doTestEnum_05(String paramName) throws Exception {
   String typeName = TestEnum.class.getName();
   JvmEnumerationType type = (JvmEnumerationType) getTypeProvider().findTypeByName(typeName);
   boolean constructorSeen = false;
   for (JvmMember member : type.getMembers()) {
     if (member instanceof JvmConstructor) {
       constructorSeen = true;
       List<JvmFormalParameter> parameters = ((JvmConstructor) member).getParameters();
       assertEquals(1, parameters.size());
       JvmFormalParameter singleParam = parameters.get(0);
       assertEquals(paramName, singleParam.getName());
     }
   }
   assertTrue("constructorSeen", constructorSeen);
 }
 protected String _case(
     JvmOperation input,
     XBinaryOperation context,
     EReference ref,
     JvmFeatureDescription jvmFeatureDescription) {
   final int irrelevantArguments = jvmFeatureDescription.getNumberOfIrrelevantArguments();
   if (input.getParameters().size() - irrelevantArguments != 1) return INVALID_NUMBER_OF_ARGUMENTS;
   if (context.getRightOperand() != null && context.getLeftOperand() != null) {
     JvmTypeReference rightOperandType =
         getTypeProvider().getType(context.getRightOperand(), true);
     if (rightOperandType == null) return INVALID_ARGUMENT_TYPES;
     final JvmFormalParameter rightParam = input.getParameters().get(0 + irrelevantArguments);
     if (!conformance.isConformant(rightParam.getParameterType(), rightOperandType, true))
       return INVALID_ARGUMENT_TYPES;
   }
   return null;
 }
 /**
  * @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 SignatureHashBuilder appendSignature(JvmOperation operation) {
   appendVisibility(operation.getVisibility()).append(" ");
   if (operation.isAbstract()) append("abstract ");
   if (operation.isStatic()) append("static ");
   if (operation.isFinal()) append("final ");
   appendType(operation.getReturnType())
       .appendTypeParameters(operation)
       .append(" ")
       .append(operation.getSimpleName())
       .append("(");
   for (JvmFormalParameter p : operation.getParameters()) {
     appendType(p.getParameterType());
     append(" ");
   }
   append(") ");
   for (JvmTypeReference ex : operation.getExceptions()) {
     appendType(ex).append(" ");
   }
   return this;
 }
示例#11
0
 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 appendParameters(
     final StringBuilder result,
     final JvmExecutable executable,
     final int insignificantParameters,
     final LightweightTypeReferenceFactory ownedConverter) {
   final EList<JvmFormalParameter> declaredParameters = executable.getParameters();
   int _size = declaredParameters.size();
   int _min = Math.min(insignificantParameters, _size);
   int _size_1 = declaredParameters.size();
   final List<JvmFormalParameter> relevantParameters = declaredParameters.subList(_min, _size_1);
   for (int i = 0; (i < relevantParameters.size()); i++) {
     {
       final JvmFormalParameter parameter = relevantParameters.get(i);
       if ((i != 0)) {
         result.append(", ");
       }
       boolean _and = false;
       boolean _and_1 = false;
       int _size_2 = relevantParameters.size();
       int _minus = (_size_2 - 1);
       boolean _equals = (i == _minus);
       if (!_equals) {
         _and_1 = false;
       } else {
         boolean _isVarArgs = executable.isVarArgs();
         _and_1 = _isVarArgs;
       }
       if (!_and_1) {
         _and = false;
       } else {
         JvmTypeReference _parameterType = parameter.getParameterType();
         _and = (_parameterType instanceof JvmGenericArrayTypeReference);
       }
       if (_and) {
         JvmTypeReference _parameterType_1 = parameter.getParameterType();
         final JvmGenericArrayTypeReference parameterType =
             ((JvmGenericArrayTypeReference) _parameterType_1);
         JvmTypeReference _componentType = parameterType.getComponentType();
         LightweightTypeReference _lightweightReference =
             ownedConverter.toLightweightReference(_componentType);
         String _humanReadableName = _lightweightReference.getHumanReadableName();
         result.append(_humanReadableName);
         result.append("...");
       } else {
         JvmTypeReference _parameterType_2 = parameter.getParameterType();
         boolean _notEquals = (!Objects.equal(_parameterType_2, null));
         if (_notEquals) {
           JvmTypeReference _parameterType_3 = parameter.getParameterType();
           LightweightTypeReference _lightweightReference_1 =
               ownedConverter.toLightweightReference(_parameterType_3);
           final String simpleName = _lightweightReference_1.getHumanReadableName();
           boolean _notEquals_1 = (!Objects.equal(simpleName, null));
           if (_notEquals_1) {
             result.append(simpleName);
           }
         }
       }
       result.append(" ");
       String _name = parameter.getName();
       String _valueOf = String.valueOf(_name);
       result.append(_valueOf);
     }
   }
 }
 protected void _infer(
     final Model model, final IJvmDeclaredTypeAcceptor acceptor, final boolean isPrelinkingPhase) {
   Resource _eResource = model.eResource();
   URI _uRI = _eResource.getURI();
   URI _trimFileExtension = _uRI.trimFileExtension();
   final String postfix = _trimFileExtension.lastSegment();
   JvmGenericType _class =
       this._jvmTypesBuilder.toClass(model, (IModelQueryConstants.INFERRED_CLASS_NAME + postfix));
   final Procedure1<JvmGenericType> _function =
       (JvmGenericType it) -> {
         EList<JvmMember> _members = it.getMembers();
         JvmTypeReference _typeRef =
             this._typeReferenceBuilder.typeRef(IResourceDescriptions.class);
         JvmField _field =
             this._jvmTypesBuilder.toField(model, IModelQueryConstants.INDEX, _typeRef);
         this._jvmTypesBuilder.<JvmField>operator_add(_members, _field);
         EList<JvmMember> _members_1 = it.getMembers();
         JvmTypeReference _typeRef_1 = this._typeReferenceBuilder.typeRef(IProject.class);
         JvmField _field_1 =
             this._jvmTypesBuilder.toField(model, IModelQueryConstants.PROJECT, _typeRef_1);
         this._jvmTypesBuilder.<JvmField>operator_add(_members_1, _field_1);
         EList<JvmMember> _members_2 = it.getMembers();
         JvmTypeReference _typeRef_2 = this._typeReferenceBuilder.typeRef(ResourceSet.class);
         JvmField _field_2 =
             this._jvmTypesBuilder.toField(model, IModelQueryConstants.RESOURCESET, _typeRef_2);
         this._jvmTypesBuilder.<JvmField>operator_add(_members_2, _field_2);
         EList<JvmMember> _members_3 = it.getMembers();
         JvmTypeReference _typeRef_3 = this._typeReferenceBuilder.typeRef(Injector.class);
         JvmField _field_3 =
             this._jvmTypesBuilder.toField(model, IModelQueryConstants.INJECTOR, _typeRef_3);
         this._jvmTypesBuilder.<JvmField>operator_add(_members_3, _field_3);
         EList<JvmMember> _members_4 = it.getMembers();
         JvmTypeReference _typeRef_4 = this._typeReferenceBuilder.typeRef(Void.TYPE);
         final Procedure1<JvmOperation> _function_1 =
             (JvmOperation it_1) -> {
               XBlockExpression _body = model.getBody();
               this._jvmTypesBuilder.setBody(it_1, _body);
               EList<JvmTypeReference> _exceptions = it_1.getExceptions();
               JvmTypeReference _typeRef_5 = this._typeReferenceBuilder.typeRef(Exception.class);
               this._jvmTypesBuilder.<JvmTypeReference>operator_add(_exceptions, _typeRef_5);
             };
         JvmOperation _method =
             this._jvmTypesBuilder.toMethod(model, "main", _typeRef_4, _function_1);
         this._jvmTypesBuilder.<JvmOperation>operator_add(_members_4, _method);
         EList<XMethodDeclaration> _methods = model.getMethods();
         for (final XMethodDeclaration op : _methods) {
           EList<JvmMember> _members_5 = it.getMembers();
           String _name = op.getName();
           JvmTypeReference _elvis = null;
           JvmTypeReference _type = op.getType();
           if (_type != null) {
             _elvis = _type;
           } else {
             JvmTypeReference _inferredType = this._jvmTypesBuilder.inferredType();
             _elvis = _inferredType;
           }
           final Procedure1<JvmOperation> _function_2 =
               (JvmOperation it_1) -> {
                 EList<JvmFormalParameter> _parameters = op.getParameters();
                 for (final JvmFormalParameter p : _parameters) {
                   EList<JvmFormalParameter> _parameters_1 = it_1.getParameters();
                   String _name_1 = p.getName();
                   JvmTypeReference _parameterType = p.getParameterType();
                   JvmFormalParameter _parameter =
                       this._jvmTypesBuilder.toParameter(p, _name_1, _parameterType);
                   this._jvmTypesBuilder.<JvmFormalParameter>operator_add(
                       _parameters_1, _parameter);
                 }
                 EList<JvmTypeParameter> _typeParameters = op.getTypeParameters();
                 this.copyAndFixTypeParameters(_typeParameters, it_1);
                 XExpression _body = op.getBody();
                 this._jvmTypesBuilder.setBody(it_1, _body);
               };
           JvmOperation _method_1 = this._jvmTypesBuilder.toMethod(op, _name, _elvis, _function_2);
           this._jvmTypesBuilder.<JvmOperation>operator_add(_members_5, _method_1);
         }
       };
   acceptor.<JvmGenericType>accept(_class, _function);
 }
示例#14
0
 protected IScope createLocalScopeForParameter(JvmFormalParameter p, IScope parentScope) {
   return (p.getName() != null)
       ? new JvmFeatureScope(parentScope, "JvmFormalParameter", createLocalVarDescription(p))
       : parentScope;
 }
示例#15
0
 protected IValidatedEObjectDescription createLocalVarDescription(JvmFormalParameter p) {
   return new LocalVarDescription(QualifiedName.create(p.getName()), p);
 }
示例#16
0
  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));
  }
示例#17
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 XbaseIdeCrossrefProposalProvider.ProposalBracketInfo getProposalBracketInfo(
     final IEObjectDescription proposedDescription,
     final ContentAssistContext contentAssistContext) {
   final XbaseIdeCrossrefProposalProvider.ProposalBracketInfo info =
       new XbaseIdeCrossrefProposalProvider.ProposalBracketInfo();
   if ((proposedDescription instanceof IIdentifiableElementDescription)) {
     final JvmIdentifiableElement jvmFeature =
         ((IIdentifiableElementDescription) proposedDescription).getElementOrProxy();
     if ((jvmFeature instanceof JvmExecutable)) {
       final EList<JvmFormalParameter> parameters = ((JvmExecutable) jvmFeature).getParameters();
       int _numberOfParameters =
           ((IIdentifiableElementDescription) proposedDescription).getNumberOfParameters();
       boolean _equals = (_numberOfParameters == 1);
       if (_equals) {
         boolean _and = false;
         String _simpleName = ((JvmExecutable) jvmFeature).getSimpleName();
         boolean _startsWith = _simpleName.startsWith("set");
         if (!_startsWith) {
           _and = false;
         } else {
           QualifiedName _name = ((IIdentifiableElementDescription) proposedDescription).getName();
           String _firstSegment = _name.getFirstSegment();
           boolean _startsWith_1 = _firstSegment.startsWith("set");
           boolean _not = (!_startsWith_1);
           _and = _not;
         }
         if (_and) {
           info.brackets = " = value";
           int _length = "value".length();
           int _minus = (-_length);
           info.selectionOffset = _minus;
           int _length_1 = "value".length();
           info.selectionLength = _length_1;
           return info;
         }
         JvmFormalParameter _last = IterableExtensions.<JvmFormalParameter>last(parameters);
         final JvmTypeReference parameterType = _last.getParameterType();
         XtextResource _resource = contentAssistContext.getResource();
         LightweightTypeReferenceFactory _typeConverter = this.getTypeConverter(_resource);
         final LightweightTypeReference light =
             _typeConverter.toLightweightReference(parameterType);
         boolean _isFunctionType = light.isFunctionType();
         if (_isFunctionType) {
           FunctionTypeReference _asFunctionTypeReference = light.getAsFunctionTypeReference();
           List<LightweightTypeReference> _parameterTypes =
               _asFunctionTypeReference.getParameterTypes();
           final int numParameters = _parameterTypes.size();
           if ((numParameters == 1)) {
             info.brackets = "[]";
             info.caretOffset = (-1);
             return info;
           } else {
             if ((numParameters == 0)) {
               info.brackets = "[|]";
               info.caretOffset = (-1);
               return info;
             } else {
               final StringBuilder b = new StringBuilder();
               for (int i = 0; (i < numParameters); i++) {
                 {
                   if ((i != 0)) {
                     b.append(", ");
                   }
                   b.append(("p" + Integer.valueOf((i + 1))));
                 }
               }
               String _string = b.toString();
               String _plus = ("[" + _string);
               String _plus_1 = (_plus + "|]");
               info.brackets = _plus_1;
               info.caretOffset = (-1);
               int _length_2 = b.length();
               int _minus_1 = (-_length_2);
               int _minus_2 = (_minus_1 - 2);
               info.selectionOffset = _minus_2;
               int _length_3 = b.length();
               info.selectionLength = _length_3;
               return info;
             }
           }
         }
       }
     }
     boolean _isExplicitOperationCall =
         this.isExplicitOperationCall(((IIdentifiableElementDescription) proposedDescription));
     if (_isExplicitOperationCall) {
       info.brackets = "()";
       info.selectionOffset = (-1);
     }
   }
   return info;
 }
  public CharSequence apply(ImportManager importManager) {
    JvmOperation cacheMethod =
        (JvmOperation)
            logicalContainerProvider.getLogicalContainer(createExtensionInfo.getCreateExpression());
    StringBuilderBasedAppendable appendable = new StringBuilderBasedAppendable(importManager);
    JvmDeclaredType containerType = cacheMethod.getDeclaringType();
    JvmTypeReference listType = typeReferences.getTypeForName(ArrayList.class, containerType);
    JvmTypeReference collectonLiterals =
        typeReferences.getTypeForName(CollectionLiterals.class, containerType);
    String cacheVarName = cacheField.getSimpleName();
    String cacheKeyVarName = appendable.declareVariable("CacheKey", "_cacheKey");
    appendable.append("final ");
    typeReferenceSerializer.serialize(listType, containerType, appendable);
    appendable.append(cacheKeyVarName).append(" = ");
    typeReferenceSerializer.serialize(collectonLiterals, containerType, appendable);
    appendable.append(".newArrayList(");
    EList<JvmFormalParameter> list = cacheMethod.getParameters();
    for (Iterator<JvmFormalParameter> iterator = list.iterator(); iterator.hasNext(); ) {
      JvmFormalParameter jvmFormalParameter = iterator.next();
      appendable.append(getVarName(jvmFormalParameter));
      if (iterator.hasNext()) {
        appendable.append(", ");
      }
    }
    appendable.append(");");
    // declare result variable
    JvmTypeReference returnType = typeProvider.getType(createExtensionInfo.getCreateExpression());
    appendable.append("\nfinal ");
    typeReferenceSerializer.serialize(returnType, containerType, appendable);
    String resultVarName = "_result";
    appendable.append(" ").append(resultVarName).append(";");
    // open synchronize block
    appendable.append("\nsynchronized (").append(cacheVarName).append(") {");
    appendable.increaseIndentation();
    // if the cache contains the key return the previously created object.
    appendable
        .append("\nif (")
        .append(cacheVarName)
        .append(".containsKey(")
        .append(cacheKeyVarName)
        .append(")) {");
    appendable.increaseIndentation();
    appendable
        .append("\nreturn ")
        .append(cacheVarName)
        .append(".get(")
        .append(cacheKeyVarName)
        .append(");");
    appendable.decreaseIndentation().append("\n}");
    // execute the creation
    compiler.toJavaStatement(createExtensionInfo.getCreateExpression(), appendable, true);
    appendable.append("\n");
    appendable.append(resultVarName).append(" = ");
    compiler.toJavaExpression(createExtensionInfo.getCreateExpression(), appendable);
    appendable.append(";");

    // store the newly created object in the cache
    appendable
        .append("\n")
        .append(cacheVarName)
        .append(".put(")
        .append(cacheKeyVarName)
        .append(", ")
        .append(resultVarName)
        .append(");");

    // close synchronize block
    appendable.decreaseIndentation();
    appendable.append("\n}");
    appendable
        .append("\n")
        .append(initializerMethod.getSimpleName())
        .append("(")
        .append(resultVarName);
    for (JvmFormalParameter parameter : cacheMethod.getParameters()) {
      appendable.append(", ").append(parameter.getName());
    }
    appendable.append(");");
    // return the result
    appendable.append("\nreturn ");
    appendable.append(resultVarName).append(";");
    return appendable.toString();
  }
示例#20
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("}");
  }