Ejemplo n.º 1
0
  public static boolean processImplicitImports(
      @NotNull PsiScopeProcessor processor,
      ResolveState state,
      PsiElement lastParent,
      PsiElement place,
      @NotNull GroovyFile file) {
    JavaPsiFacade facade = JavaPsiFacade.getInstance(file.getProject());

    final DelegatingScopeProcessor packageSkipper =
        new DelegatingScopeProcessor(processor) {
          @Override
          public boolean execute(@NotNull PsiElement element, ResolveState state) {
            if (element instanceof PsiPackage) return true;
            return super.execute(element, state);
          }
        };

    for (final String implicitlyImported : getImplicitlyImportedPackages(file)) {
      PsiPackage aPackage = facade.findPackage(implicitlyImported);
      if (aPackage == null) continue;

      if (!aPackage.processDeclarations(packageSkipper, state, lastParent, place)) {
        return false;
      }
    }

    GroovyPsiManager groovyPsiManager = GroovyPsiManager.getInstance(file.getProject());
    for (String implicitlyImportedClass : GroovyFileBase.IMPLICITLY_IMPORTED_CLASSES) {
      PsiClass clazz =
          groovyPsiManager.findClassWithCache(implicitlyImportedClass, file.getResolveScope());
      if (clazz != null && !ResolveUtil.processElement(processor, clazz, state)) return false;
    }
    return true;
  }
  @NotNull
  public static PsiClassType[] getExtendsListTypes(GrTypeDefinition grType) {
    final PsiClassType[] extendsTypes = getReferenceListTypes(grType.getExtendsClause());
    if (grType.isInterface()) {
      return extendsTypes;
    }

    for (PsiClassType type : extendsTypes) {
      final PsiClass superClass = type.resolve();
      if (superClass instanceof GrTypeDefinition && !superClass.isInterface()
          || superClass != null
              && GroovyCommonClassNames.GROOVY_OBJECT_SUPPORT.equals(
                  superClass.getQualifiedName())) {
        return extendsTypes;
      }
    }

    PsiClass grObSupport =
        GroovyPsiManager.getInstance(grType.getProject())
            .findClassWithCache(
                GroovyCommonClassNames.GROOVY_OBJECT_SUPPORT, grType.getResolveScope());
    if (grObSupport != null) {
      final PsiClassType type =
          JavaPsiFacade.getInstance(grType.getProject())
              .getElementFactory()
              .createType(grObSupport);
      return ArrayUtil.append(extendsTypes, type, PsiClassType.ARRAY_FACTORY);
    }
    return extendsTypes;
  }
Ejemplo n.º 3
0
 private void getVariantsFromQualifierType(
     @NotNull PsiType qualifierType, @NotNull Project project) {
   final ResolveState state = ResolveState.initial();
   if (qualifierType instanceof PsiClassType) {
     PsiClassType.ClassResolveResult result = ((PsiClassType) qualifierType).resolveGenerics();
     PsiClass qualifierClass = result.getElement();
     if (qualifierClass != null) {
       qualifierClass.processDeclarations(
           myProcessor, state.put(PsiSubstitutor.KEY, result.getSubstitutor()), null, myRefExpr);
     }
   } else if (qualifierType instanceof PsiArrayType) {
     final GrTypeDefinition arrayClass =
         GroovyPsiManager.getInstance(project)
             .getArrayClass(((PsiArrayType) qualifierType).getComponentType());
     if (arrayClass != null) {
       if (!arrayClass.processDeclarations(myProcessor, state, null, myRefExpr)) return;
     }
   } else if (qualifierType instanceof PsiIntersectionType) {
     for (PsiType conjunct : ((PsiIntersectionType) qualifierType).getConjuncts()) {
       getVariantsFromQualifierType(conjunct, project);
     }
     return;
   }
   ResolveUtil.processNonCodeMembers(qualifierType, myProcessor, myRefExpr, state);
 }
Ejemplo n.º 4
0
        @Override
        public PsiType fun(GrMethodBaseImpl method) {
          PsiType nominal = method.getNominalType();
          if (nominal != null) {
            if (!(nominal instanceof PsiClassType
                && hasTypeParametersToInfer((PsiClassType) nominal))) {
              return nominal;
            }
          }

          if (!GppTypeConverter.hasTypedContext(method)) {
            LOG.assertTrue(method.isValid(), "invalid method");

            final GrOpenBlock block = method.getBlock();
            if (block != null) {
              LOG.assertTrue(block.isValid(), "invalid code block");
              PsiType inferred =
                  GroovyPsiManager.inferType(method, new MethodTypeInferencer(block));
              if (inferred != null) {
                if (nominal == null || nominal.isAssignableFrom(inferred)) {
                  return inferred;
                }
              }
            }
          }
          if (nominal != null) {
            return nominal;
          }

          return TypesUtil.getJavaLangObject(method);
        }
Ejemplo n.º 5
0
  public static PsiType boxPrimitiveType(
      @Nullable PsiType result,
      @NotNull PsiManager manager,
      @NotNull GlobalSearchScope resolveScope,
      boolean boxVoid) {
    if (result instanceof PsiPrimitiveType && (boxVoid || result != PsiType.VOID)) {
      PsiPrimitiveType primitive = (PsiPrimitiveType) result;
      String boxedTypeName = primitive.getBoxedTypeName();
      if (boxedTypeName != null) {
        return GroovyPsiManager.getInstance(manager.getProject())
            .createTypeByFQClassName(boxedTypeName, resolveScope);
      }
    }

    return result;
  }
  @Override
  public void processDynamicElements(
      final @NotNull PsiType qualifierType,
      final PsiScopeProcessor processor,
      final GroovyPsiElement place,
      final ResolveState state) {
    if (!GroovyPsiManager.isInheritorCached(qualifierType, CommonClassNames.JAVA_UTIL_COLLECTION))
      return;

    final PsiType collectionType = PsiUtil.extractIterableTypeParameter(qualifierType, true);
    if (collectionType == null) return;

    final PsiScopeProcessor fieldSearcher =
        new FieldSearcher(
            processor,
            JavaPsiFacade.getInstance(place.getProject())
                .findClass(CommonClassNames.JAVA_UTIL_COLLECTION, place.getResolveScope()));
    ResolveUtil.processAllDeclarations(collectionType, fieldSearcher, state, place);
  }
 public PsiType getType() {
   return GroovyPsiManager.getInstance(getProject()).getType(this, TYPE_CALCULATOR);
 }
Ejemplo n.º 8
0
 @NotNull
 public static PsiClassType createTypeByFQClassName(
     @NotNull String fqName, @NotNull PsiElement context) {
   return GroovyPsiManager.getInstance(context.getProject())
       .createTypeByFQClassName(fqName, context.getResolveScope());
 }