Search in sources :

Example 6 with TypeEvaluator

use of com.intellij.refactoring.typeMigration.TypeEvaluator in project intellij-community by JetBrains.

the class GuavaOptionalConversionRule method fillSimpleDescriptors.

@Override
protected void fillSimpleDescriptors(Map<String, TypeConversionDescriptorBase> descriptorsMap) {
    descriptorsMap.put("absent", new TypeConversionDescriptor("'Optional*.absent()", "java.util.Optional.empty()") {

        @Override
        public PsiExpression replace(PsiExpression expression, @NotNull TypeEvaluator evaluator) {
            LOG.assertTrue(expression instanceof PsiMethodCallExpression);
            final PsiReferenceParameterList typeArguments = ((PsiMethodCallExpression) expression).getTypeArgumentList();
            PsiReferenceParameterList typeArgumentsCopy = typeArguments.getTypeArguments().length == 0 ? null : (PsiReferenceParameterList) typeArguments.copy();
            final PsiMethodCallExpression replacedExpression = (PsiMethodCallExpression) super.replace(expression, evaluator);
            if (typeArgumentsCopy != null) {
                replacedExpression.getTypeArgumentList().replace(typeArgumentsCopy);
            }
            return replacedExpression;
        }
    });
    descriptorsMap.put("of", new TypeConversionDescriptor("'Optional*.of($ref$)", "java.util.Optional.of($ref$)"));
    descriptorsMap.put("fromNullable", new TypeConversionDescriptor("'Optional*.fromNullable($ref$)", "java.util.Optional.ofNullable($ref$)"));
    descriptorsMap.put("presentInstances", new TypeConversionDescriptor("'Optional*.presentInstances($it$)", "java.util.stream.StreamSupport.stream($it$.spliterator(), false).map(java.util.Optional::get).collect(java.util.Collectors.toList())"));
    final TypeConversionDescriptorBase identity = new TypeConversionDescriptorBase();
    descriptorsMap.put("get", identity);
    descriptorsMap.put("isPresent", identity);
    descriptorsMap.put("orNull", new TypeConversionDescriptor("$val$.orNull()", "$val$.orElse(null)"));
    descriptorsMap.put("asSet", new TypeConversionDescriptor("$val$.asSet()", "$val$.map(java.util.Collections::singleton).orElse(java.util.Collections.emptySet())"));
}
Also used : TypeEvaluator(com.intellij.refactoring.typeMigration.TypeEvaluator) TypeConversionDescriptorBase(com.intellij.refactoring.typeMigration.TypeConversionDescriptorBase) TypeConversionDescriptor(com.intellij.refactoring.typeMigration.TypeConversionDescriptor)

Example 7 with TypeEvaluator

use of com.intellij.refactoring.typeMigration.TypeEvaluator in project intellij-community by JetBrains.

the class GuavaOptionalConversionRule method findConversionForMethod.

@Nullable
@Override
protected TypeConversionDescriptorBase findConversionForMethod(@Nullable PsiType from, @Nullable PsiType to, @NotNull PsiMethod method, @NotNull String methodName, PsiExpression context, TypeMigrationLabeler labeler) {
    if (!(context instanceof PsiMethodCallExpression)) {
        if ("or".equals(methodName)) {
            PsiMethodCallExpression methodCallExpression = null;
            if (context.getParent() instanceof PsiMethodCallExpression) {
                methodCallExpression = (PsiMethodCallExpression) context.getParent();
            }
            if (methodCallExpression == null) {
                return null;
            }
            final PsiClass aClass = getParameterClass(method);
            if (aClass != null) {
                final String qName = aClass.getQualifiedName();
                if (GUAVA_OPTIONAL.equals(qName)) {
                    TypeConversionDescriptor descriptor = new TypeConversionDescriptor(null, "java.util.Optional.ofNullable($val$.orElseGet($o$::get))") {

                        @Override
                        public PsiExpression replace(PsiExpression expression, @NotNull TypeEvaluator evaluator) {
                            setStringToReplace("$val$.or(" + GuavaOptionalConversionUtil.simplifyParameterPattern((PsiMethodCallExpression) expression) + ")");
                            return super.replace(expression, evaluator);
                        }
                    };
                    if (to != null) {
                        descriptor.withConversionType(to);
                    }
                    return descriptor;
                }
                return GuavaLambda.SUPPLIER.getClassQName().equals(qName) ? new GuavaTypeConversionDescriptor("$val$.or($other$)", "$val$.orElseGet($other$)") : new TypeConversionDescriptor("$val$.or($other$)", "$val$.orElse($other$)");
            }
            return null;
        } else if ("transform".equals(methodName)) {
            final PsiMethodCallExpression methodCall = (PsiMethodCallExpression) (context.getParent());
            final PsiExpression[] arguments = methodCall.getArgumentList().getExpressions();
            if (arguments.length != 1) {
                return null;
            }
            final PsiExpression functionArgument = arguments[0];
            final TypeConversionDescriptor descriptor = new GuavaTypeConversionDescriptor("$val$.transform($fun$)", "$val$.map($fun$)");
            final PsiType typeParameter = GuavaConversionUtil.getFunctionReturnType(functionArgument);
            if (typeParameter == null) {
                return descriptor;
            }
            final String rawOptionalType = JAVA_OPTIONAL + "<" + typeParameter.getCanonicalText(false) + ">";
            return descriptor.withConversionType(JavaPsiFacade.getElementFactory(method.getProject()).createTypeFromText(rawOptionalType, context));
        }
        return null;
    }
    final PsiClass aClass = method.getContainingClass();
    if (aClass == null || !(GuavaFluentIterableConversionRule.FLUENT_ITERABLE.equals(aClass.getQualifiedName()) || GUAVA_OPTIONAL.equals(aClass.getQualifiedName()))) {
        return null;
    }
    return GuavaFluentIterableConversionRule.buildCompoundDescriptor((PsiMethodCallExpression) context, to, labeler);
}
Also used : TypeEvaluator(com.intellij.refactoring.typeMigration.TypeEvaluator) TypeConversionDescriptor(com.intellij.refactoring.typeMigration.TypeConversionDescriptor) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

TypeEvaluator (com.intellij.refactoring.typeMigration.TypeEvaluator)7 TypeConversionDescriptor (com.intellij.refactoring.typeMigration.TypeConversionDescriptor)5 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)4 TypeConversionDescriptorBase (com.intellij.refactoring.typeMigration.TypeConversionDescriptorBase)3 IElementType (com.intellij.psi.tree.IElementType)2 Project (com.intellij.openapi.project.Project)1 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)1 SuggestedNameInfo (com.intellij.psi.codeStyle.SuggestedNameInfo)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1