-
-
Notifications
You must be signed in to change notification settings - Fork 22
Getting and setting properties of a Java bean through path
Burningwave edited this page Jul 6, 2020
·
2 revisions
For this example we will use these Java beans:
package org.burningwave.core.bean;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Complex {
private Complex.Data data;
public Complex() {
setData(new Data());
}
public Complex.Data getData() {
return data;
}
public void setData(Complex.Data data) {
this.data = data;
}
public static class Data {
private Data.Item[][] items;
private List<Data.Item> itemsList;
private Map<String, Data.Item[][]> itemsMap;
public Data() {
items = new Data.Item[][] {
new Data.Item[] {
new Item("Hello"),
new Item("World!"),
new Item("How do you do?")
},
new Data.Item[] {
new Item("How do you do?"),
new Item("Hello"),
new Item("Bye")
}
};
itemsMap = new LinkedHashMap<>();
itemsMap.put("items", items);
}
public Data.Item[][] getItems() {
return items;
}
public void setItems(Data.Item[][] items) {
this.items = items;
}
public List<Data.Item> getItemsList() {
return itemsList;
}
public void setItemsList(List<Data.Item> itemsList) {
this.itemsList = itemsList;
}
public Map<String, Data.Item[][]> getItemsMap() {
return itemsMap;
}
public void setItemsMap(Map<String, Data.Item[][]> itemsMap) {
this.itemsMap = itemsMap;
}
public static class Item {
private String name;
public Item(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
}
... And now we are going to get and set some properties:
import static org.burningwave.core.assembler.StaticComponentContainer.ByFieldOrByMethodPropertyAccessor;
import static org.burningwave.core.assembler.StaticComponentContainer.ByMethodOrByFieldPropertyAccessor;
import org.burningwave.core.bean.Complex;
public class GetAndSetPropertiesThroughPath{
public void execute() {
Complex complex = new Complex();
//This type of property accessor try to access by field introspection: if no field was found
//it will search getter method and invokes it
String nameFromObjectInArray = ByFieldOrByMethodPropertyAccessor.get(complex, "data.items[1][0].name");
String nameFromObjectMap = ByFieldOrByMethodPropertyAccessor.get(complex, "data.itemsMap[items][1][1].name");
System.out.println(nameFromObjectInArray);
System.out.println(nameFromObjectMap);
//This type of property accessor looks for getter method and invokes it: if no getter method was found
//it will search for field and try to retrieve it
nameFromObjectInArray = ByMethodOrByFieldPropertyAccessor.get(complex, "data.items[1][2].name");
nameFromObjectMap = ByMethodOrByFieldPropertyAccessor.get(complex, "data.itemsMap[items][1][1].name");
System.out.println(nameFromObjectInArray);
System.out.println(nameFromObjectMap);
ByMethodOrByFieldPropertyAccessor.set(complex, "data.itemsMap[items][1][1].name", "Good evening!");
nameFromObjectInArray = ByMethodOrByFieldPropertyAccessor.get(complex, "data.itemsMap[items][1][1].name");
System.out.println(nameFromObjectInArray);
}
public static void main(String[] args) {
new GetAndSetPropertiesThroughPath().execute();
}
}
Burningwave core is a fully indipendent, advanced, free and open source Java frameworks building library that contains AN EXTREMELY POWERFUL CLASSPATH SCANNER.
To include Burningwave Core library in your projects simply use with Apache Maven:
<dependency>
<groupId>org.burningwave</groupId>
<artifactId>core</artifactId>
<version>12.65.2</version>
</dependency>
To use Burningwave Core as a Java module add the following to your module-info.java
:
requires org.burningwave.core;
ClassFactory
ClassHunter
- In depth look to and configuration guide
- USE CASE: retrieving all classes of the classpath
- USE CASE: retrieving all classes that implement one or more interfaces
- USE CASE: finding all classes that extend a base class
- USE CASE: searching for all classes that have package name that matches a regex
- USE CASE: finding all classes for module name (Java 9 and later)
- USE CASE: finding all annotated classes
- USE CASE: how to scan classes for specific annotations and collect its values
- USE CASE: searching for all classes with a constructor that takes a specific type as first parameter and with at least 2 methods that begin for a given string
- USE CASE: searching for all classes with methods whose name begins for a given string and that takes a specific type as its first parameter
- USE CASE: finding all classes that have at least 2 protected fields