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

提交两个插件 MybatisPlus和OpenFeignPlugin #157

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public class InvokeType implements java.io.Serializable {

public static InvokeType CAFFEINE_CACHE = new InvokeType("caffeine-cache");

public static InvokeType MYBATISPLUS = new InvokeType("mybatis-plus");

public static InvokeType OPENFEIGN = new InvokeType("openfeign");

private String name;

public InvokeType(String name) {
Expand Down
37 changes: 37 additions & 0 deletions repeater-plugins/mybatis-plus-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>repeater-plugins</artifactId>
<groupId>com.alibaba.jvm.sandbox</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>mybatis-plus-plugin</artifactId>

<build>
<finalName>${project.name}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.alibaba.jvm.sandbox.repeater.plugin.mybatisplus;

import com.alibaba.jvm.sandbox.api.event.Event.Type;
import com.alibaba.jvm.sandbox.repeater.plugin.api.InvocationProcessor;
import com.alibaba.jvm.sandbox.repeater.plugin.core.impl.AbstractInvokePluginAdapter;
import com.alibaba.jvm.sandbox.repeater.plugin.core.model.EnhanceModel;
import com.alibaba.jvm.sandbox.repeater.plugin.domain.InvokeType;
import com.alibaba.jvm.sandbox.repeater.plugin.spi.InvokePlugin;
import com.google.common.collect.Lists;
import org.kohsuke.MetaInfServices;

import java.util.List;

/**
* @Author: luwenrong
* @Title: mybatisplus 插件
* @Description: {@code com.baomidou.mybatisplus.core.override}包下面的MybatisMapperMethod实现类
* @Date: 2021/10/26
*/
@MetaInfServices(InvokePlugin.class)
public class MybatisPlusPlugin extends AbstractInvokePluginAdapter {

@Override
protected List<EnhanceModel> getEnhanceModels() {
EnhanceModel em = EnhanceModel.builder()
.classPattern("com.baomidou.mybatisplus.core.override.MybatisMapperMethod")
.methodPatterns(EnhanceModel.MethodPattern.transform("execute"))
.watchTypes(Type.BEFORE, Type.RETURN, Type.THROWS)
.build();
return Lists.newArrayList(em);
}

@Override
protected InvocationProcessor getInvocationProcessor() {
return new MybatisPlusProcessor(getType());
}

@Override
public InvokeType getType() {
return InvokeType.MYBATISPLUS;
}

@Override
public String identity() {
return "mybatis-plus";
}

@Override
public boolean isEntrance() {
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.alibaba.jvm.sandbox.repeater.plugin.mybatisplus;

import com.alibaba.jvm.sandbox.api.event.BeforeEvent;
import com.alibaba.jvm.sandbox.repeater.plugin.core.impl.api.DefaultInvocationProcessor;
import com.alibaba.jvm.sandbox.repeater.plugin.domain.Identity;
import com.alibaba.jvm.sandbox.repeater.plugin.domain.InvokeType;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.reflect.MethodUtils;

import java.lang.reflect.Field;
import java.util.HashMap;

/**
* @Author: luwenrong
* @Title: MybatisPlusProcessor
* @Date: 2021/10/26
*/
class MybatisPlusProcessor extends DefaultInvocationProcessor {

MybatisPlusProcessor(InvokeType type) {
super(type);
}

@Override
public Identity assembleIdentity(BeforeEvent event) {
Object mapperMethod = event.target;
Field field = FieldUtils.getDeclaredField(mapperMethod.getClass(), "command", true);
if (field == null) {
return new Identity(InvokeType.MYBATISPLUS.name(), "Unknown", "Unknown", new HashMap<String, String>(1));
}
try {
Object command = field.get(mapperMethod);
Object name = MethodUtils.invokeMethod(command, "getName");
Object type = MethodUtils.invokeMethod(command, "getType");
return new Identity(InvokeType.MYBATISPLUS.name(), type.toString(), name.toString(), new HashMap<String, String>(1));
} catch (Exception e) {
return new Identity(InvokeType.MYBATISPLUS.name(), "Unknown", "Unknown", new HashMap<String, String>(1));
}
}

@Override
public Object[] assembleRequest(BeforeEvent event) {
// MybatisMapperMethod#execute(SqlSession sqlSession, Object[] args)
return new Object[]{event.argumentArray[1]};
}

}
37 changes: 37 additions & 0 deletions repeater-plugins/openfeign-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>repeater-plugins</artifactId>
<groupId>com.alibaba.jvm.sandbox</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>openfeign-plugin</artifactId>

<build>
<finalName>${project.name}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.alibaba.jvm.sandbox.repeater.plugin.openfeign;

import com.alibaba.jvm.sandbox.api.event.Event;
import com.alibaba.jvm.sandbox.repeater.plugin.api.InvocationProcessor;
import com.alibaba.jvm.sandbox.repeater.plugin.core.impl.AbstractInvokePluginAdapter;
import com.alibaba.jvm.sandbox.repeater.plugin.core.model.EnhanceModel;
import com.alibaba.jvm.sandbox.repeater.plugin.domain.InvokeType;
import com.alibaba.jvm.sandbox.repeater.plugin.spi.InvokePlugin;
import com.google.common.collect.Lists;
import org.kohsuke.MetaInfServices;

import java.util.List;

/**
* @Author: luwenrong
* @Title: openfeign 插件
* @Description:
* @Date: 2021/11/30
*/
@MetaInfServices(InvokePlugin.class)
public class OpenFeignPlugin extends AbstractInvokePluginAdapter {

@Override
protected List<EnhanceModel> getEnhanceModels() {
EnhanceModel enhanceModel = EnhanceModel.builder().classPattern("feign.Client$Default")
.methodPatterns(EnhanceModel.MethodPattern.transform("execute"))
.watchTypes(Event.Type.BEFORE, Event.Type.RETURN, Event.Type.THROWS)
.build();
return Lists.newArrayList(enhanceModel);
}

@Override
protected InvocationProcessor getInvocationProcessor() {
return new OpenFeignProcessor(getType());
}

@Override
public InvokeType getType() {
return InvokeType.OPENFEIGN;
}

@Override
public String identity() {
return "openfeign";
}

@Override
public boolean isEntrance() {
return false;
}
}
Loading