diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..2322723c Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..cbf550d4 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Thu Sep 03 20:14:27 PDT 2015 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-bin.zip diff --git a/gradlew b/gradlew new file mode 100755 index 00000000..91a7e269 --- /dev/null +++ b/gradlew @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# For Cygwin, ensure paths are in UNIX format before anything is touched. +if $cygwin ; then + [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` +fi + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >&- +APP_HOME="`pwd -P`" +cd "$SAVED" >&- + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 00000000..8a0b282a --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/reflekt/src/main/java/io/advantageous/boon/core/reflection/BeanUtils.java b/reflekt/src/main/java/io/advantageous/boon/core/reflection/BeanUtils.java index e25fd4dc..ccb164a6 100644 --- a/reflekt/src/main/java/io/advantageous/boon/core/reflection/BeanUtils.java +++ b/reflekt/src/main/java/io/advantageous/boon/core/reflection/BeanUtils.java @@ -91,7 +91,7 @@ private static Map getPropertyFieldAccessMap( Class claz public static FieldAccess getField( Class clazz, String name ) { - Map fields = getPropertyFieldAccessMap( clazz ); + Map fields = getPropertyFieldAccessMap(clazz); if ( fields != null) { return fields.get(name); } else { @@ -711,7 +711,7 @@ public static Class getPropertyType( final Object root, final String property Map fields = getPropertyFieldAccessMap( root.getClass() ); - FieldAccess field = fields.get( property ); + FieldAccess field = fields.get(property); return field.type(); } @@ -722,7 +722,7 @@ public static T idxGeneric( Class t, Object object, final String path ) { String[] properties = propertyPathAsStringArray(path); - return ( T ) getPropertyValue( object, properties ); + return ( T ) getPropertyValue(object, properties); } @@ -766,7 +766,7 @@ public static Object idx( Object object, String path ) { String[] properties = propertyPathAsStringArray(path); - return getPropertyValue( object, properties ); + return getPropertyValue(object, properties); } @@ -959,7 +959,7 @@ public static Object getProp( Object object, final String property ) { if ( !fields.containsKey( property ) ) { return null; } else { - return fields.get( property ).getValue( object ); + return fields.get( property ).getValue(object); } } @@ -982,15 +982,15 @@ public static int getPropertyInt( final Object root, final String... properties Object object = baseForGetProperty( root, properties ); - Map fields = getFieldsFromObject( object ); + Map fields = getFieldsFromObject(object); - FieldAccess field = fields.get( lastProperty ); + FieldAccess field = fields.get(lastProperty); if ( field.type() == Typ.intgr ) { return field.getInt( object ); } else { - return Conversions.toInt( field.getValue( object ) ); + return Conversions.toInt(field.getValue(object)); } } @@ -1108,7 +1108,7 @@ private static String getPropertyString(Object root, String[] properties) { - Object object = baseForGetProperty( root, properties ); + Object object = baseForGetProperty(root, properties); Map fields = getFieldsFromObject( object ); @@ -1142,7 +1142,7 @@ public static byte getPropertyByte( final Object root, final String... propertie } - Object object = baseForGetProperty( root, properties ); + Object object = baseForGetProperty(root, properties); Map fields = getFieldsFromObject( object ); @@ -1151,7 +1151,7 @@ public static byte getPropertyByte( final Object root, final String... propertie if ( field.type() == Typ.bt ) { return field.getByte( object ); } else { - return Conversions.toByte( field.getValue( object ) ); + return Conversions.toByte(field.getValue(object)); } } @@ -1237,7 +1237,7 @@ public static Class getPropertyPathType( final Object root, Object object = baseForGetProperty( root, properties ); - Map fields = getFieldsFromObject( object ); + Map fields = getFieldsFromObject(object); final String lastProperty = properties[ properties.length - 1 ]; FieldAccess field = fields.get( lastProperty ); @@ -1291,7 +1291,7 @@ public static Class idxType( Object object, String path ) { String[] properties = propertyPathAsStringArray(path); - return getPropertyPathType( object, properties ); + return getPropertyPathType(object, properties); } @@ -1300,7 +1300,7 @@ public static FieldAccess idxField( Object object, String path ) { String[] properties = propertyPathAsStringArray(path); - return getPropertyPathField( object, properties ); + return getPropertyPathField(object, properties); } @@ -1469,6 +1469,37 @@ public static Map collectionToMap( String propertyKey, Collection } + + public static void copyPropertiesFromMap( final Object dest, final Map src ) { + + Set> props = src.entrySet(); + for ( Map.Entry entry : props ) { + + if (entry.getValue() instanceof Map) { + + + Object newDest; + if (dest instanceof Map) { + newDest = ((Map) dest).get(entry.getKey()); + } else { + newDest = BeanUtils.getPropByPath(dest, entry.getKey()); + } + + if (newDest == null) { + if (dest instanceof Map) { + newDest = new LinkedHashMap<>(); + ((Map) dest).put(entry.getKey(), newDest); + } + } + final Map newSrc = ((Map) entry.getValue()); + copyPropertiesFromMap(newDest, newSrc); + } else { + setPropertyValue(dest, entry.getValue(), entry.getKey()); + } + } + } + + public static void copyProperties( Object object, Map properties ) { Set> props = properties.entrySet(); diff --git a/reflekt/src/test/java/io/advantageous/boon/core/MapsTest.java b/reflekt/src/test/java/io/advantageous/boon/core/MapsTest.java index ffefbff8..5b627f9e 100644 --- a/reflekt/src/test/java/io/advantageous/boon/core/MapsTest.java +++ b/reflekt/src/test/java/io/advantageous/boon/core/MapsTest.java @@ -29,7 +29,7 @@ package io.advantageous.boon.core; -import io.advantageous.boon.core.*; +import io.advantageous.boon.core.reflection.BeanUtils; import org.junit.Test; import java.util.Arrays; @@ -50,6 +50,94 @@ public class MapsTest { + + + + + @Test + public void mergeMaps() { + Map dest = Maps.map( + "foo", (Object)"bar" + ); + + Map src = Maps.map( + "bar", (Object)"foo" + ); + + BeanUtils.copyPropertiesFromMap(dest, src); + + + puts(Maps.asPrettyJsonString(dest)); + + } + + + @Test + public void mergeMapsNested() { + Map dest = Maps.map( + "foo", (Object)"bar", + "innerMap", Maps.map( + "bar", "baz" + ) + ); + + Map src = Maps.map( + "bar", (Object)"foo", + "innerMap", Maps.map( + "rick", "high" + ), + "mapTwo", Maps.map( + "rick", "high" + ) + + ); + + BeanUtils.copyPropertiesFromMap(dest, src); + + + puts(Maps.asPrettyJsonString(dest)); + + } + + + + @Test + public void mergeMapsDeeplyNested() { + Map dest = Maps.map( + "foo", (Object)"bar", + "innerMap", Maps.map( + "bar", "baz", + "innerMap2", Maps.map( + "how", "deep" + ) + + ) + ); + + Map src = Maps.map( + "bar", (Object)"foo", + "innerMap", Maps.map( + "rick", "high", + "innerMap2", Maps.map( + "so", "deep" + ) + ), + "mapTwo", Maps.map( + "rick", "high" + ) + + ); + + /* Merge the maps. */ + BeanUtils.copyPropertiesFromMap(dest, src); + + String howDeep = BeanUtils.idxStr(dest, "innerMap.innerMap2.so"); + + assertEquals("deep", howDeep); + puts(Maps.asPrettyJsonString(dest)); + + } + @Test public void prettyPrintMap() { Map map = Maps.map(