code manipulation: reflection(Class API)
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
package org.example;
|
package org.example.annotation;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class AnnotationApp {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.example;
|
package org.example.annotation;
|
||||||
|
|
||||||
|
|
||||||
@MyAnnotation(name = "lee", number = 20)
|
@MyAnnotation(name = "lee", number = 20)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.example;
|
package org.example.annotation;
|
||||||
|
|
||||||
import java.lang.annotation.*;
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.example;
|
package org.example.annotation;
|
||||||
|
|
||||||
public class MyBook extends Book implements MyInterface{
|
public class MyBook extends Book implements MyInterface{
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.example;
|
package org.example.annotation;
|
||||||
|
|
||||||
public interface MyInterface {
|
public interface MyInterface {
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,9 @@
|
|||||||
package org.example;
|
package org.example.classapi;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class ClassApiApp {
|
public class App {
|
||||||
public static void main( String[] args ) throws ClassNotFoundException {
|
public static void main( String[] args ) throws ClassNotFoundException {
|
||||||
|
|
||||||
|
|
||||||
@@ -100,7 +99,7 @@ public class ClassApiApp {
|
|||||||
// System.out.println("============================");
|
// System.out.println("============================");
|
||||||
//
|
//
|
||||||
// // 문자열로 접근
|
// // 문자열로 접근
|
||||||
// Class<?> bookClass2 = Class.forName("org.example.Book");
|
// Class<?> bookClass2 = Class.forName("org.example.annotation.Book");
|
||||||
// System.out.println(bookClass2.getName());
|
// System.out.println(bookClass2.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package org.example.classapi;
|
||||||
|
|
||||||
|
public class Book {
|
||||||
|
|
||||||
|
private static String B = "BOOK";
|
||||||
|
|
||||||
|
private static final String C = "BOOK";
|
||||||
|
|
||||||
|
private String a = "a";
|
||||||
|
|
||||||
|
public String d = "d";
|
||||||
|
|
||||||
|
protected String e = "e";
|
||||||
|
|
||||||
|
public Book() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book(String a, String d, String e) {
|
||||||
|
this.a = a;
|
||||||
|
this.d = d;
|
||||||
|
this.e = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void f() {
|
||||||
|
System.out.println("f");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void g() {
|
||||||
|
System.out.println("g");
|
||||||
|
}
|
||||||
|
|
||||||
|
public int h() {
|
||||||
|
return 100;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package org.example.classapi2;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
public class App {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
/**
|
||||||
|
* Class API : 클래스 정보 수정, 실행 하기
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
// 메소드 실행하기
|
||||||
|
Class<?> bookClass = Class.forName("org.example.classapi2.Book");
|
||||||
|
Book book = (Book) bookClass.getConstructor().newInstance();
|
||||||
|
|
||||||
|
Method c = Book.class.getDeclaredMethod("c");
|
||||||
|
c.setAccessible(true);
|
||||||
|
c.invoke(book);
|
||||||
|
|
||||||
|
Method sum = Book.class.getDeclaredMethod("sum", int.class, int.class);
|
||||||
|
int invoke = (int) sum.invoke(book, 1, 2);
|
||||||
|
System.out.println(invoke);
|
||||||
|
|
||||||
|
|
||||||
|
// // 필드 값 접근/수정하기
|
||||||
|
// Field a = Book.class.getDeclaredField("A");
|
||||||
|
// System.out.println(a.get(null)); // static 필드 이므로 null
|
||||||
|
// a.set(null, "AAAAAA");
|
||||||
|
// System.out.println(a.get(null));
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Class<?> bookClass = Class.forName("org.example.classapi2.Book");
|
||||||
|
// Book book = (Book) bookClass.getConstructor().newInstance();
|
||||||
|
//
|
||||||
|
// Field b = Book.class.getDeclaredField("B");
|
||||||
|
// b.setAccessible(true); // private 필드 이므로 접근 할 수 있도록 변경
|
||||||
|
// System.out.println(b.get(book)); // 특정 인스턴스가 필요
|
||||||
|
// b.set(book, "BBBBBB");
|
||||||
|
// System.out.println(b.get(book));
|
||||||
|
|
||||||
|
|
||||||
|
// // 생성자로 인스턴스 만들기
|
||||||
|
// Class<?> bookClass = Class.forName("org.example.classapi2.Book");
|
||||||
|
// Constructor<?> constructor = bookClass.getConstructor();
|
||||||
|
// Constructor<?> constructor1 = bookClass.getConstructor(String.class);
|
||||||
|
// Book book = (Book) constructor.newInstance();
|
||||||
|
// Book book1 = (Book) constructor1.newInstance("myBook");
|
||||||
|
//
|
||||||
|
// System.out.println(book);
|
||||||
|
// System.out.println(book1);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package org.example.classapi2;
|
||||||
|
|
||||||
|
public class Book {
|
||||||
|
|
||||||
|
public static String A = "A";
|
||||||
|
|
||||||
|
private String B = "B";
|
||||||
|
|
||||||
|
public Book() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book(String b) {
|
||||||
|
B = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void c() {
|
||||||
|
System.out.println("C");
|
||||||
|
}
|
||||||
|
|
||||||
|
public int sum(int left, int right) {
|
||||||
|
return left + right;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
package org.example;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unit test for simple App.
|
|
||||||
*/
|
|
||||||
public class ClassApiAppTest
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Rigorous Test :-)
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void shouldAnswerWithTrue()
|
|
||||||
{
|
|
||||||
assertTrue( true );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user