diff --git a/build.gradle.kts b/build.gradle.kts index 3c12044..a393873 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "com.mapk" -version = "0.6" +version = "0.8" java { sourceCompatibility = JavaVersion.VERSION_1_8 diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 87b738c..f3d88b1 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 86d08f3..1b16c34 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,5 @@ -#Sun Mar 01 01:39:55 JST 2020 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-bin.zip -zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index af6708f..2fe81a7 100644 --- a/gradlew +++ b/gradlew @@ -1,5 +1,21 @@ #!/usr/bin/env sh +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + ############################################################################## ## ## Gradle start up script for UN*X @@ -28,7 +44,7 @@ APP_NAME="Gradle" APP_BASE_NAME=`basename "$0"` # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m"' +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" @@ -109,8 +125,8 @@ 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 +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then APP_HOME=`cygpath --path --mixed "$APP_HOME"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` JAVACMD=`cygpath --unix "$JAVACMD"` @@ -138,19 +154,19 @@ if $cygwin ; then else eval `echo args$i`="\"$arg\"" fi - i=$((i+1)) + i=`expr $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" ;; + 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 @@ -159,14 +175,9 @@ save () { for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done echo " " } -APP_ARGS=$(save "$@") +APP_ARGS=`save "$@"` # Collect all arguments for the java command, following the shell quoting and substitution rules eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" -# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong -if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then - cd "$(dirname "$0")" -fi - exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat index 6d57edc..9618d8d 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,3 +1,19 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + @if "%DEBUG%" == "" @echo off @rem ########################################################################## @rem @@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% @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="-Xmx64m" +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" @rem Find java.exe if defined JAVA_HOME goto findJavaFromJavaHome diff --git a/src/main/java/com/mapk/core/BucketGenerator.java b/src/main/java/com/mapk/core/BucketGenerator.java index 0e909d5..2f9f6be 100644 --- a/src/main/java/com/mapk/core/BucketGenerator.java +++ b/src/main/java/com/mapk/core/BucketGenerator.java @@ -1,5 +1,6 @@ package com.mapk.core; +import com.mapk.annotations.KParameterRequireNonNull; import kotlin.Pair; import kotlin.reflect.KParameter; import org.jetbrains.annotations.NotNull; @@ -14,12 +15,16 @@ class BucketGenerator { private final List initializeMask; private final int completionValue; + @NotNull + private final List isRequireNonNull; @NotNull private final KParameter[] keyArray; @NotNull private final Object[] valueArray; - BucketGenerator(int capacity, @Nullable Pair instancePair) { + BucketGenerator(List parameters, @Nullable Pair instancePair) { + final int capacity = parameters.size(); + keyArray = new KParameter[capacity]; valueArray = new Object[capacity]; @@ -32,10 +37,15 @@ class BucketGenerator { initializationStatus = 0; } + isRequireNonNull = new ArrayList<>(capacity); initializeMask = new ArrayList<>(capacity); int completionValue = 0; for (int i = 0, mask = 1; i < capacity; i++, mask <<= 1) { + isRequireNonNull.add( + i, + parameters.get(i).getAnnotations().stream().anyMatch(it -> it instanceof KParameterRequireNonNull) + ); initializeMask.add(i, mask); completionValue |= mask; } @@ -48,6 +58,7 @@ ArgumentBucket generate() { return new ArgumentBucket( keyArray.clone(), valueArray.clone(), + isRequireNonNull, initializationStatus, initializeMask, completionValue diff --git a/src/main/kotlin/com/mapk/annotations/KParameterRequireNonNull.kt b/src/main/kotlin/com/mapk/annotations/KParameterRequireNonNull.kt new file mode 100644 index 0000000..4c251bd --- /dev/null +++ b/src/main/kotlin/com/mapk/annotations/KParameterRequireNonNull.kt @@ -0,0 +1,5 @@ +package com.mapk.annotations + +@Target(AnnotationTarget.VALUE_PARAMETER) +@Retention(AnnotationRetention.RUNTIME) +annotation class KParameterRequireNonNull diff --git a/src/main/kotlin/com/mapk/core/ArgumentBucket.kt b/src/main/kotlin/com/mapk/core/ArgumentBucket.kt index 266812d..d17fd06 100644 --- a/src/main/kotlin/com/mapk/core/ArgumentBucket.kt +++ b/src/main/kotlin/com/mapk/core/ArgumentBucket.kt @@ -6,6 +6,7 @@ import kotlin.reflect.KParameter class ArgumentBucket internal constructor( private val keyArray: Array, internal val valueArray: Array, + private val isRequireNonNull: List, private var initializationStatus: Int, private val initializeMask: List, private val completionValue: Int @@ -15,7 +16,7 @@ class ArgumentBucket internal constructor( val isInitialized: Boolean get() = initializationStatus == completionValue - class MutableEntry internal constructor( + class Entry internal constructor( override val key: KParameter, override var value: Any? ) : Map.Entry @@ -37,7 +38,7 @@ class ArgumentBucket internal constructor( override fun isEmpty(): Boolean = count == 0 override val entries: Set> - get() = keyArray.mapNotNull { it?.let { MutableEntry(it, valueArray[it.index]) } }.toSet() + get() = keyArray.mapNotNull { it?.let { Entry(it, valueArray[it.index]) } }.toSet() override val keys: MutableSet get() = keyArray.filterNotNull().toMutableSet() override val values: MutableCollection @@ -45,6 +46,10 @@ class ArgumentBucket internal constructor( fun putIfAbsent(key: KParameter, value: Any?) { val index = key.index + + // null入力禁止かつnullなら無視する + if (isRequireNonNull[index] && value == null) return + val temp = initializationStatus or initializeMask[index] // 先に入ったものを優先するため、初期化済みなら何もしない diff --git a/src/main/kotlin/com/mapk/core/KFunctionForCall.kt b/src/main/kotlin/com/mapk/core/KFunctionForCall.kt index 51fa697..a8b7050 100644 --- a/src/main/kotlin/com/mapk/core/KFunctionForCall.kt +++ b/src/main/kotlin/com/mapk/core/KFunctionForCall.kt @@ -20,11 +20,9 @@ class KFunctionForCall(internal val function: KFunction, instance: Any? = // この関数には確実にアクセスするためアクセシビリティ書き換え function.isAccessible = true - generator = if (instance != null) { - BucketGenerator(parameters.size, parameters.first { it.kind == KParameter.Kind.INSTANCE } to instance) - } else { - BucketGenerator(parameters.size, null) - } + generator = BucketGenerator( + parameters, instance?.let { parameters.first { param -> param.kind == KParameter.Kind.INSTANCE } to it } + ) } fun getArgumentBucket(): ArgumentBucket = generator.generate() diff --git a/src/test/kotlin/com/mapk/core/ArgumentBucketTest.kt b/src/test/kotlin/com/mapk/core/ArgumentBucketTest.kt index c9f820c..0aaa633 100644 --- a/src/test/kotlin/com/mapk/core/ArgumentBucketTest.kt +++ b/src/test/kotlin/com/mapk/core/ArgumentBucketTest.kt @@ -1,12 +1,16 @@ package com.mapk.core +import com.mapk.annotations.KParameterRequireNonNull import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertNull import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertDoesNotThrow +import org.junit.jupiter.api.assertThrows private fun sampleFunction(arg1: Any?, arg2: Any?, arg3: Any?) { println(arg1) @@ -14,54 +18,88 @@ private fun sampleFunction(arg1: Any?, arg2: Any?, arg3: Any?) { println(arg3) } +private fun sampleAnnotatedFunction(@KParameterRequireNonNull arg1: Any, arg2: Any?) { + println(arg1) + println(arg2) +} + @DisplayName("ArgumentBucketTestのテスト") class ArgumentBucketTest { - private lateinit var argumentBucket: ArgumentBucket + @Nested + @DisplayName("シンプルな呼び出しのテスト") + inner class SimpleTest { + private lateinit var argumentBucket: ArgumentBucket - @BeforeEach - fun beforeEach() { - argumentBucket = KFunctionForCall(::sampleFunction).getArgumentBucket() - } + @BeforeEach + fun beforeEach() { + argumentBucket = KFunctionForCall(::sampleFunction).getArgumentBucket() + } - @Nested - @DisplayName("初期化状態のチェックテスト") - inner class IsInitializedTest { - @Test - @DisplayName("初期化前") - fun isNotInitialized() { - assertFalse(argumentBucket.isInitialized) + @Nested + @DisplayName("初期化状態のチェックテスト") + inner class IsInitializedTest { + @Test + @DisplayName("初期化前") + fun isNotInitialized() { + assertFalse(argumentBucket.isInitialized) + } + + @Test + @DisplayName("初期化後") + fun isInitialized() { + ::sampleFunction.parameters.forEach { + argumentBucket.putIfAbsent(it, object {}) + } + + assertTrue(argumentBucket.isInitialized) + } } - @Test - @DisplayName("初期化後") - fun isInitialized() { - ::sampleFunction.parameters.forEach { - argumentBucket.putIfAbsent(it, object {}) + @Nested + @DisplayName("引数セットのテスト") + inner class SetArgumentTest { + @Test + @DisplayName("正常に追加した場合") + fun setNewArgument() { + val parameter = ::sampleFunction.parameters.first { it.index == 0 } + argumentBucket.putIfAbsent(parameter, "argument") + assertEquals("argument", argumentBucket.getByIndex(0)) } - assertTrue(argumentBucket.isInitialized) + @Test + @DisplayName("同じインデックスに2回追加した場合") + fun setArgumentTwice() { + val parameter = ::sampleFunction.parameters.first { it.index == 0 } + + argumentBucket.putIfAbsent(parameter, "first") + argumentBucket.putIfAbsent(parameter, "second") + assertEquals("first", argumentBucket.getByIndex(0)) + } } } @Nested - @DisplayName("引数セットのテスト") - inner class SetArgumentTest { + @DisplayName("アノテーションを付与した場合のテスト") + inner class AnnotatedParametersTest { @Test - @DisplayName("正常に追加した場合") - fun setNewArgument() { - val parameter = ::sampleFunction.parameters.first { it.index == 0 } - argumentBucket.putIfAbsent(parameter, "argument") - assertEquals("argument", argumentBucket.getByIndex(0)) - } + @DisplayName("non-null要求のテスト") + fun isRequireNonNull() { + val forCall = KFunctionForCall(::sampleAnnotatedFunction) + val argumentBucket = forCall.getArgumentBucket() + val parameters = forCall.parameters - @Test - @DisplayName("同じインデックスに2回追加した場合") - fun setArgumentTwice() { - val parameter = ::sampleFunction.parameters.first { it.index == 0 } + argumentBucket.putIfAbsent(parameters[0], null) + assertThrows { argumentBucket.getByIndex(0) } - argumentBucket.putIfAbsent(parameter, "first") - argumentBucket.putIfAbsent(parameter, "second") - assertEquals("first", argumentBucket.getByIndex(0)) + argumentBucket.putIfAbsent(parameters[0], "input") + assertDoesNotThrow { + assertEquals("input", argumentBucket.getByIndex(0)) + } + + argumentBucket.putIfAbsent(parameters[1], null) + assertDoesNotThrow { + assertNull(argumentBucket.getByIndex(1)) + } } } }