-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to generate builder with copy constructor (based on #…
…74)
- Loading branch information
Showing
15 changed files
with
240 additions
and
703 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
Builder Generator Idea plugin ![Java CI with Gradle](https://github.com/mjedynak/builder-generator-idea-plugin/workflows/Java%20CI%20with%20Gradle/badge.svg?branch=master) | ||
=============== | ||
Plugin for IntelliJ IDEA that adds ability to generate builder for a class and switch between them. | ||
|
||
Switching between builder and source class is similar to 'Go To Test' action. | ||
|
||
Generated builder class does not use reflection, only setter methods or constructor. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ plugins { | |
} | ||
|
||
group = "pl.mjedynak" | ||
version = "1.3.0" | ||
version = "1.4.0" | ||
|
||
repositories { | ||
mavenCentral() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/pl/mjedynak/idea/plugins/builder/psi/CopyConstructorCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package pl.mjedynak.idea.plugins.builder.psi; | ||
|
||
import com.intellij.psi.PsiClass; | ||
import com.intellij.psi.PsiElementFactory; | ||
import com.intellij.psi.PsiField; | ||
import com.intellij.psi.PsiMethod; | ||
import com.intellij.psi.util.PropertyUtilBase; | ||
import com.intellij.util.IncorrectOperationException; | ||
|
||
import static java.util.Objects.nonNull; | ||
|
||
public class CopyConstructorCreator { | ||
|
||
private final PsiElementFactory elementFactory; | ||
|
||
public CopyConstructorCreator(PsiElementFactory elementFactory) { | ||
this.elementFactory = elementFactory; | ||
} | ||
|
||
public PsiMethod copyConstructor(PsiClass builderClass, PsiClass srcClass, boolean isInnerBuilder, boolean useSingleField) { | ||
PsiField[] fields = builderClass.getAllFields(); | ||
StringBuilder text = new StringBuilder( | ||
"public " + builderClass.getNameIdentifier().getText() + "(" + srcClass.getQualifiedName() + " other) { "); | ||
|
||
for (PsiField field : fields) { | ||
text.append("this.").append(field.getName()).append(" = other"); | ||
|
||
if (srcClass.isRecord()) { | ||
text.append(".").append(field.getName()).append("();"); | ||
} else if (isInnerBuilder) { | ||
if (useSingleField) { | ||
text.append(";"); | ||
} else { | ||
text.append(".").append(field.getName()).append(";"); | ||
} | ||
} else { | ||
if (useSingleField) { | ||
text.append(";"); | ||
} else { | ||
text.append(".").append(findFieldGetter(srcClass, field).getName()).append("();"); | ||
} | ||
} | ||
} | ||
text.append(" }"); | ||
|
||
return elementFactory.createMethodFromText(text.toString(), srcClass); | ||
} | ||
|
||
private PsiMethod findFieldGetter(final PsiClass srcClass, final PsiField field) { | ||
PsiMethod method = srcClass.findMethodBySignature(PropertyUtilBase.generateGetterPrototype(field), true); | ||
|
||
if (nonNull(method)) { | ||
return method; | ||
} | ||
|
||
throw new IncorrectOperationException("Could not create copy constructor as cannot get field getters"); | ||
} | ||
|
||
} |
Oops, something went wrong.