mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 18:18:48 +00:00
[Feature] staticConstructor should use already defined private constructor if available
This commit is contained in:
1 parent
3496a3e963
commit
d3ae4d9948
6 files changed
+135
-12
No files matched your search
@@ -14,6 +14,7 @@ Kevin Chirls <kchirls@users.noreply.github.com>
|
||||
Liu DongMiao <liudongmiao@gmail.com>
|
||||
Luan Nico <luannico27@gmail.com>
|
||||
Maarten Mulders <mthmulders@users.noreply.github.com>
|
||||
Mark Haynes <markhaynes.work@gmail.com>
|
||||
Mart Hagenaars <marthagenaars@gmail.com>
|
||||
Mateusz Matela <mateusz.matela@gmail.com>
|
||||
Michiel Verheul <cheelio@gmail.com>
|
||||
|
||||
@@ -242,8 +242,7 @@ public class HandleConstructor {
|
||||
|
||||
ASTNode source = sourceNode.get();
|
||||
boolean staticConstrRequired = staticName != null && !staticName.equals("");
|
||||
|
||||
if (skipIfConstructorExists != SkipIfConstructorExists.NO && constructorExists(typeNode) != MemberExistsResult.NOT_EXISTS) return;
|
||||
|
||||
if (skipIfConstructorExists != SkipIfConstructorExists.NO) {
|
||||
for (EclipseNode child : typeNode.down()) {
|
||||
if (child.getKind() == Kind.ANNOTATION) {
|
||||
@@ -273,12 +272,18 @@ public class HandleConstructor {
|
||||
|
||||
if (noArgs && noArgsConstructorExists(typeNode)) return;
|
||||
|
||||
ConstructorDeclaration constr = createConstructor(
|
||||
staticConstrRequired ? AccessLevel.PRIVATE : level, typeNode, fieldsToParam, forceDefaults,
|
||||
sourceNode, onConstructor);
|
||||
injectMethod(typeNode, constr);
|
||||
if (!(skipIfConstructorExists != SkipIfConstructorExists.NO && constructorExists(typeNode) != MemberExistsResult.NOT_EXISTS)) {
|
||||
ConstructorDeclaration constr = createConstructor(
|
||||
staticConstrRequired ? AccessLevel.PRIVATE : level, typeNode, fieldsToParam, forceDefaults,
|
||||
sourceNode, onConstructor);
|
||||
injectMethod(typeNode, constr);
|
||||
}
|
||||
generateStaticConstructor(staticConstrRequired, typeNode, staticName, level, fieldsToParam, source);
|
||||
}
|
||||
|
||||
private void generateStaticConstructor(boolean staticConstrRequired, EclipseNode typeNode, String staticName, AccessLevel level, Collection<EclipseNode> fields, ASTNode source) {
|
||||
if (staticConstrRequired) {
|
||||
MethodDeclaration staticConstr = createStaticConstructor(level, staticName, typeNode, fieldsToParam, source);
|
||||
MethodDeclaration staticConstr = createStaticConstructor(level, staticName, typeNode, fields, source);
|
||||
injectMethod(typeNode, staticConstr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,8 +55,10 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.core.AST.Kind;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.LombokNode;
|
||||
import lombok.delombok.LombokOptionsFactory;
|
||||
import lombok.javac.Javac;
|
||||
import lombok.javac.JavacAST;
|
||||
import lombok.javac.JavacAnnotationHandler;
|
||||
import lombok.javac.JavacNode;
|
||||
import lombok.javac.JavacTreeMaker;
|
||||
@@ -219,8 +221,8 @@ public class HandleConstructor {
|
||||
private void generate(JavacNode typeNode, AccessLevel level, List<JCAnnotation> onConstructor, List<JavacNode> fields, boolean allToDefault, String staticName, SkipIfConstructorExists skipIfConstructorExists, JavacNode source, boolean noArgs) {
|
||||
boolean staticConstrRequired = staticName != null && !staticName.equals("");
|
||||
|
||||
if (skipIfConstructorExists != SkipIfConstructorExists.NO && constructorExists(typeNode) != MemberExistsResult.NOT_EXISTS) return;
|
||||
if (skipIfConstructorExists != SkipIfConstructorExists.NO) {
|
||||
|
||||
for (JavacNode child : typeNode.down()) {
|
||||
if (child.getKind() == Kind.ANNOTATION) {
|
||||
boolean skipGeneration = annotationTypeMatches(NoArgsConstructor.class, child) ||
|
||||
@@ -230,7 +232,6 @@ public class HandleConstructor {
|
||||
if (!skipGeneration && skipIfConstructorExists == SkipIfConstructorExists.YES) {
|
||||
skipGeneration = annotationTypeMatches(Builder.class, child);
|
||||
}
|
||||
|
||||
if (skipGeneration) {
|
||||
if (staticConstrRequired) {
|
||||
// @Data has asked us to generate a constructor, but we're going to skip this instruction, as an explicit 'make a constructor' annotation
|
||||
@@ -246,8 +247,7 @@ public class HandleConstructor {
|
||||
}
|
||||
|
||||
if (noArgs && noArgsConstructorExists(typeNode)) return;
|
||||
|
||||
JCMethodDecl constr = createConstructor(staticConstrRequired ? AccessLevel.PRIVATE : level, onConstructor, typeNode, fields, allToDefault, source);
|
||||
|
||||
ListBuffer<Type> argTypes = new ListBuffer<Type>();
|
||||
for (JavacNode fieldNode : fields) {
|
||||
Type mirror = getMirrorForFieldType(fieldNode);
|
||||
@@ -258,7 +258,15 @@ public class HandleConstructor {
|
||||
argTypes.append(mirror);
|
||||
}
|
||||
List<Type> argTypes_ = argTypes == null ? null : argTypes.toList();
|
||||
injectMethod(typeNode, constr, argTypes_, Javac.createVoidType(typeNode.getSymbolTable(), CTC_VOID));
|
||||
|
||||
if (!(skipIfConstructorExists != SkipIfConstructorExists.NO && constructorExists(typeNode) != MemberExistsResult.NOT_EXISTS)) {
|
||||
JCMethodDecl constr = createConstructor(staticConstrRequired ? AccessLevel.PRIVATE : level, onConstructor, typeNode, fields, allToDefault, source);
|
||||
injectMethod(typeNode, constr, argTypes_, Javac.createVoidType(typeNode.getSymbolTable(), CTC_VOID));
|
||||
}
|
||||
generateStaticConstructor(staticConstrRequired, typeNode, staticName, level, allToDefault, fields, source, argTypes_);
|
||||
}
|
||||
|
||||
private void generateStaticConstructor(boolean staticConstrRequired, JavacNode typeNode, String staticName, AccessLevel level, boolean allToDefault, List<JavacNode> fields, LombokNode<JavacAST, JavacNode, JCTree> source, List<Type> argTypes_) {
|
||||
if (staticConstrRequired) {
|
||||
ClassSymbol sym = ((JCClassDecl) typeNode.get()).sym;
|
||||
Type returnType = sym == null ? null : sym.type;
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
public final class ValueStaticConstructorOf {
|
||||
private final String name;
|
||||
private final Double price;
|
||||
private ValueStaticConstructorOf(String name, Double price) {
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public static ValueStaticConstructorOf of(final String name, final Double price) {
|
||||
return new ValueStaticConstructorOf(name, price);
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public Double getPrice() {
|
||||
return this.price;
|
||||
}
|
||||
@java.lang.Override
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public boolean equals(final java.lang.Object o) {
|
||||
if (o == this) return true;
|
||||
if (!(o instanceof ValueStaticConstructorOf)) return false;
|
||||
final ValueStaticConstructorOf other = (ValueStaticConstructorOf) o;
|
||||
final java.lang.Object this$name = this.getName();
|
||||
final java.lang.Object other$name = other.getName();
|
||||
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
|
||||
final java.lang.Object this$price = this.getPrice();
|
||||
final java.lang.Object other$price = other.getPrice();
|
||||
if (this$price == null ? other$price != null : !this$price.equals(other$price)) return false;
|
||||
return true;
|
||||
}
|
||||
@java.lang.Override
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public int hashCode() {
|
||||
final int PRIME = 59;
|
||||
int result = 1;
|
||||
final java.lang.Object $name = this.getName();
|
||||
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
|
||||
final java.lang.Object $price = this.getPrice();
|
||||
result = result * PRIME + ($price == null ? 43 : $price.hashCode());
|
||||
return result;
|
||||
}
|
||||
@java.lang.Override
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public java.lang.String toString() {
|
||||
return "ValueStaticConstructorOf(name=" + this.getName() + ", price=" + this.getPrice() + ")";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import lombok.Value;
|
||||
public final @Value(staticConstructor = "of") class ValueStaticConstructorOf {
|
||||
private final String name;
|
||||
private final Double price;
|
||||
private ValueStaticConstructorOf(String name, Double price) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
public @java.lang.SuppressWarnings("all") String getName() {
|
||||
return this.name;
|
||||
}
|
||||
public @java.lang.SuppressWarnings("all") Double getPrice() {
|
||||
return this.price;
|
||||
}
|
||||
public @java.lang.Override @java.lang.SuppressWarnings("all") boolean equals(final java.lang.Object o) {
|
||||
if ((o == this))
|
||||
return true;
|
||||
if ((! (o instanceof ValueStaticConstructorOf)))
|
||||
return false;
|
||||
final ValueStaticConstructorOf other = (ValueStaticConstructorOf) o;
|
||||
final java.lang.Object this$name = this.getName();
|
||||
final java.lang.Object other$name = other.getName();
|
||||
if (((this$name == null) ? (other$name != null) : (! this$name.equals(other$name))))
|
||||
return false;
|
||||
final java.lang.Object this$price = this.getPrice();
|
||||
final java.lang.Object other$price = other.getPrice();
|
||||
if (((this$price == null) ? (other$price != null) : (! this$price.equals(other$price))))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
|
||||
final int PRIME = 59;
|
||||
int result = 1;
|
||||
final java.lang.Object $name = this.getName();
|
||||
result = ((result * PRIME) + (($name == null) ? 43 : $name.hashCode()));
|
||||
final java.lang.Object $price = this.getPrice();
|
||||
result = ((result * PRIME) + (($price == null) ? 43 : $price.hashCode()));
|
||||
return result;
|
||||
}
|
||||
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {
|
||||
return (((("ValueStaticConstructorOf(name=" + this.getName()) + ", price=") + this.getPrice()) + ")");
|
||||
}
|
||||
public static @java.lang.SuppressWarnings("all") ValueStaticConstructorOf of(final String name, final Double price) {
|
||||
return new ValueStaticConstructorOf(name, price);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import lombok.Value;
|
||||
@Value(staticConstructor = "of")
|
||||
public class ValueStaticConstructorOf {
|
||||
|
||||
String name;
|
||||
Double price;
|
||||
|
||||
private ValueStaticConstructorOf(String name, Double price) {
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user