Example #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()]);
 }
 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 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;
 }
 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;
 }
 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);
 }
 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 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;
 }
  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("}");
  }