@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);
        }
  @Override
  public void applyTransformation(@NotNull TransformationContext context) {
    if (!(context.getCodeClass() instanceof GroovyScriptClass)) return;
    GroovyScriptClass scriptClass = (GroovyScriptClass) context.getCodeClass();

    LightMethodBuilder mainMethod =
        new LightMethodBuilder(scriptClass.getManager(), GroovyLanguage.INSTANCE, "main")
            .setMethodReturnType(PsiType.VOID)
            .addParameter(
                "args",
                new PsiArrayType(
                    PsiType.getJavaLangString(
                        scriptClass.getManager(), scriptClass.getResolveScope())))
            .addModifiers(PsiModifier.PUBLIC, PsiModifier.STATIC);

    LightMethodBuilder runMethod =
        new LightMethodBuilder(scriptClass.getManager(), GroovyLanguage.INSTANCE, "run")
            .setMethodReturnType(TypesUtil.getJavaLangObject(scriptClass))
            .addModifier(PsiModifier.PUBLIC);

    context.addMethod(runMethod, true);
    context.addMethod(mainMethod, true);

    context.setSuperType(getBaseClassType(scriptClass));
  }
    @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);
    }
 public void visitTraditionalForClause(GrTraditionalForClause forClause) {
   if (myExpression.equals(forClause.getCondition())) {
     myResult =
         new TypeConstraint[] {
           new SubtypeConstraint(TypesUtil.getJavaLangObject(forClause), PsiType.BOOLEAN)
         };
   }
 }
 public void visitWhileStatement(GrWhileStatement whileStatement) {
   if (myExpression.equals(whileStatement.getCondition())) {
     myResult =
         new TypeConstraint[] {
           new SubtypeConstraint(TypesUtil.getJavaLangObject(whileStatement), PsiType.BOOLEAN)
         };
   }
 }
 public void visitIfStatement(GrIfStatement ifStatement) {
   if (myExpression.equals(ifStatement.getCondition())) {
     myResult =
         new TypeConstraint[] {
           new SubtypeConstraint(TypesUtil.getJavaLangObject(ifStatement), PsiType.BOOLEAN)
         };
   } else if (myExpression.equals(ifStatement.getThenBranch())
       || myExpression.equals(ifStatement.getElseBranch())) {
     checkExitPoint();
   }
 }
Beispiel #7
0
 private void getVariantsFromQualifier(@NotNull GrExpression qualifier) {
   Project project = qualifier.getProject();
   PsiType qualifierType = qualifier.getType();
   final ResolveState state = ResolveState.initial();
   if (qualifierType == null || qualifierType == PsiType.VOID) {
     if (qualifier instanceof GrReferenceExpression) {
       PsiElement resolved = ((GrReferenceExpression) qualifier).resolve();
       if (resolved instanceof PsiPackage || resolved instanceof PsiVariable) {
         resolved.processDeclarations(myProcessor, state, null, myRefExpr);
         return;
       }
     }
     getVariantsFromQualifierType(TypesUtil.getJavaLangObject(qualifier), project);
   } else if (qualifierType instanceof PsiIntersectionType) {
     for (PsiType conjunct : ((PsiIntersectionType) qualifierType).getConjuncts()) {
       getVariantsFromQualifierType(conjunct, project);
     }
   } else if (qualifierType instanceof GrTraitType) {
     GrTypeDefinition definition = ((GrTraitType) qualifierType).getMockTypeDefinition();
     if (definition != null) {
       PsiClassType classType = JavaPsiFacade.getElementFactory(project).createType(definition);
       getVariantsFromQualifierType(classType, project);
     } else {
       getVariantsFromQualifierType(((GrTraitType) qualifierType).getExprType(), project);
       for (PsiClassType traitType : ((GrTraitType) qualifierType).getTraitTypes()) {
         getVariantsFromQualifierType(traitType, project);
       }
     }
   } else {
     getVariantsFromQualifierType(qualifierType, project);
     if (qualifier instanceof GrReferenceExpression
         && !PsiUtil.isSuperReference(qualifier)
         && !PsiUtil.isInstanceThisRef(qualifier)) {
       PsiElement resolved = ((GrReferenceExpression) qualifier).resolve();
       if (resolved instanceof PsiClass) { // //omitted .class
         GlobalSearchScope scope = myRefExpr.getResolveScope();
         PsiClass javaLangClass = PsiUtil.getJavaLangClass(resolved, scope);
         if (javaLangClass != null) {
           PsiSubstitutor substitutor = PsiSubstitutor.EMPTY;
           PsiTypeParameter[] typeParameters = javaLangClass.getTypeParameters();
           if (typeParameters.length == 1) {
             substitutor = substitutor.put(typeParameters[0], qualifierType);
           }
           PsiType javaLangClassType =
               JavaPsiFacade.getElementFactory(myRefExpr.getProject())
                   .createType(javaLangClass, substitutor);
           ResolveUtil.processAllDeclarations(javaLangClassType, myProcessor, state, myRefExpr);
         }
       }
     }
   }
 }
  @Override
  @Nullable
  public PsiType getReturnType() {
    if (isConstructor()) {
      return null;
    }

    final PsiType type = getNominalType();
    if (type != null) {
      return type;
    }

    return TypesUtil.getJavaLangObject(this);
  }
Beispiel #9
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);
    }
  }
  private static void writeReturn(
      StringBuilder builder, ExpressionContext context, final GrExpression expression) {
    builder.append("return ");

    final PsiType expectedReturnType = PsiImplUtil.inferReturnType(expression);
    final PsiType nnReturnType =
        expectedReturnType == null || expectedReturnType == PsiType.VOID
            ? TypesUtil.getJavaLangObject(expression)
            : expectedReturnType;
    wrapInCastIfNeeded(
        builder,
        nnReturnType,
        expression.getNominalType(),
        expression,
        context,
        new StatementWriter() {
          @Override
          public void writeStatement(StringBuilder builder, ExpressionContext context) {
            writeExpression(expression, builder, context);
          }
        });
    builder.append(';');
  }
 public static PsiClassType createBaseClassType(GrTypeDefinition grType) {
   if (grType.isEnum()) {
     return TypesUtil.createTypeByFQClassName(CommonClassNames.JAVA_LANG_ENUM, grType);
   }
   return TypesUtil.getJavaLangObject(grType);
 }