java accessor generation completion: skip totally malformed fields (EA-100873 - IOE: PsiJavaParserFacadeImpl.createParameterFromText)

This commit is contained in:
peter
2017-05-08 09:55:12 +02:00
parent cac49fccf1
commit fa07225451
4 changed files with 41 additions and 43 deletions
@@ -35,6 +35,7 @@ import com.intellij.util.VisibilityUtil;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.java.generate.exception.GenerateCodeException;
import javax.swing.*;
import java.util.ArrayList;
@@ -90,8 +91,11 @@ public class JavaGenerateMemberCompletionContributor {
if (field instanceof PsiEnumConstant) continue;
List<PsiMethod> prototypes = ContainerUtil.newSmartList();
Collections.addAll(prototypes, GetterSetterPrototypeProvider.generateGetterSetters(field, true));
Collections.addAll(prototypes, GetterSetterPrototypeProvider.generateGetterSetters(field, false));
try {
Collections.addAll(prototypes, GetterSetterPrototypeProvider.generateGetterSetters(field, true, false));
Collections.addAll(prototypes, GetterSetterPrototypeProvider.generateGetterSetters(field, false, false));
}
catch (GenerateCodeException ignore) { }
for (final PsiMethod prototype : prototypes) {
if (parent.findMethodBySignature(prototype, false) == null && addedSignatures.add(prototype.getSignature(PsiSubstitutor.EMPTY))) {
Icon icon = prototype.getIcon(Iconable.ICON_FLAG_VISIBILITY);
@@ -701,7 +701,7 @@ public class GenerateMembersUtil {
return generatePrototype(field, result);
}
@Nullable
@NotNull
private static PsiMethod generatePrototype(@NotNull PsiField field, PsiMethod result) {
return setVisibility(field, annotateOnOverrideImplement(field.getContainingClass(), result));
}
@@ -52,14 +52,14 @@ public abstract class GetterSetterPrototypeProvider {
public static PsiMethod[] generateGetterSetters(PsiField field,
boolean generateGetter,
boolean invalidTemplate) {
boolean ignoreInvalidTemplate) {
for (GetterSetterPrototypeProvider provider : Extensions.getExtensions(EP_NAME)) {
if (provider.canGeneratePrototypeFor(field)) {
return generateGetter ? provider.generateGetters(field) : provider.generateSetters(field);
}
}
return new PsiMethod[]{generateGetter ? GenerateMembersUtil.generateGetterPrototype(field, invalidTemplate) :
GenerateMembersUtil.generateSetterPrototype(field, invalidTemplate)};
return new PsiMethod[]{generateGetter ? GenerateMembersUtil.generateGetterPrototype(field, ignoreInvalidTemplate) :
GenerateMembersUtil.generateSetterPrototype(field, ignoreInvalidTemplate)};
}
public static boolean isReadOnlyProperty(PsiField field) {
@@ -452,50 +452,44 @@ public class PropertyUtil {
VariableKind kind = codeStyleManager.getVariableKind(field);
String propertyName = codeStyleManager.variableNameToPropertyName(name, kind);
String setName = suggestSetterName(field);
try {
PsiMethod setMethod = factory
.createMethodFromText(factory.createMethod(setName, returnSelf ? factory.createType(containingClass) : PsiType.VOID).getText(),
field);
String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER);
PsiParameter param = factory.createParameter(parameterName, field.getType());
PsiMethod setMethod = factory
.createMethodFromText(factory.createMethod(setName, returnSelf ? factory.createType(containingClass) : PsiType.VOID).getText(),
field);
String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER);
PsiParameter param = factory.createParameter(parameterName, field.getType());
NullableNotNullManager.getInstance(project).copyNullableOrNotNullAnnotation(field, param);
NullableNotNullManager.getInstance(project).copyNullableOrNotNullAnnotation(field, param);
setMethod.getParameterList().add(param);
PsiUtil.setModifierProperty(setMethod, PsiModifier.PUBLIC, true);
PsiUtil.setModifierProperty(setMethod, PsiModifier.STATIC, isStatic);
setMethod.getParameterList().add(param);
PsiUtil.setModifierProperty(setMethod, PsiModifier.PUBLIC, true);
PsiUtil.setModifierProperty(setMethod, PsiModifier.STATIC, isStatic);
@NonNls StringBuilder buffer = new StringBuilder();
buffer.append("{\n");
if (name.equals(parameterName)) {
if (!isStatic) {
buffer.append("this.");
}
else {
String className = containingClass.getName();
if (className != null) {
buffer.append(className);
buffer.append(".");
}
@NonNls StringBuilder buffer = new StringBuilder();
buffer.append("{\n");
if (name.equals(parameterName)) {
if (!isStatic) {
buffer.append("this.");
}
else {
String className = containingClass.getName();
if (className != null) {
buffer.append(className);
buffer.append(".");
}
}
buffer.append(name);
buffer.append("=");
buffer.append(parameterName);
buffer.append(";\n");
if (returnSelf) {
buffer.append("return this;\n");
}
buffer.append("}");
PsiCodeBlock body = factory.createCodeBlockFromText(buffer.toString(), null);
setMethod.getBody().replace(body);
setMethod = (PsiMethod)CodeStyleManager.getInstance(project).reformat(setMethod);
return setMethod;
}
catch (IncorrectOperationException e) {
LOG.error(e);
return null;
buffer.append(name);
buffer.append("=");
buffer.append(parameterName);
buffer.append(";\n");
if (returnSelf) {
buffer.append("return this;\n");
}
buffer.append("}");
PsiCodeBlock body = factory.createCodeBlockFromText(buffer.toString(), null);
setMethod.getBody().replace(body);
setMethod = (PsiMethod)CodeStyleManager.getInstance(project).reformat(setMethod);
return setMethod;
}
/** @deprecated use {@link NullableNotNullManager#copyNullableOrNotNullAnnotation(PsiModifierListOwner, PsiModifierListOwner)} (to be removed in IDEA 17) */