From f598e79540c7b5714e916c2d2d54f21208c9678e Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Fri, 28 Sep 2018 21:12:05 +0800 Subject: [PATCH] JSR 305 intergration #101 --- appveyor.yml | 2 +- pom.xml | 6 +++++ pom4ide.xml | 6 +++++ scripts/common.sh | 4 +-- .../alibaba/ttl/TransmittableThreadLocal.java | 14 ++++++---- .../java/com/alibaba/ttl/TtlCallable.java | 20 +++++++++----- .../java/com/alibaba/ttl/TtlRunnable.java | 23 +++++++++++----- .../java/com/alibaba/ttl/TtlTimerTask.java | 14 +++++++--- .../threadpool/ExecutorServiceTtlWrapper.java | 27 ++++++++++++------- .../ttl/threadpool/ExecutorTtlWrapper.java | 6 +++-- .../ScheduledExecutorServiceTtlWrapper.java | 16 +++++++---- .../alibaba/ttl/threadpool/TtlExecutors.java | 15 +++++++---- .../ttl/threadpool/agent/TtlAgent.java | 11 +++++--- .../ttl/threadpool/agent/TtlTransformer.java | 4 ++- .../refcount/DistributedTracerUseDemo.kt | 8 +++--- ...seDemo_WeakReferenceInsteadOfRefCounter.kt | 2 +- .../java/com/alibaba/ttl/TtlCallableTest.kt | 2 +- .../java/com/alibaba/ttl/TtlRunnableTest.kt | 4 +-- .../ScheduledExecutorServiceTtlWrapperTest.kt | 2 +- 19 files changed, 125 insertions(+), 61 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 7bb074c58..d63d053ff 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -27,7 +27,7 @@ install: test_script: - ./mvnw.cmd clean install --batch-mode - ./mvnw.cmd dependency:copy-dependencies -DincludeScope=test - - rm target/dependency/javassist* + - rm target/dependency/javassist* target/dependency/jsr305-* - echo "Run Agent test" - ps: | $ttl_jar=Get-ChildItem target\transmittable-thread-local-*.jar -Exclude *-sources.jar | Select-Object -ExpandProperty FullName diff --git a/pom.xml b/pom.xml index adcba97fe..0bb53abf4 100644 --- a/pom.xml +++ b/pom.xml @@ -95,6 +95,12 @@ 3.23.1-GA true + + com.google.code.findbugs + jsr305 + 3.0.2 + true + junit diff --git a/pom4ide.xml b/pom4ide.xml index fd84125ab..b48bb166e 100644 --- a/pom4ide.xml +++ b/pom4ide.xml @@ -95,6 +95,12 @@ 3.23.1-GA true + + com.google.code.findbugs + jsr305 + 3.0.2 + true + junit diff --git a/scripts/common.sh b/scripts/common.sh index 9bd42507a..dc15992ad 100644 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -117,13 +117,13 @@ mvnCopyDependencies() { runCmd "${MVN_CMD[@]}" dependency:copy-dependencies -DincludeScope=test || die "fail to mvn copy-dependencies!" # remove repackaged and shaded javassist lib - rm $dependencies_dir/javassist-* + rm "$dependencies_dir"/javassist-* "$dependencies_dir"/jsr305-* } getClasspathOfDependencies() { [ -e "$dependencies_dir" ] || mvnCopyDependencies 1>&2 - echo $dependencies_dir/*.jar | tr ' ' : + echo "$dependencies_dir"/*.jar | tr ' ' : } getClasspathWithoutTtlJar() { diff --git a/src/main/java/com/alibaba/ttl/TransmittableThreadLocal.java b/src/main/java/com/alibaba/ttl/TransmittableThreadLocal.java index 8fe745fdc..3a30f5221 100644 --- a/src/main/java/com/alibaba/ttl/TransmittableThreadLocal.java +++ b/src/main/java/com/alibaba/ttl/TransmittableThreadLocal.java @@ -1,5 +1,7 @@ package com.alibaba.ttl; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -145,7 +147,7 @@ private static void doExecuteCallback(boolean isBefore) { /** * Debug only method! */ - static void dump(String title) { + static void dump(@Nullable String title) { if (title != null && title.length() > 0) { System.out.printf("Start TransmittableThreadLocal[%s] Dump...\n", title); } else { @@ -248,6 +250,7 @@ public static class Transmitter { * @return the captured {@link TransmittableThreadLocal} values * @since 2.3.0 */ + @Nonnull public static Object capture() { Map, Object> captured = new HashMap, Object>(); for (TransmittableThreadLocal threadLocal : holder.get().keySet()) { @@ -265,7 +268,8 @@ public static Object capture() { * @see #capture() * @since 2.3.0 */ - public static Object replay(Object captured) { + @Nonnull + public static Object replay(@Nonnull Object captured) { @SuppressWarnings("unchecked") Map, Object> capturedMap = (Map, Object>) captured; Map, Object> backup = new HashMap, Object>(); @@ -301,7 +305,7 @@ public static Object replay(Object captured) { * @param backup the backup {@link TransmittableThreadLocal} values from {@link Transmitter#replay(Object)} * @since 2.3.0 */ - public static void restore(Object backup) { + public static void restore(@Nonnull Object backup) { @SuppressWarnings("unchecked") Map, Object> backupMap = (Map, Object>) backup; // call afterExecute callback @@ -324,7 +328,7 @@ public static void restore(Object backup) { setTtlValuesTo(backupMap); } - private static void setTtlValuesTo(Map, Object> ttlValues) { + private static void setTtlValuesTo(@Nonnull Map, Object> ttlValues) { for (Map.Entry, Object> entry : ttlValues.entrySet()) { @SuppressWarnings("unchecked") TransmittableThreadLocal threadLocal = (TransmittableThreadLocal) entry.getKey(); @@ -344,7 +348,7 @@ private static void setTtlValuesTo(Map, Object> ttlV * @see #restore(Object) * @since 2.3.1 */ - public static R runSupplierWithCaptured(Object captured, Supplier bizLogic) { + public static R runSupplierWithCaptured(@Nonnull Object captured, Supplier bizLogic) { Object backup = replay(captured); try { return bizLogic.get(); diff --git a/src/main/java/com/alibaba/ttl/TtlCallable.java b/src/main/java/com/alibaba/ttl/TtlCallable.java index 01f4a7e32..fa77b0be7 100644 --- a/src/main/java/com/alibaba/ttl/TtlCallable.java +++ b/src/main/java/com/alibaba/ttl/TtlCallable.java @@ -1,5 +1,7 @@ package com.alibaba.ttl; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -31,7 +33,7 @@ public final class TtlCallable implements Callable { private final Callable callable; private final boolean releaseTtlValueReferenceAfterCall; - private TtlCallable(Callable callable, boolean releaseTtlValueReferenceAfterCall) { + private TtlCallable(@Nonnull Callable callable, boolean releaseTtlValueReferenceAfterCall) { this.capturedRef = new AtomicReference(capture()); this.callable = callable; this.releaseTtlValueReferenceAfterCall = releaseTtlValueReferenceAfterCall; @@ -55,6 +57,7 @@ public V call() throws Exception { } } + @Nonnull public Callable getCallable() { return callable; } @@ -101,7 +104,8 @@ public static TtlCallable get(Callable callable) { * @param releaseTtlValueReferenceAfterCall release TTL value reference after run, avoid memory leak even if {@link TtlRunnable} is referred. * @return Wrapped {@link Callable} */ - public static TtlCallable get(Callable callable, boolean releaseTtlValueReferenceAfterCall) { + @Nullable + public static TtlCallable get(@Nullable Callable callable, boolean releaseTtlValueReferenceAfterCall) { return get(callable, releaseTtlValueReferenceAfterCall, false); } @@ -115,7 +119,8 @@ public static TtlCallable get(Callable callable, boolean releaseTtlVal * @param idempotent is idempotent or not. {@code true} will cover up bugs! DO NOT set, only when you know why. * @return Wrapped {@link Callable} */ - public static TtlCallable get(Callable callable, boolean releaseTtlValueReferenceAfterCall, boolean idempotent) { + @Nullable + public static TtlCallable get(@Nullable Callable callable, boolean releaseTtlValueReferenceAfterCall, boolean idempotent) { if (null == callable) { return null; } @@ -137,7 +142,8 @@ public static TtlCallable get(Callable callable, boolean releaseTtlVal * @param tasks task to be wrapped * @return Wrapped {@link Callable} */ - public static List> gets(Collection> tasks) { + @Nonnull + public static List> gets(@Nullable Collection> tasks) { return gets(tasks, false, false); } @@ -148,7 +154,8 @@ public static List> gets(Collection> ta * @param releaseTtlValueReferenceAfterCall release TTL value reference after run, avoid memory leak even if {@link TtlRunnable} is referred. * @return Wrapped {@link Callable} */ - public static List> gets(Collection> tasks, boolean releaseTtlValueReferenceAfterCall) { + @Nonnull + public static List> gets(@Nullable Collection> tasks, boolean releaseTtlValueReferenceAfterCall) { return gets(tasks, releaseTtlValueReferenceAfterCall, false); } @@ -160,7 +167,8 @@ public static List> gets(Collection> ta * @param idempotent is idempotent or not. {@code true} will cover up bugs! DO NOT set, only when you know why. * @return Wrapped {@link Callable} */ - public static List> gets(Collection> tasks, boolean releaseTtlValueReferenceAfterCall, boolean idempotent) { + @Nonnull + public static List> gets(@Nullable Collection> tasks, boolean releaseTtlValueReferenceAfterCall, boolean idempotent) { if (null == tasks) { return Collections.emptyList(); } diff --git a/src/main/java/com/alibaba/ttl/TtlRunnable.java b/src/main/java/com/alibaba/ttl/TtlRunnable.java index f718b6790..3132f2f35 100644 --- a/src/main/java/com/alibaba/ttl/TtlRunnable.java +++ b/src/main/java/com/alibaba/ttl/TtlRunnable.java @@ -1,5 +1,7 @@ package com.alibaba.ttl; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -28,7 +30,7 @@ public final class TtlRunnable implements Runnable { private final Runnable runnable; private final boolean releaseTtlValueReferenceAfterRun; - private TtlRunnable(Runnable runnable, boolean releaseTtlValueReferenceAfterRun) { + private TtlRunnable(@Nonnull Runnable runnable, boolean releaseTtlValueReferenceAfterRun) { this.capturedRef = new AtomicReference(capture()); this.runnable = runnable; this.releaseTtlValueReferenceAfterRun = releaseTtlValueReferenceAfterRun; @@ -55,6 +57,7 @@ public void run() { /** * return original/unwrapped {@link Runnable}. */ + @Nonnull public Runnable getRunnable() { return runnable; } @@ -86,7 +89,8 @@ public String toString() { * @return Wrapped {@link Runnable} * @throws IllegalStateException when input is {@link TtlRunnable} already. */ - public static TtlRunnable get(Runnable runnable) { + @Nullable + public static TtlRunnable get(@Nullable Runnable runnable) { return get(runnable, false, false); } @@ -98,7 +102,8 @@ public static TtlRunnable get(Runnable runnable) { * @return Wrapped {@link Runnable} * @throws IllegalStateException when input is {@link TtlRunnable} already. */ - public static TtlRunnable get(Runnable runnable, boolean releaseTtlValueReferenceAfterRun) { + @Nullable + public static TtlRunnable get(@Nullable Runnable runnable, boolean releaseTtlValueReferenceAfterRun) { return get(runnable, releaseTtlValueReferenceAfterRun, false); } @@ -113,7 +118,8 @@ public static TtlRunnable get(Runnable runnable, boolean releaseTtlValueReferenc * @return Wrapped {@link Runnable} * @throws IllegalStateException when input is {@link TtlRunnable} already and not idempotent. */ - public static TtlRunnable get(Runnable runnable, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) { + @Nullable + public static TtlRunnable get(@Nullable Runnable runnable, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) { if (null == runnable) { return null; } @@ -136,7 +142,8 @@ public static TtlRunnable get(Runnable runnable, boolean releaseTtlValueReferenc * @return wrapped tasks * @throws IllegalStateException when input is {@link TtlRunnable} already. */ - public static List gets(Collection tasks) { + @Nonnull + public static List gets(@Nullable Collection tasks) { return gets(tasks, false, false); } @@ -148,7 +155,8 @@ public static List gets(Collection tasks) { * @return wrapped tasks * @throws IllegalStateException when input is {@link TtlRunnable} already. */ - public static List gets(Collection tasks, boolean releaseTtlValueReferenceAfterRun) { + @Nonnull + public static List gets(@Nullable Collection tasks, boolean releaseTtlValueReferenceAfterRun) { return gets(tasks, releaseTtlValueReferenceAfterRun, false); } @@ -163,7 +171,8 @@ public static List gets(Collection tasks, boole * @return wrapped tasks * @throws IllegalStateException when input is {@link TtlRunnable} already and not idempotent. */ - public static List gets(Collection tasks, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) { + @Nonnull + public static List gets(@Nullable Collection tasks, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) { if (null == tasks) { return Collections.emptyList(); } diff --git a/src/main/java/com/alibaba/ttl/TtlTimerTask.java b/src/main/java/com/alibaba/ttl/TtlTimerTask.java index d1cafde26..29b059aaf 100644 --- a/src/main/java/com/alibaba/ttl/TtlTimerTask.java +++ b/src/main/java/com/alibaba/ttl/TtlTimerTask.java @@ -1,5 +1,7 @@ package com.alibaba.ttl; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.TimerTask; import java.util.concurrent.atomic.AtomicReference; @@ -28,7 +30,7 @@ public final class TtlTimerTask extends TimerTask { private final TimerTask timerTask; private final boolean releaseTtlValueReferenceAfterRun; - private TtlTimerTask(TimerTask timerTask, boolean releaseTtlValueReferenceAfterRun) { + private TtlTimerTask(@Nonnull TimerTask timerTask, boolean releaseTtlValueReferenceAfterRun) { this.capturedRef = new AtomicReference(capture()); this.timerTask = timerTask; this.releaseTtlValueReferenceAfterRun = releaseTtlValueReferenceAfterRun; @@ -58,6 +60,7 @@ public boolean cancel() { return super.cancel(); } + @Nonnull public TimerTask getTimerTask() { return timerTask; } @@ -70,7 +73,8 @@ public TimerTask getTimerTask() { * @param timerTask input {@link TimerTask} * @return Wrapped {@link TimerTask} */ - public static TtlTimerTask get(TimerTask timerTask) { + @Nullable + public static TtlTimerTask get(@Nullable TimerTask timerTask) { return get(timerTask, false, false); } @@ -83,7 +87,8 @@ public static TtlTimerTask get(TimerTask timerTask) { * @param releaseTtlValueReferenceAfterRun release TTL value reference after run, avoid memory leak even if {@link TtlRunnable} is referred. * @return Wrapped {@link TimerTask} */ - public static TtlTimerTask get(TimerTask timerTask, boolean releaseTtlValueReferenceAfterRun) { + @Nullable + public static TtlTimerTask get(@Nullable TimerTask timerTask, boolean releaseTtlValueReferenceAfterRun) { return get(timerTask, releaseTtlValueReferenceAfterRun, false); } @@ -97,7 +102,8 @@ public static TtlTimerTask get(TimerTask timerTask, boolean releaseTtlValueRefer * @param idempotent is idempotent or not. {@code true} will cover up bugs! DO NOT set, only when you know why. * @return Wrapped {@link TimerTask} */ - public static TtlTimerTask get(TimerTask timerTask, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) { + @Nullable + public static TtlTimerTask get(@Nullable TimerTask timerTask, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) { if (null == timerTask) { return null; } diff --git a/src/main/java/com/alibaba/ttl/threadpool/ExecutorServiceTtlWrapper.java b/src/main/java/com/alibaba/ttl/threadpool/ExecutorServiceTtlWrapper.java index 881adb62e..815aa54bb 100644 --- a/src/main/java/com/alibaba/ttl/threadpool/ExecutorServiceTtlWrapper.java +++ b/src/main/java/com/alibaba/ttl/threadpool/ExecutorServiceTtlWrapper.java @@ -4,6 +4,7 @@ import com.alibaba.ttl.TtlCallable; import com.alibaba.ttl.TtlRunnable; +import javax.annotation.Nonnull; import java.util.Collection; import java.util.List; import java.util.concurrent.*; @@ -19,7 +20,7 @@ class ExecutorServiceTtlWrapper extends ExecutorTtlWrapper implements ExecutorService { private final ExecutorService executorService; - ExecutorServiceTtlWrapper(ExecutorService executorService) { + ExecutorServiceTtlWrapper(@Nonnull ExecutorService executorService) { super(executorService); this.executorService = executorService; } @@ -29,6 +30,7 @@ public void shutdown() { executorService.shutdown(); } + @Nonnull @Override public List shutdownNow() { return executorService.shutdownNow(); @@ -45,45 +47,52 @@ public boolean isTerminated() { } @Override - public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { + public boolean awaitTermination(long timeout, @Nonnull TimeUnit unit) throws InterruptedException { return executorService.awaitTermination(timeout, unit); } + @Nonnull @Override - public Future submit(Callable task) { + public Future submit(@Nonnull Callable task) { return executorService.submit(TtlCallable.get(task)); } + @Nonnull @Override - public Future submit(Runnable task, T result) { + public Future submit(@Nonnull Runnable task, T result) { return executorService.submit(TtlRunnable.get(task), result); } + @Nonnull @Override - public Future submit(Runnable task) { + public Future submit(@Nonnull Runnable task) { return executorService.submit(TtlRunnable.get(task)); } + @Nonnull @Override - public List> invokeAll(Collection> tasks) throws InterruptedException { + public List> invokeAll(@Nonnull Collection> tasks) throws InterruptedException { return executorService.invokeAll(TtlCallable.gets(tasks)); } + @Nonnull @Override - public List> invokeAll(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException { + public List> invokeAll(@Nonnull Collection> tasks, long timeout, @Nonnull TimeUnit unit) throws InterruptedException { return executorService.invokeAll(TtlCallable.gets(tasks), timeout, unit); } + @Nonnull @Override - public T invokeAny(Collection> tasks) throws InterruptedException, ExecutionException { + public T invokeAny(@Nonnull Collection> tasks) throws InterruptedException, ExecutionException { return executorService.invokeAny(TtlCallable.gets(tasks)); } @Override - public T invokeAny(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { + public T invokeAny(@Nonnull Collection> tasks, long timeout, @Nonnull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return executorService.invokeAny(TtlCallable.gets(tasks), timeout, unit); } + @Nonnull @Override public ExecutorService unwrap() { return executorService; diff --git a/src/main/java/com/alibaba/ttl/threadpool/ExecutorTtlWrapper.java b/src/main/java/com/alibaba/ttl/threadpool/ExecutorTtlWrapper.java index d926925c6..ae4f24d01 100644 --- a/src/main/java/com/alibaba/ttl/threadpool/ExecutorTtlWrapper.java +++ b/src/main/java/com/alibaba/ttl/threadpool/ExecutorTtlWrapper.java @@ -3,6 +3,7 @@ import com.alibaba.ttl.TransmittableThreadLocal; import com.alibaba.ttl.TtlRunnable; +import javax.annotation.Nonnull; import java.util.concurrent.Executor; /** @@ -16,15 +17,16 @@ class ExecutorTtlWrapper implements Executor { private final Executor executor; - ExecutorTtlWrapper(Executor executor) { + ExecutorTtlWrapper(@Nonnull Executor executor) { this.executor = executor; } @Override - public void execute(Runnable command) { + public void execute(@Nonnull Runnable command) { executor.execute(TtlRunnable.get(command)); } + @Nonnull public Executor unwrap() { return executor; } diff --git a/src/main/java/com/alibaba/ttl/threadpool/ScheduledExecutorServiceTtlWrapper.java b/src/main/java/com/alibaba/ttl/threadpool/ScheduledExecutorServiceTtlWrapper.java index 6c2641ca7..8266031b0 100644 --- a/src/main/java/com/alibaba/ttl/threadpool/ScheduledExecutorServiceTtlWrapper.java +++ b/src/main/java/com/alibaba/ttl/threadpool/ScheduledExecutorServiceTtlWrapper.java @@ -4,6 +4,7 @@ import com.alibaba.ttl.TtlCallable; import com.alibaba.ttl.TtlRunnable; +import javax.annotation.Nonnull; import java.util.concurrent.*; /** @@ -17,32 +18,37 @@ class ScheduledExecutorServiceTtlWrapper extends ExecutorServiceTtlWrapper implements ScheduledExecutorService { final ScheduledExecutorService scheduledExecutorService; - public ScheduledExecutorServiceTtlWrapper(ScheduledExecutorService scheduledExecutorService) { + public ScheduledExecutorServiceTtlWrapper(@Nonnull ScheduledExecutorService scheduledExecutorService) { super(scheduledExecutorService); this.scheduledExecutorService = scheduledExecutorService; } + @Nonnull @Override - public ScheduledFuture schedule(Runnable command, long delay, TimeUnit unit) { + public ScheduledFuture schedule(@Nonnull Runnable command, long delay, @Nonnull TimeUnit unit) { return scheduledExecutorService.schedule(TtlRunnable.get(command), delay, unit); } + @Nonnull @Override - public ScheduledFuture schedule(Callable callable, long delay, TimeUnit unit) { + public ScheduledFuture schedule(@Nonnull Callable callable, long delay, @Nonnull TimeUnit unit) { return scheduledExecutorService.schedule(TtlCallable.get(callable), delay, unit); } + @Nonnull @Override - public ScheduledFuture scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { + public ScheduledFuture scheduleAtFixedRate(@Nonnull Runnable command, long initialDelay, long period, @Nonnull TimeUnit unit) { return scheduledExecutorService.scheduleAtFixedRate(TtlRunnable.get(command), initialDelay, period, unit); } + @Nonnull @Override - public ScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { + public ScheduledFuture scheduleWithFixedDelay(@Nonnull Runnable command, long initialDelay, long delay, @Nonnull TimeUnit unit) { return scheduledExecutorService.scheduleWithFixedDelay(TtlRunnable.get(command), initialDelay, delay, unit); } @Override + @Nonnull public ScheduledExecutorService unwrap() { return scheduledExecutorService; } diff --git a/src/main/java/com/alibaba/ttl/threadpool/TtlExecutors.java b/src/main/java/com/alibaba/ttl/threadpool/TtlExecutors.java index d82b689c8..4c0e9ed8a 100644 --- a/src/main/java/com/alibaba/ttl/threadpool/TtlExecutors.java +++ b/src/main/java/com/alibaba/ttl/threadpool/TtlExecutors.java @@ -2,6 +2,7 @@ import com.alibaba.ttl.TransmittableThreadLocal; +import javax.annotation.Nullable; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.ScheduledExecutorService; @@ -25,7 +26,8 @@ public final class TtlExecutors { * transmit the {@link TransmittableThreadLocal} from the task submit time of {@link Runnable} * to the execution time of {@link Runnable}. */ - public static Executor getTtlExecutor(Executor executor) { + @Nullable + public static Executor getTtlExecutor(@Nullable Executor executor) { if (null == executor || executor instanceof ExecutorTtlWrapper) { return executor; } @@ -37,7 +39,8 @@ public static Executor getTtlExecutor(Executor executor) { * transmit the {@link TransmittableThreadLocal} from the task submit time of {@link Runnable} or {@link java.util.concurrent.Callable} * to the execution time of {@link Runnable} or {@link java.util.concurrent.Callable}. */ - public static ExecutorService getTtlExecutorService(ExecutorService executorService) { + @Nullable + public static ExecutorService getTtlExecutorService(@Nullable ExecutorService executorService) { if (executorService == null || executorService instanceof ExecutorServiceTtlWrapper) { return executorService; } @@ -49,7 +52,8 @@ public static ExecutorService getTtlExecutorService(ExecutorService executorServ * transmit the {@link TransmittableThreadLocal} from the task submit time of {@link Runnable} or {@link java.util.concurrent.Callable} * to the execution time of {@link Runnable} or {@link java.util.concurrent.Callable}. */ - public static ScheduledExecutorService getTtlScheduledExecutorService(ScheduledExecutorService scheduledExecutorService) { + @Nullable + public static ScheduledExecutorService getTtlScheduledExecutorService(@Nullable ScheduledExecutorService scheduledExecutorService) { if (scheduledExecutorService == null || scheduledExecutorService instanceof ScheduledExecutorServiceTtlWrapper) { return scheduledExecutorService; } @@ -71,7 +75,7 @@ public static ScheduledExecutorService getTtlScheduledExecutorService(ScheduledE * @see #unwrap(Executor) * @since 2.8.0 */ - public static boolean isTtlWrapper(T executor) { + public static boolean isTtlWrapper(@Nullable T executor) { return (executor instanceof ExecutorTtlWrapper); } @@ -91,8 +95,9 @@ public static boolean isTtlWrapper(T executor) { * @see #isTtlWrapper(Executor) * @since 2.8.0 */ + @Nullable @SuppressWarnings("unchecked") - public static T unwrap(T executor) { + public static T unwrap(@Nullable T executor) { if (!isTtlWrapper(executor)) return executor; return (T) ((ExecutorTtlWrapper) executor).unwrap(); diff --git a/src/main/java/com/alibaba/ttl/threadpool/agent/TtlAgent.java b/src/main/java/com/alibaba/ttl/threadpool/agent/TtlAgent.java index 7ce14d812..ec098002d 100644 --- a/src/main/java/com/alibaba/ttl/threadpool/agent/TtlAgent.java +++ b/src/main/java/com/alibaba/ttl/threadpool/agent/TtlAgent.java @@ -7,6 +7,8 @@ import com.alibaba.ttl.threadpool.agent.internal.transformlet.impl.TtlForkJoinTransformlet; import com.alibaba.ttl.threadpool.agent.internal.transformlet.impl.TtlTimerTaskTransformlet; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.Instrumentation; import java.util.ArrayList; @@ -91,7 +93,7 @@ private TtlAgent() { * @see Logger#STDERR * @see Logger#STDOUT */ - public static void premain(String agentArgs, Instrumentation inst) { + public static void premain(String agentArgs, @Nonnull Instrumentation inst) { final Map kvs = splitCommaColonStringToKV(agentArgs); Logger.setLoggerImplType(getLogImplTypeFromAgentArgs(kvs)); @@ -119,13 +121,13 @@ public static void premain(String agentArgs, Instrumentation inst) { } } - private static String getLogImplTypeFromAgentArgs(final Map kvs) { + private static String getLogImplTypeFromAgentArgs(@Nonnull final Map kvs) { return kvs.get(Logger.TTL_AGENT_LOGGER_KEY); } private static final String TTL_AGENT_ENABLE_TIMER_TASK_KEY = "ttl.agent.enable.timer.task"; - private static boolean enableTimerTask(final Map kvs) { + private static boolean enableTimerTask(@Nonnull final Map kvs) { final boolean hasEnableKey = kvs.containsKey(TTL_AGENT_ENABLE_TIMER_TASK_KEY); if (!hasEnableKey) return false; @@ -135,7 +137,8 @@ private static boolean enableTimerTask(final Map kvs) { /** * Split to {@code json} like String({@code "k1:v1,k2:v2"}) to KV map({@code "k1"->"v1", "k2"->"v2"}). */ - static Map splitCommaColonStringToKV(String commaColonString) { + @Nonnull + static Map splitCommaColonStringToKV(@Nullable String commaColonString) { Map ret = new HashMap(); if (commaColonString == null || commaColonString.trim().length() == 0) return ret; diff --git a/src/main/java/com/alibaba/ttl/threadpool/agent/TtlTransformer.java b/src/main/java/com/alibaba/ttl/threadpool/agent/TtlTransformer.java index 00894535f..b8ae28fc8 100644 --- a/src/main/java/com/alibaba/ttl/threadpool/agent/TtlTransformer.java +++ b/src/main/java/com/alibaba/ttl/threadpool/agent/TtlTransformer.java @@ -3,6 +3,8 @@ import com.alibaba.ttl.threadpool.agent.internal.logging.Logger; import com.alibaba.ttl.threadpool.agent.internal.transformlet.JavassistTransformlet; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.lang.instrument.ClassFileTransformer; import java.security.ProtectionDomain; import java.util.ArrayList; @@ -34,7 +36,7 @@ public class TtlTransformer implements ClassFileTransformer { } @Override - public final byte[] transform(final ClassLoader loader, final String classFile, final Class classBeingRedefined, + public final byte[] transform(@Nonnull final ClassLoader loader, @Nullable final String classFile, final Class classBeingRedefined, final ProtectionDomain protectionDomain, final byte[] classFileBuffer) { try { // Lambda has no class file, no need to transform, just return. diff --git a/src/test/java/com/alibaba/demo/distributed_tracer/refcount/DistributedTracerUseDemo.kt b/src/test/java/com/alibaba/demo/distributed_tracer/refcount/DistributedTracerUseDemo.kt index c57662a1e..095f3c146 100644 --- a/src/test/java/com/alibaba/demo/distributed_tracer/refcount/DistributedTracerUseDemo.kt +++ b/src/test/java/com/alibaba/demo/distributed_tracer/refcount/DistributedTracerUseDemo.kt @@ -43,11 +43,9 @@ private fun rpcInvokeIn() { decreaseSpanIdRefCount() } -private val executorService = TtlExecutors.getTtlExecutorService( - Executors.newFixedThreadPool(1) { r: Runnable -> - Thread(r, "Executors").apply { isDaemon = true } - } -) +private val executorService = Executors.newFixedThreadPool(1) { r: Runnable -> + Thread(r, "Executors").apply { isDaemon = true } +}.let { TtlExecutors.getTtlExecutorService(it) }!! private fun syncMethod() { // async call by TTL Executor, Test OK! diff --git a/src/test/java/com/alibaba/demo/distributed_tracer/weakref/DistributedTracerUseDemo_WeakReferenceInsteadOfRefCounter.kt b/src/test/java/com/alibaba/demo/distributed_tracer/weakref/DistributedTracerUseDemo_WeakReferenceInsteadOfRefCounter.kt index 66ee80aa6..4439a4e4b 100644 --- a/src/test/java/com/alibaba/demo/distributed_tracer/weakref/DistributedTracerUseDemo_WeakReferenceInsteadOfRefCounter.kt +++ b/src/test/java/com/alibaba/demo/distributed_tracer/weakref/DistributedTracerUseDemo_WeakReferenceInsteadOfRefCounter.kt @@ -17,7 +17,7 @@ private val executorService: ExecutorService = Executors.newFixedThreadPool(1) { // ensure threads in pool is pre-created. expandThreadPool(it) TtlExecutors.getTtlExecutorService(it) -} +}!! /** * DistributedTracer(DT) use demo. diff --git a/src/test/java/com/alibaba/ttl/TtlCallableTest.kt b/src/test/java/com/alibaba/ttl/TtlCallableTest.kt index 935cdaa7f..63e8acd13 100644 --- a/src/test/java/com/alibaba/ttl/TtlCallableTest.kt +++ b/src/test/java/com/alibaba/ttl/TtlCallableTest.kt @@ -94,7 +94,7 @@ class TtlCallableTest { val ttlInstances = createParentTtlInstances() val call = Call("1", ttlInstances) - val ttlCallable = TtlCallable.get(call, true) + val ttlCallable = TtlCallable.get(call, true)!! assertSame(call, ttlCallable.callable) assertEquals("ok", executorService.submit(ttlCallable).get()) diff --git a/src/test/java/com/alibaba/ttl/TtlRunnableTest.kt b/src/test/java/com/alibaba/ttl/TtlRunnableTest.kt index 6603a2b1b..dd3404667 100644 --- a/src/test/java/com/alibaba/ttl/TtlRunnableTest.kt +++ b/src/test/java/com/alibaba/ttl/TtlRunnableTest.kt @@ -24,7 +24,7 @@ class TtlRunnableTest { val ttlInstances = createParentTtlInstances() val task = Task("1", ttlInstances) - val ttlRunnable = TtlRunnable.get(task) + val ttlRunnable = TtlRunnable.get(task)!! // create after new Task, won't see parent value in in task! createParentTtlInstancesAfterCreateChild(ttlInstances) @@ -204,7 +204,7 @@ class TtlRunnableTest { @Test fun test_get_same() { val task = Task("1") - val ttlRunnable = TtlRunnable.get(task) + val ttlRunnable = TtlRunnable.get(task)!! assertSame(task, ttlRunnable.runnable) } diff --git a/src/test/java/com/alibaba/ttl/threadpool/ScheduledExecutorServiceTtlWrapperTest.kt b/src/test/java/com/alibaba/ttl/threadpool/ScheduledExecutorServiceTtlWrapperTest.kt index 1f1e705dc..c54b159ae 100644 --- a/src/test/java/com/alibaba/ttl/threadpool/ScheduledExecutorServiceTtlWrapperTest.kt +++ b/src/test/java/com/alibaba/ttl/threadpool/ScheduledExecutorServiceTtlWrapperTest.kt @@ -235,7 +235,7 @@ class ScheduledExecutorServiceTtlWrapperTest { it.setKeepAliveTime(10, TimeUnit.SECONDS) expandThreadPool(it) TtlExecutors.getTtlScheduledExecutorService(it) - } + }!! @AfterClass @Suppress("unused")