Skip to content

Commit

Permalink
work on Lombok 1.16.8 features #192
Browse files Browse the repository at this point in the history
partial generation of build method code block
  • Loading branch information
Michail Plushnikov committed Jun 11, 2016
1 parent c5e93c0 commit 0869ef9
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 40 deletions.
Expand Up @@ -25,9 +25,13 @@
import java.util.List; import java.util.List;


public abstract class AbstractSingularHandler implements BuilderElementHandler { public abstract class AbstractSingularHandler implements BuilderElementHandler {

protected final String collectionQualifiedName;

private final boolean shouldGenerateFullBodyBlock; private final boolean shouldGenerateFullBodyBlock;


AbstractSingularHandler(boolean shouldGenerateFullBodyBlock) { AbstractSingularHandler(String qualifiedName, boolean shouldGenerateFullBodyBlock) {
this.collectionQualifiedName = qualifiedName;
this.shouldGenerateFullBodyBlock = shouldGenerateFullBodyBlock; this.shouldGenerateFullBodyBlock = shouldGenerateFullBodyBlock;
} }


Expand Down Expand Up @@ -159,18 +163,18 @@ public void appendBuildPrepare(@NotNull StringBuilder buildMethodCode, @NotNull
//int collectionType = 1; //int collectionType = 1;


buildMethodCode.append(MessageFormat.format( buildMethodCode.append(MessageFormat.format(
"java.util.Collection<{0}> {1};\n" + "{2}<{1}> {0};\n" +
"switch (this.{1} == null ? 0 : this.{1}.size()) '{'\n" + "switch (this.{0} == null ? 0 : this.{0}.size()) '{'\n" +
"case 0: \n" + "case 0: \n" +
" {1} = java.util.Collections.emptyList();\n" + " {0} = java.util.Collections.emptyList();\n" +
" break;\n" + " break;\n" +
"case 1: \n" + "case 1: \n" +
" {1} = java.util.Collections.singletonList(this.{1}.get(0));\n" + " {0} = java.util.Collections.singletonList(this.{0}.get(0));\n" +
" break;\n" + " break;\n" +
"default: \n" + "default: \n" +
" {1} = java.util.Collections.unmodifiableList(new java.util.ArrayList<{0}>(this.{1}));\n" + " {0} = java.util.Collections.unmodifiableList(new java.util.ArrayList<{1}>(this.{0}));\n" +
"'}'\n", "'}'\n",
elementType.getCanonicalText(false), fieldName)); fieldName, elementType.getCanonicalText(false), collectionQualifiedName));
} }


@Override @Override
Expand Down
Expand Up @@ -11,8 +11,8 @@


class SingularCollectionHandler extends AbstractSingularHandler { class SingularCollectionHandler extends AbstractSingularHandler {


SingularCollectionHandler(boolean shouldGenerateFullBodyBlock) { SingularCollectionHandler(String qualifiedName, boolean shouldGenerateFullBodyBlock) {
super(shouldGenerateFullBodyBlock); super(qualifiedName, shouldGenerateFullBodyBlock);
} }


protected void addOneMethodParameter(@NotNull LombokLightMethodBuilder methodBuilder, @NotNull PsiType psiFieldType, @NotNull String singularName) { protected void addOneMethodParameter(@NotNull LombokLightMethodBuilder methodBuilder, @NotNull PsiType psiFieldType, @NotNull String singularName) {
Expand Down
Expand Up @@ -4,6 +4,7 @@
import com.intellij.psi.CommonClassNames; import com.intellij.psi.CommonClassNames;
import com.intellij.psi.PsiManager; import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiType; import com.intellij.psi.PsiType;
import com.intellij.psi.PsiVariable;
import de.plushnikov.intellij.plugin.psi.LombokLightMethodBuilder; import de.plushnikov.intellij.plugin.psi.LombokLightMethodBuilder;
import de.plushnikov.intellij.plugin.util.PsiTypeUtil; import de.plushnikov.intellij.plugin.util.PsiTypeUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
Expand All @@ -12,12 +13,10 @@


class SingularGuavaCollectionHandler extends SingularCollectionHandler { class SingularGuavaCollectionHandler extends SingularCollectionHandler {


private final String guavaQualifiedName;
private final boolean sortedCollection; private final boolean sortedCollection;


SingularGuavaCollectionHandler(String guavaQualifiedName, boolean sortedCollection, boolean shouldGenerateFullBodyBlock) { SingularGuavaCollectionHandler(String collectionQualifiedName, boolean sortedCollection, boolean shouldGenerateFullBodyBlock) {
super(shouldGenerateFullBodyBlock); super(collectionQualifiedName, shouldGenerateFullBodyBlock);
this.guavaQualifiedName = guavaQualifiedName;
this.sortedCollection = sortedCollection; this.sortedCollection = sortedCollection;
} }


Expand All @@ -26,7 +25,7 @@ protected PsiType getBuilderFieldType(@NotNull PsiType psiFieldType, @NotNull Pr
final PsiManager psiManager = PsiManager.getInstance(project); final PsiManager psiManager = PsiManager.getInstance(project);
final PsiType elementType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager); final PsiType elementType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager);


return PsiTypeUtil.createCollectionType(psiManager, guavaQualifiedName + ".Builder", elementType); return PsiTypeUtil.createCollectionType(psiManager, collectionQualifiedName + ".Builder", elementType);
} }


protected void addAllMethodParameter(@NotNull LombokLightMethodBuilder methodBuilder, @NotNull PsiType psiFieldType, @NotNull String singularName) { protected void addAllMethodParameter(@NotNull LombokLightMethodBuilder methodBuilder, @NotNull PsiType psiFieldType, @NotNull String singularName) {
Expand All @@ -47,16 +46,30 @@ protected String getOneMethodBody(@NotNull String singularName, @NotNull String
final String codeBlockTemplate = "if (this.{0} == null) this.{0} = {2}.{3}; \n" + final String codeBlockTemplate = "if (this.{0} == null) this.{0} = {2}.{3}; \n" +
"this.{0}.add({1});{4}"; "this.{0}.add({1});{4}";


return MessageFormat.format(codeBlockTemplate, psiFieldName, singularName, guavaQualifiedName, return MessageFormat.format(codeBlockTemplate, psiFieldName, singularName, collectionQualifiedName,
sortedCollection ? "naturalOrder()" : "builder()", fluentBuilder ? "\nreturn this;" : ""); sortedCollection ? "naturalOrder()" : "builder()", fluentBuilder ? "\nreturn this;" : "");
} }


protected String getAllMethodBody(@NotNull String singularName, @NotNull PsiType psiFieldType, @NotNull PsiManager psiManager, boolean fluentBuilder) { protected String getAllMethodBody(@NotNull String singularName, @NotNull PsiType psiFieldType, @NotNull PsiManager psiManager, boolean fluentBuilder) {
final String codeBlockTemplate = "if (this.{0} == null) this.{0} = {1}.{2}; \n" final String codeBlockTemplate = "if (this.{0} == null) this.{0} = {1}.{2}; \n"
+ "this.{0}.addAll({0});{3}"; + "this.{0}.addAll({0});{3}";


return MessageFormat.format(codeBlockTemplate, singularName, guavaQualifiedName, return MessageFormat.format(codeBlockTemplate, singularName, collectionQualifiedName,
sortedCollection ? "naturalOrder()" : "builder()", fluentBuilder ? "\nreturn this;" : ""); sortedCollection ? "naturalOrder()" : "builder()", fluentBuilder ? "\nreturn this;" : "");
} }


@Override
public void appendBuildPrepare(@NotNull StringBuilder buildMethodCode, @NotNull PsiVariable psiVariable, @NotNull String fieldName) {
final PsiManager psiManager = psiVariable.getManager();
final PsiType psiFieldType = psiVariable.getType();

final PsiType elementType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager);

buildMethodCode.append(MessageFormat.format(
"{2}<{1}> {0} = " +
"this.{0} == null ? " +
"{2}.<{1}>of() : " +
"this.{0}.build();\n",
fieldName, elementType.getCanonicalText(false), collectionQualifiedName));
}
} }
Expand Up @@ -21,12 +21,10 @@ class SingularGuavaMapHandler extends SingularMapHandler {
private static final String LOMBOK_KEY = "key"; private static final String LOMBOK_KEY = "key";
private static final String LOMBOK_VALUE = "value"; private static final String LOMBOK_VALUE = "value";


private final String guavaQualifiedName;
private final boolean sortedCollection; private final boolean sortedCollection;


SingularGuavaMapHandler(String guavaQualifiedName, boolean sortedCollection, boolean shouldGenerateFullBodyBlock) { SingularGuavaMapHandler(String guavaQualifiedName, boolean sortedCollection, boolean shouldGenerateFullBodyBlock) {
super(shouldGenerateFullBodyBlock); super(guavaQualifiedName, shouldGenerateFullBodyBlock);
this.guavaQualifiedName = guavaQualifiedName;
this.sortedCollection = sortedCollection; this.sortedCollection = sortedCollection;
} }


Expand All @@ -46,7 +44,7 @@ protected PsiType getBuilderFieldType(@NotNull PsiType psiFieldType, @NotNull Pr
final PsiType keyType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 0); final PsiType keyType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 0);
final PsiType valueType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 1); final PsiType valueType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 1);


return PsiTypeUtil.createCollectionType(psiManager, guavaQualifiedName + ".Builder", keyType, valueType); return PsiTypeUtil.createCollectionType(psiManager, collectionQualifiedName + ".Builder", keyType, valueType);
} }


protected void addOneMethodParameter(@NotNull LombokLightMethodBuilder methodBuilder, @NotNull PsiType psiFieldType, @NotNull String singularName) { protected void addOneMethodParameter(@NotNull LombokLightMethodBuilder methodBuilder, @NotNull PsiType psiFieldType, @NotNull String singularName) {
Expand All @@ -68,7 +66,7 @@ protected String getOneMethodBody(@NotNull String singularName, @NotNull String
final String codeBlockTemplate = "if (this.{0} == null) this.{0} = {2}.{3}; \n" + final String codeBlockTemplate = "if (this.{0} == null) this.{0} = {2}.{3}; \n" +
"this.{0}.put(" + LOMBOK_KEY + ", " + LOMBOK_VALUE + ");{4}"; "this.{0}.put(" + LOMBOK_KEY + ", " + LOMBOK_VALUE + ");{4}";


return MessageFormat.format(codeBlockTemplate, psiFieldName, singularName, guavaQualifiedName, return MessageFormat.format(codeBlockTemplate, psiFieldName, singularName, collectionQualifiedName,
sortedCollection ? "naturalOrder()" : "builder()", sortedCollection ? "naturalOrder()" : "builder()",
fluentBuilder ? "\nreturn this;" : ""); fluentBuilder ? "\nreturn this;" : "");
} }
Expand All @@ -77,9 +75,24 @@ protected String getAllMethodBody(@NotNull String singularName, @NotNull PsiType
final String codeBlockTemplate = "if (this.{0} == null) this.{0} = {1}.{2}; \n" final String codeBlockTemplate = "if (this.{0} == null) this.{0} = {1}.{2}; \n"
+ "this.{0}.putAll({0});{3}"; + "this.{0}.putAll({0});{3}";


return MessageFormat.format(codeBlockTemplate, singularName, guavaQualifiedName, return MessageFormat.format(codeBlockTemplate, singularName, collectionQualifiedName,
sortedCollection ? "naturalOrder()" : "builder()", sortedCollection ? "naturalOrder()" : "builder()",
fluentBuilder ? "\nreturn this;" : ""); fluentBuilder ? "\nreturn this;" : "");
} }


@Override
public void appendBuildPrepare(@NotNull StringBuilder buildMethodCode, @NotNull PsiVariable psiVariable, @NotNull String fieldName) {
final PsiManager psiManager = psiVariable.getManager();
final PsiType psiFieldType = psiVariable.getType();

final PsiType keyType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 0);
final PsiType valueType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 1);

buildMethodCode.append(MessageFormat.format(
"{3}<{1}, {2}> {0} = " +
"this.{0} == null ? " +
"{3}.<{1}, {2}>of() : " +
"this.{0}.build();\n",
fieldName, keyType.getCanonicalText(false), valueType.getCanonicalText(false), collectionQualifiedName));
}
} }
Expand Up @@ -23,12 +23,10 @@ class SingularGuavaTableHandler extends SingularMapHandler {
private static final String LOMBOK_COLUMN_KEY = "columnKey"; private static final String LOMBOK_COLUMN_KEY = "columnKey";
private static final String LOMBOK_VALUE = "value"; private static final String LOMBOK_VALUE = "value";


private final String guavaQualifiedName;
private final boolean sortedCollection; private final boolean sortedCollection;


SingularGuavaTableHandler(String guavaQualifiedName, boolean sortedCollection, boolean shouldGenerateFullBodyBlock) { SingularGuavaTableHandler(String guavaQualifiedName, boolean sortedCollection, boolean shouldGenerateFullBodyBlock) {
super(shouldGenerateFullBodyBlock); super(guavaQualifiedName, shouldGenerateFullBodyBlock);
this.guavaQualifiedName = guavaQualifiedName;
this.sortedCollection = sortedCollection; this.sortedCollection = sortedCollection;
} }


Expand All @@ -49,7 +47,7 @@ protected PsiType getBuilderFieldType(@NotNull PsiType psiFieldType, @NotNull Pr
final PsiType columnKeyType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, COM_GOOGLE_COMMON_COLLECT_TABLE, 1); final PsiType columnKeyType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, COM_GOOGLE_COMMON_COLLECT_TABLE, 1);
final PsiType valueType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, COM_GOOGLE_COMMON_COLLECT_TABLE, 2); final PsiType valueType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, COM_GOOGLE_COMMON_COLLECT_TABLE, 2);


return PsiTypeUtil.createCollectionType(psiManager, guavaQualifiedName + ".Builder", rowKeyType, columnKeyType, valueType); return PsiTypeUtil.createCollectionType(psiManager, collectionQualifiedName + ".Builder", rowKeyType, columnKeyType, valueType);
} }


protected void addOneMethodParameter(@NotNull LombokLightMethodBuilder methodBuilder, @NotNull PsiType psiFieldType, @NotNull String singularName) { protected void addOneMethodParameter(@NotNull LombokLightMethodBuilder methodBuilder, @NotNull PsiType psiFieldType, @NotNull String singularName) {
Expand Down Expand Up @@ -84,16 +82,33 @@ protected String getOneMethodBody(@NotNull String singularName, @NotNull String
final String codeBlockTemplate = "if (this.{0} == null) this.{0} = {2}.{3}; \n" + final String codeBlockTemplate = "if (this.{0} == null) this.{0} = {2}.{3}; \n" +
"this.{0}.put(" + LOMBOK_ROW_KEY + ", " + LOMBOK_COLUMN_KEY + ", " + LOMBOK_VALUE + ");{4}"; "this.{0}.put(" + LOMBOK_ROW_KEY + ", " + LOMBOK_COLUMN_KEY + ", " + LOMBOK_VALUE + ");{4}";


return MessageFormat.format(codeBlockTemplate, psiFieldName, singularName, guavaQualifiedName, return MessageFormat.format(codeBlockTemplate, psiFieldName, singularName, collectionQualifiedName,
sortedCollection ? "naturalOrder()" : "builder()", fluentBuilder ? "\nreturn this;" : ""); sortedCollection ? "naturalOrder()" : "builder()", fluentBuilder ? "\nreturn this;" : "");
} }


protected String getAllMethodBody(@NotNull String singularName, @NotNull PsiType psiFieldType, @NotNull PsiManager psiManager, boolean fluentBuilder) { protected String getAllMethodBody(@NotNull String singularName, @NotNull PsiType psiFieldType, @NotNull PsiManager psiManager, boolean fluentBuilder) {
final String codeBlockTemplate = "if (this.{0} == null) this.{0} = {1}.{2}; \n" final String codeBlockTemplate = "if (this.{0} == null) this.{0} = {1}.{2}; \n"
+ "this.{0}.putAll({0});{3}"; + "this.{0}.putAll({0});{3}";


return MessageFormat.format(codeBlockTemplate, singularName, guavaQualifiedName, return MessageFormat.format(codeBlockTemplate, singularName, collectionQualifiedName,
sortedCollection ? "naturalOrder()" : "builder()", fluentBuilder ? "\nreturn this;" : ""); sortedCollection ? "naturalOrder()" : "builder()", fluentBuilder ? "\nreturn this;" : "");
} }


@Override
public void appendBuildPrepare(@NotNull StringBuilder buildMethodCode, @NotNull PsiVariable psiVariable, @NotNull String fieldName) {
final PsiManager psiManager = psiVariable.getManager();
final PsiType psiFieldType = psiVariable.getType();

final PsiType rowKeyType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, COM_GOOGLE_COMMON_COLLECT_TABLE, 0);
final PsiType columnKeyType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, COM_GOOGLE_COMMON_COLLECT_TABLE, 1);
final PsiType valueType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, COM_GOOGLE_COMMON_COLLECT_TABLE, 2);

buildMethodCode.append(MessageFormat.format(
"{4}<{1}, {2}, {3}> {0} = " +
"this.{0} == null ? " +
"{4}.<{1}, {2}, {3}>of() : " +
"this.{0}.build();\n",
fieldName, rowKeyType.getCanonicalText(false), columnKeyType.getCanonicalText(false), valueType.getCanonicalText(false), collectionQualifiedName));
}

} }
Expand Up @@ -71,10 +71,10 @@ public static BuilderElementHandler getHandlerFor(@NotNull PsiVariable psiVariab
final String qualifiedName = PsiTypeUtil.getQualifiedName(psiType); final String qualifiedName = PsiTypeUtil.getQualifiedName(psiType);
if (!isInvalidSingularType(qualifiedName)) { if (!isInvalidSingularType(qualifiedName)) {
if (COLLECTION_TYPES.contains(qualifiedName)) { if (COLLECTION_TYPES.contains(qualifiedName)) {
return new SingularCollectionHandler(shouldGenerateFullBodyBlock); return new SingularCollectionHandler(qualifiedName, shouldGenerateFullBodyBlock);
} }
if (MAP_TYPES.contains(qualifiedName)) { if (MAP_TYPES.contains(qualifiedName)) {
return new SingularMapHandler(shouldGenerateFullBodyBlock); return new SingularMapHandler(qualifiedName, shouldGenerateFullBodyBlock);
} }
if (GUAVA_COLLECTION_TYPES.contains(qualifiedName)) { if (GUAVA_COLLECTION_TYPES.contains(qualifiedName)) {
String qualifiedName2Use = GUAVA_IMMUTABLE_COLLECTION.equals(qualifiedName) ? GUAVA_IMMUTABLE_LIST : qualifiedName; String qualifiedName2Use = GUAVA_IMMUTABLE_COLLECTION.equals(qualifiedName) ? GUAVA_IMMUTABLE_LIST : qualifiedName;
Expand Down
Expand Up @@ -24,8 +24,8 @@ class SingularMapHandler extends AbstractSingularHandler {
private static final String LOMBOK_KEY = "$key"; private static final String LOMBOK_KEY = "$key";
private static final String LOMBOK_VALUE = "$value"; private static final String LOMBOK_VALUE = "$value";


SingularMapHandler(boolean shouldGenerateFullBodyBlock) { SingularMapHandler(String qualifiedName, boolean shouldGenerateFullBodyBlock) {
super(shouldGenerateFullBodyBlock); super(qualifiedName, shouldGenerateFullBodyBlock);
} }


public void addBuilderField(@NotNull List<PsiField> fields, @NotNull PsiVariable psiVariable, @NotNull PsiClass innerClass, @NotNull AccessorsInfo accessorsInfo) { public void addBuilderField(@NotNull List<PsiField> fields, @NotNull PsiVariable psiVariable, @NotNull PsiClass innerClass, @NotNull AccessorsInfo accessorsInfo) {
Expand Down
Expand Up @@ -8,10 +8,6 @@
* Unit tests for IntelliJPlugin for Lombok, based on lombok test classes * Unit tests for IntelliJPlugin for Lombok, based on lombok test classes
*/ */
public class BuilderTest extends AbstractLombokParsingTestCase { public class BuilderTest extends AbstractLombokParsingTestCase {
protected boolean shouldCompareCodeBlocks() {
return true;
}

@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
Expand Down
6 changes: 2 additions & 4 deletions testData/after/builder/singular/SingularList.java
Expand Up @@ -132,9 +132,6 @@ public SingularListBuilder<T> clearExtendsGenerics() {
@java.lang.SuppressWarnings("all") @java.lang.SuppressWarnings("all")
@javax.annotation.Generated("lombok") @javax.annotation.Generated("lombok")
public SingularList<T> build() { public SingularList<T> build() {
return new SingularList<T>(rawTypes, integers, generics, extendsGenerics);
}
/*
java.util.List<java.lang.Object> rawTypes; java.util.List<java.lang.Object> rawTypes;
switch (this.rawTypes == null ? 0 : this.rawTypes.size()) { switch (this.rawTypes == null ? 0 : this.rawTypes.size()) {
case 0: case 0:
Expand Down Expand Up @@ -187,7 +184,8 @@ public SingularList<T> build() {
default: default:
extendsGenerics = java.util.Collections.unmodifiableList(new java.util.ArrayList<Number>(this.extendsGenerics)); extendsGenerics = java.util.Collections.unmodifiableList(new java.util.ArrayList<Number>(this.extendsGenerics));
} }
*/ return new SingularList<T>(rawTypes, integers, generics, extendsGenerics);
}




@java.lang.Override @java.lang.Override
Expand Down

0 comments on commit 0869ef9

Please sign in to comment.