Search in sources :

Example 1 with TypeConversionRule

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

the class GuavaFluentIterableConversionRule method buildCompoundDescriptor.

@Nullable
public static GuavaChainedConversionDescriptor buildCompoundDescriptor(PsiMethodCallExpression expression, PsiType to, TypeMigrationLabeler labeler) {
    List<TypeConversionDescriptorBase> methodDescriptors = new SmartList<>();
    NotNullLazyValue<TypeConversionRule> optionalDescriptor = new NotNullLazyValue<TypeConversionRule>() {

        @NotNull
        @Override
        protected TypeConversionRule compute() {
            for (TypeConversionRule rule : TypeConversionRule.EP_NAME.getExtensions()) {
                if (rule instanceof GuavaOptionalConversionRule) {
                    return rule;
                }
            }
            throw new RuntimeException("GuavaOptionalConversionRule extension is not found");
        }
    };
    PsiMethodCallExpression current = expression;
    while (true) {
        final PsiMethod method = current.resolveMethod();
        if (method == null) {
            break;
        }
        final String methodName = method.getName();
        final PsiClass containingClass = method.getContainingClass();
        if (containingClass == null) {
            break;
        }
        TypeConversionDescriptorBase descriptor = null;
        if (FLUENT_ITERABLE.equals(containingClass.getQualifiedName())) {
            descriptor = getOneMethodDescriptor(methodName, method, current.getType(), null, current);
            if (descriptor == null) {
                return null;
            }
        } else if (GuavaOptionalConversionRule.GUAVA_OPTIONAL.equals(containingClass.getQualifiedName())) {
            descriptor = optionalDescriptor.getValue().findConversion(null, null, method, current.getMethodExpression(), labeler);
            if (descriptor == null) {
                return null;
            }
        }
        if (descriptor == null) {
            addToMigrateChainQualifier(labeler, current);
            break;
        }
        methodDescriptors.add(descriptor);
        final PsiExpression qualifier = current.getMethodExpression().getQualifierExpression();
        if (qualifier instanceof PsiMethodCallExpression) {
            current = (PsiMethodCallExpression) qualifier;
        } else if (method.hasModifierProperty(PsiModifier.STATIC)) {
            if (!CHAIN_HEAD_METHODS.contains(methodName)) {
                return null;
            }
            final PsiClass aClass = method.getContainingClass();
            if (aClass == null || !(FLUENT_ITERABLE.equals(aClass.getQualifiedName()) || GuavaOptionalConversionRule.GUAVA_OPTIONAL.equals(aClass.getQualifiedName()))) {
                return null;
            }
            break;
        } else if (qualifier instanceof PsiReferenceExpression && ((PsiReferenceExpression) qualifier).resolve() instanceof PsiVariable) {
            addToMigrateChainQualifier(labeler, qualifier);
            break;
        } else {
            return null;
        }
    }
    return new GuavaChainedConversionDescriptor(methodDescriptors, to);
}
Also used : TypeConversionRule(com.intellij.refactoring.typeMigration.rules.TypeConversionRule) NotNullLazyValue(com.intellij.openapi.util.NotNullLazyValue) TypeConversionDescriptorBase(com.intellij.refactoring.typeMigration.TypeConversionDescriptorBase) SmartList(com.intellij.util.SmartList) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

NotNullLazyValue (com.intellij.openapi.util.NotNullLazyValue)1 TypeConversionDescriptorBase (com.intellij.refactoring.typeMigration.TypeConversionDescriptorBase)1 TypeConversionRule (com.intellij.refactoring.typeMigration.rules.TypeConversionRule)1 SmartList (com.intellij.util.SmartList)1 Nullable (org.jetbrains.annotations.Nullable)1