コード例 #1
0
    @Override
    public void visitBinaryExpression(GrBinaryExpression expression) {
      final IElementType type = expression.getOperationTokenType();
      final GrExpression left = expression.getLeftOperand();
      final GrExpression right = expression.getRightOperand();

      if (type == mREGEX_FIND || type == mREGEX_MATCH) {
        final PsiClassType string =
            TypesUtil.createType(CommonClassNames.JAVA_LANG_STRING, expression);
        myResult = createSimpleSubTypeResult(string);
        return;
      }

      final GrExpression other = myExpression == left ? right : left;
      final PsiType otherType = other != null ? other.getType() : null;

      if (otherType == null) return;

      if (type == mPLUS && otherType.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
        final PsiClassType obj = TypesUtil.getJavaLangObject(expression);
        myResult = createSimpleSubTypeResult(obj);
        return;
      }

      myResult = createSimpleSubTypeResult(otherType);
    }
コード例 #2
0
 private static boolean isAcceptableAnnotationValueType(PsiType type) {
   //noinspection ConstantConditions
   return type instanceof PsiPrimitiveType
       || type.equalsToText(CommonClassNames.JAVA_LANG_STRING)
       || type.equalsToText(CommonClassNames.JAVA_LANG_CLASS)
       || type instanceof PsiClassType
           && ((PsiClassType) type).resolve() != null
           && ((PsiClassType) type).resolve().isEnum();
 }
コード例 #3
0
 @Nullable
 private ExceptionInfo findCatch(PsiType thrownType) {
   final Iterator<ExceptionInfo> iterator = myCaughtExceptionInfos.descendingIterator();
   while (iterator.hasNext()) {
     final ExceptionInfo info = iterator.next();
     final GrCatchClause clause = info.myClause;
     final GrParameter parameter = clause.getParameter();
     if (parameter != null) {
       final PsiType type = parameter.getType();
       if (type.isAssignableFrom(thrownType)) return info;
     }
   }
   return null;
 }
  private static GrParameter createNewParameter(
      @NotNull PsiSubstitutor substitutor,
      @NotNull PsiMethod context,
      @NotNull GrParameterList parameterList,
      @NotNull JavaParameterInfo newParameter,
      @NotNull String newName) {
    GroovyPsiElementFactory factory =
        GroovyPsiElementFactory.getInstance(parameterList.getProject());
    String typeText = newParameter.getTypeText();
    if (newParameter instanceof GrParameterInfo && (typeText.isEmpty() || "def".equals(typeText))) {
      return factory.createParameter(newName, null, getInitializer(newParameter), parameterList);
    }

    PsiType type =
        substitutor.substitute(newParameter.createType(context, parameterList.getManager()));
    return factory.createParameter(
        newName,
        type == null ? null : type.getCanonicalText(),
        getInitializer(newParameter),
        parameterList);
  }
コード例 #5
0
 @Override
 public void visitThrowStatement(GrThrowStatement throwStatement) {
   final PsiClassType throwable =
       PsiType.getJavaLangThrowable(myExpression.getManager(), throwStatement.getResolveScope());
   myResult = new TypeConstraint[] {SubtypeConstraint.create(throwable)};
 }
  @Nullable
  private static GrExpression createDefaultValue(
      GroovyPsiElementFactory factory,
      JavaChangeInfo changeInfo,
      JavaParameterInfo info,
      final GrArgumentList list) {
    if (info.isUseAnySingleVariable()) {
      final PsiResolveHelper resolveHelper =
          JavaPsiFacade.getInstance(list.getProject()).getResolveHelper();
      final PsiType type = info.getTypeWrapper().getType(changeInfo.getMethod(), list.getManager());
      final VariablesProcessor processor =
          new VariablesProcessor(false) {
            @Override
            protected boolean check(PsiVariable var, ResolveState state) {
              if (var instanceof PsiField
                  && !resolveHelper.isAccessible((PsiField) var, list, null)) return false;
              if (var instanceof GrVariable
                  && PsiUtil.isLocalVariable(var)
                  && list.getTextRange().getStartOffset() <= var.getTextRange().getStartOffset()) {
                return false;
              }
              if (PsiTreeUtil.isAncestor(var, list, false)) return false;
              final PsiType _type =
                  var instanceof GrVariable ? ((GrVariable) var).getTypeGroovy() : var.getType();
              final PsiType varType = state.get(PsiSubstitutor.KEY).substitute(_type);
              return type.isAssignableFrom(varType);
            }

            @Override
            public boolean execute(@NotNull PsiElement pe, @NotNull ResolveState state) {
              super.execute(pe, state);
              return size() < 2;
            }
          };
      ResolveUtil.treeWalkUp(list, processor, false);
      if (processor.size() == 1) {
        final PsiVariable result = processor.getResult(0);
        return factory.createExpressionFromText(result.getName(), list);
      }
      if (processor.size() == 0) {
        final PsiClass parentClass = PsiTreeUtil.getParentOfType(list, PsiClass.class);
        if (parentClass != null) {
          PsiClass containingClass = parentClass;
          final Set<PsiClass> containingClasses = new HashSet<PsiClass>();
          final PsiElementFactory jfactory = JavaPsiFacade.getElementFactory(list.getProject());
          while (containingClass != null) {
            if (type.isAssignableFrom(jfactory.createType(containingClass, PsiSubstitutor.EMPTY))) {
              containingClasses.add(containingClass);
            }
            containingClass = PsiTreeUtil.getParentOfType(containingClass, PsiClass.class);
          }
          if (containingClasses.size() == 1) {
            return factory.createThisExpression(
                containingClasses.contains(parentClass)
                    ? null
                    : containingClasses.iterator().next());
          }
        }
      }
    }

    final String value = info.getDefaultValue();
    return !StringUtil.isEmpty(value) ? factory.createExpressionFromText(value, list) : null;
  }
コード例 #7
0
  private static boolean processPrimaryMethodInner(
      JavaChangeInfo changeInfo, GrMethod method, @Nullable PsiMethod baseMethod) {
    if (changeInfo.isNameChanged()) {
      String newName =
          baseMethod == null
              ? changeInfo.getNewName()
              : RefactoringUtil.suggestNewOverriderName(
                  method.getName(), baseMethod.getName(), changeInfo.getNewName());
      if (newName != null && !newName.equals(method.getName())) {
        method.setName(changeInfo.getNewName());
      }
    }

    final GrModifierList modifierList = method.getModifierList();
    if (changeInfo.isVisibilityChanged()) {
      modifierList.setModifierProperty(changeInfo.getNewVisibility(), true);
    }

    PsiSubstitutor substitutor =
        baseMethod != null ? calculateSubstitutor(method, baseMethod) : PsiSubstitutor.EMPTY;

    final PsiMethod context = changeInfo.getMethod();
    GrTypeElement oldReturnTypeElement = method.getReturnTypeElementGroovy();
    if (changeInfo.isReturnTypeChanged()) {
      CanonicalTypes.Type newReturnType = changeInfo.getNewReturnType();
      if (newReturnType == null) {
        if (oldReturnTypeElement != null) {
          oldReturnTypeElement.delete();
          if (modifierList.getModifiers().length == 0) {
            modifierList.setModifierProperty(GrModifier.DEF, true);
          }
        }
      } else {
        PsiType type = newReturnType.getType(context, method.getManager());
        method.setReturnType(substitutor.substitute(type));
        if (oldReturnTypeElement == null) {
          modifierList.setModifierProperty(GrModifier.DEF, false);
        }
      }
    }

    JavaParameterInfo[] newParameters = changeInfo.getNewParameters();
    final GrParameterList parameterList = method.getParameterList();
    GrParameter[] oldParameters = parameterList.getParameters();
    final PsiParameter[] oldBaseParams =
        baseMethod != null ? baseMethod.getParameterList().getParameters() : null;

    Set<GrParameter> toRemove = new HashSet<GrParameter>(oldParameters.length);
    ContainerUtil.addAll(toRemove, oldParameters);

    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
    GrParameter anchor = null;
    final GrDocComment docComment = method.getDocComment();
    final GrDocTag[] tags = docComment == null ? null : docComment.getTags();

    for (JavaParameterInfo newParameter : newParameters) {
      PsiType type;
      if (newParameter instanceof GrParameterInfo && ((GrParameterInfo) newParameter).hasNoType()) {
        type = null;
      } else {
        type = substitutor.substitute(newParameter.createType(context, method.getManager()));
      }

      // if old parameter name differs from base method parameter name we don't change it
      final String newName;
      final int oldIndex = newParameter.getOldIndex();
      if (oldIndex >= 0 && oldBaseParams != null) {
        final String oldName = oldParameters[oldIndex].getName();
        if (oldName.equals(oldBaseParams[oldIndex].getName())) {
          newName = newParameter.getName();
        } else {
          newName = oldName;
        }
      } else {
        newName = newParameter.getName();
      }

      if (docComment != null) {
        if (oldIndex >= 0) {
          final GrParameter oldParameter = oldParameters[oldIndex];
          final String oldName = oldParameter.getName();
          for (GrDocTag tag : tags) {
            if ("@param".equals(tag.getName())) {
              final GrDocParameterReference parameterReference = tag.getDocParameterReference();
              if (parameterReference != null && oldName.equals(parameterReference.getText())) {
                parameterReference.handleElementRename(newName);
              }
            }
          }
        }
      }

      GrParameter grParameter =
          factory.createParameter(
              newName,
              type == null ? null : type.getCanonicalText(),
              getInitializer(newParameter),
              parameterList);

      anchor = (GrParameter) parameterList.addAfter(grParameter, anchor);
    }

    for (GrParameter oldParameter : toRemove) {
      oldParameter.delete();
    }
    JavaCodeStyleManager.getInstance(parameterList.getProject())
        .shortenClassReferences(parameterList);
    CodeStyleManager.getInstance(parameterList.getProject()).reformat(parameterList);

    if (changeInfo.isExceptionSetOrOrderChanged()) {
      final ThrownExceptionInfo[] infos = changeInfo.getNewExceptions();
      PsiClassType[] exceptionTypes = new PsiClassType[infos.length];
      for (int i = 0; i < infos.length; i++) {
        ThrownExceptionInfo info = infos[i];
        exceptionTypes[i] = (PsiClassType) info.createType(method, method.getManager());
      }

      PsiReferenceList thrownList =
          GroovyPsiElementFactory.getInstance(method.getProject()).createThrownList(exceptionTypes);
      thrownList = (PsiReferenceList) method.getThrowsList().replace(thrownList);
      JavaCodeStyleManager.getInstance(thrownList.getProject()).shortenClassReferences(thrownList);
      CodeStyleManager.getInstance(method.getProject()).reformat(method.getThrowsList());
    }
    return true;
  }