Skip to content

Commit

Permalink
Merge common logic
Browse files Browse the repository at this point in the history
Signed-off-by: Hendrix-Shen <HendrixShen@hendrixshen.top>
  • Loading branch information
Hendrix-Shen committed Jun 9, 2024
1 parent f37c251 commit f96a408
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import top.hendrixshen.magiclib.util.MiscUtil;
import top.hendrixshen.magiclib.util.collect.InfoNode;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

@ApiStatus.Internal
@NoArgsConstructor(access = AccessLevel.PRIVATE)
Expand Down Expand Up @@ -57,7 +57,7 @@ public void check() {
}

private @Nullable DependencyCheckException check(ModMetaDataAdapter modMetaDataAdapter, ClassNode entryPoint) {
List<DependenciesContainer<?>> dependencies = Lists.newArrayList();
List<DependenciesContainer<Object>> dependencies = Lists.newArrayList();

if (MagicLib.getInstance().getCurrentPlatform().getCurrentDistType()
.matches(DistType.CLIENT)) {
Expand Down Expand Up @@ -85,12 +85,9 @@ public void check() {
continue;
}

return DependencyUtil.parseDependencies(method)
.stream()
.map(node -> DependenciesContainer.of(node, null))
.collect(Collectors.toList());
return DependencyUtil.parseDependencies(method, null);

Check warning on line 88 in magiclib-core/common/src/main/java/top/hendrixshen/magiclib/impl/dependency/EntryPointDependency.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

magiclib-core/common/src/main/java/top/hendrixshen/magiclib/impl/dependency/EntryPointDependency.java#L88

Avoid using a branching statement as the last in a loop.
}

return Lists.newArrayList();
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package top.hendrixshen.magiclib.impl.mixin.checker;

import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import top.hendrixshen.magiclib.api.dependency.DependencyCheckException;
import top.hendrixshen.magiclib.api.i18n.I18n;
Expand All @@ -30,11 +29,9 @@
import top.hendrixshen.magiclib.util.DependencyUtil;
import top.hendrixshen.magiclib.util.MiscUtil;
import top.hendrixshen.magiclib.util.collect.InfoNode;
import top.hendrixshen.magiclib.util.collect.ValueContainer;
import top.hendrixshen.magiclib.util.mixin.MixinUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
* Reference to <a href="https://github.com/Fallen-Breath/conditional-mixin/blob/88cbb739c375925b134a464428a1f67ee3bd74e2/common/src/main/java/me/fallenbreath/conditionalmixin/impl/SimpleRestrictionChecker.java">conditional mixin<a/>
Expand All @@ -51,24 +48,19 @@ public boolean check(String targetClassName, String mixinClassName) {
return false;
}

List<AnnotationNode> nodes = DependencyUtil.parseDependencies(mixinClassNode);
List<DependenciesContainer<ClassNode>> nodes = DependencyUtil.parseDependencies(mixinClassNode, targetClassNode);

if (nodes.isEmpty()) {
return true;
}

List<DependenciesContainer<?>> dependencies = nodes
.stream()
.map(node -> DependenciesContainer.of(node, targetClassNode))
.collect(Collectors.toList());

if (dependencies.stream().anyMatch(DependenciesContainer::isSatisfied)) {
if (nodes.stream().anyMatch(DependenciesContainer::isSatisfied)) {
return true;
}

InfoNode rootNode = new InfoNode(null, I18n.tr("magiclib.dependency.checker.mixin.title",
mixinClassName, targetClassName));
MiscUtil.generateDependencyCheckMessage(dependencies, rootNode);
MiscUtil.generateDependencyCheckMessage(nodes, rootNode);
this.onCheckFailure(targetClassName, mixinClassName, new DependencyCheckException(rootNode.toString()));

return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package top.hendrixshen.magiclib.util;

import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
Expand All @@ -16,57 +15,60 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DependencyUtil {
public static @NotNull List<AnnotationNode> parseDependencies(ClassNode classNode) {
AnnotationNode compositeNode = Annotations.getVisible(classNode, CompositeDependencies.class);
public static <T> List<DependenciesContainer<T>> parseDependencies(ClassNode classNode, T instance) {
List<DependenciesContainer<T>> composite = DependencyUtil.convertCompositeDependencies(ValueContainer
.ofNullable(Annotations.getVisible(classNode, CompositeDependencies.class)), instance);

if (compositeNode == null) {
AnnotationNode dependenciesNode = Annotations.getVisible(classNode, Dependencies.class);

if (dependenciesNode != null) {
return Lists.newArrayList(dependenciesNode);
}
} else {
return Annotations.getValue(compositeNode, "value", true);
if (!composite.isEmpty()) {
return composite;
}

return Collections.emptyList();
return DependencyUtil.convertDependencies(ValueContainer.ofNullable(Annotations.getVisible(classNode,
Dependencies.class)), instance);
}

public static @NotNull List<AnnotationNode> parseDependencies(MethodNode methodNode) {
AnnotationNode compositeNode = Annotations.getVisible(methodNode, CompositeDependencies.class);

if (compositeNode == null) {
AnnotationNode dependenciesNode = Annotations.getVisible(methodNode, Dependencies.class);
public static @NotNull <T> List<DependenciesContainer<T>> parseDependencies(MethodNode methodNode, T instance) {
List<DependenciesContainer<T>> composite = DependencyUtil.convertCompositeDependencies(ValueContainer
.ofNullable(Annotations.getVisible(methodNode, CompositeDependencies.class)), instance);

if (dependenciesNode != null) {
return Lists.newArrayList(dependenciesNode);
}
} else {
return Annotations.getValue(compositeNode, "value", true);
if (!composite.isEmpty()) {
return composite;
}

return Collections.emptyList();
return DependencyUtil.convertDependencies(ValueContainer.ofNullable(Annotations.getVisible(methodNode,
Dependencies.class)), instance);
}

public static @NotNull List<AnnotationNode> parseDependencies(FieldNode fieldNode) {
AnnotationNode compositeNode = Annotations.getVisible(fieldNode, CompositeDependencies.class);
public static @NotNull <T> List<DependenciesContainer<T>> parseDependencies(FieldNode fieldNode, T instance) {
List<DependenciesContainer<T>> composite = DependencyUtil.convertCompositeDependencies(ValueContainer
.ofNullable(Annotations.getVisible(fieldNode, CompositeDependencies.class)), instance);

if (compositeNode == null) {
AnnotationNode dependenciesNode = Annotations.getVisible(fieldNode, Dependencies.class);

if (dependenciesNode != null) {
return Lists.newArrayList(dependenciesNode);
}
} else {
return Annotations.getValue(compositeNode, "value", true);
if (!composite.isEmpty()) {
return composite;
}

return Collections.emptyList();
return DependencyUtil.convertDependencies(ValueContainer.ofNullable(Annotations.getVisible(fieldNode,
Dependencies.class)), instance);
}

private static <T> List<DependenciesContainer<T>> convertCompositeDependencies(
@NotNull ValueContainer<AnnotationNode> composite, T instance) {
return composite.map(compositeNode -> Annotations.getValue(compositeNode, "value", true))
.orElse(Collections.emptyList())
.stream()
.map(dependenciesNode -> DependenciesContainer.of((AnnotationNode) dependenciesNode, instance))
.collect(Collectors.toList());
}

private static <T> List<DependenciesContainer<T>> convertDependencies(
@NotNull ValueContainer<AnnotationNode> dependencies, T instance) {
return dependencies.stream()
.map(d -> DependenciesContainer.of(d, instance))
.collect(Collectors.toList());
}

public static <T> @NotNull List<DependenciesContainer<T>> parseDependencies(@NotNull Field field, T instance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static <T> T cast(Object obj) {
return (T) obj;
}

public static void generateDependencyCheckMessage(@NotNull List<DependenciesContainer<?>> dependencies,
public static <T> void generateDependencyCheckMessage(@NotNull List<DependenciesContainer<T>> dependencies,
InfoNode rootNode) {
boolean first = true;
boolean composite = false;
Expand Down

0 comments on commit f96a408

Please sign in to comment.