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

完善kotlin支持,支持传递函数引用变量 #29

Closed
wants to merge 1 commit into from
Closed
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 @@ -3,11 +3,15 @@

import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.toolkit.support.*;
import kotlin.jvm.internal.CallableReference;
import kotlin.jvm.internal.ClassReference;
import kotlin.jvm.internal.FunctionReference;
import org.apache.ibatis.reflection.property.PropertyNamer;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Ref;
import java.util.Map;

/**
Expand Down Expand Up @@ -49,11 +53,30 @@ public static <T> LambdaMeta extract(SFunction<T, ?> func) {
return new IdeaProxyLambdaMeta((Proxy) func);
}
// 2. 反射读取
// 3. 处理Kotlin lambda var like val fieldGetter=UserDTO::getAddresses
{
Class<?> funcClass=func.getClass();
Field wrappedField= ReflectionKit.getFieldOptional(func.getClass(),"arg$1")
.orElse(null);
if(wrappedField!=null) {
try {
Object wrapperObj = ReflectionKit.setAccessible(wrappedField).get(func);
CallableReference functionReference = (CallableReference) wrapperObj;
ClassReference classReference = (ClassReference) functionReference.getOwner();
String funcName = functionReference.getName();
Class<?> ownerClass = classReference.getJClass();
return new KotlinLambdaMeta(funcName, ownerClass);
} catch (Throwable e) { //unreachable
throw new RuntimeException(e);
}
}
}
//4. 处理Java method ref的情况
try {
Method method = func.getClass().getDeclaredMethod("writeReplace");
return new ReflectLambdaMeta((java.lang.invoke.SerializedLambda) ReflectionKit.setAccessible(method).invoke(func));
} catch (Throwable e) {
// 3. 反射失败使用序列化的方式读取
// 5. 反射失败使用序列化的方式读取
return new ShadowLambdaMeta(SerializedLambda.extract(func));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Modifier;
import java.security.AccessController;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -197,4 +199,13 @@ public static <T extends AccessibleObject> T setAccessible(T object) {
return AccessController.doPrivileged(new SetAccessibleAction<>(object));
}

public static Optional<Field> getFieldOptional(Class<?> cls,String name){
return Arrays.stream(cls.getDeclaredFields()).filter(new Predicate<Field>() {
@Override
public boolean test(Field field) {
return field.getName().equals(name);
}
}).findFirst();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.yulichang.toolkit.support;

public class KotlinLambdaMeta implements LambdaMeta{
private final String implMethodName;
private final Class<?> instantiatedClass;

public KotlinLambdaMeta(String implMethodName,Class<?> instantiatedClass){
this.implMethodName=implMethodName;
this.instantiatedClass=instantiatedClass;
}

@Override
public String getImplMethodName() {
return implMethodName;
}

@Override
public Class<?> getInstantiatedClass() {
return instantiatedClass;
}
}
1 change: 1 addition & 0 deletions mybatis-plus-join-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<module>test-join</module>
<module>test-collection</module>
<module>test-mapping</module>
<module>test-kotlin</module>
</modules>

<description>An enhanced toolkit of Mybatis-Plus to simplify development.</description>
Expand Down
110 changes: 110 additions & 0 deletions mybatis-plus-join-test/test-kotlin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join-test</artifactId>
<version>1.4.2.2</version>
</parent>

<artifactId>test-kotlin</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.8.10</kotlin.version>
</properties>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-lombok</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
<configuration>
<compilerPlugins>
<plugin>lombok</plugin>
</compilerPlugins>
<jvmTarget>${maven.compiler.target}</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>

</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.yulichang.test.kotlin;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.github.yulichang.test.kotlin.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.yulichang.test.kotlin.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* mybatis-plus配置
*/
@Configuration
public class MybatisPlusConfig {

/**
* 分页插件
*/
@Bean
public MybatisPlusInterceptor paginationInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor page = new PaginationInnerInterceptor();
interceptor.addInnerInterceptor(page);
return interceptor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.yulichang.test.kotlin.dto;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;

public class AddressDTO {
private Integer id;

private String userID;

private String tel;

private String address;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.yulichang.test.kotlin.dto;

import lombok.Data;

import java.util.List;

@Data
public class UserDTO {
private Integer id;

private String userID;

private String name;

private List<AddressDTO> addresses;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.yulichang.test.kotlin.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;

@Data
@ToString
@Accessors(chain = true)
@EqualsAndHashCode
@TableName("address")
public class AddressDO {

@TableId
private Integer id;

@TableField("user_id")
private String userID;

private String tel;

private String address;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.yulichang.test.kotlin.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.github.yulichang.annotation.EntityMapping;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;

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

@Data
@ToString
@Accessors(chain = true)
@EqualsAndHashCode
@TableName(value = "`user`", autoResultMap = true)
public class UserDO {
@TableId
private Integer id;

@TableField("user_id")
private String userID;

private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.yulichang.test.kotlin.mapper;

import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.base.MPJBaseMapper;
import com.github.yulichang.test.kotlin.dto.AddressDTO;
import com.github.yulichang.test.kotlin.entity.AddressDO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;

@Mapper
@SuppressWarnings("unused")
public interface AddressMapper extends MPJBaseMapper<AddressDO> {
public static <T,S> MPJLambdaWrapper<T> appendAddressesSubPredicate(MPJLambdaWrapper<T> pred,
SFunction<S, Collection<AddressDTO>> dtoAddressesField,
SFunction<T,?> entityUserIDField){
return pred.selectCollection(AddressDO.class, dtoAddressesField)
.rightJoin(AddressDO.class, AddressDO::getUserID, entityUserIDField);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.yulichang.test.kotlin.mapper

import com.baomidou.mybatisplus.core.toolkit.support.SFunction
import com.github.yulichang.test.kotlin.dto.AddressDTO
import com.github.yulichang.test.kotlin.dto.UserDTO
import com.github.yulichang.test.kotlin.entity.AddressDO
import com.github.yulichang.test.kotlin.entity.UserDO
import com.github.yulichang.wrapper.MPJLambdaWrapper

fun <T,S> MPJLambdaWrapper<T>.addressesSubPredicate(dtoAddressesField:SFunction<S,Collection<AddressDTO>>,
entityUserIDField:SFunction<T,Any>): MPJLambdaWrapper<T> {
return this.selectCollection<S, AddressDO,Any,Collection<AddressDTO>>(AddressDO::class.java, dtoAddressesField)
.rightJoin(AddressDO::class.java, AddressDO::getUserID, entityUserIDField)
}

fun <T,S> MPJLambdaWrapper<T>.addressesSubPredicateFromJavaMapper(dtoAddressesField:SFunction<S,Collection<AddressDTO>>,
entityUserIDField:SFunction<T,Any>): MPJLambdaWrapper<T> {
return AddressMapper.appendAddressesSubPredicate(this,dtoAddressesField,entityUserIDField)
}
Loading