Skip to content

Commit 9be72af

Browse files
author
yongjie.zhang
committed
javassist操作注解
1 parent c3005fd commit 9be72af

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.zyj.javassist;
2+
3+
public @interface Author {
4+
String name();
5+
int year();
6+
7+
}

java-Javassist/src/com/zyj/javassist/Demo02.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,33 @@ public static void test04()throws Exception{
116116
Object re = getter.invoke(obj);
117117
System.out.println("salary:::"+re);
118118
}
119+
120+
/**
121+
* 操作注解
122+
* @throws Exception
123+
*/
124+
public static void test06() throws Exception{
125+
CtClass cc = ClassPool.getDefault().get("com.zyj.javassist.Emp");
126+
Object annotation = cc.getAnnotation(Author.class);
127+
Author author = (Author) annotation;
128+
System.out.println("name:"+author.name()+",year:"+author.year());
129+
}
130+
131+
/**
132+
* g构造器
133+
* @throws Exception
134+
*/
135+
public static void test05()throws Exception{
136+
ClassPool pool = ClassPool.getDefault();
137+
CtClass cc = pool.get("com.zyj.javassist.Emp");
138+
CtConstructor[] cs = cc.getConstructors();
139+
for(CtConstructor c:cs){
140+
System.out.println(c.getLongName());
141+
//c.insertBeforeBody();
142+
}
143+
144+
}
119145
public static void main(String[] args) throws Exception {
120-
test04();
146+
test06();
121147
}
122148
}

java-Javassist/src/com/zyj/javassist/Emp.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @Version : v1.0
99
* @description
1010
**/
11+
@Author(name="zyj",year = 2018)
1112
public class Emp {
1213
private String empno;
1314
private String ename;

0 commit comments

Comments
 (0)