Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opti Reflection: Remake reflect part & util. #15

Closed
wants to merge 13 commits into from
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package band.kessoku.lib.base.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public final class ModifiersUtil {

/* === Common === */

public static boolean isPublic(Field field) {
return Modifier.isPublic(field.getModifiers());
}

public static boolean isPublic(Method method) {
return Modifier.isPublic(method.getModifiers());
}

public static boolean isStatic(Field field) {
return Modifier.isStatic(field.getModifiers());
}

public static boolean isStatic(Method method) {
return Modifier.isStatic(method.getModifiers());
}

/* === Common Combo === */

public static boolean isPublicAndStatic(Field field) {
return isPublic(field) && isStatic(field);
}

public static boolean isPublicAndStatic(Method method) {
return isPublic(method) && isStatic(method);
}

public static boolean isPublicOrStatic(Field field, boolean shouldPublic, boolean shouldStatic) {
return shouldPublic == isPublic(field) && shouldStatic == isStatic(field);
}

public static boolean isPublicOrStatic(Method method, boolean shouldPublic, boolean shouldStatic) {
return shouldPublic == isPublic(method) && shouldStatic == isStatic(method);
}

/* === Common End === */

/* === Data Object === */

public static boolean isVolatile(Field field) {
return Modifier.isVolatile(field.getModifiers());
}

public static boolean isVolatile(Method method) {
return Modifier.isVolatile(method.getModifiers());
}

public static boolean isTransient(Field field) {
return Modifier.isTransient(field.getModifiers());
}

public static boolean isTransient(Method method) {
return Modifier.isTransient(method.getModifiers());
}

/* === Data Object End === */

/* === Low Usage Tools === */

public static boolean isFinal(Field field) {
return Modifier.isFinal(field.getModifiers());
}

public static boolean isFinal(Method method) {
return Modifier.isFinal(method.getModifiers());
}

public static boolean isProtected(Field field) {
return Modifier.isProtected(field.getModifiers());
}

public static boolean isProtected(Method method) {
return Modifier.isProtected(method.getModifiers());
}

/* === Low Usage Tools End === */
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package band.kessoku.lib.base.reflect;

import org.apache.logging.log4j.core.util.ReflectionUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

public final class ReflectUtil {
public static void makeAccessible(Field field) {
ReflectionUtil.makeAccessible(field);
}

public static void makeAccessible(Method method) {
ReflectionUtil.makeAccessible(method);
}

public static boolean isAssignableFrom(Field field, Class<?> ... clazzs) {
var flag = Arrays.stream(clazzs).anyMatch(clazz -> !field.getType().isAssignableFrom(clazz));
return !flag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package band.kessoku.lib.config.api;

import band.kessoku.lib.base.ModUtils;
import band.kessoku.lib.base.reflect.ModifiersUtil;
import band.kessoku.lib.base.reflect.ReflectUtil;
import band.kessoku.lib.config.KessokuConfig;
import band.kessoku.lib.config.api.annotations.Comment;
import band.kessoku.lib.config.api.annotations.Comments;
Expand Down Expand Up @@ -84,7 +86,9 @@ public boolean load() {
// Check the value is public and not static
try {
Field field = this.getClass().getField(key);
if (!Modifier.isPublic(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) continue;
if (!ModifiersUtil.isPublicOrStatic(field, true, false)) {
continue;
}
value = (ConfigValue) ReflectionUtil.getFieldValue(field, this);
} catch (NoSuchFieldException e) {
continue;
Expand Down Expand Up @@ -143,12 +147,9 @@ public void reset() {

List<Field> fields = new ArrayList<>();
for (Field declaredField : this.getClass().getDeclaredFields()) {
declaredField.setAccessible(true);

final boolean flag0 = declaredField.getDeclaringClass().isAssignableFrom(ConfigValue.class);
final boolean flag1 = Modifier.isPublic(declaredField.getModifiers());
final boolean flag2 = !Modifier.isStatic(declaredField.getModifiers());
if (flag0 && flag1 && flag2) {
final boolean flag0 = ReflectUtil.isAssignableFrom(declaredField, ConfigValue.class);
final boolean flag1 = ModifiersUtil.isPublicOrStatic(declaredField, true, false);
if (flag0 && flag1) {
fields.add(declaredField);
}
}
Expand All @@ -167,8 +168,8 @@ public ImmutableList<AbstractConfig> getValidCategories() {
for (Field declaredField : this.getClass().getDeclaredFields()) {
declaredField.setAccessible(true);

final boolean flag0 = declaredField.getDeclaringClass().isAssignableFrom(AbstractConfig.class);
final boolean flag1 = Modifier.isPublic(declaredField.getModifiers());
final boolean flag0 = ReflectUtil.isAssignableFrom(declaredField, AbstractConfig.class);
final boolean flag1 = ModifiersUtil.isPublic(declaredField);
if (flag0 && flag1){
fields.add(declaredField);
}
Expand All @@ -181,15 +182,10 @@ public ImmutableList<AbstractConfig> getValidCategories() {
private ImmutableList<Field> getValidFields() {
ImmutableList.Builder<Field> builder = ImmutableList.builder();
for (Field declaredField : this.getClass().getDeclaredFields()) {
declaredField.setAccessible(true);

final boolean flag0 = declaredField.getDeclaringClass().isAssignableFrom(AbstractConfig.class);
final boolean flag1 = declaredField.getDeclaringClass().isAssignableFrom(ConfigValue.class);
final boolean flag2 = Modifier.isPublic(declaredField.getModifiers());

final var flag = flag0 || flag1;
final boolean flag0 = ReflectUtil.isAssignableFrom(declaredField, AbstractConfig.class, ConfigValue.class);
final boolean flag1 = Modifier.isPublic(declaredField.getModifiers());

if (flag && flag2){
if (flag0 && flag1){
builder.add(declaredField);
}
}
Expand All @@ -199,7 +195,7 @@ private ImmutableList<Field> getValidFields() {
private Map<String, ValueWithComment> serialize() {
ImmutableMap.Builder<String, ValueWithComment> builder = ImmutableMap.builder();
for (Field field : this.getValidFields()) {
field.setAccessible(true);
ReflectUtil.makeAccessible(field);

final String name = field.isAnnotationPresent(Name.class) ? field.getAnnotation(Name.class).value() : field.getName();
final String[] comments = field.isAnnotationPresent(Comments.class) ? (String[]) Arrays.stream(field.getAnnotation(Comments.class).value()).map(Comment::value).toArray() : new String[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.config.api.serializers;
package band.kessoku.lib.config.serializers;

import band.kessoku.lib.config.api.AbstractConfig;
import band.kessoku.lib.config.api.ConfigSerializer;
Expand Down Expand Up @@ -45,9 +45,13 @@ private Json5Builder.ObjectBean toBean(Map<String, AbstractConfig.ValueWithComme
Json5Builder builder = new Json5Builder();
Json5Builder.ObjectBean objectBean = builder.getObjectBean();
value.forEach((s, valueWithComment) -> {
for (String comment : valueWithComment.comments()) objectBean.addNote(comment);
if (valueWithComment.object() instanceof Map<?, ?>)
for (String comment : valueWithComment.comments()) {
objectBean.addNote(comment);
}

if (valueWithComment.object() instanceof Map<?, ?>) {
objectBean.addBean(s, this.toBean((Map<String, AbstractConfig.ValueWithComment>) valueWithComment.object()));
}
});
return objectBean;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.config.api.serializers;
package band.kessoku.lib.config.serializers;

import band.kessoku.lib.config.api.AbstractConfig;
import band.kessoku.lib.config.api.ConfigSerializer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.config.api.serializers;
package band.kessoku.lib.config.serializers;

import band.kessoku.lib.config.api.AbstractConfig;
import band.kessoku.lib.config.api.ConfigSerializer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.config.api.values;
package band.kessoku.lib.config.values;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.config.api.values;
package band.kessoku.lib.config.values;

import band.kessoku.lib.config.api.ConfigValue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.config.api.values;
package band.kessoku.lib.config.values;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.config.api.values;
package band.kessoku.lib.config.values;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.config.api.values;
package band.kessoku.lib.config.values;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.config.api.values;
package band.kessoku.lib.config.values;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;
Expand Down Expand Up @@ -113,7 +114,11 @@ public boolean remove(Object o) {

@Override
public boolean containsAll(@NotNull Collection<?> c) {
return this.value.containsAll(c);
/* Amarok Note:
* ArrayList 的数据存储量不可知的情况下,悲观假设有大量数据,那么 ArrayList#containsAll 的效率会低的令人发指...
* 虽说如果数据量少的话套一层 Set 也是无端浪费性能... 但我依然建议悲观演算。
* */
return Sets.newHashSet(this.value).containsAll(c);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.config.api.values;
package band.kessoku.lib.config.values;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.config.api.values;
package band.kessoku.lib.config.values;

import com.google.common.collect.ImmutableMap;
import org.jetbrains.annotations.Contract;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.config.api.values;
package band.kessoku.lib.config.values;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand Down