Skip to content

Commit

Permalink
CR
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov committed Oct 7, 2024
1 parent 482bca5 commit 3f2cde1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -678,11 +678,11 @@ private CodeBlock renderExpression(@Nullable ObjectDef objectDef, MethodDef meth
if (expressionDef instanceof VariableDef variableDef) {
return renderVariable(objectDef, methodDef, variableDef);
}
if (expressionDef instanceof ExpressionDef.GetClassValue getClassValue) {
return renderExpression(objectDef, methodDef, getClassValue.instance().invoke("getClass", TypeDef.of(Class.class)));
if (expressionDef instanceof ExpressionDef.InvokeGetClassMethod invokeGetClassMethod) {
return renderExpression(objectDef, methodDef, invokeGetClassMethod.instance().invoke("getClass", TypeDef.of(Class.class)));
}
if (expressionDef instanceof ExpressionDef.HashCode hashCode) {
ExpressionDef instance = hashCode.instance();
if (expressionDef instanceof ExpressionDef.InvokeHashCodeMethod invokeHashCodeMethod) {
ExpressionDef instance = invokeHashCodeMethod.instance();
TypeDef type = instance.type();
if (type.isArray()) {
if (type instanceof TypeDef.Array array && array.dimensions() > 1) {
Expand All @@ -693,7 +693,7 @@ private CodeBlock renderExpression(@Nullable ObjectDef objectDef, MethodDef meth
.invokeStatic(
"deepHashCode",
TypeDef.Primitive.BOOLEAN,
hashCode.instance()
invokeHashCodeMethod.instance()
));
}
return renderExpression(
Expand All @@ -703,7 +703,7 @@ private CodeBlock renderExpression(@Nullable ObjectDef objectDef, MethodDef meth
.invokeStatic(
"hashCode",
TypeDef.Primitive.BOOLEAN,
hashCode.instance()
invokeHashCodeMethod.instance()
));
}
if (type instanceof TypeDef.Primitive primitive) {
Expand All @@ -714,7 +714,7 @@ private CodeBlock renderExpression(@Nullable ObjectDef objectDef, MethodDef meth
.invokeStatic(
"hashCode",
TypeDef.Primitive.BOOLEAN,
hashCode.instance()
invokeHashCodeMethod.instance()
));
}
return renderExpression(objectDef, methodDef, instance.isNull().asConditionIfElse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -957,11 +957,11 @@ class KotlinPoetSourceGenerator : SourceGenerator {
if (expressionDef is PrimitiveInstance) {
return renderExpressionCode(objectDef, methodDef, expressionDef.value)
}
if (expressionDef is GetClassValue) {
if (expressionDef is InvokeGetClassMethod) {
val instanceExp = renderExpressionCode(objectDef, methodDef, expressionDef.instance)
return instanceExp.toBuilder().add(".javaClass").build()
}
if (expressionDef is HashCode) {
if (expressionDef is InvokeHashCodeMethod) {
val instanceExp = renderExpressionCode(objectDef, methodDef, expressionDef.instance)
val type = expressionDef.instance.type()
if (type.isArray) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ private static void createEqualsMethod(ClassDef.ClassDefBuilder classDefBuilder,
VariableDef o = parameterDef.get(1);

return StatementDef.multi(
new ExpressionDef.EqualsReferentially(instance, o).asConditionIf(ExpressionDef.trueValue().returning()),
instance.equalsReferentially(o).asConditionIf(ExpressionDef.trueValue().returning()),
o.isNull().asConditionOr(
new ExpressionDef.GetClassValue(instance).asCondition(" != ", new ExpressionDef.GetClassValue(o)))
instance.invokeGetClass().asCondition(" != ", new ExpressionDef.InvokeGetClassMethod(o)))
.asConditionIf(ExpressionDef.falseValue().returning()),
o.cast(selfType).newLocal("other", variableDef -> {
ExpressionDef exp = null;
Expand All @@ -215,7 +215,7 @@ private static void createEqualsMethod(ClassDef.ClassDefBuilder classDefBuilder,
// String methodName = beanProperty.getArrayDimensions() > 1 ? "deepEquals" : "equals";
// equalsMethod = ClassTypeDef.of(Arrays.class).invokeStatic(methodName, TypeDef.Primitive.BOOLEAN, firstProperty, secondProperty);
// }
ExpressionDef equalsMethod = new ExpressionDef.EqualsStructurally(firstProperty, secondProperty);
ExpressionDef equalsMethod = firstProperty.equalsStructurally(secondProperty);
newEqualsExpression = newEqualsExpression
.asConditionOr(firstProperty.isNonNull().asConditionAnd(equalsMethod));
}
Expand Down Expand Up @@ -249,13 +249,15 @@ private static void createHashCodeMethod(ClassDef.ClassDefBuilder classDefBuilde
return ExpressionDef.constant(0).returning();
}
VariableDef.MethodParameter instance = parameterDef.get(0);
return StatementDef.multi(
PropertyElement propertyElement1 = iterator.next();
return StatementDef.multi(
instance.isNull().asConditionIf(ExpressionDef.constant(0).returning()),
TypeDef.Primitive.INT.initialize(getHashCode(instance, iterator.next())).newLocal("hashValue", hashValue -> {
TypeDef.Primitive.INT.initialize(instance.getPropertyValue(propertyElement1).invokeHashCode()).newLocal("hashValue", hashValue -> {
List<StatementDef> hashUpdates = new ArrayList<>();
while (iterator.hasNext()) {
PropertyElement propertyElement = iterator.next();
ExpressionDef condition = hashValue.asCondition(" * ", ExpressionDef.constant(HASH_MULTIPLIER))
.asCondition(" + ", getHashCode(instance, iterator.next()));
.asCondition(" + ", instance.getPropertyValue(propertyElement).invokeHashCode());
hashUpdates.add(hashValue.assign(condition));
}
hashUpdates.add(hashValue.returning());
Expand All @@ -267,8 +269,4 @@ private static void createHashCodeMethod(ClassDef.ClassDefBuilder classDefBuilde
classDefBuilder.addMethod(method);
}

private static ExpressionDef.HashCode getHashCode(VariableDef instance, PropertyElement propertyElement) {
return new ExpressionDef.HashCode(instance.getPropertyValue(propertyElement));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
*/
@Experimental
public sealed interface ExpressionDef
permits ExpressionDef.And, ExpressionDef.CallInstanceMethod, ExpressionDef.CallStaticMethod, ExpressionDef.Cast, ExpressionDef.Condition, ExpressionDef.Constant, ExpressionDef.Convert, ExpressionDef.EqualsReferentially, ExpressionDef.EqualsStructurally, ExpressionDef.GetClassValue, ExpressionDef.GetPropertyValue, ExpressionDef.HashCode, ExpressionDef.IfElse, ExpressionDef.NewArrayInitialized, ExpressionDef.NewArrayOfSize, ExpressionDef.NewInstance, ExpressionDef.Or, ExpressionDef.Switch, ExpressionDef.SwitchYieldCase, TypeDef.Primitive.PrimitiveInstance, VariableDef {
permits ExpressionDef.And, ExpressionDef.CallInstanceMethod, ExpressionDef.CallStaticMethod, ExpressionDef.Cast, ExpressionDef.Condition, ExpressionDef.Constant, ExpressionDef.Convert, ExpressionDef.EqualsReferentially, ExpressionDef.EqualsStructurally, ExpressionDef.InvokeGetClassMethod, ExpressionDef.GetPropertyValue, ExpressionDef.InvokeHashCodeMethod, ExpressionDef.IfElse, ExpressionDef.NewArrayInitialized, ExpressionDef.NewArrayOfSize, ExpressionDef.NewInstance, ExpressionDef.Or, ExpressionDef.Switch, ExpressionDef.SwitchYieldCase, TypeDef.Primitive.PrimitiveInstance, VariableDef {

/**
* The condition of this variable.
Expand Down Expand Up @@ -338,6 +338,46 @@ default CallInstanceMethod invoke(MethodElement methodElement, List<ExpressionDe
);
}

/**
* The invocation of the {@link Object#hashCode()} or equivalent method for the expression.
*
* @return The hash code invocation
* @since 1.2
*/
default InvokeHashCodeMethod invokeHashCode() {
return new InvokeHashCodeMethod(this);
}

/**
* The invocation of the {@link Object#getClass()}} or equivalent method for the expression.
*
* @return The get class invocation
* @since 1.2
*/
default InvokeGetClassMethod invokeGetClass() {
return new InvokeGetClassMethod(this);
}

/**
* The structurally equals {@link Object#equals(Object)} of this expression and the other expression.
*
* @return The equals expression
* @since 1.3
*/
default EqualsStructurally equalsStructurally(ExpressionDef other) {
return new EqualsStructurally(this, other);
}

/**
* The referentially equals (==) of this expression and the other expression.
*
* @return The equals expression
* @since 1.3
*/
default EqualsReferentially equalsReferentially(ExpressionDef other) {
return new EqualsReferentially(this, other);
}

/**
* The get property value expression.
*
Expand Down Expand Up @@ -724,7 +764,7 @@ public TypeDef type() {
* @since 1.3
*/
@Experimental
record GetClassValue(ExpressionDef instance) implements ExpressionDef {
record InvokeGetClassMethod(ExpressionDef instance) implements ExpressionDef {

@Override
public TypeDef type() {
Expand All @@ -740,7 +780,7 @@ public TypeDef type() {
* @since 1.3
*/
@Experimental
record HashCode(ExpressionDef instance) implements ExpressionDef {
record InvokeHashCodeMethod(ExpressionDef instance) implements ExpressionDef {

@Override
public TypeDef type() {
Expand Down

0 comments on commit 3f2cde1

Please sign in to comment.