Skip to content

Commit

Permalink
优化反射实例化的类
Browse files Browse the repository at this point in the history
  • Loading branch information
leaderli committed Jul 25, 2024
1 parent eee8515 commit a3e5482
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public static <T> Lino<T> newInstance(Class<T> cls) {
return Lino.none();
}
if (cls.isEnum()) {
return getMethod(cls, "values").mapIgnoreError(m -> m.invoke(null)).toLira(cls).first();
return invokeStaticMethodByName(cls, "values").toLira(cls).first();
}
Lino<T> instance = getConstructor(cls).unzip(ReflectUtil::newInstance);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.leaderli.litool.core.bean;

public class EnumTest {

enum State {
A
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.leaderli.litool.core.type;

import io.leaderli.litool.core.bean.EnumTest;
import io.leaderli.litool.core.lang.DisposableRunnableProxy;
import io.leaderli.litool.core.meta.Lino;
import io.leaderli.litool.core.meta.WhenThrowBehavior;
Expand Down Expand Up @@ -217,6 +218,11 @@ void newInstance() throws NoSuchMethodException, InstantiationException, Illegal
Class<? extends Supplier> cls = supplier.getClass();

Assertions.assertNotNull(ReflectUtil.newInstance(cls).get().get());

// enum

Class<?> declaredClass = EnumTest.class.getDeclaredClasses()[0];
Assertions.assertEquals("A", ReflectUtil.newInstance(declaredClass).get().toString());
}

enum NoneEnum {
Expand Down Expand Up @@ -532,5 +538,4 @@ public class Test1 {

}


}

0 comments on commit a3e5482

Please sign in to comment.