how to fill a composite object using java reflection?

Elad Benda2

I have a config file like this:

Provider Search,ConfigURL,http://myUrl

I want to fill this object using reflection

public class MyPrefrences {

    public ProviderSearch ProviderSearch;
}



    public class ProviderSearch {

        public String ConfigURL;

    }

here is my code, but I fail to set the composite object:

    MyPrefrences myPrefrences = new MyPrefrences();

    try {
        Field field = myPrefrences.getClass().getDeclaredField(normalizedCategory);
        Field field2 = field.getClass().getDeclaredField(normalizedKey);
        field2.set(field, val);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

how would you do it

a) if all leaves members are String?

b) if I the leaves can be primitives and non-primitives?

Tkachuk_Evgen

First of all You need to pass your Object reference which value you want set in field.set().

And second: field.getClass() gives you java.lang.reflect.Field, use field.getType()to get Class you need. So, it should be like this

Field field1=myPref.getClass().getDeclaredField("ProviderSearch");
Object obj=field1.getType().newInstance();
Field field2=field1.getType().getDeclaredField("ConfigURL");
field2.set(obj, "www.stackoverflow.com");
field1.set(myPref, obj);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to create a property on a dynamic object using reflection

분류에서Dev

Lazy initialization of an object using reflection

분류에서Dev

How to fill IEnumerable-Property of Object using MEF and DirectoryCatalog

분류에서Dev

Fill object from dataset using grouping in Linq

분류에서Dev

converting Object returned by Field.get() to String[] in java reflection

분류에서Dev

Can I obtain method Fields name using Java reflection?

분류에서Dev

How to fill the outline text by PIL to using tesseract?

분류에서Dev

choosing the right pattern for object composite

분류에서Dev

Using reflection Invoke

분류에서Dev

Using reflection Invoke

분류에서Dev

Using Reflection in .NET 3.0

분류에서Dev

Java Class Literals and Reflection?

분류에서Dev

JS fill Object as an array

분류에서Dev

How to call reflection method

분류에서Dev

How can I access methods of different classes in java using the array of type Object

분류에서Dev

Java Reflection Casting Method ReturnType

분류에서Dev

Java - How to initialize parameters for an object?

분류에서Dev

Javascript Reflection - Get properties of a JsonResult object

분류에서Dev

How can i fill the other form fields automatically if user fill one field in jsp (while using spring mvc and hibernate)

분류에서Dev

Create Page object in Java Class using URL

분류에서Dev

How to set max composite size in SWT

분류에서Dev

How to create a table with _id as composite primary key?

분류에서Dev

Creating a composite object from two other core data objects

분류에서Dev

How to update document in an object in an array in an object in an array using MongoDB?

분류에서Dev

How to detect object from video using SVM

분류에서Dev

How to retrieve/import object using MEF in Prism

분류에서Dev

Runtime reflection - extracting Symbols using a single method

분류에서Dev

How to add gradient color fill to the points of scatter plot using google Charts?

분류에서Dev

how can i get my local DB data to fill my txt box using LINQ?

Related 관련 기사

  1. 1

    How to create a property on a dynamic object using reflection

  2. 2

    Lazy initialization of an object using reflection

  3. 3

    How to fill IEnumerable-Property of Object using MEF and DirectoryCatalog

  4. 4

    Fill object from dataset using grouping in Linq

  5. 5

    converting Object returned by Field.get() to String[] in java reflection

  6. 6

    Can I obtain method Fields name using Java reflection?

  7. 7

    How to fill the outline text by PIL to using tesseract?

  8. 8

    choosing the right pattern for object composite

  9. 9

    Using reflection Invoke

  10. 10

    Using reflection Invoke

  11. 11

    Using Reflection in .NET 3.0

  12. 12

    Java Class Literals and Reflection?

  13. 13

    JS fill Object as an array

  14. 14

    How to call reflection method

  15. 15

    How can I access methods of different classes in java using the array of type Object

  16. 16

    Java Reflection Casting Method ReturnType

  17. 17

    Java - How to initialize parameters for an object?

  18. 18

    Javascript Reflection - Get properties of a JsonResult object

  19. 19

    How can i fill the other form fields automatically if user fill one field in jsp (while using spring mvc and hibernate)

  20. 20

    Create Page object in Java Class using URL

  21. 21

    How to set max composite size in SWT

  22. 22

    How to create a table with _id as composite primary key?

  23. 23

    Creating a composite object from two other core data objects

  24. 24

    How to update document in an object in an array in an object in an array using MongoDB?

  25. 25

    How to detect object from video using SVM

  26. 26

    How to retrieve/import object using MEF in Prism

  27. 27

    Runtime reflection - extracting Symbols using a single method

  28. 28

    How to add gradient color fill to the points of scatter plot using google Charts?

  29. 29

    how can i get my local DB data to fill my txt box using LINQ?

뜨겁다태그

보관