ingwersaft / valueclassgenerator Goto Github PK
View Code? Open in Web Editor NEWIntelliJ IDEA Plugin that helps generating Value classes
License: GNU General Public License v3.0
IntelliJ IDEA Plugin that helps generating Value classes
License: GNU General Public License v3.0
example:
public static class Status {
private static final String STARTED = "started";
private static final String FINISHED = "finished";
private String status;
public static Status of(String given) {
if (STARTED.equals(given)) {
return Status.started();
}
if (FINISHED.equals(given)) {
return Status.finished();
}
throw new IllegalArgumentException(given + " not a valid Status");
}
public static Status started() {
return new Status(STARTED);
}
public static Status finished() {
return new Status(FINISHED);
}
private Status(String status) {
this.status = status;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Status status1 = (Status) o;
return status.equals(status1.status);
}
@Override
public int hashCode() {
return status.hashCode();
}
@Override
public String toString() {
return "Status[" + status + "]";
}
public String get() {
return status;
}
}
best if this is done with the gui: select fields which shall be value enums. for selected field(s), open new gui asking for possible values.
Trying to invoke ValueClassGenerator on a class which contains no properties at all throws the following exception:
String index out of range: -3
java.lang.StringIndexOutOfBoundsException: String index out of range: -3
at java.lang.String.substring(String.java:1967)
at com.kesselring.valuegenerator.generator.ValueClass.createEquals(ValueClass.java:94)
at com.kesselring.valuegenerator.generator.ValueClass.asString(ValueClass.java:57)
at com.kesselring.valuegenerator.generator.ValueClass.getGeneratedPsiElements(ValueClass.java:126)
at com.kesselring.valuegenerator.GeneratedValueWriter.run(GeneratedValueWriter.java:47)
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:1031)
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:141)
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.kesselring.valuegenerator.GenerateValueClassHandler.doExecute(GenerateValueClassHandler.java:64)
at com.intellij.openapi.editor.actionSystem.EditorActionHandler$4.perform(EditorActionHandler.java:217)
For better UX either disable the context menu entry or show modal dialog. KISS approach would be do nothing.
Cheers!
starting code:
public class User {
private String name, surname, address;
private Integer age;
}
to:
public class User {
public final Name name;
public final Surname surname;
public final Address address;
public final Age age;
public User(Name name, Surname surname, Address address, Age age) {
this.name = name;
this.surname = surname;
this.address = address;
this.age = age;
}
public static final class Name {
private final String name;
public String get() {
return name;
}
public Name(String name) {
this.name = name;
}
}
public static final class Surname {
private final String surname;
public String get() {
return surname;
}
public Surname(String surname) {
this.surname = surname;
}
}
public static final class Address {
private final String adress;
public String get() {
return adress;
}
public Address(String adress) {
this.adress = adress;
}
}
public static final class Age {
private final Integer age;
public Integer get() {
return age;
}
public Age(Integer age) {
this.age = age;
}
}
}
shouldn't be to redundant, which might happen if you use the intellij toString default for value classes
additionally to the constructor, create of method:
public SubClass of(final String subClass){
return new SubClass(subClass);
}
TBD
also make package choosable
new Person(new Surname("Wurst"), new Name("Hans"), new Age(1))
should equal
new Person(new Surname("Wurst"), new Name("Hans"), new Age(1))
Seems that we have the following issues:
classes
directory and thus are not detected. Putting them straight in the root directory fixes the problem.plugin.xml
is wrapped in a CDATA-tag, which seems to be removed after the plugin is compiled. Thus, IntelliJ can not parse the xml file.<idea-version since-build='...'>
is too high.Currently, I have no clue why the heck the plugin.xml
is modified in that way.
see #2 +
public static final class Builder {
public String name;
public String surname;
public String address;
public Integer age;
private Builder() {
}
public static Builder anUser() {
return new Builder();
}
public Builder withName(String name) {
this.name = name;
return this;
}
public Builder withSurname(String surname) {
this.surname = surname;
return this;
}
public Builder withAddress(String address) {
this.address = address;
return this;
}
public Builder withAge(Integer age) {
this.age = age;
return this;
}
public User build() {
User user = new User(new Name(name), new Surname(surname), new Address(address), new Age(age));
return user;
}
}
note: the builder uses the primitives, not the generated value classe
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.