This commit is contained in:
Looly
2026-01-03 11:55:56 +08:00
parent 270f5944de
commit 933be58956
18 changed files with 283 additions and 160 deletions

View File

@@ -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<ResolvedAnnotationMapping> 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<GenericAnnotationMapping> 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<ResolvedAnnotationMapping> 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<GenericAnnotationMapping> 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))
));
}

View File

@@ -67,7 +67,7 @@ public final class AnnotationMappingProxy<T extends Annotation> implements Invoc
* @return 代理对象
*/
@SuppressWarnings("unchecked")
public static <A extends Annotation> A create(final Class<? extends A> annotationType, final AnnotationMapping<A> mapping) {
public static <A extends Annotation> A of(final Class<? extends A> annotationType, final AnnotationMapping<A> mapping) {
Objects.requireNonNull(annotationType);
Objects.requireNonNull(mapping);
final AnnotationMappingProxy<A> invocationHandler = new AnnotationMappingProxy<>(mapping);

View File

@@ -40,7 +40,7 @@ public class GenericAnnotationMapping implements AnnotationMapping<Annotation> {
* @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);
}

View File

@@ -133,8 +133,8 @@ public class ResolvedAnnotationMapping implements AnnotationMapping<Annotation>
* @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<Annotation>
* @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<Annotation>
if (Objects.isNull(proxied)) {
synchronized (this) {
if (Objects.isNull(proxied)) {
proxied = AnnotationMappingProxy.create(annotationType(), this);
proxied = AnnotationMappingProxy.of(annotationType(), this);
}
}
}

View File

@@ -75,7 +75,7 @@ public class MetaAnnotatedElement<T extends AnnotationMapping<Annotation>> imple
* @param <A> {@link AnnotationMapping}类型
* @return {@link AnnotatedElement}上的注解结构
*/
public static <A extends AnnotationMapping<Annotation>> MetaAnnotatedElement<A> create(
public static <A extends AnnotationMapping<Annotation>> MetaAnnotatedElement<A> of(
final AnnotatedElement element, final BiFunction<A, Annotation, A> mappingFactory) {
return new MetaAnnotatedElement<>(element, mappingFactory);
}

View File

@@ -85,9 +85,9 @@ public class RepeatableMetaAnnotatedElement<T extends AnnotationMapping<Annotati
* @param <A> {@link AnnotationMapping}类型
* @return {@link AnnotatedElement}上的注解结构
*/
public static <A extends AnnotationMapping<Annotation>> RepeatableMetaAnnotatedElement<A> create(
public static <A extends AnnotationMapping<Annotation>> RepeatableMetaAnnotatedElement<A> of(
final AnnotatedElement element, final BiFunction<A, Annotation, A> mappingFactory) {
return create(RepeatableAnnotationCollector.standard(), element, mappingFactory);
return of(RepeatableAnnotationCollector.standard(), element, mappingFactory);
}
/**
@@ -99,7 +99,7 @@ public class RepeatableMetaAnnotatedElement<T extends AnnotationMapping<Annotati
* @param <A> {@link AnnotationMapping}类型
* @return {@link AnnotatedElement}上的注解结构
*/
public static <A extends AnnotationMapping<Annotation>> RepeatableMetaAnnotatedElement<A> create(
public static <A extends AnnotationMapping<Annotation>> RepeatableMetaAnnotatedElement<A> of(
final RepeatableAnnotationCollector collector,
final AnnotatedElement element,
final BiFunction<A, Annotation, A> mappingFactory) {

View File

@@ -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));
}

View File

@@ -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<ResolvedAnnotationMapping> element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY);
final MetaAnnotatedElement<ResolvedAnnotationMapping> 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<ResolvedAnnotationMapping> element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY);
final MetaAnnotatedElement<ResolvedAnnotationMapping> 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<ResolvedAnnotationMapping> element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY);
final MetaAnnotatedElement<ResolvedAnnotationMapping> 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<ResolvedAnnotationMapping> element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY);
final MetaAnnotatedElement<ResolvedAnnotationMapping> 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<ResolvedAnnotationMapping> element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY);
final MetaAnnotatedElement<ResolvedAnnotationMapping> 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<ResolvedAnnotationMapping> element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY);
final MetaAnnotatedElement<ResolvedAnnotationMapping> 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<ResolvedAnnotationMapping> element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY);
final MetaAnnotatedElement<ResolvedAnnotationMapping> 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<ResolvedAnnotationMapping> element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY);
final MetaAnnotatedElement<ResolvedAnnotationMapping> 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<ResolvedAnnotationMapping> element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY);
final MetaAnnotatedElement<ResolvedAnnotationMapping> 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<ResolvedAnnotationMapping> element = MetaAnnotatedElement.create(Foo.class, MAPPING_FACTORY);
final MetaAnnotatedElement<ResolvedAnnotationMapping> 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<ResolvedAnnotationMapping> element = MetaAnnotatedElement.create(source, MAPPING_FACTORY);
final MetaAnnotatedElement<ResolvedAnnotationMapping> element = MetaAnnotatedElement.of(source, MAPPING_FACTORY);
Assertions.assertSame(source, element.getElement());
}

View File

@@ -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<Class<? extends Annotation>> 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<Class<? extends Annotation>> 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<GenericAnnotationMapping> repeatableMetaAnnotatedElement = RepeatableMetaAnnotatedElement.create(
final RepeatableMetaAnnotatedElement<GenericAnnotationMapping> 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<GenericAnnotationMapping> element = RepeatableMetaAnnotatedElement.create(
final RepeatableMetaAnnotatedElement<GenericAnnotationMapping> element = RepeatableMetaAnnotatedElement.of(
RepeatableAnnotationCollector.standard(), Foo.class, (s, a) -> new GenericAnnotationMapping(a, Objects.isNull(s))
);
int count = 0;

View File

@@ -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存在别名value1value2因此也一并被覆写
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));

View File

@@ -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和端口<br>
* 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);
}
}
/**
* 远程主机是否处于连接状态<br>
* 通过判断远程地址获取成功与否判断
*
* @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);

View File

@@ -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和端口<br>
* 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);
}
}
/**
* 远程主机是否处于连接状态<br>
* 通过判断远程地址获取成功与否判断
*
* @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 <T> 消息类型
* @param host 目标主机地址
* @param port 目标端口号
* @param message 要发送的消息内容
* @param encoder 消息编码器用于将指定类型的消息转为bytes
*/
public static <T> void sendByUdp(final String host, final int port, final T message, final UdpEncoder<T> 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<ByteBuffer> 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<ByteBuffer> 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<ByteBuffer> 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<ByteBuffer> 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
}

View File

@@ -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<ByteBuffer> ioAction) {
public AioClient(final SocketAddress address, final IoAction<ByteBuffer> 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<ByteBuffer> ioAction, final SocketConfig config) {
public AioClient(final SocketAddress address, final IoAction<ByteBuffer> 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 <T> AioClient setOption(final SocketOption<T> 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

View File

@@ -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(), // 默认线程池大小

View File

@@ -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);
}
/**

View File

@@ -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();

View File

@@ -134,10 +134,11 @@ public class UdpSession<T> implements Closeable {
* 发送数据
*
* @param data 发送的数据包
* @return this
* @throws IORuntimeException IO异常
*/
public void send(final T data) throws IORuntimeException {
send(data, this.remoteAddress);
public UdpSession<T> send(final T data) throws IORuntimeException {
return send(data, this.remoteAddress);
}
/**
@@ -145,9 +146,10 @@ public class UdpSession<T> implements Closeable {
*
* @param data 发送的数据包
* @param remoteAddress 远程地址
* @return this
* @throws IORuntimeException IO异常
*/
public void send(final T data, final SocketAddress remoteAddress) throws IORuntimeException {
public UdpSession<T> 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<T> implements Closeable {
} catch (final IOException e) {
throw new IORuntimeException(e);
}
return this;
}
/**
@@ -163,9 +166,10 @@ public class UdpSession<T> implements Closeable {
*
* @param heartbeatMsg 心跳消息
* @throws IORuntimeException IO异常
* @return this
*/
public void sendHeartbeat(final T heartbeatMsg) throws IORuntimeException {
send(heartbeatMsg);
public UdpSession<T> sendHeartbeat(final T heartbeatMsg) throws IORuntimeException {
return send(heartbeatMsg);
}
/**
@@ -190,6 +194,7 @@ public class UdpSession<T> implements Closeable {
* @param scheduler 定时线程
* @return 定时任务
*/
@SuppressWarnings("resource")
public ScheduledFuture<?> scheduleHeartbeat(final T heartbeatMsg, final long interval, final ScheduledExecutorService scheduler) {
return scheduler.scheduleAtFixedRate(() -> {
try {

View File

@@ -36,6 +36,21 @@ import java.util.concurrent.ExecutorService;
public class UdpUtil {
// region ----- client
/**
* 通过UDP发送消息
*
* @param <T> 消息类型
* @param host 目标主机地址
* @param port 目标端口号
* @param message 要发送的消息内容
* @param encoder 消息编码器用于将指定类型的消息转为bytes
*/
public static <T> void sendByUdp(final String host, final int port, final T message, final UdpEncoder<T> encoder) {
try (final UdpSession<T> session = UdpUtil.ofSender(host, port, encoder)) {
session.send(message);
}
}
/**
* 创建UDP客户端消息发送器发送器没有状态没有连接不会接收消息只是单纯发送
*