generate members when non-physical (lombok) element exist (IDEA-164829)

This commit is contained in:
Anna.Kozlova
2016-12-05 15:19:21 +01:00
parent 78483a38f5
commit 29ea8e3828
5 changed files with 16 additions and 11 deletions
@@ -193,7 +193,9 @@ public class GenerateConstructorHandler extends GenerateMembersHandlerBase {
private static List<? extends GenerationInfo> filterOutAlreadyInsertedConstructors(PsiClass aClass, List<? extends GenerationInfo> constructors) {
boolean alreadyExist = true;
for (GenerationInfo constructor : constructors) {
alreadyExist &= aClass.findMethodBySignature((PsiMethod)constructor.getPsiMember(), false) != null;
PsiMethod existingMethod = aClass.findMethodBySignature((PsiMethod)constructor.getPsiMember(), false);
alreadyExist &= existingMethod != null && existingMethod.isPhysical();
}
if (alreadyExist) {
return Collections.emptyList();
@@ -57,8 +57,8 @@ public class GenerateEqualsHandler extends GenerateMembersHandlerBase {
final PsiMethod equalsMethod = GenerateEqualsHelper.findMethod(aClass, GenerateEqualsHelper.getEqualsSignature(project, scope));
final PsiMethod hashCodeMethod = GenerateEqualsHelper.findMethod(aClass, GenerateEqualsHelper.getHashCodeSignature());
boolean needEquals = equalsMethod == null;
boolean needHashCode = hashCodeMethod == null;
boolean needEquals = needToGenerateMethod(equalsMethod);
boolean needHashCode = needToGenerateMethod(hashCodeMethod);
if (!needEquals && !needHashCode) {
String text = aClass instanceof PsiAnonymousClass
? CodeInsightBundle.message("generate.equals.and.hashcode.already.defined.warning.anonymous")
@@ -107,6 +107,10 @@ public class GenerateEqualsHandler extends GenerateMembersHandlerBase {
return DUMMY_RESULT;
}
static boolean needToGenerateMethod(PsiMethod equalsMethod) {
return equalsMethod == null || !equalsMethod.isPhysical();
}
private static boolean hasNonStaticFields(PsiClass aClass) {
for (PsiField field : aClass.getFields()) {
if (!field.hasModifierProperty(PsiModifier.STATIC)) {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@ package com.intellij.codeInsight.generation;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.*;
import com.intellij.psi.search.GlobalSearchScope;
@@ -113,12 +112,12 @@ public class GenerateEqualsHelper implements Runnable {
public Collection<PsiMethod> generateMembers() throws IncorrectOperationException {
PsiMethod equals = null;
if (myEqualsFields != null && findMethod(myClass, getEqualsSignature(myProject, myClass.getResolveScope())) == null) {
if (myEqualsFields != null && GenerateEqualsHandler.needToGenerateMethod(findMethod(myClass, getEqualsSignature(myProject, myClass.getResolveScope())))) {
equals = createEquals();
}
PsiMethod hashCode = null;
if (myHashCodeFields != null && findMethod(myClass, getHashCodeSignature()) == null) {
if (myHashCodeFields != null && GenerateEqualsHandler.needToGenerateMethod(findMethod(myClass, getHashCodeSignature()))) {
if (myHashCodeFields.length > 0) {
hashCode = createHashCode();
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -86,6 +86,6 @@ public class PsiFieldMember extends PsiElementClassMember<PsiField> implements P
@Nullable
private static PsiMethod createMethodIfNotExists(PsiClass aClass, final PsiMethod template) {
PsiMethod existing = aClass.findMethodBySignature(template, false);
return existing == null ? template : null;
return existing == null || !existing.isPhysical() ? template : null;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -57,7 +57,7 @@ public class PsiGenerationInfo<T extends PsiMember> extends GenerationInfoBase i
else {
existingMember = null;
}
if (existingMember == null || !myMergeIfExists) {
if (existingMember == null || !existingMember.isPhysical() || !myMergeIfExists) {
PsiElement newMember = GenerateMembersUtil.insert(aClass, myMember, anchor, before);
myMember = (T)JavaCodeStyleManager.getInstance(aClass.getProject()).shortenClassReferences(newMember);
LOG.assertTrue(myMember.isValid(), myMember);