Skip to content

Commit

Permalink
plugin formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Linyuzai committed Jun 8, 2024
1 parent 374bf0a commit 0f4901d
Show file tree
Hide file tree
Showing 16 changed files with 166 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.github.linyuzai.plugin.core.exception.PluginException;
import com.github.linyuzai.plugin.core.handle.PluginHandler;
import com.github.linyuzai.plugin.core.match.PluginText;
import com.github.linyuzai.plugin.core.match.PluginProperties;
import com.github.linyuzai.plugin.core.resolve.PluginResolver;
import lombok.Getter;
import lombok.NonNull;
Expand Down Expand Up @@ -75,7 +74,6 @@ public DynamicExtractor(@NonNull Object target) {
/**
* 通过 {@link Parameter} 获得一个执行器。
* 如果标注了特殊的注解将会直接匹配,
* {@link PluginProperties} 返回 {@link PropertiesExtractor} 对应的执行器,
* {@link PluginText} 返回 {@link ContentExtractor} 对应的执行器。
* 否则按照 {@link PluginContextExtractor} {@link PluginObjectExtractor}
* {@link PropertiesExtractor} {@link ContentExtractor} 的顺序匹配执行器。
Expand Down Expand Up @@ -116,23 +114,21 @@ public Invoker getInvoker(Parameter parameter) {
* @return 如果是明确指定的返回 true,否则返回 false
*/
public boolean hasExplicitAnnotation(Annotation annotation) {
return annotation.annotationType() == PluginProperties.class ||
annotation.annotationType() == PluginText.class;
return annotation.annotationType() == PluginText.class;
}

/**
* 根据明确指定的注解获得对应的执行器。
* {@link PluginProperties} 返回 {@link PropertiesExtractor} 对应的执行器,
* {@link PluginText} 返回 {@link ContentExtractor} 对应的执行器。
*
* @param annotation 注解
* @param parameter 参数 {@link Parameter}
* @return 插件提取执行器
*/
public Invoker getExplicitInvoker(Annotation annotation, Parameter parameter) {
if (annotation.annotationType() == PluginProperties.class) {
/*if (annotation.annotationType() == PluginProperties.class) {
return getPropertiesInvoker(parameter);
}
}*/
if (annotation.annotationType() == PluginText.class) {
String charset = ((PluginText) annotation).charset();
return getContentInvoker(parameter, charset.isEmpty() ? null : Charset.forName(charset));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.github.linyuzai.plugin.core.format.PluginFormatter;
import com.github.linyuzai.plugin.core.match.ContentMatcher;
import com.github.linyuzai.plugin.core.match.PluginMatcher;
import com.github.linyuzai.plugin.core.match.PluginProperties;
import com.github.linyuzai.plugin.core.match.PropertiesMatcher;
import com.github.linyuzai.plugin.core.type.MapTypeMetadata;
import com.github.linyuzai.plugin.core.type.ObjectTypeMetadata;
Expand All @@ -31,7 +30,7 @@ public abstract class PropertiesExtractor<T> extends TypeMetadataPluginExtractor
/**
* 匹配类型为 {@link Properties} {@link Map}
* 及对应类型的 {@link java.util.Collection} {@link java.util.List} {@link java.util.Set}
* {@link java.util.Map} 和数组或是添加了 {@link PluginProperties} 注解
* {@link java.util.Map} 和数组
*
* @param metadata {@link TypeMetadata}
* @param annotations 注解
Expand All @@ -44,11 +43,11 @@ public PluginMatcher getMatcher(TypeMetadata metadata, Annotation[] annotations)
|| metadata instanceof MapTypeMetadata && elementClass == String.class) {
return new PropertiesMatcher(annotations);
}
for (Annotation annotation : annotations) {
/*for (Annotation annotation : annotations) {
if (annotation.annotationType() == PluginProperties.class) {
return new PropertiesMatcher(annotations);
}
}
}*/
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
/**
* {@link Properties} 过滤器
*/
@Deprecated
@Getter
public class PropertiesFilter extends AbstractPluginFilter<Supplier<Properties>> {
class PropertiesFilter extends AbstractPluginFilter<Supplier<Properties>> {

/**
* 属性健模式
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.linyuzai.plugin.core.format;

import com.github.linyuzai.plugin.core.tree.PluginTree;
import lombok.AllArgsConstructor;
import lombok.Getter;

Expand All @@ -13,7 +14,7 @@
*/
@Getter
@AllArgsConstructor
public class MapToArrayFormatter extends AbstractPluginFormatter<Map<?, ?>, Object> {
public class MapToArrayFormatter extends TreeNodePluginFormatter<Object> {

/**
* 数组的类型
Expand All @@ -23,16 +24,15 @@ public class MapToArrayFormatter extends AbstractPluginFormatter<Map<?, ?>, Obje
/**
* 格式化,根据数组类型实例化并添加 {@link Map} 的 value
*
* @param source 被格式化的对象
* @param nodes 被格式化的对象
* @return 数组格式的插件
*/
@Override
public Object doFormat(Map<?, ?> source) {
List<Object> values = new ArrayList<>(source.values());
Object array = Array.newInstance(arrayClass, values.size());
for (int i = 0; i < values.size(); i++) {
Object o = values.get(i);
Array.set(array, i, o);
public Object formatNodes(List<PluginTree.Node> nodes) {
Object array = Array.newInstance(arrayClass, nodes.size());
for (int i = 0; i < nodes.size(); i++) {
PluginTree.Node node = nodes.get(i);
Array.set(array, i, node.getValue());
}
return array;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package com.github.linyuzai.plugin.core.format;

import com.github.linyuzai.plugin.core.tree.PluginTree;
import com.github.linyuzai.plugin.core.util.ReflectionUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;

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

/**
* {@link Map} 转 {@link List} 的格式器
*/
@Getter
@AllArgsConstructor
public class MapToListFormatter extends AbstractPluginFormatter<Map<?, ?>, List<Object>> {
public class MapToListFormatter extends TreeNodePluginFormatter<List<Object>> {

/**
* {@link List} 的类型
Expand All @@ -26,13 +29,15 @@ public MapToListFormatter() {
/**
* 格式化,根据 {@link List} 类型实例化并添加 {@link Map} 的 value
*
* @param source 被格式化的对象
* @param nodes 被格式化的对象
* @return {@link List} 格式的插件
*/
@Override
public List<Object> doFormat(Map<?, ?> source) {
public List<Object> formatNodes(List<PluginTree.Node> nodes) {
List<Object> list = ReflectionUtils.newList(listClass);
list.addAll(source.values());
list.addAll(nodes.stream()
.map(PluginTree.Node::getValue)
.collect(Collectors.toList()));
return list;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.github.linyuzai.plugin.core.format;

import com.github.linyuzai.plugin.core.tree.PluginTree;
import com.github.linyuzai.plugin.core.util.ReflectionUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* {@link Map} 转 {@link Map} 的格式器
*/
@Getter
@AllArgsConstructor
public class MapToMapFormatter extends AbstractPluginFormatter<Map<?, ?>, Map<Object, Object>> {
public class MapToMapFormatter extends TreeNodePluginFormatter<Map<Object, Object>> {

/**
* {@link Map} 的类型
Expand All @@ -25,13 +28,15 @@ public MapToMapFormatter() {
/**
* 格式化,根据 {@link Map} 类型实例化并添加 {@link Map} 的 entry
*
* @param source 被格式化的对象
* @param nodes 被格式化的对象
* @return {@link Map} 格式的插件
*/
@Override
public Map<Object, Object> doFormat(Map<?, ?> source) {
public Map<Object, Object> formatNodes(List<PluginTree.Node> nodes) {
Map<Object, Object> map = ReflectionUtils.newMap(mapClass);
map.putAll(source);
for (PluginTree.Node node : nodes) {
map.put(node.getId(), node.getValue());
}
return map;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.linyuzai.plugin.core.format;

import com.github.linyuzai.plugin.core.exception.PluginException;
import com.github.linyuzai.plugin.core.tree.PluginTree;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -9,20 +10,19 @@
/**
* {@link Map} 转单个 {@link Object} 的格式器
*/
public class MapToObjectFormatter extends AbstractPluginFormatter<Map<?, ?>, Object> {
public class MapToObjectFormatter extends TreeNodePluginFormatter<Object> {

/**
* 格式化,获得 {@link Map} 中的 value,如果有多个则抛出 {@link PluginException}
*
* @param source 被格式化的对象
* @param nodes 被格式化的对象
* @return 单个插件对象
*/
@Override
public Object doFormat(Map<?, ?> source) {
List<Object> list = new ArrayList<>(source.values());
if (list.size() > 1) {
throw new PluginException("More than one plugin matched: " + list);
public Object formatNodes(List<PluginTree.Node> nodes) {
if (nodes.size() > 1) {
throw new PluginException("More than one plugin matched: " + nodes);
}
return list.get(0);
return nodes.get(0).getValue();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.github.linyuzai.plugin.core.format;

import com.github.linyuzai.plugin.core.tree.PluginTree;
import com.github.linyuzai.plugin.core.util.ReflectionUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* {@link Map} 转 {@link Set} 的格式器
*/
@Getter
@AllArgsConstructor
public class MapToSetFormatter extends AbstractPluginFormatter<Map<?, ?>, Set<Object>> {
public class MapToSetFormatter extends TreeNodePluginFormatter<Set<Object>> {

/**
* {@link Set} 的类型
Expand All @@ -26,13 +30,15 @@ public MapToSetFormatter() {
/**
* 格式化,根据 {@link Set} 类型实例化并添加 {@link Map} 的 value
*
* @param source 被格式化的对象
* @param nodes 被格式化的对象
* @return {@link Set} 格式的插件
*/
@Override
public Set<Object> doFormat(Map<?, ?> source) {
public Set<Object> formatNodes(List<PluginTree.Node> nodes) {
Set<Object> set = ReflectionUtils.newSet(setClass);
set.addAll(source.values());
set.addAll(nodes.stream()
.map(PluginTree.Node::getValue)
.collect(Collectors.toList()));
return set;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.linyuzai.plugin.core.format;

import com.github.linyuzai.plugin.core.tree.PluginTree;

import java.util.ArrayList;
import java.util.List;

public abstract class TreeNodePluginFormatter<T> extends AbstractPluginFormatter<PluginTree.Node, T> {

@Override
public T doFormat(PluginTree.Node node) {
List<PluginTree.Node> nodes = new ArrayList<>();
node.forEach(it -> {
if (!it.isPluginNode()) {
nodes.add(it);
}
});
return formatNodes(nodes);
}

public abstract T formatNodes(List<PluginTree.Node> nodes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,9 @@ public boolean filter(PluginTree.Node node, PluginContext context) {
public boolean hasContent(PluginTree.Node node) {
AtomicInteger size = new AtomicInteger(0);
node.forEach(it -> {
if (it instanceof Plugin) {
return;
if (!it.isPluginNode()) {
size.incrementAndGet();
}
size.incrementAndGet();
});
return size.get() > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Deprecated
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface PluginProperties {
@interface PluginProperties {

/**
* 属性名
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.linyuzai.plugin.core.match;

import com.github.linyuzai.plugin.core.context.PluginContext;
import com.github.linyuzai.plugin.core.filter.PropertiesFilter;
import com.github.linyuzai.plugin.core.handle.HandlerDependency;
import com.github.linyuzai.plugin.core.resolve.PropertiesResolver;
import lombok.Getter;
Expand All @@ -17,21 +16,8 @@
@HandlerDependency(PropertiesResolver.class)
public class PropertiesMatcher extends AbstractPluginMatcher<Supplier<Properties>> {

/**
* {@link Properties} 过滤器
*/
private PropertiesFilter propertiesFilter;

public PropertiesMatcher(Annotation[] annotations) {
super(annotations);
for (Annotation annotation : annotations) {
if (annotation.annotationType() == PluginProperties.class) {
String[] propertiesKeys = ((PluginProperties) annotation).value();
if (propertiesKeys.length > 0) {
propertiesFilter = new PropertiesFilter(propertiesKeys);
}
}
}
}

@Override
Expand All @@ -41,7 +27,6 @@ public Object getKey() {

@Override
public boolean doFilter(Supplier<Properties> source, PluginContext context) {
//TODO 需要过滤?
return true;
}
}
Loading

0 comments on commit 0f4901d

Please sign in to comment.