Search in sources :

Example 11 with PsiPrimitiveType

use of com.intellij.psi.PsiPrimitiveType in project intellij-community by JetBrains.

the class NullParameterConstraintChecker method checkMethodParameters.

@NotNull
static PsiParameter[] checkMethodParameters(PsiMethod method) {
    if (method.getBody() == null)
        return PsiParameter.EMPTY_ARRAY;
    final Collection<PsiParameter> nullableParameters = new SmartList<>();
    final PsiParameter[] parameters = method.getParameterList().getParameters();
    for (int index = 0; index < parameters.length; index++) {
        PsiParameter parameter = parameters[index];
        if (!(parameter.getType() instanceof PsiPrimitiveType) && !NullableNotNullManager.isNotNull(parameter) && !NullableNotNullManager.isNullable(parameter) && JavaNullMethodArgumentUtil.hasNullArgument(method, index)) {
            nullableParameters.add(parameter);
        }
    }
    if (nullableParameters.isEmpty())
        return PsiParameter.EMPTY_ARRAY;
    final NullParameterConstraintChecker checker = new NullParameterConstraintChecker(nullableParameters);
    checker.analyzeMethod(method.getBody(), new StandardInstructionVisitor());
    return checker.myPossiblyViolatedParameters.stream().filter(checker.myUsedParameters::contains).toArray(PsiParameter[]::new);
}
Also used : PsiPrimitiveType(com.intellij.psi.PsiPrimitiveType) PsiParameter(com.intellij.psi.PsiParameter) SmartList(com.intellij.util.SmartList) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with PsiPrimitiveType

use of com.intellij.psi.PsiPrimitiveType in project intellij-community by JetBrains.

the class ContractInference method postProcessContracts.

@NotNull
private static List<MethodContract> postProcessContracts(@NotNull PsiMethodImpl method, MethodData data, List<PreContract> rawContracts) {
    List<MethodContract> contracts = ContainerUtil.concat(rawContracts, c -> c.toContracts(method, data.methodBody(method)));
    if (contracts.isEmpty())
        return Collections.emptyList();
    final PsiType returnType = method.getReturnType();
    if (returnType != null && !(returnType instanceof PsiPrimitiveType)) {
        contracts = boxReturnValues(contracts);
    }
    List<MethodContract> compatible = ContainerUtil.filter(contracts, contract -> isContractCompatibleWithMethod(method, returnType, contract));
    if (compatible.size() > MAX_CONTRACT_COUNT) {
        LOG.debug("Too many contracts for " + PsiUtil.getMemberQualifiedName(method) + ", shrinking the list");
        return compatible.subList(0, MAX_CONTRACT_COUNT);
    }
    return compatible;
}
Also used : PsiPrimitiveType(com.intellij.psi.PsiPrimitiveType) PsiType(com.intellij.psi.PsiType) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with PsiPrimitiveType

use of com.intellij.psi.PsiPrimitiveType in project intellij-community by JetBrains.

the class PsiTypeCanonicalLookupElement method renderElement.

@Override
public void renderElement(LookupElementPresentation presentation) {
    final PsiClass psiClass = getPsiClass();
    if (psiClass != null) {
        presentation.setIcon(presentation.isReal() ? psiClass.getIcon(Iconable.ICON_FLAG_VISIBILITY) : EMPTY_ICON);
        presentation.setTailText(" (" + PsiFormatUtil.getPackageDisplayName(psiClass) + ")", true);
    }
    final PsiType type = getPsiType();
    presentation.setItemText(type.getPresentableText());
    presentation.setItemTextBold(type instanceof PsiPrimitiveType);
}
Also used : PsiPrimitiveType(com.intellij.psi.PsiPrimitiveType) PsiClass(com.intellij.psi.PsiClass) PsiType(com.intellij.psi.PsiType)

Example 14 with PsiPrimitiveType

use of com.intellij.psi.PsiPrimitiveType in project intellij-community by JetBrains.

the class GrReplacePrimitiveTypeWithWrapperFix method doFix.

@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
    final PsiElement element = descriptor.getPsiElement();
    assert element instanceof GrTypeElement : element;
    GrTypeElement typeElement = (GrTypeElement) element;
    final PsiType type = typeElement.getType();
    if (!(type instanceof PsiPrimitiveType))
        return;
    final PsiClassType boxed = ((PsiPrimitiveType) type).getBoxedType(typeElement);
    if (boxed == null)
        return;
    final GrTypeElement newTypeElement = GroovyPsiElementFactory.getInstance(project).createTypeElement(boxed);
    final PsiElement replaced = typeElement.replace(newTypeElement);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(replaced);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) PsiClassType(com.intellij.psi.PsiClassType) PsiPrimitiveType(com.intellij.psi.PsiPrimitiveType) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType)

Example 15 with PsiPrimitiveType

use of com.intellij.psi.PsiPrimitiveType in project intellij-community by JetBrains.

the class GrCharConverter method isConvertibleEx.

@Nullable
@Override
public ConversionResult isConvertibleEx(@NotNull PsiType lType, @NotNull PsiType rType, @NotNull GroovyPsiElement context, @NotNull ApplicableTo currentPosition) {
    if (!PsiType.CHAR.equals(TypesUtil.unboxPrimitiveTypeWrapper(lType)))
        return null;
    if (PsiType.CHAR.equals(TypesUtil.unboxPrimitiveTypeWrapper(rType)))
        return ConversionResult.OK;
    // can assign numeric types to char
    if (TypesUtil.isNumericType(rType)) {
        if (rType instanceof PsiPrimitiveType || TypesUtil.unboxPrimitiveTypeWrapper(rType) instanceof PsiPrimitiveType) {
            return PsiType.CHAR.equals(lType) ? ConversionResult.OK : ConversionResult.ERROR;
        } else {
            // BigDecimal && BigInteger
            return ConversionResult.ERROR;
        }
    }
    {
        // special case 'c = []' will throw RuntimeError
        final GrExpression rValue;
        if (context instanceof GrAssignmentExpression) {
            final GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) context;
            rValue = assignmentExpression.getRValue();
        } else if (context instanceof GrVariable) {
            final GrVariable assignmentExpression = (GrVariable) context;
            rValue = assignmentExpression.getInitializerGroovy();
        } else {
            rValue = null;
        }
        if (rValue instanceof GrListOrMap && ((GrListOrMap) rValue).isEmpty()) {
            return ConversionResult.WARNING;
        }
    }
    if (PsiType.BOOLEAN.equals(TypesUtil.unboxPrimitiveTypeWrapper(rType))) {
        switch(currentPosition) {
            case ASSIGNMENT:
            case RETURN_VALUE:
                return ConversionResult.WARNING;
            default:
                return null;
        }
    }
    // one-symbol string-to-char conversion doesn't work with return value
    if (currentPosition == ApplicableTo.RETURN_VALUE) {
        return null;
    }
    // can cast and assign one-symbol strings to char
    if (!TypesUtil.isClassType(rType, CommonClassNames.JAVA_LANG_STRING))
        return null;
    return checkSingleSymbolLiteral(context) ? ConversionResult.OK : ConversionResult.ERROR;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) PsiPrimitiveType(com.intellij.psi.PsiPrimitiveType) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiPrimitiveType (com.intellij.psi.PsiPrimitiveType)17 PsiType (com.intellij.psi.PsiType)12 PsiClassType (com.intellij.psi.PsiClassType)3 PsiElement (com.intellij.psi.PsiElement)3 PsiField (com.intellij.psi.PsiField)3 PsiTypeElement (com.intellij.psi.PsiTypeElement)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 PsiClass (com.intellij.psi.PsiClass)2 PsiExpression (com.intellij.psi.PsiExpression)2 PsiParameter (com.intellij.psi.PsiParameter)2 Nullable (com.android.annotations.Nullable)1 EvaluateException (com.intellij.debugger.engine.evaluation.EvaluateException)1 PsiArrayInitializerExpression (com.intellij.psi.PsiArrayInitializerExpression)1 PsiArrayType (com.intellij.psi.PsiArrayType)1 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)1 PsiBinaryExpression (com.intellij.psi.PsiBinaryExpression)1 PsiConditionalExpression (com.intellij.psi.PsiConditionalExpression)1 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)1 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)1