mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -5,7 +5,7 @@ import org.dromara.hutool.core.collection.iter.LineIter;
|
||||
import org.dromara.hutool.core.io.resource.ResourceUtil;
|
||||
import org.dromara.hutool.core.io.stream.EmptyOutputStream;
|
||||
import org.dromara.hutool.core.io.stream.StrInputStream;
|
||||
import org.dromara.hutool.core.lang.func.SerConsumer;
|
||||
import org.dromara.hutool.core.func.SerConsumer;
|
||||
import org.dromara.hutool.core.util.CharsetUtil;
|
||||
import org.dromara.hutool.core.util.RandomUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
@@ -3,6 +3,7 @@ package org.dromara.hutool.core.lang.func;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.core.date.StopWatch;
|
||||
import org.dromara.hutool.core.func.FunctionPool;
|
||||
import org.dromara.hutool.core.util.RandomUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@@ -1,12 +1,14 @@
|
||||
package org.dromara.hutool.core.lang.func;
|
||||
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.reflect.ConstructorUtil;
|
||||
import org.dromara.hutool.core.reflect.MethodHandleUtil;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.SneakyThrows;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.func.LambdaFactory;
|
||||
import org.dromara.hutool.core.func.SerSupplier;
|
||||
import org.dromara.hutool.core.reflect.ConstructorUtil;
|
||||
import org.dromara.hutool.core.reflect.lookup.LookupUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -143,7 +145,7 @@ public class LambdaFactoryTest {
|
||||
something.setId(1L);
|
||||
something.setName("name");
|
||||
final Method getByReflect = Something.class.getMethod("getId");
|
||||
final MethodHandle getByMh = MethodHandleUtil.findMethod(Something.class, "getId", MethodType.methodType(Long.class));
|
||||
final MethodHandle getByMh = LookupUtil.findMethod(Something.class, "getId", MethodType.methodType(Long.class));
|
||||
final Function getByProxy = MethodHandleProxies.asInterfaceInstance(Function.class, MethodHandles.lookup().unreflect(getByReflect));
|
||||
final Function getByLambda = LambdaFactory.build(Function.class, getByReflect);
|
||||
final Task lambdaTask = new Task("lambda", () -> getByLambda.apply(something));
|
||||
@@ -230,7 +232,7 @@ public class LambdaFactoryTest {
|
||||
something.setId(1L);
|
||||
something.setName("name");
|
||||
final Method setByReflect = Something.class.getMethod("setName", String.class);
|
||||
final MethodHandle setByMh = MethodHandleUtil.findMethod(Something.class, "setName", MethodType.methodType(Void.TYPE, String.class));
|
||||
final MethodHandle setByMh = LookupUtil.findMethod(Something.class, "setName", MethodType.methodType(Void.TYPE, String.class));
|
||||
final BiConsumer setByProxy = MethodHandleProxies.asInterfaceInstance(BiConsumer.class, setByMh);
|
||||
final BiConsumer setByLambda = LambdaFactory.build(BiConsumer.class, setByReflect);
|
||||
final String name = "name1";
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package org.dromara.hutool.core.lang.func;
|
||||
|
||||
import org.dromara.hutool.core.func.*;
|
||||
import org.dromara.hutool.core.lang.Tuple;
|
||||
import org.dromara.hutool.core.reflect.MethodUtil;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package org.dromara.hutool.core.lang.func;
|
||||
|
||||
import org.dromara.hutool.core.collection.set.SetUtil;
|
||||
import org.dromara.hutool.core.func.PredicateUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@@ -4,8 +4,6 @@ import org.dromara.hutool.core.classloader.ClassLoaderUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
@@ -66,65 +64,6 @@ public class MethodHandleUtilTest {
|
||||
Assertions.assertEquals("Duck 78", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findMethodTest() throws Throwable {
|
||||
MethodHandle handle = MethodHandleUtil.findMethod(Duck.class, "quack",
|
||||
MethodType.methodType(String.class));
|
||||
Assertions.assertNotNull(handle);
|
||||
// 对象方法自行需要绑定对象或者传入对象参数
|
||||
final String invoke = (String) handle.invoke(new BigDuck());
|
||||
Assertions.assertEquals("Quack", invoke);
|
||||
|
||||
// 对象的方法获取
|
||||
handle = MethodHandleUtil.findMethod(BigDuck.class, "getSize",
|
||||
MethodType.methodType(int.class));
|
||||
Assertions.assertNotNull(handle);
|
||||
final int invokeInt = (int) handle.invoke(new BigDuck());
|
||||
Assertions.assertEquals(36, invokeInt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findStaticMethodTest() throws Throwable {
|
||||
final MethodHandle handle = MethodHandleUtil.findMethod(Duck.class, "getDuck",
|
||||
MethodType.methodType(String.class, int.class));
|
||||
Assertions.assertNotNull(handle);
|
||||
|
||||
// static 方法执行不需要绑定或者传入对象,直接传入参数即可
|
||||
final String invoke = (String) handle.invoke(12);
|
||||
Assertions.assertEquals("Duck 12", invoke);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findPrivateMethodTest() throws Throwable {
|
||||
final MethodHandle handle = MethodHandleUtil.findMethod(BigDuck.class, "getPrivateValue",
|
||||
MethodType.methodType(String.class));
|
||||
Assertions.assertNotNull(handle);
|
||||
|
||||
final String invoke = (String) handle.invoke(new BigDuck());
|
||||
Assertions.assertEquals("private value", invoke);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSuperMethodTest() throws Throwable {
|
||||
// 查找父类的方法
|
||||
final MethodHandle handle = MethodHandleUtil.findMethod(BigDuck.class, "quack",
|
||||
MethodType.methodType(String.class));
|
||||
Assertions.assertNotNull(handle);
|
||||
|
||||
final String invoke = (String) handle.invoke(new BigDuck());
|
||||
Assertions.assertEquals("Quack", invoke);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findPrivateStaticMethodTest() throws Throwable {
|
||||
final MethodHandle handle = MethodHandleUtil.findMethod(BigDuck.class, "getPrivateStaticValue",
|
||||
MethodType.methodType(String.class));
|
||||
Assertions.assertNotNull(handle);
|
||||
|
||||
final String invoke = (String) handle.invoke();
|
||||
Assertions.assertEquals("private static value", invoke);
|
||||
}
|
||||
|
||||
interface Duck {
|
||||
default String quack() {
|
||||
return "Quack";
|
||||
|
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.core.reflect.lookup;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodType;
|
||||
|
||||
public class LookupUtilTest {
|
||||
@Test
|
||||
public void findMethodTest() throws Throwable {
|
||||
MethodHandle handle = LookupUtil.findMethod(Duck.class, "quack",
|
||||
MethodType.methodType(String.class));
|
||||
Assertions.assertNotNull(handle);
|
||||
// 对象方法自行需要绑定对象或者传入对象参数
|
||||
final String invoke = (String) handle.invoke(new BigDuck());
|
||||
Assertions.assertEquals("Quack", invoke);
|
||||
|
||||
// 对象的方法获取
|
||||
handle = LookupUtil.findMethod(BigDuck.class, "getSize",
|
||||
MethodType.methodType(int.class));
|
||||
Assertions.assertNotNull(handle);
|
||||
final int invokeInt = (int) handle.invoke(new BigDuck());
|
||||
Assertions.assertEquals(36, invokeInt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findStaticMethodTest() throws Throwable {
|
||||
final MethodHandle handle = LookupUtil.findMethod(Duck.class, "getDuck",
|
||||
MethodType.methodType(String.class, int.class));
|
||||
Assertions.assertNotNull(handle);
|
||||
|
||||
// static 方法执行不需要绑定或者传入对象,直接传入参数即可
|
||||
final String invoke = (String) handle.invoke(12);
|
||||
Assertions.assertEquals("Duck 12", invoke);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findPrivateMethodTest() throws Throwable {
|
||||
final MethodHandle handle = LookupUtil.findMethod(BigDuck.class, "getPrivateValue",
|
||||
MethodType.methodType(String.class));
|
||||
Assertions.assertNotNull(handle);
|
||||
|
||||
final String invoke = (String) handle.invoke(new BigDuck());
|
||||
Assertions.assertEquals("private value", invoke);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSuperMethodTest() throws Throwable {
|
||||
// 查找父类的方法
|
||||
final MethodHandle handle = LookupUtil.findMethod(BigDuck.class, "quack",
|
||||
MethodType.methodType(String.class));
|
||||
Assertions.assertNotNull(handle);
|
||||
|
||||
final String invoke = (String) handle.invoke(new BigDuck());
|
||||
Assertions.assertEquals("Quack", invoke);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findPrivateStaticMethodTest() throws Throwable {
|
||||
final MethodHandle handle = LookupUtil.findMethod(BigDuck.class, "getPrivateStaticValue",
|
||||
MethodType.methodType(String.class));
|
||||
Assertions.assertNotNull(handle);
|
||||
|
||||
final String invoke = (String) handle.invoke();
|
||||
Assertions.assertEquals("private static value", invoke);
|
||||
}
|
||||
|
||||
interface Duck {
|
||||
default String quack() {
|
||||
return "Quack";
|
||||
}
|
||||
|
||||
static String getDuck(final int count){
|
||||
return "Duck " + count;
|
||||
}
|
||||
}
|
||||
|
||||
static class BigDuck implements Duck {
|
||||
public int getSize(){
|
||||
return 36;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private String getPrivateValue(){
|
||||
return "private value";
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static String getPrivateStaticValue(){
|
||||
return "private static value";
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user