From 933be58956cdde02a1cab51ed9891d41e1fa504f Mon Sep 17 00:00:00 2001 From: Looly Date: Sat, 3 Jan 2026 11:55:56 +0800 Subject: [PATCH] fix code --- .../core/annotation/AnnotatedElementUtil.java | 24 ++-- .../annotation/AnnotationMappingProxy.java | 2 +- .../annotation/GenericAnnotationMapping.java | 2 +- .../annotation/ResolvedAnnotationMapping.java | 8 +- .../elements/MetaAnnotatedElement.java | 2 +- .../RepeatableMetaAnnotatedElement.java | 6 +- .../GenericAnnotationMappingTest.java | 34 ++--- .../annotation/MetaAnnotatedElementTest.java | 32 ++--- .../RepeatableMetaAnnotatedElementTest.java | 34 ++--- .../ResolvedAnnotationMappingTest.java | 70 ++++----- .../java/cn/hutool/v7/socket/ChannelUtil.java | 40 +++++- .../java/cn/hutool/v7/socket/SocketUtil.java | 136 +++++++++++++----- .../cn/hutool/v7/socket/aio/AioClient.java | 9 +- .../cn/hutool/v7/socket/aio/AioServer.java | 5 +- .../cn/hutool/v7/socket/aio/AioSession.java | 4 +- .../cn/hutool/v7/socket/nio/NioClient.java | 5 +- .../cn/hutool/v7/socket/udp/UdpSession.java | 15 +- .../java/cn/hutool/v7/socket/udp/UdpUtil.java | 15 ++ 18 files changed, 283 insertions(+), 160 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/v7/core/annotation/AnnotatedElementUtil.java b/hutool-core/src/main/java/cn/hutool/v7/core/annotation/AnnotatedElementUtil.java index 8a2affb004..63baab4d30 100644 --- a/hutool-core/src/main/java/cn/hutool/v7/core/annotation/AnnotatedElementUtil.java +++ b/hutool-core/src/main/java/cn/hutool/v7/core/annotation/AnnotatedElementUtil.java @@ -633,12 +633,12 @@ public class AnnotatedElementUtil { } collector = ObjUtil.defaultIfNull(collector, RepeatableAnnotationCollector.none()); if (resolved) { - return RepeatableMetaAnnotatedElement.create( - collector, element, (source, annotation) -> ResolvedAnnotationMapping.create((ResolvedAnnotationMapping)source, annotation, true) + return RepeatableMetaAnnotatedElement.of( + collector, element, (source, annotation) -> ResolvedAnnotationMapping.of((ResolvedAnnotationMapping)source, annotation, true) ); } - return RepeatableMetaAnnotatedElement.create( - collector, element, (source, annotation) -> GenericAnnotationMapping.create(annotation, Objects.isNull(source)) + return RepeatableMetaAnnotatedElement.of( + collector, element, (source, annotation) -> GenericAnnotationMapping.of(annotation, Objects.isNull(source)) ); } @@ -676,8 +676,8 @@ public class AnnotatedElementUtil { * @return {@link MetaAnnotatedElement}实例 */ static MetaAnnotatedElement getResolvedMetaElementCache(final AnnotatedElement element) { - return RESOLVED_ELEMENT_CACHE.computeIfAbsent(element, ele -> MetaAnnotatedElement.create( - element, (source, annotation) -> ResolvedAnnotationMapping.create(source, annotation, true) + return RESOLVED_ELEMENT_CACHE.computeIfAbsent(element, ele -> MetaAnnotatedElement.of( + element, (source, annotation) -> ResolvedAnnotationMapping.of(source, annotation, true) )); } @@ -688,8 +688,8 @@ public class AnnotatedElementUtil { * @return {@link MetaAnnotatedElement}实例 */ static MetaAnnotatedElement getMetaElementCache(final AnnotatedElement element) { - return ELEMENT_CACHE.computeIfAbsent(element, ele -> MetaAnnotatedElement.create( - element, (source, annotation) -> GenericAnnotationMapping.create(annotation, Objects.isNull(source)) + return ELEMENT_CACHE.computeIfAbsent(element, ele -> MetaAnnotatedElement.of( + element, (source, annotation) -> GenericAnnotationMapping.of(annotation, Objects.isNull(source)) )); } @@ -700,8 +700,8 @@ public class AnnotatedElementUtil { * @return {@link MetaAnnotatedElement}实例 */ static RepeatableMetaAnnotatedElement getResolvedRepeatableMetaElementCache(final AnnotatedElement element) { - return RESOLVED_REPEATABLE_ELEMENT_CACHE.computeIfAbsent(element, ele -> RepeatableMetaAnnotatedElement.create( - element, (source, annotation) -> ResolvedAnnotationMapping.create(source, annotation, true) + return RESOLVED_REPEATABLE_ELEMENT_CACHE.computeIfAbsent(element, ele -> RepeatableMetaAnnotatedElement.of( + element, (source, annotation) -> ResolvedAnnotationMapping.of(source, annotation, true) )); } @@ -712,8 +712,8 @@ public class AnnotatedElementUtil { * @return {@link MetaAnnotatedElement}实例 */ static RepeatableMetaAnnotatedElement getRepeatableMetaElementCache(final AnnotatedElement element) { - return REPEATABLE_ELEMENT_CACHE.computeIfAbsent(element, ele -> RepeatableMetaAnnotatedElement.create( - element, (source, annotation) -> GenericAnnotationMapping.create(annotation, Objects.isNull(source)) + return REPEATABLE_ELEMENT_CACHE.computeIfAbsent(element, ele -> RepeatableMetaAnnotatedElement.of( + element, (source, annotation) -> GenericAnnotationMapping.of(annotation, Objects.isNull(source)) )); } diff --git a/hutool-core/src/main/java/cn/hutool/v7/core/annotation/AnnotationMappingProxy.java b/hutool-core/src/main/java/cn/hutool/v7/core/annotation/AnnotationMappingProxy.java index 67470107bf..822b44e520 100644 --- a/hutool-core/src/main/java/cn/hutool/v7/core/annotation/AnnotationMappingProxy.java +++ b/hutool-core/src/main/java/cn/hutool/v7/core/annotation/AnnotationMappingProxy.java @@ -67,7 +67,7 @@ public final class AnnotationMappingProxy implements Invoc * @return 代理对象 */ @SuppressWarnings("unchecked") - public static A create(final Class annotationType, final AnnotationMapping mapping) { + public static A of(final Class annotationType, final AnnotationMapping mapping) { Objects.requireNonNull(annotationType); Objects.requireNonNull(mapping); final AnnotationMappingProxy invocationHandler = new AnnotationMappingProxy<>(mapping); diff --git a/hutool-core/src/main/java/cn/hutool/v7/core/annotation/GenericAnnotationMapping.java b/hutool-core/src/main/java/cn/hutool/v7/core/annotation/GenericAnnotationMapping.java index bf24201e01..c1108666c4 100644 --- a/hutool-core/src/main/java/cn/hutool/v7/core/annotation/GenericAnnotationMapping.java +++ b/hutool-core/src/main/java/cn/hutool/v7/core/annotation/GenericAnnotationMapping.java @@ -40,7 +40,7 @@ public class GenericAnnotationMapping implements AnnotationMapping { * @param isRoot 是否根注解 * @return {@code GenericAnnotationMapping}实例 */ - public static GenericAnnotationMapping create(final Annotation annotation, final boolean isRoot) { + public static GenericAnnotationMapping of(final Annotation annotation, final boolean isRoot) { return new GenericAnnotationMapping(annotation, isRoot); } diff --git a/hutool-core/src/main/java/cn/hutool/v7/core/annotation/ResolvedAnnotationMapping.java b/hutool-core/src/main/java/cn/hutool/v7/core/annotation/ResolvedAnnotationMapping.java index a9c543821b..31634d7a7e 100644 --- a/hutool-core/src/main/java/cn/hutool/v7/core/annotation/ResolvedAnnotationMapping.java +++ b/hutool-core/src/main/java/cn/hutool/v7/core/annotation/ResolvedAnnotationMapping.java @@ -133,8 +133,8 @@ public class ResolvedAnnotationMapping implements AnnotationMapping * @param resolveAnnotationAttribute 是否解析注解属性,为{@code true}时获得的注解皆支持属性覆盖与属性别名机制 * @return 注解映射对象 */ - public static ResolvedAnnotationMapping create(final Annotation annotation, final boolean resolveAnnotationAttribute) { - return create(null, annotation, resolveAnnotationAttribute); + public static ResolvedAnnotationMapping of(final Annotation annotation, final boolean resolveAnnotationAttribute) { + return of(null, annotation, resolveAnnotationAttribute); } /** @@ -146,7 +146,7 @@ public class ResolvedAnnotationMapping implements AnnotationMapping * @param resolveAnnotationAttribute 是否解析注解属性,为{@code true}时获得的注解皆支持属性覆盖与属性别名机制 * @return 注解映射对象 */ - public static ResolvedAnnotationMapping create( + public static ResolvedAnnotationMapping of( final ResolvedAnnotationMapping source, final Annotation annotation, final boolean resolveAnnotationAttribute) { return new ResolvedAnnotationMapping(source, annotation, resolveAnnotationAttribute); } @@ -280,7 +280,7 @@ public class ResolvedAnnotationMapping implements AnnotationMapping if (Objects.isNull(proxied)) { synchronized (this) { if (Objects.isNull(proxied)) { - proxied = AnnotationMappingProxy.create(annotationType(), this); + proxied = AnnotationMappingProxy.of(annotationType(), this); } } } diff --git a/hutool-core/src/main/java/cn/hutool/v7/core/annotation/elements/MetaAnnotatedElement.java b/hutool-core/src/main/java/cn/hutool/v7/core/annotation/elements/MetaAnnotatedElement.java index f054ff350f..9373ea91c3 100644 --- a/hutool-core/src/main/java/cn/hutool/v7/core/annotation/elements/MetaAnnotatedElement.java +++ b/hutool-core/src/main/java/cn/hutool/v7/core/annotation/elements/MetaAnnotatedElement.java @@ -75,7 +75,7 @@ public class MetaAnnotatedElement> imple * @param {@link AnnotationMapping}类型 * @return {@link AnnotatedElement}上的注解结构 */ - public static > MetaAnnotatedElement create( + public static > MetaAnnotatedElement of( final AnnotatedElement element, final BiFunction mappingFactory) { return new MetaAnnotatedElement<>(element, mappingFactory); } diff --git a/hutool-core/src/main/java/cn/hutool/v7/core/annotation/elements/RepeatableMetaAnnotatedElement.java b/hutool-core/src/main/java/cn/hutool/v7/core/annotation/elements/RepeatableMetaAnnotatedElement.java index ed46c11f22..e65dbb91ee 100644 --- a/hutool-core/src/main/java/cn/hutool/v7/core/annotation/elements/RepeatableMetaAnnotatedElement.java +++ b/hutool-core/src/main/java/cn/hutool/v7/core/annotation/elements/RepeatableMetaAnnotatedElement.java @@ -85,9 +85,9 @@ public class RepeatableMetaAnnotatedElement {@link AnnotationMapping}类型 * @return {@link AnnotatedElement}上的注解结构 */ - public static > RepeatableMetaAnnotatedElement create( + public static > RepeatableMetaAnnotatedElement of( final AnnotatedElement element, final BiFunction mappingFactory) { - return create(RepeatableAnnotationCollector.standard(), element, mappingFactory); + return of(RepeatableAnnotationCollector.standard(), element, mappingFactory); } /** @@ -99,7 +99,7 @@ public class RepeatableMetaAnnotatedElement {@link AnnotationMapping}类型 * @return {@link AnnotatedElement}上的注解结构 */ - public static > RepeatableMetaAnnotatedElement create( + public static > RepeatableMetaAnnotatedElement of( final RepeatableAnnotationCollector collector, final AnnotatedElement element, final BiFunction mappingFactory) { diff --git a/hutool-core/src/test/java/cn/hutool/v7/core/annotation/GenericAnnotationMappingTest.java b/hutool-core/src/test/java/cn/hutool/v7/core/annotation/GenericAnnotationMappingTest.java index 04b435c7e1..6f2240c77c 100644 --- a/hutool-core/src/test/java/cn/hutool/v7/core/annotation/GenericAnnotationMappingTest.java +++ b/hutool-core/src/test/java/cn/hutool/v7/core/annotation/GenericAnnotationMappingTest.java @@ -36,42 +36,42 @@ public class GenericAnnotationMappingTest { @Test public void testEquals() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final GenericAnnotationMapping mapping = GenericAnnotationMapping.create(annotation, false); + final GenericAnnotationMapping mapping = GenericAnnotationMapping.of(annotation, false); Assertions.assertNotEquals(null, mapping); - Assertions.assertEquals(mapping, GenericAnnotationMapping.create(annotation, false)); - Assertions.assertNotEquals(mapping, GenericAnnotationMapping.create(annotation, true)); + Assertions.assertEquals(mapping, GenericAnnotationMapping.of(annotation, false)); + Assertions.assertNotEquals(mapping, GenericAnnotationMapping.of(annotation, true)); } @Test public void testHashCode() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final int hashCode = GenericAnnotationMapping.create(annotation, false).hashCode(); - Assertions.assertEquals(hashCode, GenericAnnotationMapping.create(annotation, false).hashCode()); - Assertions.assertNotEquals(hashCode, GenericAnnotationMapping.create(annotation, true).hashCode()); + final int hashCode = GenericAnnotationMapping.of(annotation, false).hashCode(); + Assertions.assertEquals(hashCode, GenericAnnotationMapping.of(annotation, false).hashCode()); + Assertions.assertNotEquals(hashCode, GenericAnnotationMapping.of(annotation, true).hashCode()); } @Test - public void testCreate() { + public void testOf() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final GenericAnnotationMapping mapping = GenericAnnotationMapping.create(annotation, false); + final GenericAnnotationMapping mapping = GenericAnnotationMapping.of(annotation, false); Assertions.assertNotNull(mapping); } @Test public void testIsRoot() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - GenericAnnotationMapping mapping = GenericAnnotationMapping.create(annotation, true); + GenericAnnotationMapping mapping = GenericAnnotationMapping.of(annotation, true); Assertions.assertTrue(mapping.isRoot()); - mapping = GenericAnnotationMapping.create(annotation, false); + mapping = GenericAnnotationMapping.of(annotation, false); Assertions.assertFalse(mapping.isRoot()); } @Test public void testGetAnnotation() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final GenericAnnotationMapping mapping = GenericAnnotationMapping.create(annotation, false); + final GenericAnnotationMapping mapping = GenericAnnotationMapping.of(annotation, false); Assertions.assertSame(annotation, mapping.getAnnotation()); } @@ -79,7 +79,7 @@ public class GenericAnnotationMappingTest { @Test public void testGetAttributes() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final GenericAnnotationMapping mapping = GenericAnnotationMapping.create(annotation, false); + final GenericAnnotationMapping mapping = GenericAnnotationMapping.of(annotation, false); for (int i = 0; i < mapping.getAttributes().length; i++) { final Method method = mapping.getAttributes()[i]; Assertions.assertEquals(Annotation1.class.getDeclaredMethod(method.getName()), method); @@ -89,21 +89,21 @@ public class GenericAnnotationMappingTest { @Test public void testAnnotationType() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final GenericAnnotationMapping mapping = GenericAnnotationMapping.create(annotation, false); + final GenericAnnotationMapping mapping = GenericAnnotationMapping.of(annotation, false); Assertions.assertEquals(annotation.annotationType(), mapping.annotationType()); } @Test public void testIsResolved() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final GenericAnnotationMapping mapping = GenericAnnotationMapping.create(annotation, false); + final GenericAnnotationMapping mapping = GenericAnnotationMapping.of(annotation, false); Assertions.assertFalse(mapping.isResolved()); } @Test public void testGetAttributeValue() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final GenericAnnotationMapping mapping = GenericAnnotationMapping.create(annotation, false); + final GenericAnnotationMapping mapping = GenericAnnotationMapping.of(annotation, false); Assertions.assertEquals(annotation.value(), mapping.getAttributeValue("value", String.class)); Assertions.assertNull(mapping.getAttributeValue("value", Integer.class)); } @@ -111,14 +111,14 @@ public class GenericAnnotationMappingTest { @Test public void testGetResolvedAnnotation() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final GenericAnnotationMapping mapping = GenericAnnotationMapping.create(annotation, false); + final GenericAnnotationMapping mapping = GenericAnnotationMapping.of(annotation, false); Assertions.assertSame(annotation, mapping.getResolvedAnnotation()); } @Test public void testGetResolvedAttributeValue() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final GenericAnnotationMapping mapping = GenericAnnotationMapping.create(annotation, false); + final GenericAnnotationMapping mapping = GenericAnnotationMapping.of(annotation, false); Assertions.assertEquals(annotation.value(), mapping.getResolvedAttributeValue("value", String.class)); Assertions.assertNull(mapping.getResolvedAttributeValue("value", Integer.class)); } diff --git a/hutool-core/src/test/java/cn/hutool/v7/core/annotation/MetaAnnotatedElementTest.java b/hutool-core/src/test/java/cn/hutool/v7/core/annotation/MetaAnnotatedElementTest.java index 4e91f7e1de..b8e424dd3c 100644 --- a/hutool-core/src/test/java/cn/hutool/v7/core/annotation/MetaAnnotatedElementTest.java +++ b/hutool-core/src/test/java/cn/hutool/v7/core/annotation/MetaAnnotatedElementTest.java @@ -58,17 +58,17 @@ public class MetaAnnotatedElementTest { } @Test - public void testCreate() { + public void testOf() { // 第二次创建时优先从缓存中获取 - final AnnotatedElement resolvedElement = MetaAnnotatedElement.create(Foo.class, RESOLVED_MAPPING_FACTORY); - Assertions.assertEquals(resolvedElement, MetaAnnotatedElement.create(Foo.class, RESOLVED_MAPPING_FACTORY)); - final AnnotatedElement element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY); - Assertions.assertEquals(element, MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY)); + final AnnotatedElement resolvedElement = MetaAnnotatedElement.of(Foo.class, RESOLVED_MAPPING_FACTORY); + Assertions.assertEquals(resolvedElement, MetaAnnotatedElement.of(Foo.class, RESOLVED_MAPPING_FACTORY)); + final AnnotatedElement element = MetaAnnotatedElement.of(Foo.class, MAPPING_FACTORY); + Assertions.assertEquals(element, MetaAnnotatedElement.of(Foo.class, MAPPING_FACTORY)); } @Test public void testGetMapping() { - final MetaAnnotatedElement element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY); + final MetaAnnotatedElement element = MetaAnnotatedElement.of(Foo.class, MAPPING_FACTORY); Assertions.assertTrue(element.getMapping(Annotation1.class).isPresent()); Assertions.assertTrue(element.getMapping(Annotation2.class).isPresent()); Assertions.assertTrue(element.getMapping(Annotation3.class).isPresent()); @@ -77,7 +77,7 @@ public class MetaAnnotatedElementTest { @Test public void testGetDeclaredMapping() { - final MetaAnnotatedElement element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY); + final MetaAnnotatedElement element = MetaAnnotatedElement.of(Foo.class, MAPPING_FACTORY); Assertions.assertFalse(element.getDeclaredMapping(Annotation1.class).isPresent()); Assertions.assertFalse(element.getDeclaredMapping(Annotation2.class).isPresent()); Assertions.assertTrue(element.getDeclaredMapping(Annotation3.class).isPresent()); @@ -86,7 +86,7 @@ public class MetaAnnotatedElementTest { @Test public void testIsAnnotationPresent() { - final MetaAnnotatedElement element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY); + final MetaAnnotatedElement element = MetaAnnotatedElement.of(Foo.class, MAPPING_FACTORY); Assertions.assertTrue(element.isAnnotationPresent(Annotation1.class)); Assertions.assertTrue(element.isAnnotationPresent(Annotation2.class)); Assertions.assertTrue(element.isAnnotationPresent(Annotation3.class)); @@ -95,7 +95,7 @@ public class MetaAnnotatedElementTest { @Test public void testGetAnnotation() { - final MetaAnnotatedElement element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY); + final MetaAnnotatedElement element = MetaAnnotatedElement.of(Foo.class, MAPPING_FACTORY); final Annotation4 annotation4 = Foo.class.getAnnotation(Annotation4.class); final Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class); final Annotation2 annotation2 = Annotation3.class.getAnnotation(Annotation2.class); @@ -109,7 +109,7 @@ public class MetaAnnotatedElementTest { @Test public void testGetDeclaredAnnotation() { - final MetaAnnotatedElement element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY); + final MetaAnnotatedElement element = MetaAnnotatedElement.of(Foo.class, MAPPING_FACTORY); final Annotation4 annotation4 = Foo.class.getAnnotation(Annotation4.class); final Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class); @@ -121,7 +121,7 @@ public class MetaAnnotatedElementTest { @Test public void testGetAnnotationByType() { - final MetaAnnotatedElement element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY); + final MetaAnnotatedElement element = MetaAnnotatedElement.of(Foo.class, MAPPING_FACTORY); final Annotation4 annotation4 = Foo.class.getAnnotation(Annotation4.class); Assertions.assertArrayEquals( new Annotation[]{ annotation4 }, @@ -132,7 +132,7 @@ public class MetaAnnotatedElementTest { @Test public void testGetDeclaredAnnotationByType() { - final MetaAnnotatedElement element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY); + final MetaAnnotatedElement element = MetaAnnotatedElement.of(Foo.class, MAPPING_FACTORY); final Annotation4 annotation4 = Foo.class.getAnnotation(Annotation4.class); Assertions.assertArrayEquals( new Annotation[]{ annotation4 }, @@ -143,7 +143,7 @@ public class MetaAnnotatedElementTest { @Test public void testGetAnnotations() { - final MetaAnnotatedElement element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY); + final MetaAnnotatedElement element = MetaAnnotatedElement.of(Foo.class, MAPPING_FACTORY); final Annotation4 annotation4 = Foo.class.getAnnotation(Annotation4.class); final Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class); final Annotation2 annotation2 = Annotation3.class.getAnnotation(Annotation2.class); @@ -155,7 +155,7 @@ public class MetaAnnotatedElementTest { @Test public void testGetDeclaredAnnotations() { - final MetaAnnotatedElement element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY); + final MetaAnnotatedElement element = MetaAnnotatedElement.of(Foo.class, MAPPING_FACTORY); final Annotation4 annotation4 = Foo.class.getAnnotation(Annotation4.class); final Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class); final Annotation[] annotations = new Annotation[]{ annotation3, annotation4 }; @@ -165,7 +165,7 @@ public class MetaAnnotatedElementTest { @Test public void testIterator() { - final MetaAnnotatedElement element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY); + final MetaAnnotatedElement element = MetaAnnotatedElement.of(Foo.class, MAPPING_FACTORY); final Annotation4 annotation4 = Foo.class.getAnnotation(Annotation4.class); final Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class); final Annotation2 annotation2 = Annotation3.class.getAnnotation(Annotation2.class); @@ -182,7 +182,7 @@ public class MetaAnnotatedElementTest { @Test public void testGetElement() { final AnnotatedElement source = Foo.class; - final MetaAnnotatedElement element = MetaAnnotatedElement.create(source, MAPPING_FACTORY); + final MetaAnnotatedElement element = MetaAnnotatedElement.of(source, MAPPING_FACTORY); Assertions.assertSame(source, element.getElement()); } diff --git a/hutool-core/src/test/java/cn/hutool/v7/core/annotation/RepeatableMetaAnnotatedElementTest.java b/hutool-core/src/test/java/cn/hutool/v7/core/annotation/RepeatableMetaAnnotatedElementTest.java index 1ab993a57a..32a42996ba 100644 --- a/hutool-core/src/test/java/cn/hutool/v7/core/annotation/RepeatableMetaAnnotatedElementTest.java +++ b/hutool-core/src/test/java/cn/hutool/v7/core/annotation/RepeatableMetaAnnotatedElementTest.java @@ -45,24 +45,24 @@ public class RepeatableMetaAnnotatedElementTest { @Test public void testEquals() { - final AnnotatedElement element = RepeatableMetaAnnotatedElement.create(Foo.class, RESOLVED_MAPPING_FACTORY); + final AnnotatedElement element = RepeatableMetaAnnotatedElement.of(Foo.class, RESOLVED_MAPPING_FACTORY); Assertions.assertNotEquals(null, element); - Assertions.assertEquals(element, RepeatableMetaAnnotatedElement.create(Foo.class, RESOLVED_MAPPING_FACTORY)); - Assertions.assertNotEquals(element, RepeatableMetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY)); - Assertions.assertNotEquals(element, RepeatableMetaAnnotatedElement.create(Annotation1.class, MAPPING_FACTORY)); + Assertions.assertEquals(element, RepeatableMetaAnnotatedElement.of(Foo.class, RESOLVED_MAPPING_FACTORY)); + Assertions.assertNotEquals(element, RepeatableMetaAnnotatedElement.of(Foo.class, MAPPING_FACTORY)); + Assertions.assertNotEquals(element, RepeatableMetaAnnotatedElement.of(Annotation1.class, MAPPING_FACTORY)); } @Test public void testHashCode() { - final int hashCode = RepeatableMetaAnnotatedElement.create(Foo.class, RESOLVED_MAPPING_FACTORY).hashCode(); - Assertions.assertEquals(hashCode, RepeatableMetaAnnotatedElement.create(Foo.class, RESOLVED_MAPPING_FACTORY).hashCode()); - Assertions.assertNotEquals(hashCode, RepeatableMetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY).hashCode()); - Assertions.assertNotEquals(hashCode, RepeatableMetaAnnotatedElement.create(Annotation1.class, MAPPING_FACTORY).hashCode()); + final int hashCode = RepeatableMetaAnnotatedElement.of(Foo.class, RESOLVED_MAPPING_FACTORY).hashCode(); + Assertions.assertEquals(hashCode, RepeatableMetaAnnotatedElement.of(Foo.class, RESOLVED_MAPPING_FACTORY).hashCode()); + Assertions.assertNotEquals(hashCode, RepeatableMetaAnnotatedElement.of(Foo.class, MAPPING_FACTORY).hashCode()); + Assertions.assertNotEquals(hashCode, RepeatableMetaAnnotatedElement.of(Annotation1.class, MAPPING_FACTORY).hashCode()); } @Test public void testIsAnnotationPresent() { - final AnnotatedElement element = RepeatableMetaAnnotatedElement.create( + final AnnotatedElement element = RepeatableMetaAnnotatedElement.of( RepeatableAnnotationCollector.standard(), Foo.class, (s, a) -> new GenericAnnotationMapping(a, Objects.isNull(s)) ); Assertions.assertTrue(element.isAnnotationPresent(Annotation1.class)); @@ -73,7 +73,7 @@ public class RepeatableMetaAnnotatedElementTest { @Test public void testGetAnnotations() { - final AnnotatedElement element = RepeatableMetaAnnotatedElement.create( + final AnnotatedElement element = RepeatableMetaAnnotatedElement.of( RepeatableAnnotationCollector.standard(), Foo.class, (s, a) -> new GenericAnnotationMapping(a, Objects.isNull(s)) ); final List> annotationTypes = Arrays.stream(element.getAnnotations()) @@ -89,7 +89,7 @@ public class RepeatableMetaAnnotatedElementTest { @Test public void testGetAnnotation() { - final AnnotatedElement element = RepeatableMetaAnnotatedElement.create( + final AnnotatedElement element = RepeatableMetaAnnotatedElement.of( RepeatableAnnotationCollector.standard(), Foo.class, (s, a) -> new GenericAnnotationMapping(a, Objects.isNull(s)) ); @@ -108,7 +108,7 @@ public class RepeatableMetaAnnotatedElementTest { @Test public void testGetAnnotationsByType() { - final AnnotatedElement element = RepeatableMetaAnnotatedElement.create( + final AnnotatedElement element = RepeatableMetaAnnotatedElement.of( RepeatableAnnotationCollector.standard(), Foo.class, (s, a) -> new GenericAnnotationMapping(a, Objects.isNull(s)) ); @@ -127,7 +127,7 @@ public class RepeatableMetaAnnotatedElementTest { @Test public void testGetDeclaredAnnotations() { - final AnnotatedElement element = RepeatableMetaAnnotatedElement.create( + final AnnotatedElement element = RepeatableMetaAnnotatedElement.of( RepeatableAnnotationCollector.standard(), Foo.class, (s, a) -> new GenericAnnotationMapping(a, Objects.isNull(s)) ); final List> annotationTypes = Arrays.stream(element.getDeclaredAnnotations()) @@ -143,7 +143,7 @@ public class RepeatableMetaAnnotatedElementTest { @Test public void testGetDeclaredAnnotation() { - final AnnotatedElement element = RepeatableMetaAnnotatedElement.create( + final AnnotatedElement element = RepeatableMetaAnnotatedElement.of( RepeatableAnnotationCollector.standard(), Foo.class, (s, a) -> new GenericAnnotationMapping(a, Objects.isNull(s)) ); @@ -156,7 +156,7 @@ public class RepeatableMetaAnnotatedElementTest { @Test public void testGetDeclaredAnnotationsByType() { - final AnnotatedElement element = RepeatableMetaAnnotatedElement.create( + final AnnotatedElement element = RepeatableMetaAnnotatedElement.of( RepeatableAnnotationCollector.standard(), Foo.class, (s, a) -> new GenericAnnotationMapping(a, Objects.isNull(s)) ); @@ -176,7 +176,7 @@ public class RepeatableMetaAnnotatedElementTest { @Test public void testGetElement() { final AnnotatedElement element = Foo.class; - final RepeatableMetaAnnotatedElement repeatableMetaAnnotatedElement = RepeatableMetaAnnotatedElement.create( + final RepeatableMetaAnnotatedElement repeatableMetaAnnotatedElement = RepeatableMetaAnnotatedElement.of( RepeatableAnnotationCollector.standard(), element, (s, a) -> new GenericAnnotationMapping(a, Objects.isNull(s)) ); Assertions.assertSame(element, repeatableMetaAnnotatedElement.getElement()); @@ -184,7 +184,7 @@ public class RepeatableMetaAnnotatedElementTest { @Test public void testIterator() { - final RepeatableMetaAnnotatedElement element = RepeatableMetaAnnotatedElement.create( + final RepeatableMetaAnnotatedElement element = RepeatableMetaAnnotatedElement.of( RepeatableAnnotationCollector.standard(), Foo.class, (s, a) -> new GenericAnnotationMapping(a, Objects.isNull(s)) ); int count = 0; diff --git a/hutool-core/src/test/java/cn/hutool/v7/core/annotation/ResolvedAnnotationMappingTest.java b/hutool-core/src/test/java/cn/hutool/v7/core/annotation/ResolvedAnnotationMappingTest.java index 47d3a20a87..3f563f9106 100644 --- a/hutool-core/src/test/java/cn/hutool/v7/core/annotation/ResolvedAnnotationMappingTest.java +++ b/hutool-core/src/test/java/cn/hutool/v7/core/annotation/ResolvedAnnotationMappingTest.java @@ -36,80 +36,80 @@ public class ResolvedAnnotationMappingTest { @Test public void testEquals() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.create(annotation, false); + final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.of(annotation, false); Assertions.assertNotEquals(null, mapping); - Assertions.assertEquals(mapping, ResolvedAnnotationMapping.create(annotation, false)); - Assertions.assertNotEquals(mapping, ResolvedAnnotationMapping.create(annotation, true)); + Assertions.assertEquals(mapping, ResolvedAnnotationMapping.of(annotation, false)); + Assertions.assertNotEquals(mapping, ResolvedAnnotationMapping.of(annotation, true)); // Annotation3没有需要解析的属性,因此即使在构造函数指定false也一样 final Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class); Assertions.assertEquals( - ResolvedAnnotationMapping.create(annotation3, false), - ResolvedAnnotationMapping.create(annotation3, true) + ResolvedAnnotationMapping.of(annotation3, false), + ResolvedAnnotationMapping.of(annotation3, true) ); } @Test public void testHashCode() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final int hashCode = ResolvedAnnotationMapping.create(annotation, false).hashCode(); - Assertions.assertEquals(hashCode, ResolvedAnnotationMapping.create(annotation, false).hashCode()); - Assertions.assertNotEquals(hashCode, ResolvedAnnotationMapping.create(annotation, true).hashCode()); + final int hashCode = ResolvedAnnotationMapping.of(annotation, false).hashCode(); + Assertions.assertEquals(hashCode, ResolvedAnnotationMapping.of(annotation, false).hashCode()); + Assertions.assertNotEquals(hashCode, ResolvedAnnotationMapping.of(annotation, true).hashCode()); // Annotation3没有需要解析的属性,因此即使在构造函数指定false也一样 final Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class); Assertions.assertEquals( - ResolvedAnnotationMapping.create(annotation3, false).hashCode(), - ResolvedAnnotationMapping.create(annotation3, true).hashCode() + ResolvedAnnotationMapping.of(annotation3, false).hashCode(), + ResolvedAnnotationMapping.of(annotation3, true).hashCode() ); } @Test - public void testCreate() { + public void testOf() { final Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class); - final ResolvedAnnotationMapping mapping3 = ResolvedAnnotationMapping.create(annotation3, false); + final ResolvedAnnotationMapping mapping3 = ResolvedAnnotationMapping.of(annotation3, false); Assertions.assertNotNull(mapping3); final Annotation2 annotation2 = Foo.class.getAnnotation(Annotation2.class); - final ResolvedAnnotationMapping mapping2 = ResolvedAnnotationMapping.create(mapping3, annotation2, false); + final ResolvedAnnotationMapping mapping2 = ResolvedAnnotationMapping.of(mapping3, annotation2, false); Assertions.assertNotNull(mapping2); final Annotation1 annotation1 = Foo.class.getAnnotation(Annotation1.class); - final ResolvedAnnotationMapping mapping1 = ResolvedAnnotationMapping.create(mapping2, annotation1, false); + final ResolvedAnnotationMapping mapping1 = ResolvedAnnotationMapping.of(mapping2, annotation1, false); Assertions.assertNotNull(mapping1); } @Test public void testIsRoot() { final Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class); - final ResolvedAnnotationMapping mapping3 = ResolvedAnnotationMapping.create(annotation3, false); + final ResolvedAnnotationMapping mapping3 = ResolvedAnnotationMapping.of(annotation3, false); Assertions.assertTrue(mapping3.isRoot()); final Annotation2 annotation2 = Foo.class.getAnnotation(Annotation2.class); - final ResolvedAnnotationMapping mapping2 = ResolvedAnnotationMapping.create(mapping3, annotation2, false); + final ResolvedAnnotationMapping mapping2 = ResolvedAnnotationMapping.of(mapping3, annotation2, false); Assertions.assertFalse(mapping2.isRoot()); } @Test public void testGetRoot() { final Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class); - final ResolvedAnnotationMapping mapping3 = ResolvedAnnotationMapping.create(annotation3, false); + final ResolvedAnnotationMapping mapping3 = ResolvedAnnotationMapping.of(annotation3, false); Assertions.assertSame(mapping3, mapping3.getRoot()); final Annotation2 annotation2 = Foo.class.getAnnotation(Annotation2.class); - final ResolvedAnnotationMapping mapping2 = ResolvedAnnotationMapping.create(mapping3, annotation2, false); + final ResolvedAnnotationMapping mapping2 = ResolvedAnnotationMapping.of(mapping3, annotation2, false); Assertions.assertSame(mapping3, mapping2.getRoot()); final Annotation1 annotation1 = Foo.class.getAnnotation(Annotation1.class); - final ResolvedAnnotationMapping mapping1 = ResolvedAnnotationMapping.create(mapping2, annotation1, false); + final ResolvedAnnotationMapping mapping1 = ResolvedAnnotationMapping.of(mapping2, annotation1, false); Assertions.assertSame(mapping3, mapping1.getRoot()); } @Test public void testGetAnnotation() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.create(annotation, false); + final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.of(annotation, false); Assertions.assertSame(annotation, mapping.getAnnotation()); } @@ -117,7 +117,7 @@ public class ResolvedAnnotationMappingTest { @Test public void testGetAttributes() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.create(annotation, false); + final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.of(annotation, false); for (int i = 0; i < mapping.getAttributes().length; i++) { final Method method = mapping.getAttributes()[i]; Assertions.assertEquals(Annotation1.class.getDeclaredMethod(method.getName()), method); @@ -127,7 +127,7 @@ public class ResolvedAnnotationMappingTest { @Test public void testHasAttribute() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.create(annotation, false); + final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.of(annotation, false); Assertions.assertTrue(mapping.hasAttribute("value", String.class)); Assertions.assertFalse(mapping.hasAttribute("value", Integer.class)); @@ -140,7 +140,7 @@ public class ResolvedAnnotationMappingTest { @Test public void testAnnotationType() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.create(annotation, false); + final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.of(annotation, false); Assertions.assertEquals(annotation.annotationType(), mapping.annotationType()); } @@ -148,22 +148,22 @@ public class ResolvedAnnotationMappingTest { public void testIsResolved() { final Annotation1 annotation1 = Foo.class.getAnnotation(Annotation1.class); - final ResolvedAnnotationMapping mapping1 = ResolvedAnnotationMapping.create(annotation1, true); + final ResolvedAnnotationMapping mapping1 = ResolvedAnnotationMapping.of(annotation1, true); Assertions.assertTrue(mapping1.isResolved()); - Assertions.assertFalse(ResolvedAnnotationMapping.create(annotation1, false).isResolved()); + Assertions.assertFalse(ResolvedAnnotationMapping.of(annotation1, false).isResolved()); final Annotation2 annotation2 = Foo.class.getAnnotation(Annotation2.class); - ResolvedAnnotationMapping mapping2 = ResolvedAnnotationMapping.create(annotation2, true); + ResolvedAnnotationMapping mapping2 = ResolvedAnnotationMapping.of(annotation2, true); Assertions.assertFalse(mapping2.isResolved()); - mapping2 = ResolvedAnnotationMapping.create(mapping1, annotation2, true); + mapping2 = ResolvedAnnotationMapping.of(mapping1, annotation2, true); Assertions.assertTrue(mapping2.isResolved()); } @Test public void testGetAttributeIndex() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.create(annotation, false); + final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.of(annotation, false); for (int i = 0; i < mapping.getAttributes().length; i++) { final Method method = mapping.getAttributes()[i]; Assertions.assertEquals(i, mapping.getAttributeIndex(method.getName(), method.getReturnType())); @@ -175,7 +175,7 @@ public class ResolvedAnnotationMappingTest { @Test public void testGetAttributeValue() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.create(annotation, false); + final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.of(annotation, false); Assertions.assertNull(mapping.getAttribute(Integer.MAX_VALUE)); @@ -195,7 +195,7 @@ public class ResolvedAnnotationMappingTest { @Test public void testGetResolvedAnnotation() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.create(annotation, true); + final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.of(annotation, true); final Annotation1 synthesis = (Annotation1)mapping.getResolvedAnnotation(); Assertions.assertEquals(annotation.annotationType(), synthesis.annotationType()); @@ -211,7 +211,7 @@ public class ResolvedAnnotationMappingTest { Assertions.assertNotEquals(synthesis.toString(), annotation.toString()); final Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class); - Assertions.assertSame(annotation3, ResolvedAnnotationMapping.create(annotation3, true).getResolvedAnnotation()); + Assertions.assertSame(annotation3, ResolvedAnnotationMapping.of(annotation3, true).getResolvedAnnotation()); } // ======================= resolved attribute value ======================= @@ -219,7 +219,7 @@ public class ResolvedAnnotationMappingTest { @Test public void testGetResolvedAttributeValueWhenAliased() { final Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class); - final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.create(annotation, true); + final ResolvedAnnotationMapping mapping = ResolvedAnnotationMapping.of(annotation, true); Assertions.assertNull(mapping.getResolvedAttributeValue(Integer.MIN_VALUE)); // value = value1 = value2 @@ -248,19 +248,19 @@ public class ResolvedAnnotationMappingTest { @Test public void testGetResolvedAttributeWhenOverwritten() { final Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class); - final ResolvedAnnotationMapping mapping3 = ResolvedAnnotationMapping.create(annotation3, true); + final ResolvedAnnotationMapping mapping3 = ResolvedAnnotationMapping.of(annotation3, true); Assertions.assertEquals(annotation3.value(), mapping3.getResolvedAttributeValue("value", String.class)); Assertions.assertEquals((Integer)annotation3.alias(), mapping3.getResolvedAttributeValue("alias", Integer.class)); // annotation2中与annotation3同名同类型的属性value、alias被覆写 final Annotation2 annotation2 = Foo.class.getAnnotation(Annotation2.class); - final ResolvedAnnotationMapping mapping2 = ResolvedAnnotationMapping.create(mapping3, annotation2, true); + final ResolvedAnnotationMapping mapping2 = ResolvedAnnotationMapping.of(mapping3, annotation2, true); Assertions.assertEquals(annotation3.value(), mapping2.getResolvedAttributeValue("value", String.class)); Assertions.assertEquals((Integer)annotation3.alias(), mapping2.getResolvedAttributeValue("alias", Integer.class)); // annotation1中与annotation3同名同类型的属性value被覆写,由于value存在别名value1,value2因此也一并被覆写 final Annotation1 annotation1 = Foo.class.getAnnotation(Annotation1.class); - final ResolvedAnnotationMapping mapping1 = ResolvedAnnotationMapping.create(mapping2, annotation1, true); + final ResolvedAnnotationMapping mapping1 = ResolvedAnnotationMapping.of(mapping2, annotation1, true); Assertions.assertEquals(annotation3.value(), mapping1.getResolvedAttributeValue("value", String.class)); Assertions.assertEquals(annotation3.value(), mapping1.getResolvedAttributeValue("value1", String.class)); Assertions.assertEquals(annotation3.value(), mapping1.getResolvedAttributeValue("value2", String.class)); diff --git a/hutool-socket/src/main/java/cn/hutool/v7/socket/ChannelUtil.java b/hutool-socket/src/main/java/cn/hutool/v7/socket/ChannelUtil.java index 52d897a83e..da8a185e0b 100644 --- a/hutool-socket/src/main/java/cn/hutool/v7/socket/ChannelUtil.java +++ b/hutool-socket/src/main/java/cn/hutool/v7/socket/ChannelUtil.java @@ -22,11 +22,8 @@ import cn.hutool.v7.core.thread.ThreadFactoryBuilder; import cn.hutool.v7.socket.nio.Operation; import java.io.IOException; -import java.net.InetSocketAddress; -import java.nio.channels.AsynchronousChannelGroup; -import java.nio.channels.AsynchronousSocketChannel; -import java.nio.channels.SelectableChannel; -import java.nio.channels.Selector; +import java.net.SocketAddress; +import java.nio.channels.*; import java.util.concurrent.ExecutionException; /** @@ -37,6 +34,37 @@ import java.util.concurrent.ExecutionException; */ public class ChannelUtil { + /** + * 获取远程端的地址信息,包括host和端口
+ * null表示channel为null或者远程主机未连接 + * + * @param channel {@link AsynchronousSocketChannel} + * @return 远程端的地址信息,包括host和端口,null表示channel为null或者远程主机未连接 + * @throws IORuntimeException IO异常 + */ + public static SocketAddress getRemoteAddress(final AsynchronousSocketChannel channel) throws IORuntimeException { + try { + return (null == channel) ? null : channel.getRemoteAddress(); + } catch (final ClosedChannelException e) { + // Channel未打开或已关闭,返回null表示未连接 + return null; + } catch (final IOException e) { + throw new IORuntimeException(e); + } + } + + /** + * 远程主机是否处于连接状态
+ * 通过判断远程地址获取成功与否判断 + * + * @param channel {@link AsynchronousSocketChannel} + * @return 远程主机是否处于连接状态 + * @throws IORuntimeException IO异常 + */ + public static boolean isConnected(final AsynchronousSocketChannel channel) throws IORuntimeException { + return null != getRemoteAddress(channel); + } + /** * 注册通道的指定操作到指定Selector上 * @@ -84,7 +112,7 @@ public class ChannelUtil { * @param address 地址信息,包括地址和端口 * @return {@link AsynchronousSocketChannel} */ - public static AsynchronousSocketChannel connect(final AsynchronousChannelGroup group, final InetSocketAddress address) { + public static AsynchronousSocketChannel connect(final AsynchronousChannelGroup group, final SocketAddress address) { final AsynchronousSocketChannel channel; try { channel = AsynchronousSocketChannel.open(group); diff --git a/hutool-socket/src/main/java/cn/hutool/v7/socket/SocketUtil.java b/hutool-socket/src/main/java/cn/hutool/v7/socket/SocketUtil.java index 48fc4a1971..31c4ed3899 100644 --- a/hutool-socket/src/main/java/cn/hutool/v7/socket/SocketUtil.java +++ b/hutool-socket/src/main/java/cn/hutool/v7/socket/SocketUtil.java @@ -17,13 +17,20 @@ package cn.hutool.v7.socket; import cn.hutool.v7.core.io.IORuntimeException; +import cn.hutool.v7.socket.aio.AioClient; +import cn.hutool.v7.socket.aio.AioServer; +import cn.hutool.v7.socket.aio.IoAction; +import cn.hutool.v7.socket.nio.ChannelHandler; +import cn.hutool.v7.socket.nio.NioClient; +import cn.hutool.v7.socket.nio.NioServer; +import cn.hutool.v7.socket.udp.UdpUtil; +import cn.hutool.v7.socket.udp.protocol.UdpEncoder; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; -import java.nio.channels.AsynchronousSocketChannel; -import java.nio.channels.ClosedChannelException; +import java.nio.ByteBuffer; /** * Socket相关工具类 @@ -33,36 +40,7 @@ import java.nio.channels.ClosedChannelException; */ public class SocketUtil { - /** - * 获取远程端的地址信息,包括host和端口
- * null表示channel为null或者远程主机未连接 - * - * @param channel {@link AsynchronousSocketChannel} - * @return 远程端的地址信息,包括host和端口,null表示channel为null或者远程主机未连接 - * @throws IORuntimeException IO异常 - */ - public static SocketAddress getRemoteAddress(final AsynchronousSocketChannel channel) throws IORuntimeException { - try { - return (null == channel) ? null : channel.getRemoteAddress(); - } catch (final ClosedChannelException e) { - // Channel未打开或已关闭,返回null表示未连接 - return null; - } catch (final IOException e) { - throw new IORuntimeException(e); - } - } - - /** - * 远程主机是否处于连接状态
- * 通过判断远程地址获取成功与否判断 - * - * @param channel {@link AsynchronousSocketChannel} - * @return 远程主机是否处于连接状态 - * @throws IORuntimeException IO异常 - */ - public static boolean isConnected(final AsynchronousSocketChannel channel) throws IORuntimeException{ - return null != getRemoteAddress(channel); - } + // region ----- connect /** * 创建Socket并连接到指定地址的服务器 @@ -113,4 +91,98 @@ public class SocketUtil { } return socket; } + // endregion + + // region ----- udp + /** + * 通过UDP发送消息 + * + * @param 消息类型 + * @param host 目标主机地址 + * @param port 目标端口号 + * @param message 要发送的消息内容 + * @param encoder 消息编码器,用于将指定类型的消息转为bytes + */ + public static void sendByUdp(final String host, final int port, final T message, final UdpEncoder encoder) { + UdpUtil.sendByUdp(host, port, message, encoder); + } + // endregion + + // region ----- aio + + /** + * 创建AIO客户端 + * + * @param address 服务器地址 + * @param ioAction 数据读写回调 + * @return AIO客户端实例 + */ + public static AioClient ofAioClient(final InetSocketAddress address, final IoAction ioAction) { + return new AioClient(address, ioAction); + } + + /** + * 创建AIO客户端 + * + * @param address 服务器地址 + * @param ioAction 数据读写回调 + * @param config Socket配置 + * @return AIO客户端实例 + */ + public static AioClient ofAioClient(final InetSocketAddress address, final IoAction ioAction, final SocketConfig config) { + return new AioClient(address, ioAction, config); + } + + /** + * 创建AIO服务器 + * + * @param port 端口号 + * @param ioAction 数据读写回调 + * @return AIO服务器实例 + */ + @SuppressWarnings("resource") + public static AioServer ofAioServer(final int port, final IoAction ioAction) { + return new AioServer(port).setIoAction(ioAction); + } + + /** + * 创建一个AIO服务器 + * + * @param address 服务器地址 + * @param config 套接字配置 + * @param ioAction IO操作回调 + * @return AIO服务器 + */ + @SuppressWarnings("resource") + public static AioServer ofAioServer(final SocketAddress address, final SocketConfig config, final IoAction ioAction) { + return new AioServer(address, config).setIoAction(ioAction); + } + // endregion + + // region ----- nio + + /** + * 创建NIO客户端 + * + * @param address 服务器地址 + * @param channelHandler 数据读写回调 + * @return NIO客户端实例 + */ + @SuppressWarnings("resource") + public static NioClient ofNioClient(final SocketAddress address, final ChannelHandler channelHandler) { + return new NioClient(address).setChannelHandler(channelHandler); + } + + /** + * 创建NIO服务器 + * + * @param port 端口号 + * @param channelHandler 数据读写回调 + * @return NIO服务器实例 + */ + @SuppressWarnings("resource") + public static NioServer ofNioServer(final int port, final ChannelHandler channelHandler) { + return new NioServer(port).setChannelHandler(channelHandler); + } + // endregion } diff --git a/hutool-socket/src/main/java/cn/hutool/v7/socket/aio/AioClient.java b/hutool-socket/src/main/java/cn/hutool/v7/socket/aio/AioClient.java index 7d31669c6e..3dfc867af5 100644 --- a/hutool-socket/src/main/java/cn/hutool/v7/socket/aio/AioClient.java +++ b/hutool-socket/src/main/java/cn/hutool/v7/socket/aio/AioClient.java @@ -21,7 +21,7 @@ import cn.hutool.v7.socket.SocketConfig; import java.io.Closeable; import java.io.IOException; -import java.net.InetSocketAddress; +import java.net.SocketAddress; import java.net.SocketOption; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousSocketChannel; @@ -42,7 +42,7 @@ public class AioClient implements Closeable { * @param address 地址 * @param ioAction IO处理类 */ - public AioClient(final InetSocketAddress address, final IoAction ioAction) { + public AioClient(final SocketAddress address, final IoAction ioAction) { this(address, ioAction, new SocketConfig()); } @@ -53,7 +53,7 @@ public class AioClient implements Closeable { * @param ioAction IO处理类 * @param config 配置项 */ - public AioClient(final InetSocketAddress address, final IoAction ioAction, final SocketConfig config) { + public AioClient(final SocketAddress address, final IoAction ioAction, final SocketConfig config) { this(createChannel(address, config.getThreadPoolSize()), ioAction, config); } @@ -79,6 +79,7 @@ public class AioClient implements Closeable { * @return this * @throws IOException IO异常 */ + @SuppressWarnings("resource") public AioClient setOption(final SocketOption name, final T value) throws IOException { this.session.getChannel().setOption(name, value); return this; @@ -131,7 +132,7 @@ public class AioClient implements Closeable { * @param poolSize 线程池大小 * @return this */ - private static AsynchronousSocketChannel createChannel(final InetSocketAddress address, final int poolSize) { + private static AsynchronousSocketChannel createChannel(final SocketAddress address, final int poolSize) { return ChannelUtil.connect(ChannelUtil.createFixedGroup(poolSize), address); } // ------------------------------------------------------------------------------------- Private method end diff --git a/hutool-socket/src/main/java/cn/hutool/v7/socket/aio/AioServer.java b/hutool-socket/src/main/java/cn/hutool/v7/socket/aio/AioServer.java index 432890886b..e19e9dfeb1 100644 --- a/hutool-socket/src/main/java/cn/hutool/v7/socket/aio/AioServer.java +++ b/hutool-socket/src/main/java/cn/hutool/v7/socket/aio/AioServer.java @@ -26,6 +26,7 @@ import cn.hutool.v7.socket.SocketConfig; import java.io.Closeable; import java.io.IOException; import java.net.InetSocketAddress; +import java.net.SocketAddress; import java.net.SocketOption; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousChannelGroup; @@ -68,7 +69,7 @@ public class AioServer implements Closeable { * @param config {@link SocketConfig} 配置项 */ @SuppressWarnings("resource") - public AioServer(final InetSocketAddress address, final SocketConfig config) { + public AioServer(final SocketAddress address, final SocketConfig config) { this.config = config; init(address); } @@ -79,7 +80,7 @@ public class AioServer implements Closeable { * @param address 地址和端口 * @return this */ - public AioServer init(final InetSocketAddress address) { + public AioServer init(final SocketAddress address) { try { this.group = AsynchronousChannelGroup.withFixedThreadPool(// config.getThreadPoolSize(), // 默认线程池大小 diff --git a/hutool-socket/src/main/java/cn/hutool/v7/socket/aio/AioSession.java b/hutool-socket/src/main/java/cn/hutool/v7/socket/aio/AioSession.java index 7535174bac..6f92bc0f5d 100644 --- a/hutool-socket/src/main/java/cn/hutool/v7/socket/aio/AioSession.java +++ b/hutool-socket/src/main/java/cn/hutool/v7/socket/aio/AioSession.java @@ -18,8 +18,8 @@ package cn.hutool.v7.socket.aio; import cn.hutool.v7.core.io.IORuntimeException; import cn.hutool.v7.core.io.IoUtil; +import cn.hutool.v7.socket.ChannelUtil; import cn.hutool.v7.socket.SocketConfig; -import cn.hutool.v7.socket.SocketUtil; import java.io.Closeable; import java.io.IOException; @@ -109,7 +109,7 @@ public class AioSession implements Closeable{ * @return 远程主机(客户端)地址和端口 */ public SocketAddress getRemoteAddress() { - return SocketUtil.getRemoteAddress(this.channel); + return ChannelUtil.getRemoteAddress(this.channel); } /** diff --git a/hutool-socket/src/main/java/cn/hutool/v7/socket/nio/NioClient.java b/hutool-socket/src/main/java/cn/hutool/v7/socket/nio/NioClient.java index c8a43e1ae7..27df7f00ea 100644 --- a/hutool-socket/src/main/java/cn/hutool/v7/socket/nio/NioClient.java +++ b/hutool-socket/src/main/java/cn/hutool/v7/socket/nio/NioClient.java @@ -25,6 +25,7 @@ import cn.hutool.v7.socket.SocketRuntimeException; import java.io.Closeable; import java.io.IOException; import java.net.InetSocketAddress; +import java.net.SocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; @@ -61,7 +62,7 @@ public class NioClient implements Closeable { * @param address 服务器地址 */ @SuppressWarnings("resource") - public NioClient(final InetSocketAddress address) { + public NioClient(final SocketAddress address) { init(address); } @@ -71,7 +72,7 @@ public class NioClient implements Closeable { * @param address 地址和端口 * @return this */ - public NioClient init(final InetSocketAddress address) { + public NioClient init(final SocketAddress address) { try { //创建一个SocketChannel对象,配置成非阻塞模式 this.channel = SocketChannel.open(); diff --git a/hutool-socket/src/main/java/cn/hutool/v7/socket/udp/UdpSession.java b/hutool-socket/src/main/java/cn/hutool/v7/socket/udp/UdpSession.java index be62c43ad1..16dc450dce 100644 --- a/hutool-socket/src/main/java/cn/hutool/v7/socket/udp/UdpSession.java +++ b/hutool-socket/src/main/java/cn/hutool/v7/socket/udp/UdpSession.java @@ -134,10 +134,11 @@ public class UdpSession implements Closeable { * 发送数据 * * @param data 发送的数据包 + * @return this * @throws IORuntimeException IO异常 */ - public void send(final T data) throws IORuntimeException { - send(data, this.remoteAddress); + public UdpSession send(final T data) throws IORuntimeException { + return send(data, this.remoteAddress); } /** @@ -145,9 +146,10 @@ public class UdpSession implements Closeable { * * @param data 发送的数据包 * @param remoteAddress 远程地址 + * @return this * @throws IORuntimeException IO异常 */ - public void send(final T data, final SocketAddress remoteAddress) throws IORuntimeException { + public UdpSession send(final T data, final SocketAddress remoteAddress) throws IORuntimeException { Assert.notNull(encoder, "Encoder can not be null when send data"); final byte[] payload = encoder.encode(data); final DatagramPacket packet = new DatagramPacket(payload, payload.length, remoteAddress); @@ -156,6 +158,7 @@ public class UdpSession implements Closeable { } catch (final IOException e) { throw new IORuntimeException(e); } + return this; } /** @@ -163,9 +166,10 @@ public class UdpSession implements Closeable { * * @param heartbeatMsg 心跳消息 * @throws IORuntimeException IO异常 + * @return this */ - public void sendHeartbeat(final T heartbeatMsg) throws IORuntimeException { - send(heartbeatMsg); + public UdpSession sendHeartbeat(final T heartbeatMsg) throws IORuntimeException { + return send(heartbeatMsg); } /** @@ -190,6 +194,7 @@ public class UdpSession implements Closeable { * @param scheduler 定时线程 * @return 定时任务 */ + @SuppressWarnings("resource") public ScheduledFuture scheduleHeartbeat(final T heartbeatMsg, final long interval, final ScheduledExecutorService scheduler) { return scheduler.scheduleAtFixedRate(() -> { try { diff --git a/hutool-socket/src/main/java/cn/hutool/v7/socket/udp/UdpUtil.java b/hutool-socket/src/main/java/cn/hutool/v7/socket/udp/UdpUtil.java index ccae48007a..27b296439e 100644 --- a/hutool-socket/src/main/java/cn/hutool/v7/socket/udp/UdpUtil.java +++ b/hutool-socket/src/main/java/cn/hutool/v7/socket/udp/UdpUtil.java @@ -36,6 +36,21 @@ import java.util.concurrent.ExecutorService; public class UdpUtil { // region ----- client + /** + * 通过UDP发送消息 + * + * @param 消息类型 + * @param host 目标主机地址 + * @param port 目标端口号 + * @param message 要发送的消息内容 + * @param encoder 消息编码器,用于将指定类型的消息转为bytes + */ + public static void sendByUdp(final String host, final int port, final T message, final UdpEncoder encoder) { + try (final UdpSession session = UdpUtil.ofSender(host, port, encoder)) { + session.send(message); + } + } + /** * 创建UDP客户端消息发送器,发送器没有状态,没有连接,不会接收消息,只是单纯发送 *