code manipulation : reflect(Class API)

This commit is contained in:
haerong22
2021-02-19 18:51:05 +09:00
parent 41890753df
commit b30a888de3
14 changed files with 520059 additions and 941002 deletions

View File

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>reflection-example</artifactId>
<version>1.0-SNAPSHOT</version>
<name>reflection-example</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,106 @@
package org.example;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
public class App {
public static void main( String[] args ) throws ClassNotFoundException {
/**
* Class<T> 에 접근하여 정보 가져오기
*/
// 메소드
Arrays.stream(Book.class.getDeclaredMethods()).forEach(m -> {
int modifiers = m.getModifiers();
System.out.println(m);
System.out.println(Modifier.isPrivate(modifiers));
System.out.println(m.getReturnType());
});
// // 접근지시자
// Arrays.stream(Book.class.getDeclaredFields()).forEach(f -> {
// int modifiers = f.getModifiers();
// System.out.println(f);
// System.out.println(Modifier.isPrivate(modifiers));
// System.out.println(Modifier.isStatic(modifiers));
// });
// // 메소드 가져오기
// Class<Book> bookClass = Book.class;
// System.out.println("getMethods() : Object 에서 상속받은 메소드도 가져온다.");
// Arrays.stream(bookClass.getMethods()).forEach(System.out::println);
//
// System.out.println("===============================================");
//
// System.out.println("getDeclaredMethods() : 인스턴스에 있는 메소드만 가져온다.");
// Arrays.stream(bookClass.getDeclaredMethods()).forEach(System.out::println);
//
// System.out.println("===============================================");
//
// System.out.println("getDeclaredConstructors() : 인스턴스에 있는 생성자를 가져온다.");
// Arrays.stream(bookClass.getDeclaredConstructors()).forEach(System.out::println);
//
// System.out.println("===============================================");
//
// System.out.println("getSuperclass() : 상속받은 부모클래스를 가져온다.");
// System.out.println(MyBook.class.getSuperclass());
//
// System.out.println("===============================================");
//
// System.out.println("getInterfaces() : 상속받은 인터페이스를 가져온다.");
// Arrays.stream(MyBook.class.getInterfaces()).forEach(System.out::println);
// // field 의 값을 가져오려면 생성한 인스턴스가 있어야 한다.
// Book book = new Book();
// Arrays.stream(book.getClass().getDeclaredFields())
// .forEach(f -> {
// try {
// f.setAccessible(true); // 모든 필드에 접근 가능
// System.out.printf("%s, %s\n", f, f.get(book));
// } catch (IllegalAccessException e) {
// e.printStackTrace();
// }
// });
// // 필드 가져오기
// Class<Book> bookClass = Book.class;
//
// // getFields() -> public 만 접근
// Field[] fields = bookClass.getFields();
// Arrays.stream(fields).forEach(System.out::println);
//
// System.out.println("===============================================");
//
// // getDeclaredFields() -> 모든 필드 접근
// Field[] declaredFields = bookClass.getDeclaredFields();
// Arrays.stream(declaredFields).forEach(System.out::println);
/**
* Class<T> 에 접근하기
*/
// // 클래시 로딩시 생성된 인스턴스로 접근
// Class<Book> bookClass = Book.class;
// System.out.println(bookClass.getName());
// System.out.println("============================");
//
// // 생성한 인스턴스로 접근
// Book book = new Book();
// Class<? extends Book> bookClass1 = book.getClass();
// System.out.println(bookClass1.getName());
// System.out.println("============================");
//
// // 문자열로 접근
// Class<?> bookClass2 = Class.forName("org.example.Book");
// System.out.println(bookClass2.getName());
}
}

View File

@@ -0,0 +1,35 @@
package org.example;
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;
}
}

View File

@@ -0,0 +1,4 @@
package org.example;
public class MyBook extends Book implements MyInterface{
}

View File

@@ -0,0 +1,4 @@
package org.example;
public interface MyInterface {
}

View File

@@ -0,0 +1,20 @@
package org.example;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}