Skip to content

Commit

Permalink
work on Lombok 1.16.8 features #192
Browse files Browse the repository at this point in the history
added support for guava table
fixed map handling
  • Loading branch information
Michail Plushnikov committed Jun 10, 2016
1 parent 3266987 commit 07fe110
Show file tree
Hide file tree
Showing 33 changed files with 1,194 additions and 435 deletions.
Expand Up @@ -48,7 +48,7 @@ protected PsiType getBuilderFieldType(@NotNull PsiType psiFieldType, @NotNull Pr
final PsiManager psiManager = PsiManager.getInstance(project);
final PsiType elementType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager);

return PsiTypeUtil.createCollectionType(elementType, psiManager, CommonClassNames.JAVA_UTIL_ARRAY_LIST);
return PsiTypeUtil.createCollectionType(psiManager, CommonClassNames.JAVA_UTIL_ARRAY_LIST, elementType);
}

@Override
Expand Down
Expand Up @@ -23,7 +23,7 @@ protected void addOneMethodParameter(@NotNull LombokLightMethodBuilder methodBui
protected void addAllMethodParameter(@NotNull LombokLightMethodBuilder methodBuilder, @NotNull PsiType psiFieldType, @NotNull String singularName) {
final PsiManager psiManager = methodBuilder.getManager();
final PsiType elementType = PsiTypeUtil.extractAllElementType(psiFieldType, psiManager);
final PsiType collectionType = PsiTypeUtil.createCollectionType(elementType, psiManager, CommonClassNames.JAVA_UTIL_COLLECTION);
final PsiType collectionType = PsiTypeUtil.createCollectionType(psiManager, CommonClassNames.JAVA_UTIL_COLLECTION, elementType);
methodBuilder.withParameter(singularName, collectionType);
}

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

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

protected void addAllMethodParameter(@NotNull LombokLightMethodBuilder methodBuilder, @NotNull PsiType psiFieldType, @NotNull String singularName) {
final PsiManager psiManager = methodBuilder.getManager();
final PsiType elementType = PsiTypeUtil.extractAllElementType(psiFieldType, psiManager);
final PsiType collectionType = PsiTypeUtil.createCollectionType(elementType, psiManager, CommonClassNames.JAVA_LANG_ITERABLE);
final PsiType collectionType = PsiTypeUtil.createCollectionType(psiManager, CommonClassNames.JAVA_LANG_ITERABLE, elementType);

methodBuilder.withParameter(singularName, collectionType);
}
Expand Down
@@ -1,8 +1,8 @@
package de.plushnikov.intellij.plugin.processor.handler.singular;

import com.intellij.openapi.project.Project;
import com.intellij.psi.CommonClassNames;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiClassType;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiModifier;
Expand All @@ -18,8 +18,8 @@
import java.util.List;

class SingularGuavaMapHandler extends SingularMapHandler {
private static final String LOMBOK_KEY = "$key";
private static final String LOMBOK_VALUE = "$value";
private static final String LOMBOK_KEY = "key";
private static final String LOMBOK_VALUE = "value";

private final String guavaQualifiedName;
private final boolean sortedCollection;
Expand All @@ -41,15 +41,21 @@ public void addBuilderField(@NotNull List<PsiField> fields, @NotNull PsiVariable
}

@NotNull
protected PsiType getBuilderFieldType(@NotNull PsiType psiType, @NotNull Project project) {
return PsiTypeUtil.getCollectionClassType((PsiClassType) psiType, project, guavaQualifiedName + ".Builder");
protected PsiType getBuilderFieldType(@NotNull PsiType psiFieldType, @NotNull Project project) {
final PsiManager psiManager = PsiManager.getInstance(project);
final PsiType keyType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 0);
final PsiType valueType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 1);

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

protected void addOneMethodParameter(@NotNull LombokLightMethodBuilder methodBuilder, @NotNull PsiType psiFieldType, @NotNull String singularName) {
// if (psiFieldType.length == 2) {
// methodBuilder.withParameter(singularName + LOMBOK_KEY, psiFieldType[0]);
// methodBuilder.withParameter(singularName + LOMBOK_VALUE, psiFieldType[1]);
// }
final PsiManager psiManager = methodBuilder.getManager();
final PsiType keyType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 0);
final PsiType valueType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 1);

methodBuilder.withParameter(LOMBOK_KEY, keyType);
methodBuilder.withParameter(LOMBOK_VALUE, valueType);
}

protected String getClearMethodBody(String psiFieldName, boolean fluentBuilder) {
Expand All @@ -60,18 +66,20 @@ protected String getClearMethodBody(String psiFieldName, boolean fluentBuilder)

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

return MessageFormat.format(codeBlockTemplate, psiFieldName, singularName, guavaQualifiedName,
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) {
final String codeBlockTemplate = "if (this.{0} == null) this.{0} = {1}.{2}; \n"
+ "this.{0}.putAll({0});{3}";

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

@Override
Expand Down
Expand Up @@ -2,7 +2,6 @@

import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiClassType;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiModifier;
Expand All @@ -17,10 +16,12 @@
import java.text.MessageFormat;
import java.util.List;

//TODO implement me
class SingularGuavaTableHandler extends SingularMapHandler {
private static final String LOMBOK_KEY = "$key";
private static final String LOMBOK_VALUE = "$value";
private static final String COM_GOOGLE_COMMON_COLLECT_TABLE = "com.google.common.collect.Table";

private static final String LOMBOK_ROW_KEY = "rowKey";
private static final String LOMBOK_COLUMN_KEY = "columnKey";
private static final String LOMBOK_VALUE = "value";

private final String guavaQualifiedName;
private final boolean sortedCollection;
Expand All @@ -42,15 +43,35 @@ public void addBuilderField(@NotNull List<PsiField> fields, @NotNull PsiVariable
}

@NotNull
protected PsiType getBuilderFieldType(@NotNull PsiType psiType, @NotNull Project project) {
return PsiTypeUtil.getCollectionClassType((PsiClassType) psiType, project, guavaQualifiedName + ".Builder");
protected PsiType getBuilderFieldType(@NotNull PsiType psiFieldType, @NotNull Project project) {
final PsiManager psiManager = PsiManager.getInstance(project);
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);

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

protected void addOneMethodParameter(@NotNull LombokLightMethodBuilder methodBuilder, @NotNull PsiType psiFieldType, @NotNull String singularName) {
// if (psiFieldType.length == 2) {
// methodBuilder.withParameter(singularName + LOMBOK_KEY, psiFieldType[0]);
// methodBuilder.withParameter(singularName + LOMBOK_VALUE, psiFieldType[1]);
// }
final PsiManager psiManager = methodBuilder.getManager();
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);

methodBuilder.withParameter(LOMBOK_ROW_KEY, rowKeyType);
methodBuilder.withParameter(LOMBOK_COLUMN_KEY, columnKeyType);
methodBuilder.withParameter(LOMBOK_VALUE, valueType);
}

protected void addAllMethodParameter(@NotNull LombokLightMethodBuilder methodBuilder, @NotNull PsiType psiFieldType, @NotNull String singularName) {
final PsiManager psiManager = methodBuilder.getManager();
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);

final PsiType collectionType = PsiTypeUtil.createCollectionType(psiManager, COM_GOOGLE_COMMON_COLLECT_TABLE, rowKeyType, columnKeyType, valueType);

methodBuilder.withParameter(singularName, collectionType);
}

protected String getClearMethodBody(String psiFieldName, boolean fluentBuilder) {
Expand All @@ -61,7 +82,7 @@ protected String getClearMethodBody(String psiFieldName, boolean fluentBuilder)

protected String getOneMethodBody(@NotNull String singularName, @NotNull String psiFieldName, @NotNull PsiType psiFieldType, @NotNull PsiManager psiManager, boolean fluentBuilder) {
final String codeBlockTemplate = "if (this.{0} == null) this.{0} = {2}.{3}; \n" +
"this.{0}.put({1}" + LOMBOK_KEY + ", {1}" + LOMBOK_VALUE + ");{4}";
"this.{0}.put(" + LOMBOK_ROW_KEY + ", " + LOMBOK_COLUMN_KEY + ", " + LOMBOK_VALUE + ");{4}";

return MessageFormat.format(codeBlockTemplate, psiFieldName, singularName, guavaQualifiedName,
sortedCollection ? "naturalOrder()" : "builder()", fluentBuilder ? "\nreturn this;" : "");
Expand Down
Expand Up @@ -2,15 +2,12 @@

import com.intellij.openapi.project.Project;
import com.intellij.psi.CommonClassNames;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.PsiSubstitutor;
import com.intellij.psi.PsiType;
import com.intellij.psi.PsiVariable;
import com.intellij.psi.search.GlobalSearchScope;
import de.plushnikov.intellij.plugin.processor.field.AccessorsInfo;
import de.plushnikov.intellij.plugin.psi.LombokLightFieldBuilder;
import de.plushnikov.intellij.plugin.psi.LombokLightMethodBuilder;
Expand All @@ -35,7 +32,7 @@ class SingularMapHandler extends AbstractSingularHandler {
public void appendBuildCall(@NotNull StringBuilder buildMethodParameters, @NotNull String fieldName) {
final String keyName = fieldName + LOMBOK_KEY;
final String valueName = fieldName + LOMBOK_VALUE;
buildMethodParameters.append("new HashMap() {{\n").
buildMethodParameters.append("new java.util.HashMap() {{\n").
append("int _count = null == ").append(keyName).append(" ? 0 : ").append(keyName).append(".size();\n").
append("for(int _i=0; _i<_count; _i++){\n").
append(" put(").append(keyName).append(".get(_i), ").append(valueName).append(".get(_i));\n").
Expand All @@ -45,49 +42,48 @@ public void appendBuildCall(@NotNull StringBuilder buildMethodParameters, @NotNu
public void addBuilderField(@NotNull List<PsiField> fields, @NotNull PsiVariable psiVariable, @NotNull PsiClass innerClass, @NotNull AccessorsInfo accessorsInfo) {
final String fieldName = accessorsInfo.removePrefix(psiVariable.getName());

final Project project = psiVariable.getProject();
final PsiManager psiManager = psiVariable.getManager();
final PsiType[] psiTypes = PsiTypeUtil.extractTypeParameters(psiVariable.getType(), psiManager);
if (psiTypes.length == 2) {
final Project project = psiVariable.getProject();

final PsiType builderFieldKeyType = getBuilderFieldType(psiTypes[0], project);
fields.add(new LombokLightFieldBuilder(psiManager, fieldName + LOMBOK_KEY, builderFieldKeyType)
.withModifier(PsiModifier.PRIVATE)
.withNavigationElement(psiVariable)
.withContainingClass(innerClass));

final PsiType builderFieldValueType = getBuilderFieldType(psiTypes[1], project);
fields.add(new LombokLightFieldBuilder(psiManager, fieldName + LOMBOK_VALUE, builderFieldValueType)
.withModifier(PsiModifier.PRIVATE)
.withNavigationElement(psiVariable)
.withContainingClass(innerClass));
}

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);

final PsiType builderFieldKeyType = getBuilderFieldType(keyType, project);
fields.add(new LombokLightFieldBuilder(psiManager, fieldName + LOMBOK_KEY, builderFieldKeyType)
.withModifier(PsiModifier.PRIVATE)
.withNavigationElement(psiVariable)
.withContainingClass(innerClass));

final PsiType builderFieldValueType = getBuilderFieldType(valueType, project);
fields.add(new LombokLightFieldBuilder(psiManager, fieldName + LOMBOK_VALUE, builderFieldValueType)
.withModifier(PsiModifier.PRIVATE)
.withNavigationElement(psiVariable)
.withContainingClass(innerClass));
}

@NotNull
protected PsiType getBuilderFieldType(@NotNull PsiType psiType, @NotNull Project project) {
return PsiTypeUtil.getGenericCollectionClassType(psiType, project, CommonClassNames.JAVA_UTIL_ARRAY_LIST);
final PsiManager psiManager = PsiManager.getInstance(project);
return PsiTypeUtil.createCollectionType(psiManager, CommonClassNames.JAVA_UTIL_ARRAY_LIST, psiType);
}

protected void addOneMethodParameter(@NotNull LombokLightMethodBuilder methodBuilder, @NotNull PsiType psiFieldType, @NotNull String singularName) {
// if (psiFieldType.length == 2) {
// methodBuilder.withParameter(singularName + KEY, psiFieldType[0]);
// methodBuilder.withParameter(singularName + VALUE, psiFieldType[1]);
// }
final PsiManager psiManager = methodBuilder.getManager();
final PsiType keyType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 0);
final PsiType valueType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 1);

methodBuilder.withParameter(singularName + KEY, keyType);
methodBuilder.withParameter(singularName + VALUE, valueType);
}

protected void addAllMethodParameter(@NotNull LombokLightMethodBuilder methodBuilder, @NotNull PsiType psiFieldType, @NotNull String singularName) {
final Project project = methodBuilder.getProject();

final PsiType collectionType;
// PsiTypeUtil.getCollectionClassType((PsiClassType) psiFieldType, project, CommonClassNames.JAVA_UTIL_MAP);
final PsiManager psiManager = methodBuilder.getManager();
final PsiType keyType = PsiTypeUtil.extractAllElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 0);
final PsiType valueType = PsiTypeUtil.extractAllElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 1);

final GlobalSearchScope globalsearchscope = GlobalSearchScope.allScope(project);
final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
PsiClass genericClass = facade.findClass(CommonClassNames.JAVA_UTIL_MAP, globalsearchscope);

PsiSubstitutor substitutor = PsiSubstitutor.EMPTY.putAll(genericClass, new PsiType[]{psiFieldType});
collectionType = JavaPsiFacade.getElementFactory(project).createType(genericClass, substitutor);
final PsiType collectionType = PsiTypeUtil.createCollectionType(psiManager, CommonClassNames.JAVA_UTIL_MAP, keyType, valueType);

methodBuilder.withParameter(singularName, collectionType);
}
Expand All @@ -108,22 +104,31 @@ protected String getOneMethodBody(@NotNull String singularName, @NotNull String
"this.{0}" + LOMBOK_VALUE + ".add({1}" + VALUE + ");" +
"{2}";

// return MessageFormat.format(codeBlockTemplate, psiFieldName, singularName, fluentBuilder ? "\nreturn this;" : "",
// psiFieldType[0].getCanonicalText(false), psiFieldType[1].getCanonicalText(false));
return "";
final PsiType keyType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 0);
final PsiType valueType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 1);

return MessageFormat.format(codeBlockTemplate, psiFieldName, singularName, fluentBuilder ? "\nreturn this;" : "",
keyType.getCanonicalText(false), valueType.getCanonicalText(false));
}

protected String getAllMethodBody(@NotNull String singularName, @NotNull PsiType psiFieldType, @NotNull PsiManager psiManager, boolean fluentBuilder) {
final String codeBlockTemplate = "if (this.{0}" + LOMBOK_KEY + " == null) '{' \n" +
"this.{0}" + LOMBOK_KEY + " = new java.util.ArrayList<{2}>(); \n" +
"this.{0}" + LOMBOK_VALUE + " = new java.util.ArrayList<{3}>(); \n" +
"'}' \n" +
"for (java.util.Map.Entry<{2},{3}> $lombokEntry : {0}.entrySet()) '{'\n" +
"for (java.util.Map.Entry<{4},{5}> $lombokEntry : {0}.entrySet()) '{'\n" +
"this.{0}" + LOMBOK_KEY + ".add($lombokEntry.getKey());\n" +
"this.{0}" + LOMBOK_VALUE + ".add($lombokEntry.getValue());\n" +
"'}'{1}";
// return MessageFormat.format(codeBlockTemplate, singularName, fluentBuilder ? "\nreturn this;" : "",
// psiFieldType[0].getCanonicalText(false), psiFieldType[1].getCanonicalText(false));
return "";

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 keyIterType = PsiTypeUtil.extractAllElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 0);
final PsiType valueIterType = PsiTypeUtil.extractAllElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 1);

return MessageFormat.format(codeBlockTemplate, singularName, fluentBuilder ? "\nreturn this;" : "",
keyType.getCanonicalText(false), valueType.getCanonicalText(false),
keyIterType.getCanonicalText(false), valueIterType.getCanonicalText(false));
}
}

0 comments on commit 07fe110

Please sign in to comment.