I'm using groovy like an evaluator/compilator from my java application.
For example, I set a variable via the groovy binding (HS1 = 1, HS2 = 5)
binding.setVariable("HS1", 1);
binding.setVariable("HS2", 5);
and I launch an operation and catch the result via the groovy evaluate method( HS3 = HS1 + HS2)
value = (Number) shell.evaluate("HS3=HS1+HS2");
For my application, I would like to retrieve the used variable during my last operation (HS1 and HS2 in this case). I'm trying to use the binding.getVariables() method but it returns all the groovy session variable and not the last used variable.
Have you an idea to do that?
ps: Not easy to explain that with my french english level
You need retrieve the value with binding
#org.junit.Test
public void test(){
Binding binding = new Binding();
binding.setVariable("HS1", 1);
binding.setVariable("HS2", 5);
//binding.setVariable("HS3", new Integer());
GroovyShell gs = new GroovyShell(binding);
binding.beginCut(new LinkedHashMap<String, Object>());
Object o = gs.evaluate("HS3=HS1+HS2");
logger.info("GroovyTest.test: end cut");
logger.info("GroovyTest.test: "+o);
Map<String, Object> properties = binding.endCut();
logger.info("GroovyTest.test: ------");
for (Object v : properties.keySet()){
logger.info("GroovyTest.test: "+v);
}
Number sResult = (Number)binding.getVariable( "HS3" ) ;
logger.info(sResult);
}
public class MyBinding extends Binding {
private static org.apache.log4j.Logger logger = org.apache.log4j.Logger
.getLogger(MyBinding.class);
#Override
public Object getVariable(String name) {
logger.info("MyBinding.getVariable: request " + name);
Object value = super.getVariable(name);
//filter the requested variable
if (properties != null) {
properties.put(name, value);
}
return value;
}
Map<String, Object> properties = null;
public void beginCut(Map<String, Object> properties) {
this.properties = properties;
}
public Map<String, Object> endCut() {
Map<String, Object> properties = this.properties;
this.properties = null;
return properties;
}
}
Related
I've some server response (a long one) which I've converted to POJO (by using moshi library).
Eventually I have list of "Items" , each "Item" looks like follow :
public class Item
{
private String aa;
private String b;
private String abc;
private String ad;
private String dd;
private String qw;
private String arew;
private String tt;
private String asd;
private String aut;
private String id;
...
}
What I actually need, is to pull all properties which start with "a" , and then I need to use their values for further req ...
Any way to achieve it without Reflection ? (usage of streams maybe ?)
Thanks
With guava-functions tranformation you might transform your items with somethng following:
public static void main(String[] args) {
List<Item> items //
Function<Item, Map<String, Object>> transformer = new Function<Item, Map<String, Object>>() {
#Override
public Map<String, Object> apply(Item input) {
Map<String, Object> result = new HashMap<String, Object>();
for (Field f : input.getClass().getDeclaredFields()) {
if(! f.getName().startsWith("a")) {
continue;
}
Object value = null;
try {
value = f.get(input);
} catch (IllegalAccessException e) {
throw new RuntimeException("failed to cast" + e)
}
result.put(f.getName(), value);
}
return result
};
Collection<Map<String, Object> result
= Collections2.transform(items, transformer);
}
Sounds like you may want to perform your filtering on a regular Java map structure.
// Dependencies.
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Map<String, String>> itemAdapter =
moshi.adapter(Types.newParameterizedType(Map.class, String.class, String.class));
String json = "{\"aa\":\"value1\",\"b\":\"value2\",\"abc\":\"value3\"}";
// Usage.
Map<String, String> value = itemAdapter.fromJson(json);
Map<String, String> filtered = value.entrySet().stream().filter(
stringStringEntry -> stringStringEntry.getKey().charAt(0) == 'a')
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
You could wrap up the filtering logic in a custom JsonAdapter, but validation and business logic tends to be nice to leave to the application usage layer.
Output of the below code is :
This is Raja from ${Address.Street} i did my ${Education.degree} from ${Education.university}
but what I need is
This is Raja from Namakkal i did my B.E from Anna University
is it possible to achieve by using Freemarker, OGNL or by using spring.
public class Test
{
public static void main(String arg[]) throws TemplateModelException
{
Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> address = new HashMap<String, Object>();
address.put("Street", "Namakkal");
Qualification qualification = new Test.Qualification();
map.put("Name", "Raja");
map.put("Address", address);
map.put("Education", qualification);
StrSubstitutor strsub = new StrSubstitutor(map);
String str = "This is ${Name} from ${Address.Street} i did my ${Education.degree} from ${Education.university}";
System.out.println(strsub.replace(str));
}
public static class Qualification
{
public String getDegree()
{
return "B.E";
}
public String getUniversity()
{
return "Anna University";
}
}
}
please explain the simplest and effective way to achieve this.
If you want to use StrSubstitutor itself, you could try using a custom variable resolver by extending the StrLookUp class.
Example:
StrSubstitutor strsub = new StrSubstitutor(new CustomLookUp(map));
...
...
private static class CustomLookUp extends StrLookup<Object> {
Map<String, Object> map = new HashMap<String, Object>();
public CustomLookUp(Map<String, Object> map) {
this.map = map;
}
#Override
public String lookup(String key) {
// ...
// Logic for resolving your variables.
// ...
}
}
You could do it with freemarker, using the StringTemplateLoader class. This class allows you to create templates from Strings, instead of reading them from files.
Without freemarker you can use the following:
String str="This is "+(String)map.get("Name")+" from "+((Map)map.get("Address")).get("Street")+". I did my "+((Qualification)map.get("Education")).getDegree()+" from "+((Qualification)map.get("Education")).getUniversity()+".";
#Anoop example implementation with spring reflection
#Override
public String lookup(String key) {
String[] keys = key.split("\\.");
Object obj = map.get(keys[0]);
for (int i = 1; i < keys.length; i++) {
Class<?> clazz = obj.getClass();
Field field = null;
try {
field = org.springframework.util.ReflectionUtils.findField(clazz, (keys[i]));
org.springframework.util.ReflectionUtils.makeAccessible(field);
obj = field.get(obj);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return (String) obj;
}
I create object which contain feature geometry and attributes:
public class Feature {
Feature(String wkt) {
this.wkt = wkt;
}
private HashMap<Column, String> columnMap;
private String wkt;
public String getWKT() {
return wkt;
}
public void addAttribute(Column column, String value) {
columnMap.put(column, value);
}
public String getAttribute(String column) {
return columnMap.get(column) ;
}
public Map<Column, String> getAttributes(){
return columnMap;
}
}
Wkt is a geometry. ColumnMap is object contain a attributes as HashMap:
public class Column {
private String columnName;
Column(String columnName) {
this.columnName = columnName;
}
public String getName() {
return columnName;
}
}
Now i says:
columnList = new ArrayList<Column>(columns);
......
Feature feature= new Feature(WKT);
for(int p=0;p<columnList.size();p++){
for(int k=0;k<=ViewObject.getMIDInfo(totalObjects).length;k++){
if(p==k){
System.out.println("Column "+columnList.get(p).getName()+" Value "+ ViewObject.getMIDInfo(totalObjects)[k].toString());
//feature.addAttribute(columnList.get(p), ViewObject.getMIDInfo(totalObjects)[k].toString());
}
}
}
And get output:
Column id Value 22
Column kadnumm Value "66-41-0707001-19"
So how i understand columnList and ViewObject.getMIDInfo(totalObjects) is not empty. After this i change :
//feature.addAttribute(columnList.get(p), ViewObject.getMIDInfo(totalObjects)[k].toString());
to:
feature.addAttribute(columnList.get(p), ViewObject.getMIDInfo(totalObjects)[k].toString());
And get exeption:
Column id Value 22
java.lang.NullPointerException
at objects.Feature.addAttribute(Feature.java:18)
at objects.MIFParser.findRegion(MIFParser.java:181)
at objects.MIFParser.instanceNextObject(MIFParser.java:66)
at Read.main(Read.java:40)
How i understand NullPointerException means that i try to use empty objects? Whats wrong?
P.s. Sorry my english can be terrible especially with title .
UPDATE
Okey i add this: this.columnMap = new HashMap<Column, String>(); in FEature class constructor.
But now i try to do:
System.out.println(feature.getAttribute("id")+" "+feature.getAttribute("kadnumm"));
and output:
null null
What can be wrong?
You didnt initialize your columnMap:
private HashMap<Column, String> columnMap = new HashMap<Column, String>();
addAttribute tries to put something on columnMap, but you don't create columnMap anywhere. You need to add to your Feature constructor:
Feature(String wkt) {
this.wkt = wkt;
this.columnMap = new HashMap<Column, String>(); // <=== The new bit
}
...or add an initialization to your declaration:
private HashMap<Column, String> columnMap = new HashMap<Column, String>();
// The new bit--- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Just declaring the member isn't sufficient, the member just refers to an object, and starts off null. You need to create the object for it to refer to and assign that object to it.
columnMap object is not initialized when you create a new instance of Feature. So it is null when you call columnMap.put(column, value); in addAttribute
instead of
private HashMap<Column, String> columnMap;
do
private HashMap<Column, String> columnMap = new HashMap<Column, String>();
You must initialize the map:
private HashMap<Column, String> columnMap = new HashMap<Column, String>();
For my program I want to read a key from a properties file and an associated List of values for the key.
Recently I was trying like that
public static Map<String,List<String>>categoryMap = new Hashtable<String, List<String>>();
Properties prop = new Properties();
try {
prop2.load(new FileInputStream(/displayCategerization.properties));
Set<Object> keys = prop.keySet();
List<String> categoryList = new ArrayList<String>();
for (Object key : keys) {
categoryList.add((String)prop2.get(key));
LogDisplayService.categoryMap.put((String)key,categoryList);
}
System.out.println(categoryList);
System.out.println("Category Map :"+LogDisplayService.categoryMap);
keys = null;
prop = null;
} catch (Throwable e) {
e.printStackTrace();
}
and my properties file is like below -
A=APPLE
A=ALPHABET
A=ANT
B=BAT
B=BALL
B=BUS
I want for key A there should be a list which contain [APPLE, ALPHABET,ANT] and B contain [BAT,BALL,BUS].
So Map should be like this {A=[APPLE, ALPHABET,ANT], B=[BAT,BALL,BUS]} but I get {A=[ANT], B=[BUS]}
I searched on the internet for such a way but found nothing. I wish there should be a way.
Any help?
Try writing the properties as a comma separated list,
then split the value after the properties file is loaded.
For example
a=one,two,three
b=nine,ten,fourteen
You can also use org.apache.commons.configuration and change the value delimiter using the AbstractConfiguration.setListDelimiter(char) method if you're using comma in your values.
The comma separated list option is the easiest but becomes challenging if the values could include commas.
Here is an example of the a.1, a.2, ... approach:
for (String value : getPropertyList(prop, "a"))
{
System.out.println(value);
}
public static List<String> getPropertyList(Properties properties, String name)
{
List<String> result = new ArrayList<String>();
for (Map.Entry<Object, Object> entry : properties.entrySet())
{
if (((String)entry.getKey()).matches("^" + Pattern.quote(name) + "\\.\\d+$"))
{
result.add((String) entry.getValue());
}
}
return result;
}
If this is for some configuration file processing, consider using Apache configuration. https://commons.apache.org/proper/commons-configuration/javadocs/v1.10/apidocs/index.html?org/apache/commons/configuration/PropertiesConfiguration.html
It has way to multiple values to single key- The format is bit different though
key=value1,value2,valu3 gives three values against same key.
Your logic is flawed... basically, you need to:
get the list for the key
if the list is null, create a new list and put it in the map
add the word to the list
You're not doing step 2.
Here's the code you want:
Properties prop = new Properties();
prop.load(new FileInputStream("/displayCategerization.properties"));
for (Map.Entry<Object, Object> entry : prop.entrySet())
{
List<String> categoryList = categoryMap.get((String) entry.getKey());
if (categoryList == null)
{
categoryList = new ArrayList<String>();
LogDisplayService.categoryMap.put((String) entry.getKey(), categoryList);
}
categoryList.add((String) entry.getValue());
}
Note also the "correct" way to iterate over the entries of a map/properties - via its entrySet().
Create a wrapper around properties and assume your A value has keys A.1, A.2, etc. Then when asked for A your wrapper will read all the A.* items and build the list. HTH
There's probably a another way or better. But this is how I do this in Spring Boot.
My property file contains the following lines. "," is the delimiter in each line.
mml.pots=STDEP:DETY=LI3;,STDEP:DETY=LIMA;
mml.isdn.grunntengingar=STDEP:DETY=LIBAE;,STDEP:DETY=LIBAMA;
mml.isdn.stofntengingar=STDEP:DETY=LIPRAE;,STDEP:DETY=LIPRAM;,STDEP:DETY=LIPRAGS;,STDEP:DETY=LIPRVGS;
My server config
#Configuration
public class ServerConfig {
#Inject
private Environment env;
#Bean
public MMLProperties mmlProperties() {
MMLProperties properties = new MMLProperties();
properties.setMmmlPots(env.getProperty("mml.pots"));
properties.setMmmlPots(env.getProperty("mml.isdn.grunntengingar"));
properties.setMmmlPots(env.getProperty("mml.isdn.stofntengingar"));
return properties;
}
}
MMLProperties class.
public class MMLProperties {
private String mmlPots;
private String mmlIsdnGrunntengingar;
private String mmlIsdnStofntengingar;
public MMLProperties() {
super();
}
public void setMmmlPots(String mmlPots) {
this.mmlPots = mmlPots;
}
public void setMmlIsdnGrunntengingar(String mmlIsdnGrunntengingar) {
this.mmlIsdnGrunntengingar = mmlIsdnGrunntengingar;
}
public void setMmlIsdnStofntengingar(String mmlIsdnStofntengingar) {
this.mmlIsdnStofntengingar = mmlIsdnStofntengingar;
}
// These three public getXXX functions then take care of spliting the properties into List
public List<String> getMmmlCommandForPotsAsList() {
return getPropertieAsList(mmlPots);
}
public List<String> getMmlCommandsForIsdnGrunntengingarAsList() {
return getPropertieAsList(mmlIsdnGrunntengingar);
}
public List<String> getMmlCommandsForIsdnStofntengingarAsList() {
return getPropertieAsList(mmlIsdnStofntengingar);
}
private List<String> getPropertieAsList(String propertie) {
return ((propertie != null) || (propertie.length() > 0))
? Arrays.asList(propertie.split("\\s*,\\s*"))
: Collections.emptyList();
}
}
Then in my Runner class I Autowire MMLProperties
#Component
public class Runner implements CommandLineRunner {
#Autowired
MMLProperties mmlProperties;
#Override
public void run(String... arg0) throws Exception {
// Now I can call my getXXX function to retrieve the properties as List
for (String command : mmlProperties.getMmmlCommandForPotsAsList()) {
System.out.println(command);
}
}
}
Hope this helps
Is there a version of BeanUtils.describe(customer) that recursively calls the describe() method on the complex attributes of 'customer'.
class Customer {
String id;
Address address;
}
Here, I would like the describe method to retrieve the contents of the address attribute as well.
Currently, all I have can see the name of the class as follows:
{id=123, address=com.test.entities.Address#2a340e}
Funny, I would like the describe method to retrieve the contents of nested attributes as well, I don't understand why it doesn't. I went ahead and rolled my own, though. Here it is, you can just call:
Map<String,String> beanMap = BeanUtils.recursiveDescribe(customer);
A couple of caveats.
I'm wasn't sure how commons BeanUtils formatted attributes in collections, so i went with "attribute[index]".
I'm wasn't sure how it formatted attributes in maps, so i went with "attribute[key]".
For name collisions the precedence is this: First properties are loaded from the fields of super classes, then the class, then from the getter methods.
I haven't analyzed the performance of this method. If you have objects with large collections of objects that also contain collections, you might have some issues.
This is alpha code, not garunteed to be bug free.
I am assuming that you have the latest version of commons beanutils
Also, fyi, this is roughly taken from a project I've been working on called, affectionately, java in jails so you could just download it and then run:
Map<String, String[]> beanMap = new SimpleMapper().toMap(customer);
Though, you'll notice that it returns a String[], instead of a String, which may not work for your needs. Anyway, the below code should work, so have at it!
public class BeanUtils {
public static Map<String, String> recursiveDescribe(Object object) {
Set cache = new HashSet();
return recursiveDescribe(object, null, cache);
}
private static Map<String, String> recursiveDescribe(Object object, String prefix, Set cache) {
if (object == null || cache.contains(object)) return Collections.EMPTY_MAP;
cache.add(object);
prefix = (prefix != null) ? prefix + "." : "";
Map<String, String> beanMap = new TreeMap<String, String>();
Map<String, Object> properties = getProperties(object);
for (String property : properties.keySet()) {
Object value = properties.get(property);
try {
if (value == null) {
//ignore nulls
} else if (Collection.class.isAssignableFrom(value.getClass())) {
beanMap.putAll(convertAll((Collection) value, prefix + property, cache));
} else if (value.getClass().isArray()) {
beanMap.putAll(convertAll(Arrays.asList((Object[]) value), prefix + property, cache));
} else if (Map.class.isAssignableFrom(value.getClass())) {
beanMap.putAll(convertMap((Map) value, prefix + property, cache));
} else {
beanMap.putAll(convertObject(value, prefix + property, cache));
}
} catch (Exception e) {
e.printStackTrace();
}
}
return beanMap;
}
private static Map<String, Object> getProperties(Object object) {
Map<String, Object> propertyMap = getFields(object);
//getters take precedence in case of any name collisions
propertyMap.putAll(getGetterMethods(object));
return propertyMap;
}
private static Map<String, Object> getGetterMethods(Object object) {
Map<String, Object> result = new HashMap<String, Object>();
BeanInfo info;
try {
info = Introspector.getBeanInfo(object.getClass());
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
Method reader = pd.getReadMethod();
if (reader != null) {
String name = pd.getName();
if (!"class".equals(name)) {
try {
Object value = reader.invoke(object);
result.put(name, value);
} catch (Exception e) {
//you can choose to do something here
}
}
}
}
} catch (IntrospectionException e) {
//you can choose to do something here
} finally {
return result;
}
}
private static Map<String, Object> getFields(Object object) {
return getFields(object, object.getClass());
}
private static Map<String, Object> getFields(Object object, Class<?> classType) {
Map<String, Object> result = new HashMap<String, Object>();
Class superClass = classType.getSuperclass();
if (superClass != null) result.putAll(getFields(object, superClass));
//get public fields only
Field[] fields = classType.getFields();
for (Field field : fields) {
try {
result.put(field.getName(), field.get(object));
} catch (IllegalAccessException e) {
//you can choose to do something here
}
}
return result;
}
private static Map<String, String> convertAll(Collection<Object> values, String key, Set cache) {
Map<String, String> valuesMap = new HashMap<String, String>();
Object[] valArray = values.toArray();
for (int i = 0; i < valArray.length; i++) {
Object value = valArray[i];
if (value != null) valuesMap.putAll(convertObject(value, key + "[" + i + "]", cache));
}
return valuesMap;
}
private static Map<String, String> convertMap(Map<Object, Object> values, String key, Set cache) {
Map<String, String> valuesMap = new HashMap<String, String>();
for (Object thisKey : values.keySet()) {
Object value = values.get(thisKey);
if (value != null) valuesMap.putAll(convertObject(value, key + "[" + thisKey + "]", cache));
}
return valuesMap;
}
private static ConvertUtilsBean converter = BeanUtilsBean.getInstance().getConvertUtils();
private static Map<String, String> convertObject(Object value, String key, Set cache) {
//if this type has a registered converted, then get the string and return
if (converter.lookup(value.getClass()) != null) {
String stringValue = converter.convert(value);
Map<String, String> valueMap = new HashMap<String, String>();
valueMap.put(key, stringValue);
return valueMap;
} else {
//otherwise, treat it as a nested bean that needs to be described itself
return recursiveDescribe(value, key, cache);
}
}
}
The challenge (or show stopper) is problem that we have to deal with an object graph instead of a simple tree. A graph may contain cycles and that requires to develop some custom rules or requirements for the stop criteria inside the recursive algorithm.
Have a look at a dead simple bean (a tree structure, getters are assumed but not shown):
public class Node {
private Node parent;
private Node left;
private Node right;
}
and initialize it like this:
root
/ \
A B
Now call a describe on root. A non-recursive call would result in
{parent=null, left=A, right=B}
A recursive call instead would do a
1: describe(root) =>
2: {parent=describe(null), left=describe(A), right=describe(B)} =>
3: {parent=null,
{A.parent=describe(root), A.left=describe(null), A.right= describe(null)}
{B.parent=describe(root), B.left=describe(null), B.right= describe(null)}}
and run into a StackOverflowError because describe is called with objects root, A and B over and over again.
One solution for a custom implementation could be to remember all objects that have been described so far (record those instances in a set, stop if set.contains(bean) return true) and store some kind of link in your result object.
You can simple use from the same commom-beanutils:
Map<String, Object> result = PropertyUtils.describe(obj);
Return the entire set of properties for which the specified bean provides a read method.