Search in sources :

Example 6 with TypeConversionDescriptorBase

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

the class BaseGuavaTypeConversionRule method findConversion.

@Nullable
@Override
public final TypeConversionDescriptorBase findConversion(@Nullable PsiType from, @Nullable PsiType to, PsiMember member, PsiExpression context, TypeMigrationLabeler labeler) {
    if (from != null && to != null && !canConvert(from, to, ruleFromClass(), ruleToClass())) {
        return null;
    }
    if (member instanceof PsiMethod) {
        PsiMethod method = (PsiMethod) member;
        final String methodName = method.getName();
        final PsiClass aClass = method.getContainingClass();
        if (isValidMethodQualifierToConvert(aClass)) {
            final TypeConversionDescriptorBase descriptor = mySimpleDescriptors.getValue().get(methodName);
            if (descriptor != null) {
                return descriptor;
            }
        }
        return findConversionForMethod(from, to, method, methodName, context, labeler);
    } else if (context instanceof PsiNewExpression) {
        final PsiAnonymousClass anonymousClass = ((PsiNewExpression) context).getAnonymousClass();
        return anonymousClass == null ? null : findConversionForAnonymous(anonymousClass, labeler.getSettings(GuavaConversionSettings.class));
    } else if (context instanceof PsiMethodReferenceExpression) {
        final PsiType methodReferenceType = context.getType();
        if (methodReferenceType != null && to != null && to.isAssignableFrom(methodReferenceType)) {
            return new TypeConversionDescriptorBase();
        }
    } else {
        if (context instanceof PsiReferenceExpression) {
            final PsiElement resolvedElement = ((PsiReferenceExpression) context).resolve();
            if (resolvedElement instanceof PsiVariable) {
                return findConversionForVariableReference((PsiReferenceExpression) context, (PsiVariable) resolvedElement, context);
            }
        }
    }
    return null;
}
Also used : GuavaConversionSettings(com.intellij.refactoring.typeMigration.inspections.GuavaConversionSettings) TypeConversionDescriptorBase(com.intellij.refactoring.typeMigration.TypeConversionDescriptorBase) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with TypeConversionDescriptorBase

use of com.intellij.refactoring.typeMigration.TypeConversionDescriptorBase 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)

Aggregations

TypeConversionDescriptorBase (com.intellij.refactoring.typeMigration.TypeConversionDescriptorBase)7 Nullable (org.jetbrains.annotations.Nullable)4 TypeEvaluator (com.intellij.refactoring.typeMigration.TypeEvaluator)3 NotNull (org.jetbrains.annotations.NotNull)2 Project (com.intellij.openapi.project.Project)1 NotNullLazyValue (com.intellij.openapi.util.NotNullLazyValue)1 SearchScope (com.intellij.psi.search.SearchScope)1 TypeConversionDescriptor (com.intellij.refactoring.typeMigration.TypeConversionDescriptor)1 GuavaConversionSettings (com.intellij.refactoring.typeMigration.inspections.GuavaConversionSettings)1 TypeConversionRule (com.intellij.refactoring.typeMigration.rules.TypeConversionRule)1 SmartList (com.intellij.util.SmartList)1