Skip to content

Commit cac662a

Browse files
committed
添加运行时注解处理器的例子
1 parent 9713fe8 commit cac662a

6 files changed

Lines changed: 144 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cn.byhieg.annotationstutorial;
2+
3+
import javax.jws.soap.SOAPBinding;
4+
import java.lang.reflect.Constructor;
5+
import java.lang.reflect.InvocationTargetException;
6+
7+
/**
8+
* Created by byhieg on 17/2/14.
9+
* Mail to byhieg@gmail.com
10+
*/
11+
public class AConstructorProcess {
12+
13+
public static void init(Object object) throws IllegalAccessException, InvocationTargetException, InstantiationException {
14+
if (object instanceof User) {
15+
Class clz = object.getClass();
16+
Constructor [] constructors = clz.getConstructors();
17+
for (Constructor constructor : constructors) {
18+
if (constructor.isAnnotationPresent(AConstructor.class)) {
19+
AConstructor aConstructor = (AConstructor) constructor.getAnnotation(AConstructor.class);
20+
String name = aConstructor.initName();
21+
int age = aConstructor.initAge();
22+
((User) object).name = name;
23+
((User) object).age = age;
24+
}
25+
}
26+
}else{
27+
throw new RuntimeException("无法向下转型到指定类");
28+
}
29+
30+
}
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.byhieg.annotationstutorial;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Created by byhieg on 17/2/14.
10+
* Mail to byhieg@gmail.com
11+
*/
12+
@Target(ElementType.METHOD)
13+
@Retention(RetentionPolicy.RUNTIME)
14+
public @interface AMethod {
15+
public String method();
16+
17+
public String value();
18+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.byhieg.annotationstutorial;
2+
3+
import sun.jvm.hotspot.runtime.amd64.AMD64CurrentFrameGuess;
4+
5+
import java.lang.reflect.InvocationTargetException;
6+
import java.lang.reflect.Method;
7+
import java.lang.reflect.Modifier;
8+
9+
/**
10+
* Created by byhieg on 17/2/14.
11+
* Mail to byhieg@gmail.com
12+
*/
13+
public class AMethodProcess {
14+
15+
public static void initMethod(Object object) throws InvocationTargetException, IllegalAccessException {
16+
if (object instanceof User) {
17+
Class clz = object.getClass();
18+
Method [] methods = clz.getDeclaredMethods();
19+
for (Method method : methods) {
20+
if (method.isAnnotationPresent(AMethod.class)) {
21+
if(Modifier.isPrivate(method.getModifiers())){
22+
method.setAccessible(true);
23+
}else {
24+
AMethod aMethod = method.getAnnotation(AMethod.class);
25+
System.out.println(aMethod.method() + "时间为 " + aMethod.value());
26+
method.invoke(object);
27+
}
28+
}
29+
}
30+
}else {
31+
throw new RuntimeException("无法向下转型成指定类");
32+
}
33+
}
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.byhieg.annotationstutorial;
2+
3+
/**
4+
* Created by byhieg on 17/2/14.
5+
* Mail to byhieg@gmail.com
6+
*/
7+
public class User {
8+
9+
public String name;
10+
public int age;
11+
12+
@AConstructor
13+
public User(){
14+
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return "名字是 " + name + "年龄是 " + age;
20+
}
21+
22+
@AMethod(method = "Sleep",value = "60m")
23+
public void doSomeThing(){
24+
System.out.println("做一些事情");
25+
}
26+
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.byhieg.annotationstutorialtest;
2+
3+
import cn.byhieg.annotationstutorial.AConstructorProcess;
4+
import cn.byhieg.annotationstutorial.User;
5+
import junit.framework.TestCase;
6+
7+
/**
8+
* Created by byhieg on 17/2/14.
9+
* Mail to byhieg@gmail.com
10+
*/
11+
public class AConstructorProcessTest extends TestCase {
12+
public void testInit() throws Exception {
13+
User user = new User();
14+
AConstructorProcess.init(user);
15+
System.out.println(user.toString());
16+
}
17+
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.byhieg.annotationstutorialtest;
2+
3+
import cn.byhieg.annotationstutorial.AMethodProcess;
4+
import cn.byhieg.annotationstutorial.User;
5+
import junit.framework.TestCase;
6+
7+
/**
8+
* Created by byhieg on 17/2/14.
9+
* Mail to byhieg@gmail.com
10+
*/
11+
public class AMethodProcessTest extends TestCase {
12+
public void testInitMethod() throws Exception {
13+
AMethodProcess.initMethod(new User());
14+
}
15+
16+
}

0 commit comments

Comments
 (0)