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;
}
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())"));
}
Aggregations