This is an automated email from the ASF dual-hosted git repository.
chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fury.git
The following commit(s) were added to refs/heads/main by this push:
new ea979a1b feat(java): add DescriptorBuilder for easy build and copying
Descriptor (#2229)
ea979a1b is described below
commit ea979a1b850a825e0171efe70e58d04f8a736497
Author: hn <[email protected]>
AuthorDate: Wed May 14 00:42:35 2025 +0800
feat(java): add DescriptorBuilder for easy build and copying Descriptor
(#2229)
<!--
**Thanks for contributing to Fury.**
**If this is your first time opening a PR on fury, you can refer to
[CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).**
Contribution Checklist
- The **Apache Fury (incubating)** community has restrictions on the
naming of pr titles. You can also find instructions in
[CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).
- Fury has a strong focus on performance. If the PR you submit will have
an impact on performance, please benchmark it first and provide the
benchmark result here.
-->
## What does this PR do?
<!-- Describe the purpose of this PR. -->
## Related issues
#2222
<!--
Is there any related issue? Please attach here.
- #xxxx0
- #xxxx1
- #xxxx2
-->
## Does this PR introduce any user-facing change?
<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/fury/issues/new/choose) describing the
need to do so and update the document if necessary.
-->
- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?
## Benchmark
<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
-->
---------
Co-authored-by: hening <[email protected]>
---
.../main/java/org/apache/fury/type/Descriptor.java | 46 +++++++-
.../org/apache/fury/type/DescriptorBuilder.java | 120 +++++++++++++++++++++
.../java/org/apache/fury/type/DescriptorTest.java | 27 +++++
3 files changed, 188 insertions(+), 5 deletions(-)
diff --git a/java/fury-core/src/main/java/org/apache/fury/type/Descriptor.java
b/java/fury-core/src/main/java/org/apache/fury/type/Descriptor.java
index 00974c4d..0d94854a 100644
--- a/java/fury-core/src/main/java/org/apache/fury/type/Descriptor.java
+++ b/java/fury-core/src/main/java/org/apache/fury/type/Descriptor.java
@@ -85,7 +85,9 @@ public class Descriptor {
private final Field field;
private final Method readMethod;
private final Method writeMethod;
- private final FuryField furyField;
+ private FuryField furyField;
+ private boolean nullable;
+ private boolean trackingRef;
public Descriptor(Field field, TypeRef<?> typeRef, Method readMethod, Method
writeMethod) {
this.field = field;
@@ -143,14 +145,32 @@ public class Descriptor {
this.furyField = this.field == null ? null :
this.field.getAnnotation(FuryField.class);
}
+ public Descriptor(DescriptorBuilder builder) {
+ this(
+ builder.typeRef,
+ builder.typeName,
+ builder.name,
+ builder.modifier,
+ builder.declaringClass,
+ builder.field,
+ builder.readMethod,
+ builder.writeMethod);
+ this.nullable = builder.nullable;
+ this.trackingRef = builder.trackingRef;
+ this.type = builder.type;
+ this.furyField = builder.furyField;
+ }
+
+ public DescriptorBuilder copyBuilder() {
+ return new DescriptorBuilder(this);
+ }
+
public Descriptor copy(Method readMethod, Method writeMethod) {
- return new Descriptor(
- typeRef, typeName, name, modifier, declaringClass, field, readMethod,
writeMethod);
+ return new
DescriptorBuilder(this).readMethod(readMethod).writeMethod(writeMethod).build();
}
public Descriptor copyWithTypeName(String typeName) {
- return new Descriptor(
- typeRef, typeName, name, modifier, declaringClass, field, readMethod,
writeMethod);
+ return new DescriptorBuilder(this).typeName(typeName).build();
}
public Field getField() {
@@ -161,6 +181,22 @@ public class Descriptor {
return name;
}
+ public Class<?> getType() {
+ return type;
+ }
+
+ public boolean isNullable() {
+ return nullable;
+ }
+
+ public boolean isTrackingRef() {
+ return trackingRef;
+ }
+
+ public int getModifier() {
+ return modifier;
+ }
+
public String getSnakeCaseName() {
if (snakeCaseName == null) {
snakeCaseName = StringUtils.lowerCamelToLowerUnderscore(name);
diff --git
a/java/fury-core/src/main/java/org/apache/fury/type/DescriptorBuilder.java
b/java/fury-core/src/main/java/org/apache/fury/type/DescriptorBuilder.java
new file mode 100644
index 00000000..2b1b12e6
--- /dev/null
+++ b/java/fury-core/src/main/java/org/apache/fury/type/DescriptorBuilder.java
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fury.type;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import org.apache.fury.annotation.FuryField;
+import org.apache.fury.reflect.TypeRef;
+
+public class DescriptorBuilder {
+
+ TypeRef<?> typeRef;
+ Class<?> type;
+ String typeName;
+ String name;
+ int modifier;
+ String declaringClass;
+ Field field;
+ Method readMethod;
+ Method writeMethod;
+ FuryField furyField;
+ boolean nullable;
+ boolean trackingRef;
+
+ public DescriptorBuilder(Descriptor descriptor) {
+ this.typeRef = descriptor.getTypeRef();
+ this.type = descriptor.getType();
+ this.typeName = descriptor.getTypeName();
+ this.name = descriptor.getName();
+ this.modifier = descriptor.getModifier();
+ this.declaringClass = descriptor.getDeclaringClass();
+ this.field = descriptor.getField();
+ this.readMethod = descriptor.getReadMethod();
+ this.writeMethod = descriptor.getWriteMethod();
+ this.furyField = descriptor.getFuryField();
+ this.nullable = descriptor.isNullable();
+ this.trackingRef = descriptor.isTrackingRef();
+ }
+
+ public DescriptorBuilder typeRef(TypeRef<?> typeRef) {
+ this.typeRef = typeRef;
+ return this;
+ }
+
+ public DescriptorBuilder type(Class<?> type) {
+ this.type = type;
+ return this;
+ }
+
+ public DescriptorBuilder typeName(String typeName) {
+ this.typeName = typeName;
+ return this;
+ }
+
+ public DescriptorBuilder name(String name) {
+ this.name = name;
+ return this;
+ }
+
+ public DescriptorBuilder modifier(int modifier) {
+ this.modifier = modifier;
+ return this;
+ }
+
+ public DescriptorBuilder declaringClass(String declaringClass) {
+ this.declaringClass = declaringClass;
+ return this;
+ }
+
+ public DescriptorBuilder field(Field field) {
+ this.field = field;
+ return this;
+ }
+
+ public DescriptorBuilder readMethod(Method readMethod) {
+ this.readMethod = readMethod;
+ return this;
+ }
+
+ public DescriptorBuilder writeMethod(Method writeMethod) {
+ this.writeMethod = writeMethod;
+ return this;
+ }
+
+ public DescriptorBuilder nullable(boolean nullable) {
+ this.nullable = nullable;
+ return this;
+ }
+
+ public DescriptorBuilder trackingRef(boolean trackingRef) {
+ this.trackingRef = trackingRef;
+ return this;
+ }
+
+ public DescriptorBuilder furyField(FuryField furyField) {
+ this.furyField = furyField;
+ return this;
+ }
+
+ public Descriptor build() {
+ return new Descriptor(this);
+ }
+}
diff --git
a/java/fury-core/src/test/java/org/apache/fury/type/DescriptorTest.java
b/java/fury-core/src/test/java/org/apache/fury/type/DescriptorTest.java
index cdc8d4bb..abf6882f 100644
--- a/java/fury-core/src/test/java/org/apache/fury/type/DescriptorTest.java
+++ b/java/fury-core/src/test/java/org/apache/fury/type/DescriptorTest.java
@@ -94,4 +94,31 @@ public class DescriptorTest {
Descriptor.clearDescriptorCache();
Descriptor.getAllDescriptorsMap(BeanA.class);
}
+
+ @Test
+ public void testDescriptorBuilder() {
+ Descriptor descriptor = new Descriptor(TypeRef.of(A.class), "c", -1,
"TestClass");
+ // test copyBuilder
+ Descriptor descriptor1 = descriptor.copyBuilder().build();
+ Assert.assertEquals(descriptor.getTypeRef(), descriptor1.getTypeRef());
+ Assert.assertEquals(descriptor.getName(), descriptor1.getName());
+ Assert.assertEquals(descriptor.getDeclaringClass(),
descriptor1.getDeclaringClass());
+ Assert.assertEquals(descriptor.getModifier(), descriptor1.getModifier());
+ // test copyWithTypeName
+ Descriptor descriptor2 = descriptor.copyWithTypeName("test");
+ Assert.assertEquals(descriptor2.getTypeName(), "test");
+ // test builder
+ final Descriptor descriptor3 =
+ new DescriptorBuilder(descriptor)
+ .nullable(true)
+ .trackingRef(false)
+ .declaringClass("test1")
+ .build();
+ Assert.assertEquals(descriptor3.getTypeRef(), descriptor1.getTypeRef());
+ Assert.assertEquals(descriptor3.getName(), descriptor1.getName());
+ Assert.assertEquals(descriptor3.getDeclaringClass(), "test1");
+ Assert.assertEquals(descriptor3.getModifier(), descriptor1.getModifier());
+ Assert.assertTrue(descriptor3.isNullable());
+ Assert.assertFalse(descriptor3.isTrackingRef());
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]