Java 反射教程:創建 Java POJO 使用反射 API 獲取 ClassName、DeclaredFields、ObjectType、SuperType 等...
已發表: 2019-06-10
使用反射 API 以編程方式獲取 Java 中所有聲明的類字段的最佳方法
在本教程中,我將首先編寫簡單的 Java POJO,並將在 POJO 上執行所有 Java 反射示例。 您一定聽說過術語 POJO。 什麼是Plain Old Java Object
?
此外,如果您有以下問題,那麼您來對地方了:
- java – 如何創建 POJO?
- java – 創建簡單的 POJO 類
- Java 中的普通舊 Java 對象 (POJO)
- POJO(Plain Old Java Object)簡介
- 設計 POJO 類
- 創建第一個 Java POJO 類人
POJO
只是一個簡單的、舊的Java Bean
,去掉了限制。 Java Bean 必須滿足以下要求:
- 遵循
isFoo
getFoo
和名為 foo 的可變屬性的setFoo
方法; 如果 foo 是不可變的,則不要使用 setFoo。 - 默認
no-arg
構造函數 - 必須實現
java.io.Serializable
POJO 不強制要求任何這些。 顧名思義:在JDK下編譯的對象可以被認為是Plain Old Java Object。
沒有應用服務器,沒有基類,不需要使用接口。
讓我們創建名為CrunchifyPOJO.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
package crunchify . com . tutorials ; /** * @author Crunchify.com Simple POJO Example */ public class CrunchifyPOJO { public String name ; protected String webAddress ; public String email ; protected int zip ; public CrunchifyPOJO ( ) { name = "Crunchify.com" ; webAddress = "https://crunchify.com" ; } // ========================================================== // Create a Setters and Getters for all variables // ========================================================== public String getName ( ) { return name ; } public void setName ( String name ) { this . name = name ; } protected String getWebAddress ( ) { return webAddress ; } protected void setWebAddress ( String webAddress ) { this . webAddress = webAddress ; } public String getEmail ( ) { return email ; } public void setEmail ( String email ) { this . email = email ; } protected int getZip ( ) { return zip ; } protected void setZip ( int zip ) { this . zip = zip ; } public void thisIsCrunchifyReflection ( ) { System . out . println ( "- Hey This is Crunchify's Refection API tutorials. More than 400 Tutorials on Crunchify.com" ) ; } // ========================================================== // Create a String description of a Crunchify credentials // ========================================================== public String toString ( ) { String result = "Name: " + getName ( ) + "\n" ; result += "WebAddress: " + getWebAddress ( ) + "\n" ; result += "email: " + getEmail ( ) + "\n" ; result += "zip: " + getZip ( ) + "\n" ; return result ; } public static void main ( String [ ] args ) { // Create and print a CrunchifyPOJO object ... CrunchifyPOJO crunchify = new CrunchifyPOJO ( ) ; crunchify . setName ( "Crunchify.com" ) ; crunchify . setWebAddress ( "https://crunchify.com" ) ; crunchify . setZip ( 95124 ) ; System . out . println ( crunchify ) ; } } |
結果:
1 2 3 4 |
Name : Crunchify . com WebAddress : https : //crunchify.com email : test @ crunchify . com zip : 95124 |

現在讓我們有一個基於這個 POJO 的Java Reflection's Example
。
看看下面的示例類CrunchifyReflectionTutorial.java
,它涵蓋了總共 9 個不同的 Java 反射 API 示例:
您正在運行 Java 程序嗎?
如果您想在運行時檢查類、接口、字段和方法,而在編譯時不知道類、方法等的名稱,該怎麼辦。 好吧,在反射的幫助下,這很容易實現。
反射通常由需要能夠檢查或修改在 Java 虛擬機中運行的應用程序的運行時行為的程序使用。
關於 Java 反射如果您有以下任何問題,那麼您來對地方了:
- 類、方法、字段的 Java 反射教程
- 使用 Java 反射 API 進行動態類加載
- 深入了解 Java 反射 API
- java api反射——如何使用Constructor對象創建對象
- 反射——如何調用 Java 方法
- 如何在運行時使用反射調用 Java 方法
- 沒有參數的java反射調用方法
- 如何獲取類的名稱、CanonicalName 和 SimpleName
- 讓我們看看類對像是否代表一個 Array 類
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
package crunchify . com . tutorials ; import java . lang . reflect . Constructor ; import java . lang . reflect . Field ; import java . lang . reflect . Method ; /** * @author Crunchify.com * */ public class CrunchifyReflectionTutorial { public static void main ( String [ ] args ) { CrunchifyPOJO crunchify = new CrunchifyPOJO ( ) ; System . out . println ( "Crunchify Object: ====================\n" + crunchify ) ; // How to find out the Object belongs to which class? Class <? > clazz = crunchify . getClass ( ) ; // Example 1: ==================== How to get the Class's Name, // CanonicalName and SimpleName? String clazzName = clazz . getName ( ) ; String clazzCanonicalName = clazz . getCanonicalName ( ) ; String clazzSimpleName = clazz . getSimpleName ( ) ; System . out . println ( "How to get the Class's Name, CanonicalName and SimpleName? ==================== Example 1" ) ; System . out . println ( "1. clazzName: " + clazzName + ", \n2. clazzCanonicalName: " + clazzCanonicalName + ", \n3. clazzSimpleName: " + clazzSimpleName + "\n" ) ; // Example 2: ==================== Let's find out if class object // represents an Array class int [ ] [ ] crunchifyArr = { { 1 , 1 } , { 2 , 1 } } ; Class <? extends int [ ] [ ] > arrClazz = crunchifyArr . getClass ( ) ; System . out . println ( "Let's find out if class object represents an Array class ==================== Example 2" ) ; if ( arrClazz . isArray ( ) ) { System . out . println ( "- " + arrClazz . getSimpleName ( ) + " is an array class.\n" ) ; } else { System . out . println ( "- " + clazz . getSimpleName ( ) + " is not an array class.\n" ) ; } // Example 3: ==================== Let's find out Object's Type Double crunchifyDouble = 11.1 ; System . out . println ( "Let's find out Object's Type ==================== Example 3" ) ; System . out . println ( "- 11.1 is of Type: " + crunchifyDouble . getClass ( ) . getName ( ) + "\n" ) ; // Example 4: ==================== How to get SuperClass System . out . println ( "How to get SuperClass ==================== Example 4" ) ; System . out . println ( "1. Superclass of crunchify: " + crunchify . getClass ( ) . getSuperclass ( ) + "\n2. Superclass of crunchifyDouble: " + crunchifyDouble . getClass ( ) . getSuperclass ( ) + "\n" ) ; // Example 5: ==================== How to check if class is Primitive // Type of not? System . out . println ( "How to check if class is Primitive Type of not? ==================== Example 5" ) ; System . out . println ( "1. Is 'int' a Prmitive Type: " + int . class . isPrimitive ( ) ) ; System . out . println ( "2. Is 'String' a Prmitive Type: " + String . class . isPrimitive ( ) ) ; System . out . println ( "3. Is 'double' a Prmitive Type: " + double . class . isPrimitive ( ) ) ; // Example 6: ==================== How to create an object using // Constructor object? // A constructor reflection to create a string object by calling // String(String) and String(StringBuilder) constructors. Class < String > clazzString = String . class ; System . out . println ( "\nHow to create object using Constructor object? ==================== Example 6" ) ; try { Constructor <? > constructor = clazzString . getConstructor ( new Class [ ] { String . class } ) ; String object = ( String ) constructor . newInstance ( new Object [ ] { "Hello World!" } ) ; System . out . println ( "1. String.class = " + object ) ; constructor = clazzString . getConstructor ( new Class [ ] { StringBuilder . class } ) ; object = ( String ) constructor . newInstance ( new Object [ ] { new StringBuilder ( "Hello Universe!" ) } ) ; System . out . println ( "2. StringBuilder.class = " + object ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } // Example 7: ==================== How to get constructors of a class // object? System . out . println ( "\nHow to get constructors of a class object ==================== Example 7" ) ; try { Constructor <? extends CrunchifyPOJO > constructor = crunchify . getClass ( ) . getConstructor ( ) ; System . out . println ( "- Constructor = " + constructor . getName ( ) ) ; } catch ( NoSuchMethodException e ) { e . printStackTrace ( ) ; } // Example 8: ==================== How to get field of a class object try { System . out . println ( "\nHow to get field of a class object ==================== Example 8" ) ; Field [ ] methods = clazz . getFields ( ) ; for ( Field temp : methods ) { System . out . println ( "- " + temp . getName ( ) ) ; } } catch ( Exception e ) { e . printStackTrace ( ) ; } // Example 9: ==================== How to invoke a method using Method // class? try { System . out . println ( "\nHow to invoke a method using Method class ==================== Example 9" ) ; Class <? > c = Class . forName ( "crunchify.com.tutorials.CrunchifyPOJO" ) ; Object obj = c . newInstance ( ) ; Method method = c . getDeclaredMethod ( "thisIsCrunchifyReflection" ) ; method . invoke ( obj ) ; } catch ( Exception e ) { System . out . println ( e ) ; } // Example 10: ==================== get all Declared Class Fields Field [ ] crunchifyFields = CrunchifyPOJO . class . getDeclaredFields ( ) ; System . out . println ( "\nget all Declared Class Fields ==================== Example 10" ) ; for ( Field field : crunchifyFields ) { Class < ? > type = field . getType ( ) ; System . out . println ( "field name : " + field . getName ( ) + " , type : " + type ) ; } } } |
結果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
Crunchify Object : ==================== Name : Crunchify . com WebAddress : https : //crunchify.com email : test @ crunchify . com zip : 0 How to get the Class 's Name, CanonicalName and SimpleName? ==================== Example 1 1. clazzName: crunchify.com.tutorials.CrunchifyPOJO, 2. clazzCanonicalName: crunchify.com.tutorials.CrunchifyPOJO, 3. clazzSimpleName: CrunchifyPOJO Let' s find out if class object represents an Array class ==================== Example 2 - int [ ] [ ] is an array class . Let 's find out Object' s Type ==================== Example 3 - 11.1 is of Type : java . lang . Double How to get SuperClass ==================== Example 4 1. Superclass of crunchify : class java . lang . Object 2. Superclass of crunchifyDouble : class java . lang . Number How to check if class is Primitive Type of not ? ==================== Example 5 1. Is 'int' a Prmitive Type : true 2. Is 'String' a Prmitive Type : false 3. Is 'double' a Prmitive Type : true How to create object using Constructor object ? ==================== Example 6 1. String . class = Hello World ! 2. StringBuilder . class = Hello Universe ! How to get constructors of a class object ==================== Example 7 - Constructor = crunchify . com . tutorials . CrunchifyPOJO How to get field of a class object ==================== Example 8 - name - email How to invoke a method using Method class ==================== Example 9 - Hey This is Crunchify ' s Refection API tutorials . More than 400 Tutorials on Crunchify . com get all Declared Class Fields ==================== Example 10 field name : name , type : class java . lang . String field name : webAddress , type : class java . lang . String field name : email , type : class java . lang . String field name : zip , type : int |
完整的示例是自我解釋的,因為我盡我所能將盡可能多的系統輸出放入程序本身。

試試看,如果您有任何問題,請告訴我。 快樂編碼。