package com.ibeetl.code.core.om.asm;
import java.util.List;
import com.ibeetl.code.core.om.AttributeAccess;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
/**
* 通过ASM生成{@link AttributeAccess}的子类,
* 假设类名称User,则为生成的类为:User$AttributeAccess
* 实现 {@link AttributeAccess#value(Object, Object)}方法。
*<p/>
* <h3>Bean中没有get(String)或get(Object)方法:</h3>
* <pre>
public Object value(Object bean, Object attr) {
String attrStr = attr.toString();
int hash = attrStr.hashCode();
User user = (User) bean;
switch (hash) {
case 1:
return user.getName();
case 2:
return user.getAddress();
case 3:
if("numbers".equals(attrStr)){
return user.getNumbers();
}
if("birthDate".equals(attrStr)){
return user.getBirthDate();
}
}
throw new BeetlException(BeetlException.ATTRIBUTE_NOT_FOUND, "attribute : " + attrStr);
}
*</pre>
* <h3>Bean中有get(String)方法:</h3>
* <pre>
public Object value(Object bean, Object attr) {
String attrStr = attr.toString();
int hash = attrStr.hashCode();
User user = (User) bean;
switch (hash) {
case 1:
return user.getName();
case 2:
return user.getAddress();
case 3:
if("numbers".equals(attrStr)){
return user.getNumbers();
}
if("birthDate".equals(attrStr)){
return user.getBirthDate();
}
}
return user.get(attrStr);
}
*</pre>
*</pre>
* <h3>Bean中有get(Object)方法:</h3>
* <pre>
public Object value(Object bean, Object attr) {
String attrStr = attr.toString();
int hash = attrStr.hashCode();
User user = (User) bean;
switch (hash) {
case 1:
return user.getName();
case 2:
return user.getAddress();
case 3:
if("numbers".equals(attrStr)){
return user.getNumbers();
}
if("birthDate".equals(attrStr)){
return user.getBirthDate();
}
}
return user.get(attr);
}
*</pre>
* @author laozhaishaozuo@foxmail.com
*
*/
class EnhanceClassGenerator implements Opcodes {
/**
* 实例方法this变量位置
*/
private static final int VAR_THIS_INDEX = 0;
/**
* 方法中第一个参数的位置
*/
private static final int VAR_BEAN_INDEX = 1;
/**
* 方法中第二个参数的位置
*/
private static final int VAR_ATTR_INDEX = 2;
/**
* 本地变量对应attrName的toString变量
*/
private static final int LOCAL_VAR_ATTR_STRING_INDEX = 3;
/**
* 本地变量hashCode位置
*/
private static final int LOCAL_VAR_HASH_CODE_INDEX = 4;
/**
* 本地变量innerClass类型变量的位置
*/
private static final int LOCAL_VAR_INTERNAL_CLASS_INDEX = 5;
private EnhanceClassGenerator() {
}
/**
* 生成beanClass对应的增强类的字节流
*
* @param beanClass
* @return
* @throws Exception
*/
static byte[] generate(Class<?> beanClass) throws Exception {
return generate(beanClass, true);
}
/**
* 生成beanClass对应的增强类的字节流
*
* @param beanClass
* @param usePropertyDescriptor
* 是否使{@link java.beans.PropertyDescriptor}来生成属性描述
* @return
* @throws Exception
*/
static byte[] generate(Class<?> beanClass, boolean usePropertyDescriptor) throws Exception {
return generate(beanClass, BeanEnhanceConstants.SUPER_CLASS_NAME, null, usePropertyDescriptor);
}
/**
* 生成beanClass对应的增强类的字节流
*
* @param beanClass
* @param superName
* 父类 形式如 java.lang.String
* @param interfaces
* 要实现的接口 形式如 java.lang.String
* @return
* @throws Exception
*/
static byte[] generate(Class<?> beanClass, String superName, String[] interfaces, boolean usePropertyDescriptor)
throws Exception {
String beanClassName = beanClass.getName();
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);// 自动计算maxStack
String getterClassName = createGeneratedClassName(beanClassName);
cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, BeanEnhanceUtils.getInternalName(getterClassName), null, superName,
interfaces);
// 生成默认构造函数
generateDefaultConstruct(cw, superName);
// 生成GetterClass
generateMethod(cw, beanClass, usePropertyDescriptor);
cw.visitEnd();
return cw.toByteArray();
}
/**
* 生成默认的构造方法
*
* @param cw
* @param superName 父类名称
*/
static void generateDefaultConstruct(ClassWriter cw, String superName) {
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
// 生成构造方法的字节码指令
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, superName, "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
static String createGeneratedClassName(String className) {
return className + "$" + BeanEnhanceConstants.GENETRATED_CLASS_SUFFIX;
}
static String createGeneratedClassName(Class<?> beanClass) {
return createGeneratedClassName(beanClass.getName());
}
/**
* 生成方法 方法
*
* @param cw
* @param beanClass
* @param usePropertyDescriptor
* @See {@link AttributeAccess#value(Object, Object)}
*/
private static void generateMethod(ClassWriter cw, Class<?> beanClass, boolean usePropertyDescriptor) {
String internalClassName = BeanEnhanceUtils.getInternalName(beanClass.getName());
ClassDescription classDescription = BeanEnhanceUtils.getClassDescription(beanClass, usePropertyDescriptor);
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, BeanEnhanceConstants.METHOD_TO_GENERATE,
BeanEnhanceConstants.METHOD_SIGNATURE, null, null);
mv.visitCode();
// 有属性,需要调用getter方法
if (classDescription.hasField) {
generateMethodWithFileds(internalClassName, classDescription, mv);
} else {
generateMethodWithNoFiled(mv, classDescription, internalClassName);
}
mv.visitEnd();
}
private static void generateMethodWithFileds(String internalClassName, ClassDescription classDescription,
MethodVisitor mv) {
Label toStringLabel = new Label();
mv.visitLabel(toStringLabel);
mv.visitVarInsn(ALOAD, VAR_ATTR_INDEX);
mv.visitMethodInsn(INVOKEVIRTUAL, BeanEnhanceConstants.OBJECT_INTERNAL_NAME,
BeanEnhanceConstants.TO_STRING_METHOD_NAME, BeanEnhanceConstants.TO_STRING_METHOD_DESCRIPTOR, false);
mv.visitVarInsn(ASTORE, LOCAL_VAR_ATTR_STRING_INDEX);// 对应attrName的toString变量
Label hashCodeLabel = new Label();
mv.visitLabel(hashCodeLabel);
mv.visitVarInsn(ALOAD, LOCAL_VAR_ATTR_STRING_INDEX);// toString变量
mv.visitMethodInsn(INVOKEVIRTUAL, BeanEnhanceConstants.OBJECT_INTERNAL_NAME, "hashCode", "()I", false);
mv.visitVarInsn(ISTORE, LOCAL_VAR_HASH_CODE_INDEX);// hashCode
Label castLabel = new Label();
mv.visitLabel(castLabel);
mv.visitVarInsn(ALOAD, VAR_BEAN_INDEX);// 参数 bean
mv.visitTypeInsn(CHECKCAST, internalClassName);
mv.visitVarInsn(ASTORE, LOCAL_VAR_INTERNAL_CLASS_INDEX);// 对应internalClassName类型的变量
Label l2 = new Label();
mv.visitLabel(l2);
mv.visitVarInsn(ILOAD, LOCAL_VAR_HASH_CODE_INDEX);
Label[] lookupSwitchLabels = new Label[classDescription.fieldDescMap.size()];
int[] hashCodes = BeanEnhanceUtils
.convertIntegerToPrimitiveType(classDescription.fieldDescMap.keySet().toArray(new Integer[1]));
for (int i = 0; i < lookupSwitchLabels.length; i++) {
lookupSwitchLabels[i] = new Label();
}
Label df = new Label();
mv.visitLookupSwitchInsn(df, hashCodes, lookupSwitchLabels);
List<FieldDescription> fieldDescs = null;
FieldDescription curFieldDesc = null;
for (int i = 0; i
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
《高性能Java系统权威指南》新书于2022年12月出版,内附丰富示例代码。项目包含381个文件,涵盖214个Java源码、64个XML配置文件、54个
资源推荐
资源详情
资源评论
收起资源包目录
基于Java的高性能系统设计源码解析与优化技巧:新书《高性能Java系统权威指南》2022年12月出版 (257个子文件)
mapper.btl 719B
.DS_Store 8KB
.gitignore 19B
hello.html 83B
error2.html 56B
error.html 55B
while.html 49B
EnhanceClassGenerator.java 17KB
CaffeineApplicationTests.java 13KB
MultiBlockingQueue.java 9KB
FutureTest.java 9KB
BeanEnhanceUtils.java 7KB
JsonWriterJavapoetGenerator.java 4KB
BaseJmhQosThreadPool.java 4KB
JmhThreadSinglePool.java 4KB
JacksonSample.java 4KB
HighAlwaysPolicy.java 4KB
JsonWriterBeetlGenerator.java 4KB
GetterGenerator.java 3KB
ASMBeanFactory.java 3KB
TimelineStatistics.java 3KB
SimpleQosPolicy.java 3KB
NetworkTest.java 3KB
BeanValueBenchmark.java 3KB
AbstractJsonWriterGenerator.java 3KB
FutureTaskTest3.java 3KB
PrintElementInfoProcessor.java 3KB
BeetlException.java 3KB
PackTest.java 3KB
SplitJmhTest.java 3KB
Test1.java 3KB
SwitchTest2.java 3KB
CloneUtil.java 3KB
Main.java 2KB
ReplaceTest.java 2KB
MyMethodTransformer.java 2KB
MessageFormatTest.java 2KB
DateFormatTest.java 2KB
BeetlSample.java 2KB
BeetlHelper.java 2KB
ZipUtil.java 2KB
GetterProcessor.java 2KB
PerformaceAreaTest2.java 2KB
Product.java 2KB
ByteClassLoader.java 2KB
PreHandleTest.java 2KB
User.java 2KB
WatchTest.java 2KB
CloneUtilTest.java 2KB
JMHSample_11_Loops.java 2KB
HeavyweightObjectTest.java 2KB
EnumMapTest.java 2KB
MyArrayBlockingQueue.java 2KB
PerformanceAreaTest.java 2KB
PreEncodeTest.java 2KB
ReflectTest.java 2KB
StringConcatTest.java 2KB
StringSplitTest.java 2KB
TryCatchTest.java 2KB
CharArrayTest.java 2KB
JmhPolicyQueue.java 2KB
CachedJavaRelectTool.java 2KB
AttributeAccess.java 2KB
ForDeadCodeTest.java 2KB
MyBenchmark.java 2KB
StringFormatTest.java 2KB
ZipTest.java 2KB
Statistics.java 2KB
ForIntTest.java 2KB
MyWhileStatement.java 2KB
UUIDTest.java 2KB
Template.java 2KB
VirtualCallTest.java 2KB
StringReplaceTest.java 2KB
SwitchTest.java 2KB
PerformanceKeyTest.java 2KB
BeanEnhanceConstants.java 2KB
KeywordsFilterTest.java 2KB
JMHSample_10_ConstantFold.java 2KB
MethodHandleTool.java 2KB
PackSizeTest.java 1KB
ForRemove.java 1KB
QosQueue.java 1KB
SimpleDateFormatTest.java 1KB
GeValueByBeans.java 1KB
KeywordSearch.java 1KB
SimpleQos.java 1KB
BeetlGTest.java 1KB
FastSimpleQosPolicy.java 1KB
JMHSample_08_DeadCode.java 1KB
BitTest.java 1KB
SwitchStringTest.java 1KB
Int2StringTest.java 1KB
TryCatch2Test.java 1KB
JmhLargeNumQueue.java 1KB
StringBufferTest.java 1KB
NewStringTest.java 1KB
JsonWriterProcessor.java 1KB
SimpleStatistics.java 1KB
ClientTracer.java 1KB
共 257 条
- 1
- 2
- 3
资源评论
csbysj2020
- 粉丝: 2645
- 资源: 5504
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功