Skip to content

Commit

Permalink
Conflict resolution failed on renaming field together with its access…
Browse files Browse the repository at this point in the history
…ors #70

added equal/hashcode implementations
  • Loading branch information
Michail Plushnikov committed Dec 28, 2014
1 parent d7ce9e3 commit d2c2245
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 16 deletions.
Expand Up @@ -19,12 +19,11 @@
import com.intellij.psi.PsiType; import com.intellij.psi.PsiType;
import com.intellij.psi.PsiTypeParameter; import com.intellij.psi.PsiTypeParameter;
import com.intellij.psi.impl.CheckUtil; import com.intellij.psi.impl.CheckUtil;
import com.intellij.psi.impl.light.LightIdentifier;
import com.intellij.psi.impl.light.LightMethodBuilder; import com.intellij.psi.impl.light.LightMethodBuilder;
import com.intellij.psi.impl.light.LightModifierList; import com.intellij.psi.impl.light.LightModifierList;
import com.intellij.psi.impl.light.LightParameterListBuilder;
import com.intellij.util.IncorrectOperationException; import com.intellij.util.IncorrectOperationException;
import com.intellij.util.StringBuilderSpinAllocator; import com.intellij.util.StringBuilderSpinAllocator;
import de.plushnikov.intellij.plugin.util.ReflectionUtil;
import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
Expand All @@ -33,17 +32,14 @@
* @author Plushnikov Michail * @author Plushnikov Michail
*/ */
public class LombokLightMethodBuilder extends LightMethodBuilder { public class LombokLightMethodBuilder extends LightMethodBuilder {
private final LightIdentifier myNameIdentifier;
private final LombokLightReferenceListBuilder myThrowsList; private final LombokLightReferenceListBuilder myThrowsList;
private ASTNode myASTNode; private ASTNode myASTNode;
private String myName;
private PsiCodeBlock myBodyCodeBlock; private PsiCodeBlock myBodyCodeBlock;


public LombokLightMethodBuilder(@NotNull PsiManager manager, @NotNull String name) { public LombokLightMethodBuilder(@NotNull PsiManager manager, @NotNull String name) {
super(manager, JavaLanguage.INSTANCE, name, super(manager, JavaLanguage.INSTANCE, name,
new LightParameterListBuilder(manager, JavaLanguage.INSTANCE), new LombokLightModifierList(manager, JavaLanguage.INSTANCE)); new LombokLightParameterListBuilder(manager, JavaLanguage.INSTANCE),
myName = name; new LombokLightModifierList(manager, JavaLanguage.INSTANCE));
myNameIdentifier = new LombokLightIdentifier(manager, name);
myThrowsList = new LombokLightReferenceListBuilder(manager, JavaLanguage.INSTANCE, PsiReferenceList.Role.THROWS_LIST); myThrowsList = new LombokLightReferenceListBuilder(manager, JavaLanguage.INSTANCE, PsiReferenceList.Role.THROWS_LIST);
} }


Expand Down Expand Up @@ -124,7 +120,7 @@ public PsiCodeBlock getBody() {


@Override @Override
public PsiIdentifier getNameIdentifier() { public PsiIdentifier getNameIdentifier() {
return myNameIdentifier; return new LombokLightIdentifier(myManager, getName());
} }


@Override @Override
Expand Down Expand Up @@ -223,16 +219,53 @@ public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperat
return null; return null;
} }


@NotNull
@Override @Override
public String getName() { public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
return myName; ReflectionUtil.setFinalFieldPerReflection(LightMethodBuilder.class, this, String.class, name);
return this;
} }


@Override @Override
public PsiElement setName(@NotNull String name) throws IncorrectOperationException { public boolean equals(Object o) {
myName = name; if (this == o) {
return this; return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

LombokLightMethodBuilder that = (LombokLightMethodBuilder) o;

if (!getName().equals(that.getName())) {
return false;
}
if (isConstructor() != that.isConstructor()) {
return false;
}
final PsiClass containingClass = getContainingClass();
final PsiClass thatContainingClass = that.getContainingClass();
if (containingClass != null ? !containingClass.equals(thatContainingClass) : thatContainingClass != null) {
return false;
}
if (!getModifierList().equals(that.getModifierList())) {
return false;
}
if (!getParameterList().equals(that.getParameterList())) {
return false;
}
final PsiType returnType = getReturnType();
final PsiType thatReturnType = that.getReturnType();
if (returnType != null ? !returnType.equals(thatReturnType) : thatReturnType != null) {
return false;
}

return true;
}

@Override
public int hashCode() {
// should be constant because of RenameJavaMethodProcessor#renameElement and fixNameCollisionsWithInnerClassMethod(...)
return 1;
} }


@Override @Override
Expand Down
Expand Up @@ -101,4 +101,27 @@ public TextRange getTextRange() {
public String toString() { public String toString() {
return "LombokLightModifierList"; return "LombokLightModifierList";
} }

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

LombokLightModifierList that = (LombokLightModifierList) o;

if (!myAnnotations.equals(that.myAnnotations)) {
return false;
}

return true;
}

@Override
public int hashCode() {
return myAnnotations.hashCode();
}
} }
Expand Up @@ -57,4 +57,23 @@ public LombokLightParameter setModifiers(String... modifiers) {
ReflectionUtil.setFinalFieldPerReflection(LightVariableBuilder.class, this, LightModifierList.class, modifierList); ReflectionUtil.setFinalFieldPerReflection(LightVariableBuilder.class, this, LightModifierList.class, modifierList);
return this; return this;
} }

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

LombokLightParameter that = (LombokLightParameter) o;

return getType().equals(that.getType());
}

@Override
public int hashCode() {
return getType().hashCode();
}
} }
@@ -0,0 +1,32 @@
package de.plushnikov.intellij.plugin.psi;

import com.intellij.lang.Language;
import com.intellij.psi.PsiManager;
import com.intellij.psi.impl.light.LightParameterListBuilder;

import java.util.Arrays;

public class LombokLightParameterListBuilder extends LightParameterListBuilder {
public LombokLightParameterListBuilder(PsiManager manager, Language language) {
super(manager, language);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

LombokLightParameterListBuilder that = (LombokLightParameterListBuilder) o;

return Arrays.equals(getParameters(), that.getParameters());
}

@Override
public int hashCode() {
return Arrays.hashCode(getParameters());
}
}
Expand Up @@ -10,10 +10,10 @@
public class ReflectionUtil { public class ReflectionUtil {
private static final Logger LOG = Logger.getInstance(ReflectionUtil.class.getName()); private static final Logger LOG = Logger.getInstance(ReflectionUtil.class.getName());


public static <T, R> void setFinalFieldPerReflection(Class<T> clazz, T instance, Class<R> oldClazz, R newValue) { public static <T, R> void setFinalFieldPerReflection(Class<T> clazz, T instance, Class<R> fieldClass, R newValue) {
try { try {
for (Field field : clazz.getDeclaredFields()) { for (Field field : clazz.getDeclaredFields()) {
if (field.getType().equals(oldClazz)) { if (field.getType().equals(fieldClass)) {
field.setAccessible(true); field.setAccessible(true);
field.set(instance, newValue); field.set(instance, newValue);
break; break;
Expand Down
@@ -0,0 +1,20 @@
package de.plushnikov.refactor;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Builder;

@Builder
@Getter
@Setter
public class RenameBuilderClass {
private int intFielda;

public static void main(String[] args) {
RenameBuilderClass builderClass1 = new RenameBuilderClass(2);
builderClass1.getIntFielda();

RenameBuilderClass builderClass = RenameBuilderClass.builder().intFielda(123).build();
System.out.println(builderClass);
}
}
@@ -0,0 +1,15 @@
package de.plushnikov.refactor;

import lombok.Data;

@Data
public class RenameDataTest {
String data;

public static RenameDataTest factoryMethod() {
RenameDataTest foo = new RenameDataTest();
foo.setData("data");
System.out.println(foo.getData());
return foo;
}
}
@@ -0,0 +1,14 @@
package de.plushnikov.refactor;

import lombok.Getter;

@Getter
public class RenameGetterTest {
private String someString;

public static RenameGetterTest factoryMethod() {
RenameGetterTest foo = new RenameGetterTest();
foo.getSomeString();
return foo;
}
}

0 comments on commit d2c2245

Please sign in to comment.