Skip to content

Commit

Permalink
JSR 305 intergration #101
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Sep 29, 2018
1 parent 7d3c6dd commit f598e79
Show file tree
Hide file tree
Showing 19 changed files with 125 additions and 61 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@
<version>3.23.1-GA</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
<optional>true</optional>
</dependency>
<!-- Testing frameworks and related dependencies -->
<dependency>
<groupId>junit</groupId>
Expand Down
6 changes: 6 additions & 0 deletions pom4ide.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@
<version>3.23.1-GA</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
<optional>true</optional>
</dependency>
<!-- Testing frameworks and related dependencies -->
<dependency>
<groupId>junit</groupId>
Expand Down
4 changes: 2 additions & 2 deletions scripts/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/com/alibaba/ttl/TransmittableThreadLocal.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -248,6 +250,7 @@ public static class Transmitter {
* @return the captured {@link TransmittableThreadLocal} values
* @since 2.3.0
*/
@Nonnull
public static Object capture() {
Map<TransmittableThreadLocal<?>, Object> captured = new HashMap<TransmittableThreadLocal<?>, Object>();
for (TransmittableThreadLocal<?> threadLocal : holder.get().keySet()) {
Expand All @@ -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<TransmittableThreadLocal<?>, Object> capturedMap = (Map<TransmittableThreadLocal<?>, Object>) captured;
Map<TransmittableThreadLocal<?>, Object> backup = new HashMap<TransmittableThreadLocal<?>, Object>();
Expand Down Expand Up @@ -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<TransmittableThreadLocal<?>, Object> backupMap = (Map<TransmittableThreadLocal<?>, Object>) backup;
// call afterExecute callback
Expand All @@ -324,7 +328,7 @@ public static void restore(Object backup) {
setTtlValuesTo(backupMap);
}

private static void setTtlValuesTo(Map<TransmittableThreadLocal<?>, Object> ttlValues) {
private static void setTtlValuesTo(@Nonnull Map<TransmittableThreadLocal<?>, Object> ttlValues) {
for (Map.Entry<TransmittableThreadLocal<?>, Object> entry : ttlValues.entrySet()) {
@SuppressWarnings("unchecked")
TransmittableThreadLocal<Object> threadLocal = (TransmittableThreadLocal<Object>) entry.getKey();
Expand All @@ -344,7 +348,7 @@ private static void setTtlValuesTo(Map<TransmittableThreadLocal<?>, Object> ttlV
* @see #restore(Object)
* @since 2.3.1
*/
public static <R> R runSupplierWithCaptured(Object captured, Supplier<R> bizLogic) {
public static <R> R runSupplierWithCaptured(@Nonnull Object captured, Supplier<R> bizLogic) {
Object backup = replay(captured);
try {
return bizLogic.get();
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/com/alibaba/ttl/TtlCallable.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -31,7 +33,7 @@ public final class TtlCallable<V> implements Callable<V> {
private final Callable<V> callable;
private final boolean releaseTtlValueReferenceAfterCall;

private TtlCallable(Callable<V> callable, boolean releaseTtlValueReferenceAfterCall) {
private TtlCallable(@Nonnull Callable<V> callable, boolean releaseTtlValueReferenceAfterCall) {
this.capturedRef = new AtomicReference<Object>(capture());
this.callable = callable;
this.releaseTtlValueReferenceAfterCall = releaseTtlValueReferenceAfterCall;
Expand All @@ -55,6 +57,7 @@ public V call() throws Exception {
}
}

@Nonnull
public Callable<V> getCallable() {
return callable;
}
Expand Down Expand Up @@ -101,7 +104,8 @@ public static <T> TtlCallable<T> get(Callable<T> callable) {
* @param releaseTtlValueReferenceAfterCall release TTL value reference after run, avoid memory leak even if {@link TtlRunnable} is referred.
* @return Wrapped {@link Callable}
*/
public static <T> TtlCallable<T> get(Callable<T> callable, boolean releaseTtlValueReferenceAfterCall) {
@Nullable
public static <T> TtlCallable<T> get(@Nullable Callable<T> callable, boolean releaseTtlValueReferenceAfterCall) {
return get(callable, releaseTtlValueReferenceAfterCall, false);
}

Expand All @@ -115,7 +119,8 @@ public static <T> TtlCallable<T> get(Callable<T> callable, boolean releaseTtlVal
* @param idempotent is idempotent or not. {@code true} will cover up bugs! <b>DO NOT</b> set, only when you know why.
* @return Wrapped {@link Callable}
*/
public static <T> TtlCallable<T> get(Callable<T> callable, boolean releaseTtlValueReferenceAfterCall, boolean idempotent) {
@Nullable
public static <T> TtlCallable<T> get(@Nullable Callable<T> callable, boolean releaseTtlValueReferenceAfterCall, boolean idempotent) {
if (null == callable) {
return null;
}
Expand All @@ -137,7 +142,8 @@ public static <T> TtlCallable<T> get(Callable<T> callable, boolean releaseTtlVal
* @param tasks task to be wrapped
* @return Wrapped {@link Callable}
*/
public static <T> List<TtlCallable<T>> gets(Collection<? extends Callable<T>> tasks) {
@Nonnull
public static <T> List<TtlCallable<T>> gets(@Nullable Collection<? extends Callable<T>> tasks) {
return gets(tasks, false, false);
}

Expand All @@ -148,7 +154,8 @@ public static <T> List<TtlCallable<T>> gets(Collection<? extends Callable<T>> ta
* @param releaseTtlValueReferenceAfterCall release TTL value reference after run, avoid memory leak even if {@link TtlRunnable} is referred.
* @return Wrapped {@link Callable}
*/
public static <T> List<TtlCallable<T>> gets(Collection<? extends Callable<T>> tasks, boolean releaseTtlValueReferenceAfterCall) {
@Nonnull
public static <T> List<TtlCallable<T>> gets(@Nullable Collection<? extends Callable<T>> tasks, boolean releaseTtlValueReferenceAfterCall) {
return gets(tasks, releaseTtlValueReferenceAfterCall, false);
}

Expand All @@ -160,7 +167,8 @@ public static <T> List<TtlCallable<T>> gets(Collection<? extends Callable<T>> ta
* @param idempotent is idempotent or not. {@code true} will cover up bugs! <b>DO NOT</b> set, only when you know why.
* @return Wrapped {@link Callable}
*/
public static <T> List<TtlCallable<T>> gets(Collection<? extends Callable<T>> tasks, boolean releaseTtlValueReferenceAfterCall, boolean idempotent) {
@Nonnull
public static <T> List<TtlCallable<T>> gets(@Nullable Collection<? extends Callable<T>> tasks, boolean releaseTtlValueReferenceAfterCall, boolean idempotent) {
if (null == tasks) {
return Collections.emptyList();
}
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/com/alibaba/ttl/TtlRunnable.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Object>(capture());
this.runnable = runnable;
this.releaseTtlValueReferenceAfterRun = releaseTtlValueReferenceAfterRun;
Expand All @@ -55,6 +57,7 @@ public void run() {
/**
* return original/unwrapped {@link Runnable}.
*/
@Nonnull
public Runnable getRunnable() {
return runnable;
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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;
}
Expand All @@ -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<TtlRunnable> gets(Collection<? extends Runnable> tasks) {
@Nonnull
public static List<TtlRunnable> gets(@Nullable Collection<? extends Runnable> tasks) {
return gets(tasks, false, false);
}

Expand All @@ -148,7 +155,8 @@ public static List<TtlRunnable> gets(Collection<? extends Runnable> tasks) {
* @return wrapped tasks
* @throws IllegalStateException when input is {@link TtlRunnable} already.
*/
public static List<TtlRunnable> gets(Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun) {
@Nonnull
public static List<TtlRunnable> gets(@Nullable Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun) {
return gets(tasks, releaseTtlValueReferenceAfterRun, false);
}

Expand All @@ -163,7 +171,8 @@ public static List<TtlRunnable> gets(Collection<? extends Runnable> tasks, boole
* @return wrapped tasks
* @throws IllegalStateException when input is {@link TtlRunnable} already and not idempotent.
*/
public static List<TtlRunnable> gets(Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) {
@Nonnull
public static List<TtlRunnable> gets(@Nullable Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) {
if (null == tasks) {
return Collections.emptyList();
}
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/alibaba/ttl/TtlTimerTask.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<Object>(capture());
this.timerTask = timerTask;
this.releaseTtlValueReferenceAfterRun = releaseTtlValueReferenceAfterRun;
Expand Down Expand Up @@ -58,6 +60,7 @@ public boolean cancel() {
return super.cancel();
}

@Nonnull
public TimerTask getTimerTask() {
return timerTask;
}
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -97,7 +102,8 @@ public static TtlTimerTask get(TimerTask timerTask, boolean releaseTtlValueRefer
* @param idempotent is idempotent or not. {@code true} will cover up bugs! <b>DO NOT</b> 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;
}
Expand Down
Loading

0 comments on commit f598e79

Please sign in to comment.