0

I have some properties from db which need to add all those properties to a class dynamically with setters and getters

public class SearchClass{

dynamic properties 

//
getters

setters

}
David
  • 17
  • 3
  • Use a `Map` for the properties. –  Jul 26 '19 at 10:19
  • 1
    Partly answered by https://stackoverflow.com/questions/6680674/can-a-java-class-add-a-method-to-itself-at-runtime – Federico klez Culloca Jul 26 '19 at 10:20
  • What do you mean by dynamic properties? You want to do it runtime or do reverse mapping of db table? – Antoniossss Jul 26 '19 at 10:21
  • It would be possible to create class from scrach on runtime eg with JavaPoet or javaassist and other bytecode manipulation tools – Antoniossss Jul 26 '19 at 10:22
  • yes at run time@Antoniossss ,I have a class like SearchClass it dont have properties but in run time I want to add the properties in Runtime like private String userName, along with setters and getters – David Jul 26 '19 at 10:26
  • The problem is that your new properties would not be visible at compile time, so they could not be used by a conventional Java application. How do you intend to use this class? – rghome Jul 26 '19 at 11:16

1 Answers1

0

A Map will do this. Your choices for key are as follows:

  • Integer - can be declared using public static final int PROPERTY_NAME = 0; (increment as needed)
  • String - similar to Integer
  • enum - This can conveniently be iterated over if you need to apply an operation to the entire set of properties.

To access from outside your class, simply make a getter and setter for your map, and use searchClass.getMap().get(key); to retrieve the properties.

For your values, if you know their type in advance (e.g. if they will all be Strings, you can define your Map as Map<SearchKey, String> (SearchKeybeing an example enum name). If however your map contains many types it must be defined asMap`, and you must manually cast values to the desired type.

If you only use a few types of value, it may be worth having multiple maps, with one for each type, e.g.:

private Map<SearchKey, Integer> ints = ...
private Map<SearchKey, String> strings = ...
private Map<SearchKey, Boolean> booleans = ...

and so on.

cameron1024
  • 9,083
  • 2
  • 16
  • 36
  • but when I send this to request I have the map but not properties eg, send the SearchClass object to postMan here I want like this username,grade,location ..and etc where the above fields (username,grade,location)are dynamic properties – David Jul 26 '19 at 10:34
  • If you simply want a dynamic data structure for storing properties, the above will work. If you want them to be accessible as members of the class, bytecode manipulation is probably necessary. However, what advantages are there of referring to the "username" variable by "username" rather than "map.get(USERNAME)"? – cameron1024 Jul 26 '19 at 10:45
  • I dont know if I got it correctly....... when I send this object to the user ,User enters input for these particular properties and save the form i,e form data – David Jul 26 '19 at 11:18
  • The map method will work for this. You can use map.forEach() to dynamically generate html forms for a web application, javafx components for a javafx GUI, or prompt them to enter data through the terminal. You can call "map.put(KEY, null)" to create an entry in the map, and then ask the user to populate all the entries in the map – cameron1024 Jul 26 '19 at 11:27