davidburkett / builderberg Goto Github PK
View Code? Open in Web Editor NEWIntelliJ IDEA plugin to generate inner builders with constraint validation
License: MIT License
IntelliJ IDEA plugin to generate inner builders with constraint validation
License: MIT License
Should call Arrays.equals instead of arrays.equals()
If it's possible, add the "Generate Inner Builder" option to the Generate menu that can be accessed by right-clicking a class name in the code editor.
Allow annotation of fields to include in comparison, as well as the ordering of the comparison and direction of comparison. Example:
@BuilderComparison(1, inverse)
would compare that field first by calling compareTo and then taking the inverse of the result.
BuilderConstraint has a default of 0.0 for minValue but it is ignored. For instance the generated validate method has no content if I simply annotate a double field with @BuilderConstraint
Using the shortcut keys, builder generates for the file selected in tree view, which may not be the same file open in editor.
One way to do this is by adding an annotation to custom methods like @BuilderCustom
There is a currently a potential bug where the generated hashCode()
method will throw a NullPointerException
. Consider the following generated code:
public int hashCode() {
int result = 17;
result = 31 * result + firstName.hashCode();
result = 31 * result + middleName.hashCode();
result = 31 * result + lastName.hashCode();
return result;
}
If middle name is null, this will throw a NullPointerException
. Please use Objects.hashCode()
as it will convert a null value to the hash code of zero.
Should call Arrays.toString() instead
Sometimes when working with certain persistence libraries (like mongo) the object sets the field annotated with @id to the generated id. Your current implementation will not support that.
Copy javadoc from fields into getters, and add additional javadoc like "Will never be null" when using @BuilderConstraint(notNull = true). Also, add javadoc for what validation will take place in the constructor and in the builder validate method.
null
java.lang.IllegalArgumentException
at com.github.davidburkett.builderberg.generators.ValidationGenerator.generateNotEmptyStatement(ValidationGenerator.java:86)
at com.github.davidburkett.builderberg.generators.ValidationGenerator.generateValidationStatement(ValidationGenerator.java:44)
at com.github.davidburkett.builderberg.generators.ValidationGenerator.generateValidationForField(ValidationGenerator.java:29)
at com.github.davidburkett.builderberg.generators.BuilderClassGenerator.generateWithSetters(BuilderClassGenerator.java:73)
at com.github.davidburkett.builderberg.generators.BuilderClassGenerator.createBuilderClass(BuilderClassGenerator.java:37)
at com.github.davidburkett.builderberg.BuilderbergRunnable.run(BuilderbergRunnable.java:30)
at com.intellij.openapi.command.WriteCommandAction$1.run(WriteCommandAction.java:252)
at com.intellij.openapi.command.WriteCommandAction$Simple.run(WriteCommandAction.java:234)
at com.intellij.openapi.application.RunResult.run(RunResult.java:35)
at com.intellij.openapi.command.WriteCommandAction.lambda$null$1(WriteCommandAction.java:171)
at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:1003)
at com.intellij.openapi.command.WriteCommandAction.lambda$performWriteCommandAction$2(WriteCommandAction.java:170)
at com.intellij.openapi.command.WriteCommandAction.lambda$doExecuteCommand$4(WriteCommandAction.java:210)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:149)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:119)
at com.intellij.openapi.command.WriteCommandAction.doExecuteCommand(WriteCommandAction.java:212)
at com.intellij.openapi.command.WriteCommandAction.performWriteCommandAction(WriteCommandAction.java:168)
at com.intellij.openapi.command.WriteCommandAction.execute(WriteCommandAction.java:151)
at com.intellij.openapi.command.WriteCommandAction.runWriteCommandAction(WriteCommandAction.java:254)
at com.intellij.openapi.command.WriteCommandAction.runWriteCommandAction(WriteCommandAction.java:241)
at com.github.davidburkett.builderberg.BuilderbergAction.actionPerformed(BuilderbergAction.java:29)
at com.intellij.openapi.actionSystem.ex.ActionUtil$1.run(ActionUtil.java:220)
at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAware(ActionUtil.java:237)
at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.lambda$actionPerformed$0(ActionMenuItem.java:321)
at com.intellij.openapi.wm.impl.FocusManagerImpl.runOnOwnContext(FocusManagerImpl.java:911)
at com.intellij.openapi.wm.impl.IdeFocusManagerImpl.runOnOwnContext(IdeFocusManagerImpl.java:136)
at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.actionPerformed(ActionMenuItem.java:311)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at com.intellij.openapi.actionSystem.impl.ActionMenuItem.lambda$fireActionPerformed$0(ActionMenuItem.java:130)
at com.intellij.openapi.application.TransactionGuardImpl.runSyncTransaction(TransactionGuardImpl.java:88)
at com.intellij.openapi.application.TransactionGuardImpl.lambda$submitTransaction$1(TransactionGuardImpl.java:111)
at com.intellij.openapi.application.TransactionGuardImpl.submitTransaction(TransactionGuardImpl.java:120)
at com.intellij.openapi.application.TransactionGuard.submitTransaction(TransactionGuard.java:122)
at com.intellij.openapi.actionSystem.impl.ActionMenuItem.fireActionPerformed(ActionMenuItem.java:130)
at com.intellij.ui.plaf.beg.BegMenuItemUI.doClick(BegMenuItemUI.java:521)
at com.intellij.ui.plaf.beg.BegMenuItemUI.access$300(BegMenuItemUI.java:48)
at com.intellij.ui.plaf.beg.BegMenuItemUI$MyMouseInputHandler.mouseReleased(BegMenuItemUI.java:541)
at java.awt.Component.processMouseEvent(Component.java:6541)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6306)
at java.awt.Container.processEvent(Container.java:2237)
at java.awt.Component.dispatchEventImpl(Component.java:4897)
at java.awt.Container.dispatchEventImpl(Container.java:2295)
at java.awt.Component.dispatchEvent(Component.java:4719)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4526)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4467)
at java.awt.Container.dispatchEventImpl(Container.java:2281)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4719)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:764)
at java.awt.EventQueue.access$500(EventQueue.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:715)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:737)
at java.awt.EventQueue$4.run(EventQueue.java:735)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:734)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:822)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:646)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:366)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Should be:
if (intValue < 0) {
throw new AssertionError(“Constraint not met: minValue: 0.0");
}
Instead of this:
if (intValue < 0.000000) {
throw new AssertionError(“Constraint not met: minValue: 0.0”);
}
Please add an options dialog for custom generated code including hash()
, equals()
, and toString()
.
Asserts have limitations, and don't work for all users. Provide a way to specify what type of exception should be thrown when constraints are violated.
This is good stuff! I am wondering since I have public final fields on data objects that the getters not be generated. BuilderOptions could have an option there for turning on/off the behavior.
Implement support for default values using a new @BuilderDefault Annotation.
It would be nice if you had private final Collection<String> myStrings
that the builder would allow .withMyStrings("String One", "String Two", ...)
. You can leverage Collections.addAll() as seen here (https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#addAll-java.util.Collection-T...-) to make this easy
One thing that will hold this plugin back as far as large scale adoption is lack of test coverage. My assumption is that there are others out there testing plugins like this that modify code so you could follow one of those for an example.
"A" is a class that has had the builder generated
"a" is an instance of A
A newA = A.builder(a).withFieldOne("someNewValue").build();
"newA" has all of the same values as "a" but "fieldOne" has been updated
Example:
public class clazz1 {
private static final long UNSIGNED_INT_MAX = 4294967295L;
}
will try to add a with method for UNSIGNED_INT_MAX, even though it's static
Booleans currently only generate 'getBooleanName' methods, but it's standard to also generate an 'isBooleanName' method.
Need to finish populating the Readme
Still not sure the best way to handle this. I'm open to suggestions.
I would love to have support for polymorphism in Builderberg.
Simple example:
public abstract class Vehicle {
private final String make;
// [...]
}
public class Car extends Vehicle {
private final int seatingCapacity;
// [...]
}
public class Truck extends Vehicle {
private final int payloadCapacity;
// [...]
}
As a suggestion, this is how resulting builders could look like:
public abstract class Vehicle {
// [...]
// protected instead of public?
// non-final?
protected static class Builder {
// [ all fields of type Vehicle ]
}
}
public class Car extends Vehicle {
// [...]
public static final class Builder extends Vehicle.Builder {
// [ all fields of type Car ]
// PLUS:
// Overrides for all methods of Vehicle.Builder (in order to allow strongly-typed fluent usage) - delegating to Vehicle.Builder but casting return value to Car.Builder)
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.