-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
299 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
dina-base-api/src/main/java/ca/gc/aafc/dina/util/ReflectionUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ca.gc.aafc.dina.util; | ||
|
||
import java.beans.IntrospectionException; | ||
import java.beans.PropertyDescriptor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Map; | ||
|
||
import org.apache.commons.beanutils.PropertyUtils; | ||
|
||
/** | ||
* Mostly a utility class to simplify usage of reflection and {@link org.apache.commons.beanutils.BeanUtils} | ||
*/ | ||
public final class ReflectionUtils { | ||
|
||
private ReflectionUtils() { | ||
// utility class | ||
} | ||
|
||
/** | ||
* Creates a new instance of the provided class by using the default constructor. | ||
* | ||
* @param clazz | ||
* @return instance of clazz | ||
*/ | ||
public static <T> T newInstance(Class<T> clazz) { | ||
try { | ||
return clazz.getConstructor().newInstance(); | ||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | | ||
NoSuchMethodException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static Method getSetterMethod(String propertyName, Class<?> beanClass) { | ||
try { | ||
return new PropertyDescriptor(propertyName, beanClass).getWriteMethod(); | ||
} catch (IntrospectionException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Sets all provided attributes on the target object. | ||
* As opposed to BeanUtils, this method will throw an exception if the attribute doesn't exist | ||
* on the target. | ||
* @param target | ||
* @param attributes | ||
* @throws IllegalArgumentException if attributes is not found or there is a type mismatch | ||
*/ | ||
public static <T> void setAttributes(T target, Map<String, Object> attributes) | ||
throws IllegalArgumentException { | ||
for (Map.Entry<String, Object> attribute : attributes.entrySet()) { | ||
try { | ||
PropertyUtils.setProperty(target, attribute.getKey(), attribute.getValue()); | ||
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.