Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.

Commit c1c8469

Browse files
authored
Merge pull request #10 from k163377/require_non_null_feature
Add annotation that means "Use Non Null argument".
2 parents e8170ad + 4d2aa24 commit c1c8469

File tree

10 files changed

+148
-65
lines changed

10 files changed

+148
-65
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = "com.mapk"
9-
version = "0.6"
9+
version = "0.8"
1010

1111
java {
1212
sourceCompatibility = JavaVersion.VERSION_1_8

gradle/wrapper/gradle-wrapper.jar

3.42 KB
Binary file not shown.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#Sun Mar 01 01:39:55 JST 2020
21
distributionBase=GRADLE_USER_HOME
32
distributionPath=wrapper/dists
43
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-bin.zip
5-
zipStorePath=wrapper/dists
64
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

gradlew

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
#!/usr/bin/env sh
22

3+
#
4+
# Copyright 2015 the original author or authors.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# https://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
319
##############################################################################
420
##
521
## Gradle start up script for UN*X
@@ -28,7 +44,7 @@ APP_NAME="Gradle"
2844
APP_BASE_NAME=`basename "$0"`
2945

3046
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31-
DEFAULT_JVM_OPTS='"-Xmx64m"'
47+
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
3248

3349
# Use the maximum available, or set MAX_FD != -1 to use that value.
3450
MAX_FD="maximum"
@@ -109,8 +125,8 @@ if $darwin; then
109125
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110126
fi
111127

112-
# For Cygwin, switch paths to Windows format before running java
113-
if $cygwin ; then
128+
# For Cygwin or MSYS, switch paths to Windows format before running java
129+
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
114130
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115131
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116132
JAVACMD=`cygpath --unix "$JAVACMD"`
@@ -138,19 +154,19 @@ if $cygwin ; then
138154
else
139155
eval `echo args$i`="\"$arg\""
140156
fi
141-
i=$((i+1))
157+
i=`expr $i + 1`
142158
done
143159
case $i in
144-
(0) set -- ;;
145-
(1) set -- "$args0" ;;
146-
(2) set -- "$args0" "$args1" ;;
147-
(3) set -- "$args0" "$args1" "$args2" ;;
148-
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149-
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150-
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151-
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152-
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153-
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
160+
0) set -- ;;
161+
1) set -- "$args0" ;;
162+
2) set -- "$args0" "$args1" ;;
163+
3) set -- "$args0" "$args1" "$args2" ;;
164+
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
165+
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
166+
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
167+
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
168+
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
169+
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154170
esac
155171
fi
156172

@@ -159,14 +175,9 @@ save () {
159175
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160176
echo " "
161177
}
162-
APP_ARGS=$(save "$@")
178+
APP_ARGS=`save "$@"`
163179

164180
# Collect all arguments for the java command, following the shell quoting and substitution rules
165181
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166182

167-
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168-
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169-
cd "$(dirname "$0")"
170-
fi
171-
172183
exec "$JAVACMD" "$@"

gradlew.bat

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
@rem
2+
@rem Copyright 2015 the original author or authors.
3+
@rem
4+
@rem Licensed under the Apache License, Version 2.0 (the "License");
5+
@rem you may not use this file except in compliance with the License.
6+
@rem You may obtain a copy of the License at
7+
@rem
8+
@rem https://www.apache.org/licenses/LICENSE-2.0
9+
@rem
10+
@rem Unless required by applicable law or agreed to in writing, software
11+
@rem distributed under the License is distributed on an "AS IS" BASIS,
12+
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
@rem See the License for the specific language governing permissions and
14+
@rem limitations under the License.
15+
@rem
16+
117
@if "%DEBUG%" == "" @echo off
218
@rem ##########################################################################
319
@rem
@@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0
1430
set APP_HOME=%DIRNAME%
1531

1632
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17-
set DEFAULT_JVM_OPTS="-Xmx64m"
33+
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
1834

1935
@rem Find java.exe
2036
if defined JAVA_HOME goto findJavaFromJavaHome

src/main/java/com/mapk/core/BucketGenerator.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mapk.core;
22

3+
import com.mapk.annotations.KParameterRequireNonNull;
34
import kotlin.Pair;
45
import kotlin.reflect.KParameter;
56
import org.jetbrains.annotations.NotNull;
@@ -14,12 +15,16 @@ class BucketGenerator {
1415
private final List<Integer> initializeMask;
1516
private final int completionValue;
1617

18+
@NotNull
19+
private final List<Boolean> isRequireNonNull;
1720
@NotNull
1821
private final KParameter[] keyArray;
1922
@NotNull
2023
private final Object[] valueArray;
2124

22-
BucketGenerator(int capacity, @Nullable Pair<KParameter, Object> instancePair) {
25+
BucketGenerator(List<KParameter> parameters, @Nullable Pair<KParameter, Object> instancePair) {
26+
final int capacity = parameters.size();
27+
2328
keyArray = new KParameter[capacity];
2429
valueArray = new Object[capacity];
2530

@@ -32,10 +37,15 @@ class BucketGenerator {
3237
initializationStatus = 0;
3338
}
3439

40+
isRequireNonNull = new ArrayList<>(capacity);
3541
initializeMask = new ArrayList<>(capacity);
3642
int completionValue = 0;
3743

3844
for (int i = 0, mask = 1; i < capacity; i++, mask <<= 1) {
45+
isRequireNonNull.add(
46+
i,
47+
parameters.get(i).getAnnotations().stream().anyMatch(it -> it instanceof KParameterRequireNonNull)
48+
);
3949
initializeMask.add(i, mask);
4050
completionValue |= mask;
4151
}
@@ -48,6 +58,7 @@ ArgumentBucket generate() {
4858
return new ArgumentBucket(
4959
keyArray.clone(),
5060
valueArray.clone(),
61+
isRequireNonNull,
5162
initializationStatus,
5263
initializeMask,
5364
completionValue
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.mapk.annotations
2+
3+
@Target(AnnotationTarget.VALUE_PARAMETER)
4+
@Retention(AnnotationRetention.RUNTIME)
5+
annotation class KParameterRequireNonNull

src/main/kotlin/com/mapk/core/ArgumentBucket.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import kotlin.reflect.KParameter
66
class ArgumentBucket internal constructor(
77
private val keyArray: Array<KParameter?>,
88
internal val valueArray: Array<Any?>,
9+
private val isRequireNonNull: List<Boolean>,
910
private var initializationStatus: Int,
1011
private val initializeMask: List<Int>,
1112
private val completionValue: Int
@@ -15,7 +16,7 @@ class ArgumentBucket internal constructor(
1516

1617
val isInitialized: Boolean get() = initializationStatus == completionValue
1718

18-
class MutableEntry internal constructor(
19+
class Entry internal constructor(
1920
override val key: KParameter,
2021
override var value: Any?
2122
) : Map.Entry<KParameter, Any?>
@@ -37,14 +38,18 @@ class ArgumentBucket internal constructor(
3738
override fun isEmpty(): Boolean = count == 0
3839

3940
override val entries: Set<Map.Entry<KParameter, Any?>>
40-
get() = keyArray.mapNotNull { it?.let { MutableEntry(it, valueArray[it.index]) } }.toSet()
41+
get() = keyArray.mapNotNull { it?.let { Entry(it, valueArray[it.index]) } }.toSet()
4142
override val keys: MutableSet<KParameter>
4243
get() = keyArray.filterNotNull().toMutableSet()
4344
override val values: MutableCollection<Any?>
4445
get() = valueArray.filterIndexed { i, _ -> initializationStatus and initializeMask[i] != 0 }.toMutableList()
4546

4647
fun putIfAbsent(key: KParameter, value: Any?) {
4748
val index = key.index
49+
50+
// null入力禁止かつnullなら無視する
51+
if (isRequireNonNull[index] && value == null) return
52+
4853
val temp = initializationStatus or initializeMask[index]
4954

5055
// 先に入ったものを優先するため、初期化済みなら何もしない

src/main/kotlin/com/mapk/core/KFunctionForCall.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ class KFunctionForCall<T>(internal val function: KFunction<T>, instance: Any? =
2020
// この関数には確実にアクセスするためアクセシビリティ書き換え
2121
function.isAccessible = true
2222

23-
generator = if (instance != null) {
24-
BucketGenerator(parameters.size, parameters.first { it.kind == KParameter.Kind.INSTANCE } to instance)
25-
} else {
26-
BucketGenerator(parameters.size, null)
27-
}
23+
generator = BucketGenerator(
24+
parameters, instance?.let { parameters.first { param -> param.kind == KParameter.Kind.INSTANCE } to it }
25+
)
2826
}
2927

3028
fun getArgumentBucket(): ArgumentBucket = generator.generate()
Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,105 @@
11
package com.mapk.core
22

3+
import com.mapk.annotations.KParameterRequireNonNull
34
import org.junit.jupiter.api.Assertions.assertEquals
45
import org.junit.jupiter.api.Assertions.assertFalse
6+
import org.junit.jupiter.api.Assertions.assertNull
57
import org.junit.jupiter.api.Assertions.assertTrue
68
import org.junit.jupiter.api.BeforeEach
79
import org.junit.jupiter.api.DisplayName
810
import org.junit.jupiter.api.Nested
911
import org.junit.jupiter.api.Test
12+
import org.junit.jupiter.api.assertDoesNotThrow
13+
import org.junit.jupiter.api.assertThrows
1014

1115
private fun sampleFunction(arg1: Any?, arg2: Any?, arg3: Any?) {
1216
println(arg1)
1317
println(arg2)
1418
println(arg3)
1519
}
1620

21+
private fun sampleAnnotatedFunction(@KParameterRequireNonNull arg1: Any, arg2: Any?) {
22+
println(arg1)
23+
println(arg2)
24+
}
25+
1726
@DisplayName("ArgumentBucketTestのテスト")
1827
class ArgumentBucketTest {
19-
private lateinit var argumentBucket: ArgumentBucket
28+
@Nested
29+
@DisplayName("シンプルな呼び出しのテスト")
30+
inner class SimpleTest {
31+
private lateinit var argumentBucket: ArgumentBucket
2032

21-
@BeforeEach
22-
fun beforeEach() {
23-
argumentBucket = KFunctionForCall(::sampleFunction).getArgumentBucket()
24-
}
33+
@BeforeEach
34+
fun beforeEach() {
35+
argumentBucket = KFunctionForCall(::sampleFunction).getArgumentBucket()
36+
}
2537

26-
@Nested
27-
@DisplayName("初期化状態のチェックテスト")
28-
inner class IsInitializedTest {
29-
@Test
30-
@DisplayName("初期化前")
31-
fun isNotInitialized() {
32-
assertFalse(argumentBucket.isInitialized)
38+
@Nested
39+
@DisplayName("初期化状態のチェックテスト")
40+
inner class IsInitializedTest {
41+
@Test
42+
@DisplayName("初期化前")
43+
fun isNotInitialized() {
44+
assertFalse(argumentBucket.isInitialized)
45+
}
46+
47+
@Test
48+
@DisplayName("初期化後")
49+
fun isInitialized() {
50+
::sampleFunction.parameters.forEach {
51+
argumentBucket.putIfAbsent(it, object {})
52+
}
53+
54+
assertTrue(argumentBucket.isInitialized)
55+
}
3356
}
3457

35-
@Test
36-
@DisplayName("初期化後")
37-
fun isInitialized() {
38-
::sampleFunction.parameters.forEach {
39-
argumentBucket.putIfAbsent(it, object {})
58+
@Nested
59+
@DisplayName("引数セットのテスト")
60+
inner class SetArgumentTest {
61+
@Test
62+
@DisplayName("正常に追加した場合")
63+
fun setNewArgument() {
64+
val parameter = ::sampleFunction.parameters.first { it.index == 0 }
65+
argumentBucket.putIfAbsent(parameter, "argument")
66+
assertEquals("argument", argumentBucket.getByIndex(0))
4067
}
4168

42-
assertTrue(argumentBucket.isInitialized)
69+
@Test
70+
@DisplayName("同じインデックスに2回追加した場合")
71+
fun setArgumentTwice() {
72+
val parameter = ::sampleFunction.parameters.first { it.index == 0 }
73+
74+
argumentBucket.putIfAbsent(parameter, "first")
75+
argumentBucket.putIfAbsent(parameter, "second")
76+
assertEquals("first", argumentBucket.getByIndex(0))
77+
}
4378
}
4479
}
4580

4681
@Nested
47-
@DisplayName("引数セットのテスト")
48-
inner class SetArgumentTest {
82+
@DisplayName("アノテーションを付与した場合のテスト")
83+
inner class AnnotatedParametersTest {
4984
@Test
50-
@DisplayName("正常に追加した場合")
51-
fun setNewArgument() {
52-
val parameter = ::sampleFunction.parameters.first { it.index == 0 }
53-
argumentBucket.putIfAbsent(parameter, "argument")
54-
assertEquals("argument", argumentBucket.getByIndex(0))
55-
}
85+
@DisplayName("non-null要求のテスト")
86+
fun isRequireNonNull() {
87+
val forCall = KFunctionForCall(::sampleAnnotatedFunction)
88+
val argumentBucket = forCall.getArgumentBucket()
89+
val parameters = forCall.parameters
5690

57-
@Test
58-
@DisplayName("同じインデックスに2回追加した場合")
59-
fun setArgumentTwice() {
60-
val parameter = ::sampleFunction.parameters.first { it.index == 0 }
91+
argumentBucket.putIfAbsent(parameters[0], null)
92+
assertThrows<IllegalStateException> { argumentBucket.getByIndex(0) }
6193

62-
argumentBucket.putIfAbsent(parameter, "first")
63-
argumentBucket.putIfAbsent(parameter, "second")
64-
assertEquals("first", argumentBucket.getByIndex(0))
94+
argumentBucket.putIfAbsent(parameters[0], "input")
95+
assertDoesNotThrow {
96+
assertEquals("input", argumentBucket.getByIndex(0))
97+
}
98+
99+
argumentBucket.putIfAbsent(parameters[1], null)
100+
assertDoesNotThrow {
101+
assertNull(argumentBucket.getByIndex(1))
102+
}
65103
}
66104
}
67105
}

0 commit comments

Comments
 (0)