Java library to access class attributes via dot notation.
Java library to access class attributes via dot notation.
15.Aug.2014: New release v1.1.0 which adds the new map access feature.
attrib1.arrayAttrib2.attrib3
- returns an array with all values of attrib3
.With this library one can access values of attributes via a defined path from a root element. To access an attrubute value use the dot notation:
level1.level2.text
- accesses attrbute level1
in root class, then accesses attribute level2
in level1
class and then accesses attrbute text
in level2
class.levels1.text
- accesses all text
attributes in collection or array levels1
(returns an array).levels1[0].text
- accesses attribute text
in first (index 0) collection or array element of levels1
.level1.texts[0]
- accessses first (index 0) element of collection or array or map attribute texts
.level1.textMap['key']
- accessses element of map attribute textMap
via simple key (string) access (argument with single quote).level1.textMap[your.domain.YourType(keyPart1, keyPart2)]
- accessses element of map attribute textMap
via a constructor (with string arguments) which creates the key object to access the map (argument(s) without quotes).level1.textMap[your.domain.YourFactory#factoryMethod(keyPart1, keyPart2)]
- accessses element of map attribute textMap
via a factory method (with string arguments) which creates the key object to access the map (argument(s) without quotes).Attention!
Accessing collections e.g. HashSets via index doesn't make alway sense, because the order of the set isn't fix. So if you set a value at index 0 and later you want to access it, it is very probable that you won't find it at index 0 anymore.
...
DotNotationAccessor dotNotationAccessor = new DotNotationAccessor(true, false, false);
RootClass rootClass = new RootClass();
String attributePath = "simpleChildLevel1Class.simpleChildLevel2Class.simpleChildLevel3Class.simpleString";
Object result = dotNotationAccessor.getAttribute(rootClass, attributePath);
...
First it creates an instance of DotNotationAccessor
only permitting accessing the attributes via getters and setters.
Then it creates RootClass
which has a more or less complex class hierarchy. Afterwards the attribute path, from the root class to the final attribute one want to read or set, is defined. Then it is called the getAttribute
method in the DotNotationAccessor
instance to read the attribute simpleString
and save it in the result variable.
To use the library in your java project just put the binary jar file to your classpath. The maven dependency can be found here: mvnrepository
(C) 2014 by haui