Skip to content

Commit

Permalink
changed all generated method bodies to have the corresponding method …
Browse files Browse the repository at this point in the history
…as the context

fixed projectlombok#593
  • Loading branch information
mplushnikov committed Feb 26, 2019
1 parent 0ffbfca commit 6aa7d36
Show file tree
Hide file tree
Showing 16 changed files with 157 additions and 160 deletions.
1 change: 1 addition & 0 deletions parts/pluginChanges.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<li>Fixed #547: Updated support for FieldNameConstants</li>
<li>Fixed #577: 'SIMPLE_BAD_CHARACTER' error thrown during IntelliJ startup</li>
<li>Fixed #584: AccessLevel not being respected for staticConstructorMethod by @All/@RequiredArgsConstructor</li>
<li>Fixed #593: Constructor body context is not set properly</li>
</ol>
</li>
<li>0.23
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,56 +167,44 @@ private boolean shouldGenerateCanEqual(@NotNull PsiClass psiClass) {
private PsiMethod createEqualsMethod(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation, boolean hasCanEqualMethod, Collection<MemberInfo> memberInfos) {
final PsiManager psiManager = psiClass.getManager();

return new LombokLightMethodBuilder(psiManager, EQUALS_METHOD_NAME)
final String blockText = createEqualsBlockString(psiClass, psiAnnotation, hasCanEqualMethod, memberInfos);
final LombokLightMethodBuilder methodBuilder = new LombokLightMethodBuilder(psiManager, EQUALS_METHOD_NAME)
.withModifier(PsiModifier.PUBLIC)
.withMethodReturnType(PsiType.BOOLEAN)
.withContainingClass(psiClass)
.withNavigationElement(psiAnnotation)
.withFinalParameter("o", PsiType.getJavaLangObject(psiManager, GlobalSearchScope.allScope(psiClass.getProject())))
.withBody(createEqualsCodeBlock(psiClass, psiAnnotation, hasCanEqualMethod, memberInfos));
}

@NotNull
private PsiCodeBlock createEqualsCodeBlock(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation, boolean hasCanEqualMethod, Collection<MemberInfo> memberInfos) {
final String blockText = createEqualsBlockString(psiClass, psiAnnotation, hasCanEqualMethod, memberInfos);
return PsiMethodUtil.createCodeBlockFromText(blockText, psiClass);
.withFinalParameter("o", PsiType.getJavaLangObject(psiManager, GlobalSearchScope.allScope(psiClass.getProject())));
methodBuilder.withBody(PsiMethodUtil.createCodeBlockFromText(blockText, methodBuilder));
return methodBuilder;
}

@NotNull
private PsiMethod createHashCodeMethod(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation, Collection<MemberInfo> memberInfos) {
final PsiManager psiManager = psiClass.getManager();

return new LombokLightMethodBuilder(psiManager, HASH_CODE_METHOD_NAME)
final String blockText = createHashcodeBlockString(psiClass, psiAnnotation, memberInfos);
final LombokLightMethodBuilder methodBuilder = new LombokLightMethodBuilder(psiManager, HASH_CODE_METHOD_NAME)
.withModifier(PsiModifier.PUBLIC)
.withMethodReturnType(PsiType.INT)
.withContainingClass(psiClass)
.withNavigationElement(psiAnnotation)
.withBody(createHashCodeBlock(psiClass, psiAnnotation, memberInfos));
}

@NotNull
private PsiCodeBlock createHashCodeBlock(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation, Collection<MemberInfo> memberInfos) {
final String blockText = createHashcodeBlockString(psiClass, psiAnnotation, memberInfos);
return PsiMethodUtil.createCodeBlockFromText(blockText, psiClass);
.withNavigationElement(psiAnnotation);
methodBuilder.withBody(PsiMethodUtil.createCodeBlockFromText(blockText, methodBuilder));
return methodBuilder;
}

@NotNull
private PsiMethod createCanEqualMethod(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation) {
final PsiManager psiManager = psiClass.getManager();

return new LombokLightMethodBuilder(psiManager, CAN_EQUAL_METHOD_NAME)
final String blockText = String.format("return other instanceof %s;", PsiTypesUtil.getClassType(psiClass).getCanonicalText());
final LombokLightMethodBuilder methodBuilder = new LombokLightMethodBuilder(psiManager, CAN_EQUAL_METHOD_NAME)
.withModifier(PsiModifier.PROTECTED)
.withMethodReturnType(PsiType.BOOLEAN)
.withContainingClass(psiClass)
.withNavigationElement(psiAnnotation)
.withFinalParameter("other", PsiType.getJavaLangObject(psiManager, GlobalSearchScope.allScope(psiClass.getProject())))
.withBody(createCanEqualCodeBlock(psiClass));
}

@NotNull
private PsiCodeBlock createCanEqualCodeBlock(@NotNull PsiClass psiClass) {
final String blockText = String.format("return other instanceof %s;", PsiTypesUtil.getClassType(psiClass).getCanonicalText());
return PsiMethodUtil.createCodeBlockFromText(blockText, psiClass);
.withFinalParameter("other", PsiType.getJavaLangObject(psiManager, GlobalSearchScope.allScope(psiClass.getProject())));
methodBuilder.withBody(PsiMethodUtil.createCodeBlockFromText(blockText, methodBuilder));
return methodBuilder;
}

private String createEqualsBlockString(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation, boolean hasCanEqualMethod, Collection<MemberInfo> memberInfos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,16 @@ Collection<PsiMethod> createToStringMethod(@NotNull PsiClass psiClass, @NotNull
public PsiMethod createToStringMethod(@NotNull PsiClass psiClass, @NotNull Collection<MemberInfo> memberInfos, @NotNull PsiAnnotation psiAnnotation) {
final PsiManager psiManager = psiClass.getManager();

return new LombokLightMethodBuilder(psiManager, METHOD_NAME)
final String paramString = createParamString(psiClass, memberInfos, psiAnnotation);
final String blockText = String.format("return \"%s(%s)\";", getSimpleClassName(psiClass), paramString);

final LombokLightMethodBuilder methodBuilder = new LombokLightMethodBuilder(psiManager, METHOD_NAME)
.withMethodReturnType(PsiType.getJavaLangString(psiManager, GlobalSearchScope.allScope(psiClass.getProject())))
.withContainingClass(psiClass)
.withNavigationElement(psiAnnotation)
.withModifier(PsiModifier.PUBLIC)
.withBody(createCodeBlock(psiClass, memberInfos, psiAnnotation));
}

@NotNull
private PsiCodeBlock createCodeBlock(@NotNull PsiClass psiClass, @NotNull Collection<MemberInfo> memberInfos, @NotNull PsiAnnotation psiAnnotation) {
final String blockText;
final String paramString = createParamString(psiClass, memberInfos, psiAnnotation);
blockText = String.format("return \"%s(%s)\";", getSimpleClassName(psiClass), paramString);
return PsiMethodUtil.createCodeBlockFromText(blockText, psiClass);
.withModifier(PsiModifier.PUBLIC);
methodBuilder.withBody(PsiMethodUtil.createCodeBlockFromText(blockText, methodBuilder));
return methodBuilder;
}

private String getSimpleClassName(@NotNull PsiClass psiClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ private static boolean checkWrongType(PsiClass psiClass) {
@Override
protected void generatePsiElements(@NotNull final PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation, @NotNull List<? super PsiElement> target) {

LombokLightMethodBuilder constructor = new LombokLightMethodBuilder(psiClass.getManager(), psiClass.getName())
LombokLightMethodBuilder constructorBuilder = new LombokLightMethodBuilder(psiClass.getManager(), psiClass.getName())
.withConstructor(true)
.withContainingClass(psiClass)
.withNavigationElement(psiAnnotation)
.withModifier(PsiModifier.PRIVATE);

String methodBody = String.format("throw new %s(%s);", "java.lang.UnsupportedOperationException", "\"This is a utility class and cannot be instantiated\"");

constructor.withBody(PsiMethodUtil.createCodeBlockFromText(methodBody, psiClass));
target.add(constructor);
constructorBuilder.withBody(PsiMethodUtil.createCodeBlockFromText(methodBody, constructorBuilder));
target.add(constructorBuilder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ protected Collection<PsiMethod> createConstructorMethod(@NotNull PsiClass psiCla

private PsiMethod createConstructor(@NotNull PsiClass psiClass, @PsiModifier.ModifierConstant @NotNull String modifier, boolean suppressConstructorProperties,
boolean useJavaDefaults, @NotNull Collection<PsiField> params, @NotNull PsiAnnotation psiAnnotation) {
LombokLightMethodBuilder constructor = new LombokLightMethodBuilder(psiClass.getManager(), getConstructorName(psiClass))
LombokLightMethodBuilder constructorBuilder = new LombokLightMethodBuilder(psiClass.getManager(), getConstructorName(psiClass))
.withConstructor(true)
.withContainingClass(psiClass)
.withNavigationElement(psiAnnotation)
Expand All @@ -269,15 +269,15 @@ private PsiMethod createConstructor(@NotNull PsiClass psiClass, @PsiModifier.Mod
String constructorPropertiesAnnotation = "java.beans.ConstructorProperties( {" +
fieldNames.stream().collect(Collectors.joining("\", \"", "\"", "\"")) +
"} ) ";
constructor.withAnnotation(constructorPropertiesAnnotation);
constructorBuilder.withAnnotation(constructorPropertiesAnnotation);
}
constructor.withAnnotations(LombokProcessorUtil.getOnX(psiAnnotation, "onConstructor"));
constructorBuilder.withAnnotations(LombokProcessorUtil.getOnX(psiAnnotation, "onConstructor"));

if (!useJavaDefaults) {
final Iterator<String> fieldNameIterator = fieldNames.iterator();
final Iterator<PsiField> fieldIterator = params.iterator();
while (fieldNameIterator.hasNext() && fieldIterator.hasNext()) {
constructor.withParameter(fieldNameIterator.next(), fieldIterator.next().getType());
constructorBuilder.withParameter(fieldNameIterator.next(), fieldIterator.next().getType());
}
}

Expand All @@ -292,13 +292,13 @@ private PsiMethod createConstructor(@NotNull PsiClass psiClass, @PsiModifier.Mod
blockText.append(String.format("this.%s = %s;\n", param.getName(), fieldInitializer));
}

constructor.withBody(PsiMethodUtil.createCodeBlockFromText(blockText.toString(), psiClass));
constructorBuilder.withBody(PsiMethodUtil.createCodeBlockFromText(blockText.toString(), constructorBuilder));

return constructor;
return constructorBuilder;
}

private PsiMethod createStaticConstructor(PsiClass psiClass, @PsiModifier.ModifierConstant String methodModifier, String staticName, boolean useJavaDefaults, Collection<PsiField> params, PsiAnnotation psiAnnotation) {
LombokLightMethodBuilder method = new LombokLightMethodBuilder(psiClass.getManager(), staticName)
LombokLightMethodBuilder methodBuilder = new LombokLightMethodBuilder(psiClass.getManager(), staticName)
.withContainingClass(psiClass)
.withNavigationElement(psiAnnotation)
.withModifier(methodModifier)
Expand All @@ -311,27 +311,28 @@ private PsiMethod createStaticConstructor(PsiClass psiClass, @PsiModifier.Modifi
// need to create new type parameters
for (int index = 0; index < classTypeParameters.length; index++) {
final PsiTypeParameter classTypeParameter = classTypeParameters[index];
final LightTypeParameterBuilder methodTypeParameter = createTypeParameter(method, index, classTypeParameter);
final LightTypeParameterBuilder methodTypeParameter = createTypeParameter(methodBuilder, index, classTypeParameter);

method.withTypeParameter(methodTypeParameter);
methodBuilder.withTypeParameter(methodTypeParameter);

substitutor = substitutor.put(classTypeParameter, PsiSubstitutor.EMPTY.substitute(methodTypeParameter));
}
}

final PsiElementFactory factory = JavaPsiFacade.getElementFactory(psiClass.getProject());
final PsiType returnType = factory.createType(psiClass, substitutor);
method.withMethodReturnType(returnType);
methodBuilder.withMethodReturnType(returnType);

if (!useJavaDefaults) {
for (PsiField param : params) {
method.withParameter(StringUtil.notNullize(param.getName()), substitutor.substitute(param.getType()));
methodBuilder.withParameter(StringUtil.notNullize(param.getName()), substitutor.substitute(param.getType()));
}
}

method.withBody(createStaticCodeBlock(returnType, useJavaDefaults, method.getParameterList()));
final String codeBlockText = createStaticCodeBlockText(returnType, useJavaDefaults, methodBuilder.getParameterList());
methodBuilder.withBody(PsiMethodUtil.createCodeBlockFromText(codeBlockText, methodBuilder));

return method;
return methodBuilder;
}

@NotNull
Expand All @@ -347,12 +348,10 @@ private LightTypeParameterBuilder createTypeParameter(LombokLightMethodBuilder m
}

@NotNull
private PsiCodeBlock createStaticCodeBlock(@NotNull PsiType psiType, boolean useJavaDefaults, @NotNull final PsiParameterList parameterList) {
final String blockText;
private String createStaticCodeBlockText(@NotNull PsiType psiType, boolean useJavaDefaults, @NotNull final PsiParameterList parameterList) {
final String psiClassName = psiType.getPresentableText();
final String paramsText = useJavaDefaults ? "" : joinParameters(parameterList);
blockText = String.format("return new %s(%s);", psiClassName, paramsText);
return PsiMethodUtil.createCodeBlockFromText(blockText, parameterList);
return String.format("return new %s(%s);", psiClassName, paramsText);
}

private String joinParameters(PsiParameterList parameterList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,26 @@ private boolean validateAccessorPrefix(@NotNull PsiField psiField, @NotNull Prob
public PsiMethod createGetterMethod(@NotNull PsiField psiField, @NotNull PsiClass psiClass, @NotNull String methodModifier) {
final String methodName = LombokUtils.getGetterName(psiField);

LombokLightMethodBuilder method = new LombokLightMethodBuilder(psiField.getManager(), methodName)
LombokLightMethodBuilder methodBuilder = new LombokLightMethodBuilder(psiField.getManager(), methodName)
.withMethodReturnType(psiField.getType())
.withContainingClass(psiClass)
.withNavigationElement(psiField);
if (StringUtil.isNotEmpty(methodModifier)) {
method.withModifier(methodModifier);
methodBuilder.withModifier(methodModifier);
}
boolean isStatic = psiField.hasModifierProperty(PsiModifier.STATIC);
if (isStatic) {
method.withModifier(PsiModifier.STATIC);
methodBuilder.withModifier(PsiModifier.STATIC);
}

method.withBody(PsiMethodUtil.createCodeBlockFromText(String.format("return %s.%s;", isStatic ? psiClass.getName() : "this", psiField.getName()), psiClass));
final String blockText = String.format("return %s.%s;", isStatic ? psiClass.getName() : "this", psiField.getName());
methodBuilder.withBody(PsiMethodUtil.createCodeBlockFromText(blockText, methodBuilder));

PsiModifierList modifierList = method.getModifierList();
PsiModifierList modifierList = methodBuilder.getModifierList();
copyAnnotations(psiField, modifierList,
LombokUtils.NON_NULL_PATTERN, LombokUtils.NULLABLE_PATTERN, LombokUtils.DEPRECATED_PATTERN);
addOnXAnnotations(PsiAnnotationSearchUtil.findAnnotation(psiField, Getter.class), modifierList, "onMethod");
return method;
return methodBuilder;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package de.plushnikov.intellij.plugin.processor.field;

import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.PsiModifierList;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiType;
import de.plushnikov.intellij.plugin.lombokconfig.ConfigDiscovery;
import de.plushnikov.intellij.plugin.problem.ProblemBuilder;
import de.plushnikov.intellij.plugin.processor.LombokPsiElementUsage;
Expand Down Expand Up @@ -122,20 +130,20 @@ public PsiMethod createSetterMethod(@NotNull PsiField psiField, @NotNull PsiClas
final String methodName = getSetterName(psiField, PsiType.BOOLEAN.equals(psiFieldType));

PsiType returnType = getReturnType(psiField);
LombokLightMethodBuilder method = new LombokLightMethodBuilder(psiField.getManager(), methodName)
LombokLightMethodBuilder methodBuilder = new LombokLightMethodBuilder(psiField.getManager(), methodName)
.withMethodReturnType(returnType)
.withContainingClass(psiClass)
.withParameter(fieldName, psiFieldType)
.withNavigationElement(psiField);
if (StringUtil.isNotEmpty(methodModifier)) {
method.withModifier(methodModifier);
methodBuilder.withModifier(methodModifier);
}
boolean isStatic = psiField.hasModifierProperty(PsiModifier.STATIC);
if (isStatic) {
method.withModifier(PsiModifier.STATIC);
methodBuilder.withModifier(PsiModifier.STATIC);
}

PsiParameter methodParameter = method.getParameterList().getParameters()[0];
PsiParameter methodParameter = methodBuilder.getParameterList().getParameters()[0];
PsiModifierList methodParameterModifierList = methodParameter.getModifierList();
if (null != methodParameterModifierList) {
final Collection<String> annotationsToCopy = PsiAnnotationUtil.collectAnnotationsToCopy(psiField,
Expand All @@ -146,17 +154,18 @@ public PsiMethod createSetterMethod(@NotNull PsiField psiField, @NotNull PsiClas
addOnXAnnotations(setterAnnotation, methodParameterModifierList, "onParam");
}

method.withBody(createCodeBlock(psiField, psiClass, returnType, isStatic, methodParameter));
final String codeBlockText = createCodeBlockText(psiField, psiClass, returnType, isStatic, methodParameter);
methodBuilder.withBody(PsiMethodUtil.createCodeBlockFromText(codeBlockText, methodBuilder));

PsiModifierList methodModifierList = method.getModifierList();
PsiModifierList methodModifierList = methodBuilder.getModifierList();
copyAnnotations(psiField, methodModifierList, LombokUtils.DEPRECATED_PATTERN);
addOnXAnnotations(setterAnnotation, methodModifierList, "onMethod");

return method;
return methodBuilder;
}

@NotNull
private PsiCodeBlock createCodeBlock(@NotNull PsiField psiField, @NotNull PsiClass psiClass, PsiType returnType, boolean isStatic, PsiParameter methodParameter) {
private String createCodeBlockText(@NotNull PsiField psiField, @NotNull PsiClass psiClass, PsiType returnType, boolean isStatic, PsiParameter methodParameter) {
final String blockText;
final String thisOrClass = isStatic ? psiClass.getName() : "this";
blockText = String.format("%s.%s = %s; ", thisOrClass, psiField.getName(), methodParameter.getName());
Expand All @@ -166,7 +175,7 @@ private PsiCodeBlock createCodeBlock(@NotNull PsiField psiField, @NotNull PsiCla
codeBlockText += "return this;";
}

return PsiMethodUtil.createCodeBlockFromText(codeBlockText, psiClass);
return codeBlockText;
}

private PsiType getReturnType(@NotNull PsiField psiField) {
Expand Down
Loading

0 comments on commit 6aa7d36

Please sign in to comment.