Exemplo n.º 1
0
 private void checkValOnAnnotationParameter(JetClass aClass) {
   for (JetParameter parameter : aClass.getPrimaryConstructorParameters()) {
     if (!parameter.hasValOrVar()) {
       trace.report(MISSING_VAL_ON_ANNOTATION_PARAMETER.on(parameter));
     }
   }
 }
 private List<PropertyDescriptor> getDataProperties() {
   List<PropertyDescriptor> result = Lists.newArrayList();
   for (JetParameter parameter : getPrimaryConstructorParameters()) {
     if (parameter.hasValOrVar()) {
       result.add(bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter));
     }
   }
   return result;
 }
  // Source code is taken from org.jetbrains.kotlin.idea.projectView.JetDeclarationTreeNode,
  // updateImple()
  private String getPresentableElement(JetElement declaration) {
    String text = "";
    if (declaration != null) {
      text = declaration.getName();
      if (text == null) return "";
      if (declaration instanceof JetClassInitializer) {
        text = CLASS_INITIALIZER;
      } else if (declaration instanceof JetProperty) {
        JetProperty property = (JetProperty) declaration;
        JetTypeReference ref = property.getTypeReference();
        if (ref != null) {
          text += " ";
          text += ":";
          text += " ";
          text += ref.getText();
        }
      } else if (declaration instanceof JetFunction) {
        JetFunction function = (JetFunction) declaration;
        JetTypeReference receiverTypeRef = function.getReceiverTypeReference();
        if (receiverTypeRef != null) {
          text = receiverTypeRef.getText() + "." + text;
        }
        text += "(";
        List<JetParameter> parameters = function.getValueParameters();
        for (JetParameter parameter : parameters) {
          if (parameter.getName() != null) {
            text += parameter.getName();
            text += " ";
            text += ":";
            text += " ";
          }
          JetTypeReference typeReference = parameter.getTypeReference();
          if (typeReference != null) {
            text += typeReference.getText();
          }
          text += ", ";
        }
        if (parameters.size() > 0) text = text.substring(0, text.length() - 2);
        text += ")";
        JetTypeReference typeReference = function.getTypeReference();
        if (typeReference != null) {
          text += " ";
          text += ":";
          text += " ";
          text += typeReference.getText();
        }
      }
    }

    return text;
  }
  private void changeParameter(
      int parameterIndex, JetParameter parameter, JetParameterInfo parameterInfo) {
    ASTNode valOrVarAstNode = parameter.getValOrVarNode();
    PsiElement valOrVarNode = valOrVarAstNode != null ? valOrVarAstNode.getPsi() : null;
    JetValVar valOrVar = parameterInfo.getValOrVar();

    JetPsiFactory psiFactory = JetPsiFactory(getProject());
    if (valOrVarNode != null) {
      if (valOrVar == JetValVar.None) {
        valOrVarNode.delete();
      } else {
        valOrVarNode.replace(psiFactory.createValOrVarNode(valOrVar.toString()).getPsi());
      }
    } else if (valOrVar != JetValVar.None) {
      PsiElement firstChild = parameter.getFirstChild();
      parameter.addBefore(psiFactory.createValOrVarNode(valOrVar.toString()).getPsi(), firstChild);
      parameter.addBefore(psiFactory.createWhiteSpace(), firstChild);
    }

    if (parameterInfo.getIsTypeChanged() && parameter.getTypeReference() != null) {
      String renderedType = parameterInfo.renderType(parameterIndex, this);
      parameter.setTypeReference(psiFactory.createType(renderedType));
    }

    PsiElement identifier = parameter.getNameIdentifier();

    if (identifier != null) {
      //noinspection unchecked
      String newName =
          parameterInfo.getInheritedName((JetFunctionDefinitionUsage<PsiElement>) this);
      identifier.replace(psiFactory.createIdentifier(newName));
    }
  }
Exemplo n.º 5
0
 public void checkParameterHasNoValOrVar(
     @NotNull JetParameter parameter,
     @NotNull DiagnosticFactory1<PsiElement, JetKeywordToken> diagnosticFactory) {
   PsiElement valOrVar = parameter.getValOrVarKeyword();
   if (valOrVar != null) {
     trace.report(
         diagnosticFactory.on(
             valOrVar, ((JetKeywordToken) valOrVar.getNode().getElementType())));
   }
 }
Exemplo n.º 6
0
  public void resolveFunctionBody(
      @NotNull DataFlowInfo outerDataFlowInfo,
      @NotNull BindingTrace trace,
      @NotNull JetDeclarationWithBody function,
      @NotNull FunctionDescriptor functionDescriptor,
      @NotNull LexicalScope scope,
      @Nullable Function1<LexicalScope, DataFlowInfo> beforeBlockBody,
      @NotNull CallChecker callChecker) {
    LexicalScope innerScope =
        FunctionDescriptorUtil.getFunctionInnerScope(scope, functionDescriptor, trace);
    List<JetParameter> valueParameters = function.getValueParameters();
    List<ValueParameterDescriptor> valueParameterDescriptors =
        functionDescriptor.getValueParameters();

    valueParameterResolver.resolveValueParameters(
        valueParameters,
        valueParameterDescriptors,
        ExpressionTypingContext.newContext(
            trace, innerScope, outerDataFlowInfo, NO_EXPECTED_TYPE, callChecker));

    // Synthetic "field" creation
    if (functionDescriptor instanceof PropertyAccessorDescriptor) {
      PropertyAccessorDescriptor accessorDescriptor =
          (PropertyAccessorDescriptor) functionDescriptor;
      JetProperty property = (JetProperty) function.getParent();
      final SyntheticFieldDescriptor fieldDescriptor =
          new SyntheticFieldDescriptor(accessorDescriptor, property);
      innerScope =
          new LexicalScopeImpl(
              innerScope,
              functionDescriptor,
              true,
              functionDescriptor.getExtensionReceiverParameter(),
              "Accessor inner scope with synthetic field",
              RedeclarationHandler.DO_NOTHING,
              new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {
                @Override
                public Unit invoke(LexicalScopeImpl.InitializeHandler handler) {
                  handler.addVariableOrClassDescriptor(fieldDescriptor);
                  return Unit.INSTANCE$;
                }
              });
      // Check parameter name shadowing
      for (JetParameter parameter : function.getValueParameters()) {
        if (SyntheticFieldDescriptor.NAME.equals(parameter.getNameAsName())) {
          trace.report(Errors.ACCESSOR_PARAMETER_NAME_SHADOWING.on(parameter));
        }
      }
    }

    DataFlowInfo dataFlowInfo = null;

    if (beforeBlockBody != null) {
      dataFlowInfo = beforeBlockBody.invoke(innerScope);
    }

    if (function.hasBody()) {
      expressionTypingServices.checkFunctionReturnType(
          innerScope,
          function,
          functionDescriptor,
          dataFlowInfo != null ? dataFlowInfo : outerDataFlowInfo,
          null,
          trace);
    }

    assert functionDescriptor.getReturnType() != null;
  }