blob: 8a2fde792232250826408818f3d08d42aa2377e1 [file] [log] [blame]
/**
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.runtime.common.annotations;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.reflect.FieldUtils;
/**
* The Class PropertiesUtil.
*/
public class PropertiesUtil {
/**
* Returns true, if the property in object contains a @Property with the
* given key.
*
* @param object
* the object
* @param property
* the property may be nested
* @param key
* the key
* @return true, if successful
*/
public static boolean hasKey(Object object, String property, String key) {
return hasKey(object.getClass(), property, key);
}
/**
* Returns true, if the property in clazz contains a @Property with the
* given key.
*
* @param clazz
* the clazz
* @param property
* the property may be nested
* @param key
* the key
* @return true, if successful
*/
public static boolean hasKey(Class<?> clazz, String property, String key) {
return getValue(clazz, property, key) != null;
}
/**
* Returns the value of @Property, from the property in object containing a @Property
* with the given key.
*
* @param object
* the object
* @param property
* the property may be nested
* @param key
* the key
* @return the value
*/
public static String getValue(Object object, String property, String key) {
return getValue(object.getClass(), property, key);
}
/**
* Returns the value of @Property, from the property in clazz containing a @Property
* with the given key.
*
* @param clazz
* the clazz
* @param property
* the property may be nested
* @param key
* the key
* @return the value
*/
public static String getValue(Class<?> clazz, String property, String key) {
Field f = getFieldWithNested(clazz, property);
if (f == null) {
return null;
}
Properties props = f.getAnnotation(Properties.class);
if (props == null) {
return null;
}
for (Property prop : props.properties()) {
if (prop.key().equals(key)) {
return prop.value();
}
}
return null;
}
/**
* Returns the field, based on the property. Properties may be nested.
*
* @param clazz
* @param property
* @return
*/
protected static Field getFieldWithNested(Class<?> clazz, String property) {
Field result = null;
if (property.contains(".")) {
// first top-level property, then go deeper in a loop
Class<?> propertyClass = clazz;
String[] tokens = property.split("\\.");
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i].trim();
result = FieldUtils.getField(propertyClass, token, true);
if (result == null) {
throw new IllegalStateException(String.format(
"Can not access %s for %s", property,
clazz.getName()));
}
propertyClass = result.getType();
}
} else {
result = FieldUtils.getField(clazz, property, true);
}
return result;
}
/**
* Returns a map with all key value pairs contained in @Properties.
*
* @param object
* the object
* @param property
* the property may be nested
* @param key
* the key
* @return the properties
*/
public static Map<String, String> getProperties(Object object,
String property, String key) {
return getProperties(object.getClass(), property, key);
}
/**
* Returns a map with all key value pairs contained in @Properties.
*
* @param clazz
* the clazz
* @param property
* the property may be nested
* @param key
* the key
* @return the properties
*/
public static Map<String, String> getProperties(Class<?> clazz,
String property, String key) {
Field f = getFieldWithNested(clazz, property);
if (f == null) {
return null;
}
Properties props = f.getAnnotation(Properties.class);
if (props == null) {
return null;
}
Map<String, String> result = new HashMap<String, String>();
for (Property prop : props.properties()) {
result.put(prop.key(), prop.value());
}
return result;
}
}