Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "com.mapk"
version = "0.6"
version = "0.8"

java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
51 changes: 31 additions & 20 deletions gradlew
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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

Expand All @@ -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" "$@"
18 changes: 17 additions & 1 deletion gradlew.bat
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/mapk/core/BucketGenerator.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,12 +15,16 @@ class BucketGenerator {
private final List<Integer> initializeMask;
private final int completionValue;

@NotNull
private final List<Boolean> isRequireNonNull;
@NotNull
private final KParameter[] keyArray;
@NotNull
private final Object[] valueArray;

BucketGenerator(int capacity, @Nullable Pair<KParameter, Object> instancePair) {
BucketGenerator(List<KParameter> parameters, @Nullable Pair<KParameter, Object> instancePair) {
final int capacity = parameters.size();

keyArray = new KParameter[capacity];
valueArray = new Object[capacity];

Expand All @@ -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;
}
Expand All @@ -48,6 +58,7 @@ ArgumentBucket generate() {
return new ArgumentBucket(
keyArray.clone(),
valueArray.clone(),
isRequireNonNull,
initializationStatus,
initializeMask,
completionValue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.mapk.annotations

@Target(AnnotationTarget.VALUE_PARAMETER)
@Retention(AnnotationRetention.RUNTIME)
annotation class KParameterRequireNonNull
9 changes: 7 additions & 2 deletions src/main/kotlin/com/mapk/core/ArgumentBucket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlin.reflect.KParameter
class ArgumentBucket internal constructor(
private val keyArray: Array<KParameter?>,
internal val valueArray: Array<Any?>,
private val isRequireNonNull: List<Boolean>,
private var initializationStatus: Int,
private val initializeMask: List<Int>,
private val completionValue: Int
Expand All @@ -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<KParameter, Any?>
Expand All @@ -37,14 +38,18 @@ class ArgumentBucket internal constructor(
override fun isEmpty(): Boolean = count == 0

override val entries: Set<Map.Entry<KParameter, Any?>>
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<KParameter>
get() = keyArray.filterNotNull().toMutableSet()
override val values: MutableCollection<Any?>
get() = valueArray.filterIndexed { i, _ -> initializationStatus and initializeMask[i] != 0 }.toMutableList()

fun putIfAbsent(key: KParameter, value: Any?) {
val index = key.index

// null入力禁止かつnullなら無視する
if (isRequireNonNull[index] && value == null) return

val temp = initializationStatus or initializeMask[index]

// 先に入ったものを優先するため、初期化済みなら何もしない
Expand Down
8 changes: 3 additions & 5 deletions src/main/kotlin/com/mapk/core/KFunctionForCall.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ class KFunctionForCall<T>(internal val function: KFunction<T>, 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()
Expand Down
104 changes: 71 additions & 33 deletions src/test/kotlin/com/mapk/core/ArgumentBucketTest.kt
Original file line number Diff line number Diff line change
@@ -1,67 +1,105 @@
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)
println(arg2)
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<IllegalStateException> { 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))
}
}
}
}