default Optional.empty for Optional type (IDEA-174268)

This commit is contained in:
Anna Kozlova
2017-06-14 17:07:38 +03:00
parent 79b15d7356
commit f60676d110
4 changed files with 32 additions and 1 deletions

View File

@@ -353,7 +353,7 @@ public class OverrideImplementUtil extends OverrideImplementExploreUtil {
}
Properties properties = FileTemplateManager.getInstance(targetClass.getProject()).getDefaultProperties();
properties.setProperty(FileTemplate.ATTRIBUTE_RETURN_TYPE, returnType.getPresentableText());
properties.setProperty(FileTemplate.ATTRIBUTE_DEFAULT_RETURN_VALUE, PsiTypesUtil.getDefaultValueOfType(returnType));
properties.setProperty(FileTemplate.ATTRIBUTE_DEFAULT_RETURN_VALUE, PsiTypesUtil.getDefaultValueOfType(returnType, true));
properties.setProperty(FileTemplate.ATTRIBUTE_CALL_SUPER, callSuper(originalMethod, result));
properties.setProperty(FileTemplate.ATTRIBUTE_PLAIN_CALL_SUPER, callSuper(originalMethod, result, false));
JavaTemplateUtil.setClassAndMethodNameProperties(properties, targetClass, result);

View File

@@ -63,6 +63,11 @@ public class PsiTypesUtil {
@NotNull
public static String getDefaultValueOfType(PsiType type) {
return getDefaultValueOfType(type, false);
}
@NotNull
public static String getDefaultValueOfType(PsiType type, boolean customDefaultValues) {
if (type instanceof PsiArrayType) {
int count = type.getArrayDimensions() - 1;
PsiType componentType = type.getDeepComponentType();
@@ -88,6 +93,12 @@ public class PsiTypesUtil {
if (type instanceof PsiPrimitiveType) {
return PsiType.BOOLEAN.equals(type) ? PsiKeyword.FALSE : "0";
}
if (customDefaultValues) {
PsiType rawType = type instanceof PsiClassType ? ((PsiClassType)type).rawType() : null;
if (rawType != null && rawType.equalsToText(CommonClassNames.JAVA_UTIL_OPTIONAL)) {
return CommonClassNames.JAVA_UTIL_OPTIONAL + ".empty()";
}
}
return PsiKeyword.NULL;
}

View File

@@ -0,0 +1,12 @@
// "Implement methods" "true"
import java.util.Optional;
interface I<T> {
Optional<T> foo();
}
class Impl implements I<String> {
@Override
public Optional<String> foo() {
return Optional.empty();
}
}

View File

@@ -0,0 +1,8 @@
// "Implement methods" "true"
import java.util.Optional;
interface I<T> {
Optional<T> foo();
}
class I<caret>mpl implements I<String> {
}