package com.senzhikong.db.sql.wrapper;
import java.io.Serializable;
import java.util.function.Function;
@FunctionalInterface
public interface ObjectFunction<T extends Serializable, R> extends Function<T, R>, Serializable {
}
package com.senzhikong.db.entity;
import com.alibaba.fastjson.JSON;
import com.senzhikong.db.dialect.Comment;
import com.senzhikong.db.enums.CommonStatus;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.domain.Example;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
@Getter
@Setter
@MappedSuperclass
public class BaseEntity implements Serializable {
/**
* @Fields serialVersionUID
*/
protected static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "status", length = 32)
private String status;
@Column(name = "updateTime", length = 26)
private Date updateTime;
@Column(name = "remark", length = 255)
private String remark;
}
package com.senzhikong.tally.entity;
import com.senzhikong.db.entity.BaseEntity;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Getter
@Setter
@Entity
@Table(name = "tally_ledger")
public class TallyLedger extends BaseEntity{
@Column(name = "title")
private String title;
@Column(name = "description")
private String description;
@Column(name = "memberId")
private Long memberId;
}
package com.senzhikong.db.sql;
import java.lang.reflect.AccessibleObject;
import java.security.PrivilegedAction;
public class SetAccessibleAction<T extends AccessibleObject> implements PrivilegedAction<T> {
private final T obj;
public SetAccessibleAction(T obj) {
this.obj = obj;
}
public T run() {
this.obj.setAccessible(true);
return this.obj;
}
}
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.AccessController;
import com.senzhikong.db.sql.SetAccessibleAction;
public class WrapperParser{
public static <R extends Serializable, S extends Serializable> void parseFunction(ObjectFunction<R, S> column) {
try {
Method method = column.getClass().getDeclaredMethod("readResolve");
AccessController.doPrivileged(
new SetAccessibleAction<>(method));
SerializedLambda serializedLambda = (SerializedLambda) method.invoke(column);
String methodName = serializedLambda.getImplMethodName();
String className = serializedLambda.getImplClass();
System.out.println(className);
//输出结果:::com/senzhikong/db/entity/BaseEntity
//getId 方法是由父类继承的,这里拿到的是父类的类名,我想要获取实际使用的子类的类名应该怎么做
//想要获取:::com/senzhikong/tally/entity/TallyLedger
} catch (Exception e) {
throw new RuntimeException("", e);
}
}
public static void main(String[] args) {
parseFunction(TallyLedger::getId );
}
}