Skip to content
Open
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
31 changes: 31 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,39 @@
<version>0.5.9</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>1.4.10</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
<version>1.3.9</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>1.4.10</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>${project.basedir}/../languages/core/</directory>
Expand Down
43 changes: 39 additions & 4 deletions core/src/main/java/co/aikar/commands/RegisteredCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
import co.aikar.commands.annotation.Private;
import co.aikar.commands.annotation.Syntax;
import co.aikar.commands.contexts.ContextResolver;
import co.aikar.commands.kotlin.JavaContinuation;
import co.aikar.commands.kotlin.ThreadLocalStackRestorerKt;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.CoroutineContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -71,6 +76,7 @@ public class RegisteredCommand<CEC extends CommandExecutionContext<CEC, ? extend
public String helpSearchTags;

boolean isPrivate;
boolean isKotlinSuspendingFunction;

final int requiredResolvers;
final int consumeInputResolvers;
Expand Down Expand Up @@ -99,8 +105,18 @@ public class RegisteredCommand<CEC extends CommandExecutionContext<CEC, ? extend
this.helpSearchTags = annotations.getAnnotationValue(method, HelpSearchTags.class, Annotations.REPLACEMENTS | Annotations.NO_EMPTY);

Parameter[] parameters = method.getParameters();

try {
if (parameters.length > 0 && method.getParameterTypes()[parameters.length - 1] == Continuation.class) {
this.isKotlinSuspendingFunction = true;
}
} catch (NoClassDefFoundError ignored) {
}

int parametersLength = this.isKotlinSuspendingFunction ? parameters.length - 1 : parameters.length;

//noinspection unchecked
this.parameters = new CommandParameter[parameters.length];
this.parameters = new CommandParameter[parametersLength];

this.isPrivate = annotations.hasAnnotation(method, Private.class) || annotations.getAnnotationFromClass(scope.getClass(), Private.class) != null;

Expand All @@ -111,8 +127,8 @@ public class RegisteredCommand<CEC extends CommandExecutionContext<CEC, ? extend
StringBuilder syntaxBuilder = new StringBuilder(64);

CommandParameter<CEC> previousParam = null;
for (int i = 0; i < parameters.length; i++) {
CommandParameter<CEC> parameter = this.parameters[i] = new CommandParameter<>(this, parameters[i], i, i == parameters.length - 1);
for (int i = 0; i < parametersLength; i++) {
CommandParameter<CEC> parameter = this.parameters[i] = new CommandParameter<>(this, parameters[i], i, i == parametersLength - 1);
if (previousParam != null) {
previousParam.setNextParam(parameter);
}
Expand Down Expand Up @@ -157,7 +173,26 @@ void invoke(CommandIssuer sender, List<String> args, CommandOperationContext con
Map<String, Object> passedArgs = resolveContexts(sender, args);
if (passedArgs == null) return;

Object obj = method.invoke(scope, passedArgs.values().toArray());
Object[] methodArgs = passedArgs.values().toArray();
if (isKotlinSuspendingFunction) {
Object[] methodArgsCopy = new Object[methodArgs.length + 1];
System.arraycopy(methodArgs, 0, methodArgsCopy, 0, methodArgs.length);
methodArgs = methodArgsCopy;

CoroutineContext contextRestorer = ThreadLocalStackRestorerKt.toThreadLocalStackRestorerOrEmptyContext(CommandManager.commandOperationContext, context);
methodArgs[methodArgs.length - 1] = new JavaContinuation<Object>(contextRestorer) {
@Override
public void resumeWithException(@NotNull Throwable exception) {
handleException(sender, args, exception);
}

@Override
public void resume(Object result) {
}
};
}

Object obj = method.invoke(scope, methodArgs);
if (obj instanceof CompletableFuture) {
CompletableFuture<?> future = (CompletableFuture) obj;
future.exceptionally(t -> {
Expand Down
37 changes: 37 additions & 0 deletions core/src/main/java/co/aikar/commands/kotlin/JavaContinuation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2016-2020 Daniel Ennis (Aikar) - MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package co.aikar.commands.kotlin

import kotlin.coroutines.Continuation
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

abstract class JavaContinuation<T>(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<T> {
override fun resumeWith(result: Result<T>) {
result.fold(::resume, ::resumeWithException)
}

abstract fun resume(result: T)
abstract fun resumeWithException(exception: Throwable)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2016-2020 Daniel Ennis (Aikar) - MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package co.aikar.commands.kotlin

import kotlinx.coroutines.ThreadContextElement
import java.util.*
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

class ThreadLocalStackRestorer<T> @JvmOverloads constructor(
private val localStack: ThreadLocal<Stack<T>>,
private val data: T = localStack.get().peek()
) : ThreadContextElement<Unit> {

inner class Key : CoroutineContext.Key<ThreadLocalStackRestorer<T>>

override val key: CoroutineContext.Key<*> = Key()

override fun restoreThreadContext(context: CoroutineContext, oldState: Unit) {
localStack.get().pop()
}

override fun updateThreadContext(context: CoroutineContext) {
localStack.get().push(this.data)
}
}

// This method is required, because we can't have a local variable
// in code where we're not sure the class (CoroutineContext) exists at runtime.
// If it doesn't, loading the entire class fails before we have a chance to catch it.
@JvmOverloads
fun <T> ThreadLocal<Stack<T>>.toThreadLocalStackRestorerOrEmptyContext(data: T = this.get().peek()) =
try {
ThreadLocalStackRestorer(this, data)
} catch (e: NoClassDefFoundError) {
EmptyCoroutineContext
}