Skip to content

Commit 5b049bb

Browse files
committed
一些工具类的测试
1 parent 9b8b2ac commit 5b049bb

22 files changed

Lines changed: 284 additions & 439 deletions
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package 工具;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
6+
public class RunShell {
7+
public static void main(String[] args){
8+
try {
9+
String shpath="/Users/fuzhipeng/Library/Android/sdk/tools/proguard/bin/proguardgui.sh";
10+
Runtime.getRuntime().exec(shpath).waitFor();
11+
// BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
12+
// StringBuffer sb = new StringBuffer();
13+
// String line;
14+
// while ((line = br.readLine()) != null) {
15+
// sb.append(line).append("\n");
16+
// }
17+
// String result = sb.toString();
18+
// System.out.println(result);
19+
}
20+
catch (Exception e) {
21+
e.printStackTrace();
22+
}
23+
}
24+
}

‎JavaTest_Zone/src/注解学习/Child.java‎

Lines changed: 0 additions & 88 deletions
This file was deleted.

‎JavaTest_Zone/src/注解学习/EntityA.java‎

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎JavaTest_Zone/src/注解学习/MainA.java‎

Lines changed: 0 additions & 31 deletions
This file was deleted.

‎JavaTest_Zone/src/注解学习/Parent.java‎

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package 注解学习.all;
2+
3+
import 注解学习.all.annotation.*;
4+
5+
import java.lang.annotation.Annotation;
6+
import java.lang.annotation.ElementType;
7+
import java.lang.annotation.Target;
8+
import java.lang.reflect.Constructor;
9+
import java.lang.reflect.Field;
10+
import java.lang.reflect.Method;
11+
12+
/**
13+
* Created by fuzhipeng on 2016/10/28.
14+
*/
15+
public class TestMain {
16+
17+
public static void main(String[] args) {
18+
try {
19+
annotationTypeAnnotation();
20+
classAnnotation();
21+
constructorAnnotation();
22+
fieldAnnotation();
23+
methodAnnotation();
24+
parameterAnnotation();
25+
26+
//这个特殊 需要创建 package-info.java
27+
// 然后在内里添加个包 然后在包上添加注解
28+
packageAnnotation();
29+
//暂时没什么用
30+
// 但是目前的javac不会在bytecode中的local variable中保存annotation信息,
31+
// 所以就无法在runtime时获取该annotaion。
32+
// 也就是说ElementType.LOCAL_VARIABLE只能用在RetentionPolicy.SOURCE情况下。
33+
localVariableAnnnotaiton();
34+
35+
} catch (Exception e) {
36+
e.printStackTrace();
37+
}
38+
}
39+
40+
private static void packageAnnotation() {
41+
Package p = Package.getPackage("注解学习.all.annotation");
42+
//判断构造方法中是否有指定注解类型的注解
43+
//   boolean hasAnnotation = constructor.isAnnotationPresent(TestA.class);
44+
if(p!=null && p.isAnnotationPresent(PackageBinds.class)){
45+
PackageBinds nav = p.getAnnotation(PackageBinds.class);
46+
if(nav !=null){
47+
System.out.println("name:"+"======================="+nav.value());
48+
}
49+
}
50+
}
51+
52+
private static void localVariableAnnnotaiton() {
53+
54+
}
55+
56+
private static void annotationTypeAnnotation() {
57+
// @Target这个就是 此类型
58+
ClassBinds classBinds = Test.class.getAnnotation(ClassBinds.class);
59+
if (classBinds != null) {
60+
Annotation[] annotations = classBinds.annotationType().getAnnotations();
61+
Target target = classBinds.annotationType().getAnnotation(Target.class);
62+
if (target != null)
63+
System.out.print("ANNOTATION_TYPE---->targets:" );
64+
for (ElementType elementType : target.value()) {
65+
System.out.print("\t elementType:"+elementType);
66+
}
67+
System.out.println();
68+
}
69+
70+
71+
}
72+
73+
private static void parameterAnnotation() throws NoSuchMethodException {
74+
Method method = Test.class.getMethod("use", int.class, String.class);
75+
//双维 因为 一个参数可以多个 注解;
76+
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
77+
if (parameterAnnotations == null || parameterAnnotations.length == 0) {
78+
return;
79+
}
80+
for (int i = 0; i < parameterAnnotations.length; i++) {
81+
System.out.printf("第%s个参数注解-->", i);
82+
for (Annotation annotation : parameterAnnotations[i]) {
83+
if (annotation instanceof ParameterBinds)
84+
System.out.println("value:" + ((ParameterBinds) annotation).value());
85+
}
86+
}
87+
}
88+
89+
private static void methodAnnotation() {
90+
Method[] methods = Test.class.getMethods();
91+
for (Method method : methods) {
92+
MethodBinds methodBinds = method.getAnnotation(MethodBinds.class);
93+
if (methodBinds != null)
94+
System.out.println("name:" + methodBinds.value());
95+
}
96+
}
97+
98+
private static void fieldAnnotation() throws NoSuchFieldException {
99+
Field field = Test.class.getField("tag");
100+
FieldBinds fieldBinds = field.getAnnotation(FieldBinds.class);
101+
if (fieldBinds != null)
102+
System.out.println("name:" + fieldBinds.name());
103+
}
104+
105+
//构造器注解 需要得到构造器 ,构造器getAnnotation
106+
private static void constructorAnnotation() throws NoSuchMethodException {
107+
Test test = new Test();
108+
Constructor<? extends Test> cons = test.getClass().getConstructor();
109+
ConstructorBinds constructorBinds = cons.getAnnotation(ConstructorBinds.class);
110+
if (constructorBinds != null)
111+
System.out.println("name:" + constructorBinds.name());
112+
}
113+
114+
//class注解: class.getAnnotation
115+
private static void classAnnotation() {
116+
ClassBinds classBinds = Test.class.getAnnotation(ClassBinds.class);
117+
if (classBinds != null)
118+
System.out.println("name:" + classBinds.value() + "\t name:" + classBinds.name());
119+
}
120+
121+
@ClassBinds(value = {"类注解_Zone", "非主流_Zone"}, name = "kb_Zone")
122+
public static class Test {
123+
@FieldBinds(name = "field注解_Zone")
124+
public String tag = "test";
125+
126+
@ConstructorBinds(name = "构造注解_Zone")
127+
public Test() {
128+
}
129+
130+
@MethodBinds("方法注解_Zone")
131+
public void use(@ParameterBinds("par1_Zone") int par1, @ParameterBinds("par2_Zone") String par2) {
132+
@LocalVariableBinds
133+
String infoInner = "sa";
134+
}
135+
}
136+
}

JavaTest_Zone/src/注解学习/TestA.java renamed to JavaTest_Zone/src/注解学习/all/annotation/AnnotationTypeBinds.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
package 注解学习;
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-
@Target(ElementType.FIELD)
9-
@Retention(RetentionPolicy.RUNTIME)
10-
public @interface TestA {
11-
12-
String orderName();
13-
14-
String newName();
15-
// String column() default "";
16-
}
1+
package 注解学习.all.annotation;
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 fuzhipeng on 2016/10/28.
10+
*/
11+
@Target(ElementType.FIELD)
12+
@Retention(RetentionPolicy.RUNTIME)
13+
public @interface AnnotationTypeBinds {
14+
15+
}
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
package 注解学习;
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-
@Target(ElementType.TYPE)
9-
@Retention(RetentionPolicy.RUNTIME)
10-
public @interface Table {
11-
12-
String name() default "";
13-
}
1+
package 注解学习.all.annotation;
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 fuzhipeng on 2016/10/28.
10+
*/
11+
@Target(ElementType.TYPE)
12+
@Retention(RetentionPolicy.RUNTIME)
13+
public @interface ClassBinds {
14+
String[] value() ;//
15+
String name() default "" ;//
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package 注解学习.all.annotation;
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 fuzhipeng on 2016/10/28.
10+
*/
11+
@Target(ElementType.CONSTRUCTOR)
12+
@Retention(RetentionPolicy.RUNTIME)
13+
public @interface ConstructorBinds {
14+
String name() ;//
15+
}

0 commit comments

Comments
 (0)