Beispiel #1
0
    @Override
    public void consume(Object o) {
      if (!(o instanceof GroovyResolveResult)) {
        LOG.error(o);
        return;
      }

      GroovyResolveResult result = (GroovyResolveResult) o;
      if (!result.isStaticsOK()) {
        if (myInapplicable == null) myInapplicable = ContainerUtil.newArrayList();
        myInapplicable.add(result);
        return;
      }
      if (!result.isAccessible() && myParameters.getInvocationCount() < 2) return;

      if (mySkipPackages && result.getElement() instanceof PsiPackage) return;

      PsiElement element = result.getElement();
      if (element instanceof PsiVariable
          && !myMatcher.prefixMatches(((PsiVariable) element).getName())) {
        return;
      }

      if (element instanceof GrReflectedMethod) {
        element = ((GrReflectedMethod) element).getBaseMethod();
        if (!myProcessedMethodWithOptionalParams.add((GrMethod) element)) return;

        result =
            new GroovyResolveResultImpl(
                element,
                result.getCurrentFileResolveContext(),
                result.getSpreadState(),
                result.getSubstitutor(),
                result.isAccessible(),
                result.isStaticsOK(),
                result.isInvokedOnProperty(),
                result.isValidResult());
      }

      if (myFieldPointerOperator && !(element instanceof PsiVariable)) {
        return;
      }
      if (myMethodPointerOperator && !(element instanceof PsiMethod)) {
        return;
      }
      addCandidate(result);

      if (!myFieldPointerOperator && !myMethodPointerOperator) {
        if (element instanceof PsiMethod) {
          processProperty((PsiMethod) element, result);
        } else if (element instanceof GrField) {
          if (((GrField) element).isProperty()) {
            processPropertyFromField((GrField) element, result);
          }
        }
      }
      if (element instanceof GrVariable && !(element instanceof GrField)) {
        myLocalVars.add(((GrVariable) element).getName());
      }
    }
  private GroovyResolveResult[] filterCandidates() {
    List<GroovyResolveResult> array = getCandidatesInternal();
    if (array.size() == 1) return array.toArray(new GroovyResolveResult[array.size()]);

    List<GroovyResolveResult> result = new ArrayList<GroovyResolveResult>();

    Iterator<GroovyResolveResult> itr = array.iterator();

    result.add(itr.next());

    Outer:
    while (itr.hasNext()) {
      GroovyResolveResult resolveResult = itr.next();
      PsiElement currentElement = resolveResult.getElement();
      if (currentElement instanceof PsiMethod) {
        PsiMethod currentMethod = (PsiMethod) currentElement;
        for (Iterator<GroovyResolveResult> iterator = result.iterator(); iterator.hasNext(); ) {
          final GroovyResolveResult otherResolveResult = iterator.next();
          PsiElement other = otherResolveResult.getElement();
          if (other instanceof PsiMethod) {
            PsiMethod otherMethod = (PsiMethod) other;
            int res =
                compareMethods(
                    currentMethod,
                    resolveResult.getSubstitutor(),
                    resolveResult.getCurrentFileResolveContext(),
                    otherMethod,
                    otherResolveResult.getSubstitutor(),
                    otherResolveResult.getCurrentFileResolveContext());
            if (res > 0) {
              continue Outer;
            } else if (res < 0) {
              iterator.remove();
            }
          }
        }
      }

      result.add(resolveResult);
    }

    return result.toArray(new GroovyResolveResult[result.size()]);
  }
Beispiel #3
0
  @NotNull
  private static GroovyResolveResult generatePropertyResolveResult(
      @NotNull String name,
      @NotNull PsiMethod method,
      @Nullable PsiType type,
      @Nullable GroovyResolveResult resolveResult) {
    PsiType nonNullType = type != null ? type : TypesUtil.getJavaLangObject(method);

    final GrPropertyForCompletion field = new GrPropertyForCompletion(method, name, nonNullType);
    if (resolveResult != null) {
      return new GroovyResolveResultImpl(
          field,
          resolveResult.getCurrentFileResolveContext(),
          resolveResult.getSpreadState(),
          resolveResult.getSubstitutor(),
          resolveResult.isAccessible(),
          resolveResult.isStaticsOK());
    } else {
      return new GroovyResolveResultImpl(field, true);
    }
  }
  /**
   * priority: inside class C: local variable, c.method, c.property, c.getter in other places: local
   * variable, c.method, c.getter, c.property
   */
  @NotNull
  private GroovyResolveResult[] resolveMethodOrProperty(
      boolean allVariants, @Nullable GrExpression upToArgument, boolean genericsMatter) {
    final String name = getReferenceName();
    if (name == null) return GroovyResolveResult.EMPTY_ARRAY;

    PropertyResolverProcessor propertyResolver = new PropertyResolverProcessor(name, this);
    GrReferenceResolveUtil.resolveImpl(propertyResolver, this);
    final GroovyResolveResult[] propertyCandidates = propertyResolver.getCandidates();

    if (!allVariants) { // search for local variables
      for (GroovyResolveResult candidate : propertyCandidates) {
        if (candidate.getElement() instanceof GrVariable
            && !(candidate.getElement() instanceof GrField)) {
          return propertyResolver.getCandidates();
        }
      }
    }

    final Pair<Boolean, GroovyResolveResult[]> shapeResults =
        resolveByShape(allVariants, upToArgument);
    if (!genericsMatter && !allVariants && shapeResults.first) {
      assertAllAreValid(shapeResults.second);
      return shapeResults.second;
    }

    MethodResolverProcessor methodResolver = null;
    if (genericsMatter) {
      methodResolver = createMethodProcessor(allVariants, name, false, upToArgument);

      for (GroovyResolveResult result : shapeResults.second) {
        final ResolveState state =
            ResolveState.initial()
                .put(PsiSubstitutor.KEY, result.getSubstitutor())
                .put(ResolverProcessor.RESOLVE_CONTEXT, result.getCurrentFileResolveContext())
                .put(SpreadState.SPREAD_STATE, result.getSpreadState());
        methodResolver.execute(result.getElement(), state);
      }

      if (!allVariants && methodResolver.hasApplicableCandidates()) {
        return methodResolver.getCandidates();
      }
    }

    // search for fields inside its class
    if (!allVariants) {
      for (GroovyResolveResult candidate : propertyCandidates) {
        final PsiElement element = candidate.getElement();
        if (element instanceof GrField) {
          final PsiClass containingClass = ((PsiField) element).getContainingClass();
          if (containingClass != null && PsiTreeUtil.isContextAncestor(containingClass, this, true))
            return propertyCandidates;
        }
      }
    }

    List<GroovyResolveResult> allCandidates = new ArrayList<GroovyResolveResult>();
    ContainerUtil.addAll(allCandidates, propertyCandidates);
    ContainerUtil.addAll(
        allCandidates, genericsMatter ? methodResolver.getCandidates() : shapeResults.second);

    // search for getters
    for (String getterName : GroovyPropertyUtils.suggestGettersName(name)) {
      AccessorResolverProcessor getterResolver =
          new AccessorResolverProcessor(
              getterName,
              name,
              this,
              true,
              genericsMatter,
              GrReferenceResolveUtil.getQualifierType(this),
              getTypeArguments());
      GrReferenceResolveUtil.resolveImpl(getterResolver, this);
      final GroovyResolveResult[] candidates =
          getterResolver.getCandidates(); // can be only one candidate
      if (!allVariants && candidates.length == 1) {
        return candidates;
      }
      ContainerUtil.addAll(allCandidates, candidates);
    }

    if (allCandidates.size() > 0) {
      return allCandidates.toArray(new GroovyResolveResult[allCandidates.size()]);
    }
    return GroovyResolveResult.EMPTY_ARRAY;
  }