瀏覽代碼

어노테이션 추가

tobby48 6 年之前
父節點
當前提交
18ebd6f096

+ 25
- 0
src/kr/co/swh/lecture/java/scene4/PlusAnnotation.java 查看文件

@@ -0,0 +1,25 @@
1
+package kr.co.swh.lecture.java.scene4;
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
+ * <pre>
10
+ * kr.co.swh.lecture.java.scene4 
11
+ * PlusAnnotation.java
12
+ *
13
+ * 설명 :	어노테이션 만들기
14
+ * </pre>
15
+ * 
16
+ * @since : 2019. 3. 13.
17
+ * @author : tobby48
18
+ * @version : v1.0
19
+ */
20
+@Target({ElementType.METHOD})
21
+@Retention(RetentionPolicy.RUNTIME)
22
+public @interface PlusAnnotation {
23
+	String value() default "integer";
24
+	String plus() default "산술연산";
25
+}

+ 39
- 0
src/kr/co/swh/lecture/java/scene4/PlusAnnotationMain.java 查看文件

@@ -0,0 +1,39 @@
1
+package kr.co.swh.lecture.java.scene4;
2
+
3
+import java.lang.reflect.InvocationTargetException;
4
+import java.lang.reflect.Method;
5
+
6
+/**
7
+ * <pre>
8
+ * kr.co.swh.lecture.java.scene4 
9
+ * PlusAnnotationMain.java
10
+ *
11
+ * 설명 :	어노테이션 메인
12
+ * </pre>
13
+ * 
14
+ * @since : 2019. 3. 13.
15
+ * @author : tobby48
16
+ * @version : v1.0
17
+ */
18
+public class PlusAnnotationMain {
19
+	public static void main(String[] args) {
20
+		//	PlusService 클래스에 있는 모든 메소드 얻기
21
+        Method[] methos = PlusService.class.getDeclaredMethods();
22
+        for (Method method : methos) {
23
+            //	메소드에 어노테이션이 적용되어 있다면
24
+            if(method.isAnnotationPresent(PlusAnnotation.class)) {
25
+                //	PlusAnnotation 객체를 얻은 후, 
26
+            	PlusAnnotation greetingAnnotation = method.getAnnotation(PlusAnnotation.class);
27
+                System.out.print(greetingAnnotation.plus());
28
+                try {
29
+                    //	PlusService의 메소드를 호출
30
+                    method.invoke(new PlusService());
31
+                } catch (IllegalAccessException e) {
32
+                    e.printStackTrace();
33
+                } catch (InvocationTargetException e) {
34
+                    e.printStackTrace();
35
+                }
36
+            }
37
+        }
38
+    }
39
+}

+ 25
- 0
src/kr/co/swh/lecture/java/scene4/PlusService.java 查看文件

@@ -0,0 +1,25 @@
1
+package kr.co.swh.lecture.java.scene4; 
2
+
3
+/**
4
+ * <pre>
5
+ * kr.co.swh.lecture.java.scene4 
6
+ * PlusService.java
7
+ *
8
+ * 설명 :	어노테이션 적용 클래스
9
+ * </pre>
10
+ * 
11
+ * @since : 2019. 3. 13.
12
+ * @author : tobby48
13
+ * @version : v1.0
14
+ */
15
+public class PlusService {
16
+	@PlusAnnotation()
17
+    public void numericOperator() {
18
+        System.out.println("numericOperator()");
19
+    }
20
+
21
+    @PlusAnnotation(value = "string", plus = "문자열 결합")
22
+    public void stringOperator() {
23
+        System.out.println("stringOperator");
24
+    }
25
+}