Browse Source

DEC-14578 feat: 去掉无用的jetbrain.NotNull依赖, 拆分Utils方法

release/10.0
Elijah 4 years ago
parent
commit
0dc7ada046
  1. 4
      base-third-project/base-third-step8/pom.xml
  2. 5
      fine-transmittable-thread-local/pom.xml
  3. 44
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/TransmittableThreadLocal.java
  4. 44
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/TtlCallable.java
  5. 4
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/TtlEnhanced.java
  6. 44
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/TtlRunnable.java
  7. 30
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/TtlTimerTask.java
  8. 8
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/TtlUnwrap.java
  9. 44
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/TtlWrappers.java
  10. 5
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/spi/TtlAttachments.java
  11. 5
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/spi/TtlAttachmentsDelegate.java
  12. 3
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/spi/TtlWrapper.java
  13. 3
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/DisableInheritableForkJoinWorkerThreadFactory.java
  14. 4
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/DisableInheritableForkJoinWorkerThreadFactoryWrapper.java
  15. 3
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/DisableInheritableThreadFactory.java
  16. 7
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/DisableInheritableThreadFactoryWrapper.java
  17. 35
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/ExecutorServiceTtlWrapper.java
  18. 7
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/ExecutorTtlWrapper.java
  19. 21
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/ScheduledExecutorServiceTtlWrapper.java
  20. 31
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/TtlExecutors.java
  21. 13
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/TtlForkJoinPoolHelper.java
  22. 39
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/TtlUtils.java
  23. 20
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/TtlAgent.java
  24. 10
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/TtlTransformer.java
  25. 27
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/internal/transformlet/ClassInfo.java
  26. 3
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/internal/transformlet/JavassistTransformlet.java
  27. 28
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/internal/transformlet/impl/TtlExecutorTransformlet.java
  28. 20
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/internal/transformlet/impl/TtlForkJoinTransformlet.java
  29. 15
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/internal/transformlet/impl/TtlTimerTaskTransformlet.java
  30. 49
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/internal/transformlet/impl/Utils.java
  31. 2
      fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/package-info.java

4
base-third-project/base-third-step8/pom.xml

@ -7,14 +7,16 @@
<parent>
<groupId>com.fr.third</groupId>
<artifactId>base-third</artifactId>
<version>${revision}</version>
<version>10.0-FEATURE-SNAPSHOT</version>
</parent>
<artifactId>step8</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<modules>
<module>../../fine-ehcache</module>
<module>../../fine-transmittable-thread-local</module>
</modules>
<build>

5
fine-transmittable-thread-local/pom.xml

@ -30,11 +30,6 @@
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.fr.third</groupId>
<artifactId>fine-jetbrains</artifactId>
<version>${revision}</version>
</dependency>
<!-- Testing frameworks and related dependencies -->
</dependencies>

44
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/TransmittableThreadLocal.java

@ -3,8 +3,6 @@ package com.fr.third.alibaba.ttl;
import com.fr.third.alibaba.ttl.threadpool.TtlExecutors;
import com.fr.third.alibaba.ttl.threadpool.TtlForkJoinPoolHelper;
import com.fr.third.alibaba.ttl.threadpool.agent.TtlAgent;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Iterator;
@ -244,7 +242,7 @@ public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> imple
/**
* Debug only method!
*/
static void dump(@Nullable String title) {
static void dump( String title) {
if (title != null && title.length() > 0) {
System.out.printf("Start TransmittableThreadLocal[%s] Dump...%n", title);
} else {
@ -370,7 +368,7 @@ public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> imple
* @return the captured {@link TransmittableThreadLocal} values
* @since 2.3.0
*/
@NotNull
public static Object capture() {
return new Snapshot(captureTtlValues(), captureThreadLocalValues());
}
@ -403,14 +401,14 @@ public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> imple
* @see #capture()
* @since 2.3.0
*/
@NotNull
public static Object replay(@NotNull Object captured) {
public static Object replay(Object captured) {
final Snapshot capturedSnapshot = (Snapshot) captured;
return new Snapshot(replayTtlValues(capturedSnapshot.ttl2Value), replayThreadLocalValues(capturedSnapshot.threadLocal2Value));
}
@NotNull
private static HashMap<TransmittableThreadLocal<Object>, Object> replayTtlValues(@NotNull HashMap<TransmittableThreadLocal<Object>, Object> captured) {
private static HashMap<TransmittableThreadLocal<Object>, Object> replayTtlValues(HashMap<TransmittableThreadLocal<Object>, Object> captured) {
HashMap<TransmittableThreadLocal<Object>, Object> backup = new HashMap<TransmittableThreadLocal<Object>, Object>();
for (final Iterator<TransmittableThreadLocal<Object>> iterator = holder.get().keySet().iterator(); iterator.hasNext(); ) {
@ -436,7 +434,7 @@ public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> imple
return backup;
}
private static HashMap<ThreadLocal<Object>, Object> replayThreadLocalValues(@NotNull HashMap<ThreadLocal<Object>, Object> captured) {
private static HashMap<ThreadLocal<Object>, Object> replayThreadLocalValues(HashMap<ThreadLocal<Object>, Object> captured) {
final HashMap<ThreadLocal<Object>, Object> backup = new HashMap<ThreadLocal<Object>, Object>();
for (Map.Entry<ThreadLocal<Object>, Object> entry : captured.entrySet()) {
@ -458,7 +456,7 @@ public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> imple
* @return the backup {@link TransmittableThreadLocal} values before clear
* @since 2.9.0
*/
@NotNull
public static Object clear() {
final HashMap<TransmittableThreadLocal<Object>, Object> ttl2Value = new HashMap<TransmittableThreadLocal<Object>, Object>();
@ -480,13 +478,13 @@ public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> imple
* @see #clear()
* @since 2.3.0
*/
public static void restore(@NotNull Object backup) {
public static void restore(Object backup) {
final Snapshot backupSnapshot = (Snapshot) backup;
restoreTtlValues(backupSnapshot.ttl2Value);
restoreThreadLocalValues(backupSnapshot.threadLocal2Value);
}
private static void restoreTtlValues(@NotNull HashMap<TransmittableThreadLocal<Object>, Object> backup) {
private static void restoreTtlValues(HashMap<TransmittableThreadLocal<Object>, Object> backup) {
// call afterExecute callback
doExecuteCallback(false);
@ -505,14 +503,14 @@ public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> imple
setTtlValuesTo(backup);
}
private static void setTtlValuesTo(@NotNull HashMap<TransmittableThreadLocal<Object>, Object> ttlValues) {
private static void setTtlValuesTo(HashMap<TransmittableThreadLocal<Object>, Object> ttlValues) {
for (Map.Entry<TransmittableThreadLocal<Object>, Object> entry : ttlValues.entrySet()) {
TransmittableThreadLocal<Object> threadLocal = entry.getKey();
threadLocal.set(entry.getValue());
}
}
private static void restoreThreadLocalValues(@NotNull HashMap<ThreadLocal<Object>, Object> backup) {
private static void restoreThreadLocalValues(HashMap<ThreadLocal<Object>, Object> backup) {
for (Map.Entry<ThreadLocal<Object>, Object> entry : backup.entrySet()) {
final ThreadLocal<Object> threadLocal = entry.getKey();
threadLocal.set(entry.getValue());
@ -541,7 +539,7 @@ public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> imple
* @see #restore(Object)
* @since 2.3.1
*/
public static <R> R runSupplierWithCaptured(@NotNull Object captured, @NotNull Supplier<R> bizLogic) {
public static <R> R runSupplierWithCaptured(Object captured, Supplier<R> bizLogic) {
Object backup = replay(captured);
try {
return bizLogic.get();
@ -560,7 +558,7 @@ public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> imple
* @see #restore(Object)
* @since 2.9.0
*/
public static <R> R runSupplierWithClear(@NotNull Supplier<R> bizLogic) {
public static <R> R runSupplierWithClear(Supplier<R> bizLogic) {
Object backup = clear();
try {
return bizLogic.get();
@ -582,7 +580,7 @@ public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> imple
* @see #restore(Object)
* @since 2.3.1
*/
public static <R> R runCallableWithCaptured(@NotNull Object captured, @NotNull Callable<R> bizLogic) throws Exception {
public static <R> R runCallableWithCaptured(Object captured, Callable<R> bizLogic) throws Exception {
Object backup = replay(captured);
try {
return bizLogic.call();
@ -602,7 +600,7 @@ public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> imple
* @see #restore(Object)
* @since 2.9.0
*/
public static <R> R runCallableWithClear(@NotNull Callable<R> bizLogic) throws Exception {
public static <R> R runCallableWithClear(Callable<R> bizLogic) throws Exception {
Object backup = clear();
try {
return bizLogic.call();
@ -629,7 +627,7 @@ public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> imple
* @see #registerThreadLocal(ThreadLocal, TtlCopier, boolean)
* @since 2.11.0
*/
public static <T> boolean registerThreadLocal(@NotNull ThreadLocal<T> threadLocal, @NotNull TtlCopier<T> copier) {
public static <T> boolean registerThreadLocal(ThreadLocal<T> threadLocal, TtlCopier<T> copier) {
return registerThreadLocal(threadLocal, copier, false);
}
@ -652,7 +650,7 @@ public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> imple
* @since 2.11.0
*/
@SuppressWarnings("unchecked")
public static <T> boolean registerThreadLocalWithShadowCopier(@NotNull ThreadLocal<T> threadLocal) {
public static <T> boolean registerThreadLocalWithShadowCopier(ThreadLocal<T> threadLocal) {
return registerThreadLocal(threadLocal, (TtlCopier<T>) shadowCopier, false);
}
@ -673,7 +671,7 @@ public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> imple
* @since 2.11.0
*/
@SuppressWarnings("unchecked")
public static <T> boolean registerThreadLocal(@NotNull ThreadLocal<T> threadLocal, @NotNull TtlCopier<T> copier, boolean force) {
public static <T> boolean registerThreadLocal(ThreadLocal<T> threadLocal, TtlCopier<T> copier, boolean force) {
if (threadLocal instanceof TransmittableThreadLocal) {
logger.warning("register a TransmittableThreadLocal instance, this is unnecessary!");
return true;
@ -710,7 +708,7 @@ public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> imple
* @since 2.11.0
*/
@SuppressWarnings("unchecked")
public static <T> boolean registerThreadLocalWithShadowCopier(@NotNull ThreadLocal<T> threadLocal, boolean force) {
public static <T> boolean registerThreadLocalWithShadowCopier(ThreadLocal<T> threadLocal, boolean force) {
return registerThreadLocal(threadLocal, (TtlCopier<T>) shadowCopier, force);
}
@ -724,7 +722,7 @@ public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> imple
* @see #registerThreadLocalWithShadowCopier(ThreadLocal)
* @since 2.11.0
*/
public static <T> boolean unregisterThreadLocal(@NotNull ThreadLocal<T> threadLocal) {
public static <T> boolean unregisterThreadLocal(ThreadLocal<T> threadLocal) {
if (threadLocal instanceof TransmittableThreadLocal) {
logger.warning("unregister a TransmittableThreadLocal instance, this is unnecessary!");
return true;

44
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/TtlCallable.java

@ -5,8 +5,6 @@ import com.fr.third.alibaba.ttl.spi.TtlAttachmentsDelegate;
import com.fr.third.alibaba.ttl.spi.TtlEnhanced;
import com.fr.third.alibaba.ttl.spi.TtlWrapper;
import com.fr.third.alibaba.ttl.threadpool.TtlExecutors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
@ -42,7 +40,7 @@ public final class TtlCallable<V> implements Callable<V>, TtlWrapper<Callable<V>
private final Callable<V> callable;
private final boolean releaseTtlValueReferenceAfterCall;
private TtlCallable(@NotNull Callable<V> callable, boolean releaseTtlValueReferenceAfterCall) {
private TtlCallable(Callable<V> callable, boolean releaseTtlValueReferenceAfterCall) {
this.capturedRef = new AtomicReference<Object>(capture());
this.callable = callable;
this.releaseTtlValueReferenceAfterCall = releaseTtlValueReferenceAfterCall;
@ -69,7 +67,7 @@ public final class TtlCallable<V> implements Callable<V>, TtlWrapper<Callable<V>
/**
* return the original/underneath {@link Callable}.
*/
@NotNull
public Callable<V> getCallable() {
return unwrap();
}
@ -80,7 +78,7 @@ public final class TtlCallable<V> implements Callable<V>, TtlWrapper<Callable<V>
* @see TtlUnwrap#unwrap(Object)
* @since 2.11.4
*/
@NotNull
@Override
public Callable<V> unwrap() {
return callable;
@ -114,8 +112,8 @@ public final class TtlCallable<V> implements Callable<V>, TtlWrapper<Callable<V>
* @param callable input {@link Callable}
* @return Wrapped {@link Callable}
*/
@Nullable
public static <T> TtlCallable<T> get(@Nullable Callable<T> callable) {
public static <T> TtlCallable<T> get( Callable<T> callable) {
return get(callable, false);
}
@ -129,8 +127,8 @@ public final class TtlCallable<V> implements Callable<V>, TtlWrapper<Callable<V>
* @param releaseTtlValueReferenceAfterCall release TTL value reference after run, avoid memory leak even if {@link TtlRunnable} is referred.
* @return Wrapped {@link Callable}
*/
@Nullable
public static <T> TtlCallable<T> get(@Nullable Callable<T> callable, boolean releaseTtlValueReferenceAfterCall) {
public static <T> TtlCallable<T> get( Callable<T> callable, boolean releaseTtlValueReferenceAfterCall) {
return get(callable, releaseTtlValueReferenceAfterCall, false);
}
@ -144,8 +142,8 @@ public final class TtlCallable<V> implements Callable<V>, TtlWrapper<Callable<V>
* @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}
*/
@Nullable
public static <T> TtlCallable<T> get(@Nullable Callable<T> callable, boolean releaseTtlValueReferenceAfterCall, boolean idempotent) {
public static <T> TtlCallable<T> get( Callable<T> callable, boolean releaseTtlValueReferenceAfterCall, boolean idempotent) {
if (null == callable) return null;
if (callable instanceof TtlEnhanced) {
@ -162,8 +160,8 @@ public final class TtlCallable<V> implements Callable<V>, TtlWrapper<Callable<V>
* @param tasks task to be wrapped
* @return Wrapped {@link Callable}
*/
@NotNull
public static <T> List<TtlCallable<T>> gets(@Nullable Collection<? extends Callable<T>> tasks) {
public static <T> List<TtlCallable<T>> gets( Collection<? extends Callable<T>> tasks) {
return gets(tasks, false, false);
}
@ -174,8 +172,8 @@ public final class TtlCallable<V> implements Callable<V>, TtlWrapper<Callable<V>
* @param releaseTtlValueReferenceAfterCall release TTL value reference after run, avoid memory leak even if {@link TtlRunnable} is referred.
* @return Wrapped {@link Callable}
*/
@NotNull
public static <T> List<TtlCallable<T>> gets(@Nullable Collection<? extends Callable<T>> tasks, boolean releaseTtlValueReferenceAfterCall) {
public static <T> List<TtlCallable<T>> gets( Collection<? extends Callable<T>> tasks, boolean releaseTtlValueReferenceAfterCall) {
return gets(tasks, releaseTtlValueReferenceAfterCall, false);
}
@ -187,8 +185,8 @@ public final class TtlCallable<V> implements Callable<V>, TtlWrapper<Callable<V>
* @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}
*/
@NotNull
public static <T> List<TtlCallable<T>> gets(@Nullable Collection<? extends Callable<T>> tasks, boolean releaseTtlValueReferenceAfterCall, boolean idempotent) {
public static <T> List<TtlCallable<T>> gets( Collection<? extends Callable<T>> tasks, boolean releaseTtlValueReferenceAfterCall, boolean idempotent) {
if (null == tasks) return Collections.emptyList();
List<TtlCallable<T>> copy = new ArrayList<TtlCallable<T>>();
@ -209,8 +207,8 @@ public final class TtlCallable<V> implements Callable<V>, TtlWrapper<Callable<V>
* @see #get(Callable)
* @since 2.10.2
*/
@Nullable
public static <T> Callable<T> unwrap(@Nullable Callable<T> callable) {
public static <T> Callable<T> unwrap( Callable<T> callable) {
if (!(callable instanceof TtlCallable)) return callable;
else return ((TtlCallable<T>) callable).getCallable();
}
@ -226,8 +224,8 @@ public final class TtlCallable<V> implements Callable<V>, TtlWrapper<Callable<V>
* @see #unwrap(Callable)
* @since 2.10.2
*/
@NotNull
public static <T> List<Callable<T>> unwraps(@Nullable Collection<? extends Callable<T>> tasks) {
public static <T> List<Callable<T>> unwraps( Collection<? extends Callable<T>> tasks) {
if (null == tasks) return Collections.emptyList();
List<Callable<T>> copy = new ArrayList<Callable<T>>();
@ -246,7 +244,7 @@ public final class TtlCallable<V> implements Callable<V>, TtlWrapper<Callable<V>
* @since 2.11.0
*/
@Override
public void setTtlAttachment(@NotNull String key, Object value) {
public void setTtlAttachment(String key, Object value) {
ttlAttachment.setTtlAttachment(key, value);
}
@ -256,7 +254,7 @@ public final class TtlCallable<V> implements Callable<V>, TtlWrapper<Callable<V>
* @since 2.11.0
*/
@Override
public <T> T getTtlAttachment(@NotNull String key) {
public <T> T getTtlAttachment(String key) {
return ttlAttachment.getTtlAttachment(key);
}
}

4
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/TtlEnhanced.java

@ -9,7 +9,7 @@ import com.fr.third.alibaba.ttl.spi.TtlAttachments;
*/
@Deprecated
// [ERROR] The class name com.alibaba.ttl.TtlEnhanced shadows
// the simple name of implemented interface com.alibaba.ttl.spi.TtlEnhanced [com.alibaba.ttl.TtlEnhanced]
// [ERROR] The class name com.fr.third.alibaba.ttl.TtlEnhanced shadows
// the simple name of implemented interface com.fr.third.alibaba.ttl.spi.TtlEnhanced [com.fr.third.alibaba.ttl.TtlEnhanced]
public interface TtlEnhanced extends com.fr.third.alibaba.ttl.spi.TtlEnhanced {
}

44
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/TtlRunnable.java

@ -5,8 +5,6 @@ import com.fr.third.alibaba.ttl.spi.TtlAttachmentsDelegate;
import com.fr.third.alibaba.ttl.spi.TtlEnhanced;
import com.fr.third.alibaba.ttl.spi.TtlWrapper;
import com.fr.third.alibaba.ttl.threadpool.TtlExecutors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
@ -39,7 +37,7 @@ public final class TtlRunnable implements Runnable, TtlWrapper<Runnable>, TtlEnh
private final Runnable runnable;
private final boolean releaseTtlValueReferenceAfterRun;
private TtlRunnable(@NotNull Runnable runnable, boolean releaseTtlValueReferenceAfterRun) {
private TtlRunnable(Runnable runnable, boolean releaseTtlValueReferenceAfterRun) {
this.capturedRef = new AtomicReference<Object>(capture());
this.runnable = runnable;
this.releaseTtlValueReferenceAfterRun = releaseTtlValueReferenceAfterRun;
@ -66,7 +64,7 @@ public final class TtlRunnable implements Runnable, TtlWrapper<Runnable>, TtlEnh
/**
* return original/unwrapped {@link Runnable}.
*/
@NotNull
public Runnable getRunnable() {
return unwrap();
}
@ -77,7 +75,7 @@ public final class TtlRunnable implements Runnable, TtlWrapper<Runnable>, TtlEnh
* @see TtlUnwrap#unwrap(Object)
* @since 2.11.4
*/
@NotNull
@Override
public Runnable unwrap() {
return runnable;
@ -110,8 +108,8 @@ public final class TtlRunnable implements Runnable, TtlWrapper<Runnable>, TtlEnh
* @return Wrapped {@link Runnable}
* @throws IllegalStateException when input is {@link TtlRunnable} already.
*/
@Nullable
public static TtlRunnable get(@Nullable Runnable runnable) {
public static TtlRunnable get( Runnable runnable) {
return get(runnable, false, false);
}
@ -123,8 +121,8 @@ public final class TtlRunnable implements Runnable, TtlWrapper<Runnable>, TtlEnh
* @return Wrapped {@link Runnable}
* @throws IllegalStateException when input is {@link TtlRunnable} already.
*/
@Nullable
public static TtlRunnable get(@Nullable Runnable runnable, boolean releaseTtlValueReferenceAfterRun) {
public static TtlRunnable get( Runnable runnable, boolean releaseTtlValueReferenceAfterRun) {
return get(runnable, releaseTtlValueReferenceAfterRun, false);
}
@ -139,8 +137,8 @@ public final class TtlRunnable implements Runnable, TtlWrapper<Runnable>, TtlEnh
* @return Wrapped {@link Runnable}
* @throws IllegalStateException when input is {@link TtlRunnable} already and not idempotent.
*/
@Nullable
public static TtlRunnable get(@Nullable Runnable runnable, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) {
public static TtlRunnable get( Runnable runnable, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) {
if (null == runnable) return null;
if (runnable instanceof TtlEnhanced) {
@ -158,8 +156,8 @@ public final class TtlRunnable implements Runnable, TtlWrapper<Runnable>, TtlEnh
* @return wrapped tasks
* @throws IllegalStateException when input is {@link TtlRunnable} already.
*/
@NotNull
public static List<TtlRunnable> gets(@Nullable Collection<? extends Runnable> tasks) {
public static List<TtlRunnable> gets( Collection<? extends Runnable> tasks) {
return gets(tasks, false, false);
}
@ -171,8 +169,8 @@ public final class TtlRunnable implements Runnable, TtlWrapper<Runnable>, TtlEnh
* @return wrapped tasks
* @throws IllegalStateException when input is {@link TtlRunnable} already.
*/
@NotNull
public static List<TtlRunnable> gets(@Nullable Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun) {
public static List<TtlRunnable> gets( Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun) {
return gets(tasks, releaseTtlValueReferenceAfterRun, false);
}
@ -187,8 +185,8 @@ public final class TtlRunnable implements Runnable, TtlWrapper<Runnable>, TtlEnh
* @return wrapped tasks
* @throws IllegalStateException when input is {@link TtlRunnable} already and not idempotent.
*/
@NotNull
public static List<TtlRunnable> gets(@Nullable Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) {
public static List<TtlRunnable> gets( Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) {
if (null == tasks) return Collections.emptyList();
List<TtlRunnable> copy = new ArrayList<TtlRunnable>();
@ -209,8 +207,8 @@ public final class TtlRunnable implements Runnable, TtlWrapper<Runnable>, TtlEnh
* @see #get(Runnable)
* @since 2.10.2
*/
@Nullable
public static Runnable unwrap(@Nullable Runnable runnable) {
public static Runnable unwrap( Runnable runnable) {
if (!(runnable instanceof TtlRunnable)) return runnable;
else return ((TtlRunnable) runnable).getRunnable();
}
@ -226,8 +224,8 @@ public final class TtlRunnable implements Runnable, TtlWrapper<Runnable>, TtlEnh
* @see #unwrap(Runnable)
* @since 2.10.2
*/
@NotNull
public static List<Runnable> unwraps(@Nullable Collection<? extends Runnable> tasks) {
public static List<Runnable> unwraps( Collection<? extends Runnable> tasks) {
if (null == tasks) return Collections.emptyList();
List<Runnable> copy = new ArrayList<Runnable>();
@ -246,7 +244,7 @@ public final class TtlRunnable implements Runnable, TtlWrapper<Runnable>, TtlEnh
* @since 2.11.0
*/
@Override
public void setTtlAttachment(@NotNull String key, Object value) {
public void setTtlAttachment(String key, Object value) {
ttlAttachment.setTtlAttachment(key, value);
}
@ -256,7 +254,7 @@ public final class TtlRunnable implements Runnable, TtlWrapper<Runnable>, TtlEnh
* @since 2.11.0
*/
@Override
public <T> T getTtlAttachment(@NotNull String key) {
public <T> T getTtlAttachment(String key) {
return ttlAttachment.getTtlAttachment(key);
}
}

30
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/TtlTimerTask.java

@ -3,8 +3,8 @@ package com.fr.third.alibaba.ttl;
import com.fr.third.alibaba.ttl.spi.TtlEnhanced;
import com.fr.third.alibaba.ttl.spi.TtlWrapper;
import com.fr.third.alibaba.ttl.threadpool.agent.TtlAgent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
@ -35,7 +35,7 @@ public final class TtlTimerTask extends TimerTask implements TtlWrapper<TimerTas
private final TimerTask timerTask;
private final boolean releaseTtlValueReferenceAfterRun;
private TtlTimerTask(@NotNull TimerTask timerTask, boolean releaseTtlValueReferenceAfterRun) {
private TtlTimerTask(TimerTask timerTask, boolean releaseTtlValueReferenceAfterRun) {
this.capturedRef = new AtomicReference<Object>(capture());
this.timerTask = timerTask;
this.releaseTtlValueReferenceAfterRun = releaseTtlValueReferenceAfterRun;
@ -68,7 +68,7 @@ public final class TtlTimerTask extends TimerTask implements TtlWrapper<TimerTas
/**
* return original/unwrapped {@link TimerTask}.
*/
@NotNull
public TimerTask getTimerTask() {
return unwrap();
}
@ -79,7 +79,7 @@ public final class TtlTimerTask extends TimerTask implements TtlWrapper<TimerTas
* @see TtlUnwrap#unwrap(Object)
* @since 2.11.4
*/
@NotNull
@Override
public TimerTask unwrap() {
return timerTask;
@ -113,8 +113,8 @@ public final class TtlTimerTask extends TimerTask implements TtlWrapper<TimerTas
* @param timerTask input {@link TimerTask}
* @return Wrapped {@link TimerTask}
*/
@Nullable
public static TtlTimerTask get(@Nullable TimerTask timerTask) {
public static TtlTimerTask get( TimerTask timerTask) {
return get(timerTask, false, false);
}
@ -127,8 +127,8 @@ public final class TtlTimerTask extends TimerTask implements TtlWrapper<TimerTas
* @param releaseTtlValueReferenceAfterRun release TTL value reference after run, avoid memory leak even if {@link TtlTimerTask} is referred.
* @return Wrapped {@link TimerTask}
*/
@Nullable
public static TtlTimerTask get(@Nullable TimerTask timerTask, boolean releaseTtlValueReferenceAfterRun) {
public static TtlTimerTask get( TimerTask timerTask, boolean releaseTtlValueReferenceAfterRun) {
return get(timerTask, releaseTtlValueReferenceAfterRun, false);
}
@ -142,8 +142,8 @@ public final class TtlTimerTask extends TimerTask implements TtlWrapper<TimerTas
* @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}
*/
@Nullable
public static TtlTimerTask get(@Nullable TimerTask timerTask, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) {
public static TtlTimerTask get( TimerTask timerTask, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) {
if (null == timerTask) return null;
if (timerTask instanceof TtlEnhanced) {
@ -163,8 +163,8 @@ public final class TtlTimerTask extends TimerTask implements TtlWrapper<TimerTas
* @see #get(TimerTask)
* @since 2.10.2
*/
@Nullable
public static TimerTask unwrap(@Nullable TimerTask timerTask) {
public static TimerTask unwrap( TimerTask timerTask) {
if (!(timerTask instanceof TtlTimerTask)) return timerTask;
else return ((TtlTimerTask) timerTask).getTimerTask();
}
@ -179,8 +179,8 @@ public final class TtlTimerTask extends TimerTask implements TtlWrapper<TimerTas
* @see #unwrap(TimerTask)
* @since 2.10.2
*/
@NotNull
public static List<TimerTask> unwraps(@Nullable Collection<? extends TimerTask> tasks) {
public static List<TimerTask> unwraps( Collection<? extends TimerTask> tasks) {
if (null == tasks) return Collections.emptyList();
List<TimerTask> copy = new ArrayList<TimerTask>();

8
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/TtlUnwrap.java

@ -3,7 +3,7 @@ package com.fr.third.alibaba.ttl;
import com.fr.third.alibaba.ttl.spi.TtlWrapper;
import com.fr.third.alibaba.ttl.threadpool.TtlExecutors;
import com.fr.third.alibaba.ttl.threadpool.TtlForkJoinPoolHelper;
import org.jetbrains.annotations.Nullable;
import java.util.concurrent.Callable;
@ -48,9 +48,9 @@ public class TtlUnwrap {
* @see TtlForkJoinPoolHelper#unwrap(java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory)
* @since 2.11.4
*/
@Nullable
@SuppressWarnings("unchecked")
public static <T> T unwrap(@Nullable T obj) {
public static <T> T unwrap( T obj) {
if (!isWrapper(obj)) return obj;
else return ((TtlWrapper<T>) obj).unwrap();
}
@ -60,7 +60,7 @@ public class TtlUnwrap {
*
* @since 2.11.4
*/
public static <T> boolean isWrapper(@Nullable T obj) {
public static <T> boolean isWrapper( T obj) {
return obj instanceof TtlWrapper;
}

44
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/TtlWrappers.java

@ -2,8 +2,8 @@ package com.fr.third.alibaba.ttl;
import com.fr.third.alibaba.ttl.spi.TtlEnhanced;
import com.fr.third.alibaba.ttl.spi.TtlWrapper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.*;
@ -34,8 +34,8 @@ public class TtlWrappers {
* @see TtlUnwrap#unwrap(Object)
* @since 2.11.4
*/
@Nullable
public static <T> Supplier<T> wrap(@Nullable Supplier<T> supplier) {
public static <T> Supplier<T> wrap( Supplier<T> supplier) {
if (supplier == null) return null;
else if (supplier instanceof TtlEnhanced) return supplier;
else return new TtlSupplier<T>(supplier);
@ -45,7 +45,7 @@ public class TtlWrappers {
final Supplier<T> supplier;
final Object captured;
TtlSupplier(@NotNull Supplier<T> supplier) {
TtlSupplier(Supplier<T> supplier) {
this.supplier = supplier;
this.captured = capture();
}
@ -60,7 +60,7 @@ public class TtlWrappers {
}
}
@NotNull
@Override
public Supplier<T> unwrap() {
return supplier;
@ -96,8 +96,8 @@ public class TtlWrappers {
* @see TtlUnwrap#unwrap(Object)
* @since 2.11.4
*/
@Nullable
public static <T> Consumer<T> wrap(@Nullable Consumer<T> consumer) {
public static <T> Consumer<T> wrap( Consumer<T> consumer) {
if (consumer == null) return null;
else if (consumer instanceof TtlEnhanced) return consumer;
else return new TtlConsumer<T>(consumer);
@ -107,7 +107,7 @@ public class TtlWrappers {
final Consumer<T> consumer;
final Object captured;
TtlConsumer(@NotNull Consumer<T> consumer) {
TtlConsumer(Consumer<T> consumer) {
this.consumer = consumer;
this.captured = capture();
}
@ -122,7 +122,7 @@ public class TtlWrappers {
}
}
@NotNull
@Override
public Consumer<T> unwrap() {
return consumer;
@ -158,8 +158,8 @@ public class TtlWrappers {
* @see TtlUnwrap#unwrap(Object)
* @since 2.11.4
*/
@Nullable
public static <T, U> BiConsumer<T, U> wrap(@Nullable BiConsumer<T, U> consumer) {
public static <T, U> BiConsumer<T, U> wrap( BiConsumer<T, U> consumer) {
if (consumer == null) return null;
else if (consumer instanceof TtlEnhanced) return consumer;
else return new TtlBiConsumer<T, U>(consumer);
@ -169,7 +169,7 @@ public class TtlWrappers {
final BiConsumer<T, U> consumer;
final Object captured;
TtlBiConsumer(@NotNull BiConsumer<T, U> consumer) {
TtlBiConsumer(BiConsumer<T, U> consumer) {
this.consumer = consumer;
this.captured = capture();
}
@ -184,7 +184,7 @@ public class TtlWrappers {
}
}
@NotNull
@Override
public BiConsumer<T, U> unwrap() {
return consumer;
@ -220,8 +220,8 @@ public class TtlWrappers {
* @see TtlUnwrap#unwrap(Object)
* @since 2.11.4
*/
@Nullable
public static <T, R> Function<T, R> wrap(@Nullable Function<T, R> fn) {
public static <T, R> Function<T, R> wrap( Function<T, R> fn) {
if (fn == null) return null;
else if (fn instanceof TtlEnhanced) return fn;
else return new TtlFunction<T, R>(fn);
@ -231,7 +231,7 @@ public class TtlWrappers {
final Function<T, R> fn;
final Object captured;
TtlFunction(@NotNull Function<T, R> fn) {
TtlFunction(Function<T, R> fn) {
this.fn = fn;
this.captured = capture();
}
@ -246,7 +246,7 @@ public class TtlWrappers {
}
}
@NotNull
@Override
public Function<T, R> unwrap() {
return fn;
@ -282,8 +282,8 @@ public class TtlWrappers {
* @see TtlUnwrap#unwrap(Object)
* @since 2.11.4
*/
@Nullable
public static <T, U, R> BiFunction<T, U, R> wrap(@Nullable BiFunction<T, U, R> fn) {
public static <T, U, R> BiFunction<T, U, R> wrap( BiFunction<T, U, R> fn) {
if (fn == null) return null;
else if (fn instanceof TtlEnhanced) return fn;
else return new TtlBiFunction<T, U, R>(fn);
@ -293,7 +293,7 @@ public class TtlWrappers {
final BiFunction<T, U, R> fn;
final Object captured;
TtlBiFunction(@NotNull BiFunction<T, U, R> fn) {
TtlBiFunction(BiFunction<T, U, R> fn) {
this.fn = fn;
this.captured = capture();
}
@ -308,7 +308,7 @@ public class TtlWrappers {
}
}
@NotNull
@Override
public BiFunction<T, U, R> unwrap() {
return fn;

5
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/spi/TtlAttachments.java

@ -2,7 +2,6 @@ package com.fr.third.alibaba.ttl.spi;
import com.fr.third.alibaba.ttl.TtlCallable;
import com.fr.third.alibaba.ttl.TtlRunnable;
import org.jetbrains.annotations.NotNull;
/**
* The TTL attachments for TTL tasks, eg: {@link TtlRunnable}, {@link TtlCallable}.
@ -18,7 +17,7 @@ public interface TtlAttachments extends TtlEnhanced {
* @param value attachment value
* @since 2.11.0
*/
void setTtlAttachment(@NotNull String key, Object value);
void setTtlAttachment(String key, Object value);
/**
* get the TTL attachment for TTL tasks
@ -26,7 +25,7 @@ public interface TtlAttachments extends TtlEnhanced {
* @param key attachment key
* @since 2.11.0
*/
<T> T getTtlAttachment(@NotNull String key);
<T> T getTtlAttachment(String key);
/**
* The attachment key of TTL task, weather this task is a auto wrapper task.

5
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/spi/TtlAttachmentsDelegate.java

@ -2,7 +2,6 @@ package com.fr.third.alibaba.ttl.spi;
import com.fr.third.alibaba.ttl.TtlCallable;
import com.fr.third.alibaba.ttl.TtlRunnable;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@ -19,13 +18,13 @@ public class TtlAttachmentsDelegate implements TtlAttachments {
private final ConcurrentMap<String, Object> attachments = new ConcurrentHashMap<String, Object>();
@Override
public void setTtlAttachment(@NotNull String key, Object value) {
public void setTtlAttachment(String key, Object value) {
attachments.put(key, value);
}
@Override
@SuppressWarnings("unchecked")
public <T> T getTtlAttachment(@NotNull String key) {
public <T> T getTtlAttachment(String key) {
return (T) attachments.get(key);
}
}

3
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/spi/TtlWrapper.java

@ -1,7 +1,6 @@
package com.fr.third.alibaba.ttl.spi;
import com.fr.third.alibaba.ttl.TtlUnwrap;
import org.jetbrains.annotations.NotNull;
/**
* Ttl Wrapper interface.
@ -21,6 +20,6 @@ public interface TtlWrapper<T> extends TtlEnhanced {
*
* @see TtlUnwrap#unwrap(Object)
*/
@NotNull
T unwrap();
}

3
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/DisableInheritableForkJoinWorkerThreadFactory.java

@ -1,7 +1,6 @@
package com.fr.third.alibaba.ttl.threadpool;
import com.fr.third.alibaba.ttl.spi.TtlWrapper;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory;
@ -16,6 +15,6 @@ public interface DisableInheritableForkJoinWorkerThreadFactory extends ForkJoinW
* Unwrap {@link DisableInheritableThreadFactory} to the original/underneath one.
*/
@Override
@NotNull
ForkJoinWorkerThreadFactory unwrap();
}

4
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/DisableInheritableForkJoinWorkerThreadFactoryWrapper.java

@ -1,6 +1,5 @@
package com.fr.third.alibaba.ttl.threadpool;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory;
@ -16,7 +15,7 @@ import static com.fr.third.alibaba.ttl.TransmittableThreadLocal.Transmitter.rest
class DisableInheritableForkJoinWorkerThreadFactoryWrapper implements DisableInheritableForkJoinWorkerThreadFactory {
private final ForkJoinWorkerThreadFactory threadFactory;
DisableInheritableForkJoinWorkerThreadFactoryWrapper(@NotNull ForkJoinWorkerThreadFactory threadFactory) {
DisableInheritableForkJoinWorkerThreadFactoryWrapper(ForkJoinWorkerThreadFactory threadFactory) {
this.threadFactory = threadFactory;
}
@ -31,7 +30,6 @@ class DisableInheritableForkJoinWorkerThreadFactoryWrapper implements DisableInh
}
@Override
@NotNull
public ForkJoinWorkerThreadFactory unwrap() {
return threadFactory;
}

3
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/DisableInheritableThreadFactory.java

@ -1,7 +1,6 @@
package com.fr.third.alibaba.ttl.threadpool;
import com.fr.third.alibaba.ttl.spi.TtlWrapper;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.ThreadFactory;
@ -17,6 +16,6 @@ public interface DisableInheritableThreadFactory extends ThreadFactory, TtlWrapp
* Unwrap {@link DisableInheritableThreadFactory} to the original/underneath one.
*/
@Override
@NotNull
ThreadFactory unwrap();
}

7
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/DisableInheritableThreadFactoryWrapper.java

@ -1,6 +1,5 @@
package com.fr.third.alibaba.ttl.threadpool;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.ThreadFactory;
@ -14,12 +13,12 @@ import static com.fr.third.alibaba.ttl.TransmittableThreadLocal.Transmitter.rest
class DisableInheritableThreadFactoryWrapper implements DisableInheritableThreadFactory {
private final ThreadFactory threadFactory;
DisableInheritableThreadFactoryWrapper(@NotNull ThreadFactory threadFactory) {
DisableInheritableThreadFactoryWrapper(ThreadFactory threadFactory) {
this.threadFactory = threadFactory;
}
@Override
public Thread newThread(@NotNull Runnable r) {
public Thread newThread(Runnable r) {
final Object backup = clear();
try {
return threadFactory.newThread(r);
@ -28,7 +27,7 @@ class DisableInheritableThreadFactoryWrapper implements DisableInheritableThread
}
}
@NotNull
@Override
public ThreadFactory unwrap() {
return threadFactory;

35
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/ExecutorServiceTtlWrapper.java

@ -4,7 +4,6 @@ import com.fr.third.alibaba.ttl.TransmittableThreadLocal;
import com.fr.third.alibaba.ttl.TtlCallable;
import com.fr.third.alibaba.ttl.TtlRunnable;
import com.fr.third.alibaba.ttl.spi.TtlEnhanced;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.List;
@ -21,7 +20,7 @@ import java.util.concurrent.*;
class ExecutorServiceTtlWrapper extends ExecutorTtlWrapper implements ExecutorService, TtlEnhanced {
private final ExecutorService executorService;
ExecutorServiceTtlWrapper(@NotNull ExecutorService executorService) {
ExecutorServiceTtlWrapper(ExecutorService executorService) {
super(executorService);
this.executorService = executorService;
}
@ -31,7 +30,7 @@ class ExecutorServiceTtlWrapper extends ExecutorTtlWrapper implements ExecutorSe
executorService.shutdown();
}
@NotNull
@Override
public List<Runnable> shutdownNow() {
return executorService.shutdownNow();
@ -48,52 +47,52 @@ class ExecutorServiceTtlWrapper extends ExecutorTtlWrapper implements ExecutorSe
}
@Override
public boolean awaitTermination(long timeout, @NotNull TimeUnit unit) throws InterruptedException {
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
return executorService.awaitTermination(timeout, unit);
}
@NotNull
@Override
public <T> Future<T> submit(@NotNull Callable<T> task) {
public <T> Future<T> submit(Callable<T> task) {
return executorService.submit(TtlCallable.get(task));
}
@NotNull
@Override
public <T> Future<T> submit(@NotNull Runnable task, T result) {
public <T> Future<T> submit(Runnable task, T result) {
return executorService.submit(TtlRunnable.get(task), result);
}
@NotNull
@Override
public Future<?> submit(@NotNull Runnable task) {
public Future<?> submit(Runnable task) {
return executorService.submit(TtlRunnable.get(task));
}
@NotNull
@Override
public <T> List<Future<T>> invokeAll(@NotNull Collection<? extends Callable<T>> tasks) throws InterruptedException {
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
return executorService.invokeAll(TtlCallable.gets(tasks));
}
@NotNull
@Override
public <T> List<Future<T>> invokeAll(@NotNull Collection<? extends Callable<T>> tasks, long timeout, @NotNull TimeUnit unit) throws InterruptedException {
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
return executorService.invokeAll(TtlCallable.gets(tasks), timeout, unit);
}
@NotNull
@Override
public <T> T invokeAny(@NotNull Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
return executorService.invokeAny(TtlCallable.gets(tasks));
}
@Override
public <T> T invokeAny(@NotNull Collection<? extends Callable<T>> tasks, long timeout, @NotNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return executorService.invokeAny(TtlCallable.gets(tasks), timeout, unit);
}
@NotNull
@Override
public ExecutorService unwrap() {
return executorService;

7
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/ExecutorTtlWrapper.java

@ -4,7 +4,6 @@ import com.fr.third.alibaba.ttl.TransmittableThreadLocal;
import com.fr.third.alibaba.ttl.TtlRunnable;
import com.fr.third.alibaba.ttl.spi.TtlEnhanced;
import com.fr.third.alibaba.ttl.spi.TtlWrapper;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.Executor;
@ -19,17 +18,17 @@ import java.util.concurrent.Executor;
class ExecutorTtlWrapper implements Executor, TtlWrapper<Executor>, TtlEnhanced {
private final Executor executor;
ExecutorTtlWrapper(@NotNull Executor executor) {
ExecutorTtlWrapper(Executor executor) {
this.executor = executor;
}
@Override
public void execute(@NotNull Runnable command) {
public void execute(Runnable command) {
executor.execute(TtlRunnable.get(command));
}
@Override
@NotNull
public Executor unwrap() {
return executor;
}

21
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/ScheduledExecutorServiceTtlWrapper.java

@ -4,7 +4,6 @@ import com.fr.third.alibaba.ttl.TransmittableThreadLocal;
import com.fr.third.alibaba.ttl.TtlCallable;
import com.fr.third.alibaba.ttl.TtlRunnable;
import com.fr.third.alibaba.ttl.spi.TtlEnhanced;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.Callable;
@ -24,37 +23,37 @@ import java.util.concurrent.TimeUnit;
class ScheduledExecutorServiceTtlWrapper extends ExecutorServiceTtlWrapper implements ScheduledExecutorService, TtlEnhanced {
final ScheduledExecutorService scheduledExecutorService;
public ScheduledExecutorServiceTtlWrapper(@NotNull ScheduledExecutorService scheduledExecutorService) {
public ScheduledExecutorServiceTtlWrapper(ScheduledExecutorService scheduledExecutorService) {
super(scheduledExecutorService);
this.scheduledExecutorService = scheduledExecutorService;
}
@NotNull
@Override
public ScheduledFuture<?> schedule(@NotNull Runnable command, long delay, @NotNull TimeUnit unit) {
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
return scheduledExecutorService.schedule(TtlRunnable.get(command), delay, unit);
}
@NotNull
@Override
public <V> ScheduledFuture<V> schedule(@NotNull Callable<V> callable, long delay, @NotNull TimeUnit unit) {
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
return scheduledExecutorService.schedule(TtlCallable.get(callable), delay, unit);
}
@NotNull
@Override
public ScheduledFuture<?> scheduleAtFixedRate(@NotNull Runnable command, long initialDelay, long period, @NotNull TimeUnit unit) {
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
return scheduledExecutorService.scheduleAtFixedRate(TtlRunnable.get(command), initialDelay, period, unit);
}
@NotNull
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(@NotNull Runnable command, long initialDelay, long delay, @NotNull TimeUnit unit) {
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
return scheduledExecutorService.scheduleWithFixedDelay(TtlRunnable.get(command), initialDelay, delay, unit);
}
@Override
@NotNull
public ScheduledExecutorService unwrap() {
return scheduledExecutorService;
}

31
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/TtlExecutors.java

@ -3,7 +3,6 @@ package com.fr.third.alibaba.ttl.threadpool;
import com.fr.third.alibaba.ttl.TransmittableThreadLocal;
import com.fr.third.alibaba.ttl.spi.TtlEnhanced;
import com.fr.third.alibaba.ttl.threadpool.agent.TtlAgent;
import org.jetbrains.annotations.Nullable;
import java.util.concurrent.*;
@ -41,8 +40,8 @@ public final class TtlExecutors {
* transmit the {@link TransmittableThreadLocal} from the task submit time of {@link Runnable}
* to the execution time of {@link Runnable}.
*/
@Nullable
public static Executor getTtlExecutor(@Nullable Executor executor) {
public static Executor getTtlExecutor( Executor executor) {
if (TtlAgent.isTtlAgentLoaded() || null == executor || executor instanceof TtlEnhanced) {
return executor;
}
@ -54,8 +53,8 @@ public final class TtlExecutors {
* 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}.
*/
@Nullable
public static ExecutorService getTtlExecutorService(@Nullable ExecutorService executorService) {
public static ExecutorService getTtlExecutorService( ExecutorService executorService) {
if (TtlAgent.isTtlAgentLoaded() || executorService == null || executorService instanceof TtlEnhanced) {
return executorService;
}
@ -67,8 +66,8 @@ public final class TtlExecutors {
* 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}.
*/
@Nullable
public static ScheduledExecutorService getTtlScheduledExecutorService(@Nullable ScheduledExecutorService scheduledExecutorService) {
public static ScheduledExecutorService getTtlScheduledExecutorService( ScheduledExecutorService scheduledExecutorService) {
if (TtlAgent.isTtlAgentLoaded() || scheduledExecutorService == null || scheduledExecutorService instanceof TtlEnhanced) {
return scheduledExecutorService;
}
@ -90,7 +89,7 @@ public final class TtlExecutors {
* @see #unwrap(Executor)
* @since 2.8.0
*/
public static <T extends Executor> boolean isTtlWrapper(@Nullable T executor) {
public static <T extends Executor> boolean isTtlWrapper( T executor) {
return executor instanceof TtlEnhanced;
}
@ -110,9 +109,9 @@ public final class TtlExecutors {
* @see #isTtlWrapper(Executor)
* @since 2.8.0
*/
@Nullable
@SuppressWarnings("unchecked")
public static <T extends Executor> T unwrap(@Nullable T executor) {
public static <T extends Executor> T unwrap( T executor) {
if (!isTtlWrapper(executor)) return executor;
return (T) ((ExecutorTtlWrapper) executor).unwrap();
@ -125,8 +124,8 @@ public final class TtlExecutors {
* @see DisableInheritableThreadFactory
* @since 2.10.0
*/
@Nullable
public static ThreadFactory getDisableInheritableThreadFactory(@Nullable ThreadFactory threadFactory) {
public static ThreadFactory getDisableInheritableThreadFactory( ThreadFactory threadFactory) {
if (threadFactory == null || isDisableInheritableThreadFactory(threadFactory)) return threadFactory;
return new DisableInheritableThreadFactoryWrapper(threadFactory);
@ -138,7 +137,7 @@ public final class TtlExecutors {
* @see #getDisableInheritableThreadFactory(ThreadFactory)
* @since 2.10.0
*/
@Nullable
public static ThreadFactory getDefaultDisableInheritableThreadFactory() {
return getDisableInheritableThreadFactory(Executors.defaultThreadFactory());
}
@ -149,7 +148,7 @@ public final class TtlExecutors {
* @see DisableInheritableThreadFactory
* @since 2.10.0
*/
public static boolean isDisableInheritableThreadFactory(@Nullable ThreadFactory threadFactory) {
public static boolean isDisableInheritableThreadFactory( ThreadFactory threadFactory) {
return threadFactory instanceof DisableInheritableThreadFactory;
}
@ -159,8 +158,8 @@ public final class TtlExecutors {
* @see DisableInheritableThreadFactory
* @since 2.10.0
*/
@Nullable
public static ThreadFactory unwrap(@Nullable ThreadFactory threadFactory) {
public static ThreadFactory unwrap( ThreadFactory threadFactory) {
if (!isDisableInheritableThreadFactory(threadFactory)) return threadFactory;
return ((DisableInheritableThreadFactory) threadFactory).unwrap();

13
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/TtlForkJoinPoolHelper.java

@ -1,6 +1,5 @@
package com.fr.third.alibaba.ttl.threadpool;
import org.jetbrains.annotations.Nullable;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory;
@ -26,8 +25,8 @@ public class TtlForkJoinPoolHelper {
* @see DisableInheritableForkJoinWorkerThreadFactory
* @since 2.10.1
*/
@Nullable
public static ForkJoinWorkerThreadFactory getDisableInheritableForkJoinWorkerThreadFactory(@Nullable ForkJoinWorkerThreadFactory threadFactory) {
public static ForkJoinWorkerThreadFactory getDisableInheritableForkJoinWorkerThreadFactory( ForkJoinWorkerThreadFactory threadFactory) {
if (threadFactory == null || isDisableInheritableForkJoinWorkerThreadFactory(threadFactory))
return threadFactory;
@ -40,7 +39,7 @@ public class TtlForkJoinPoolHelper {
* @see #getDisableInheritableForkJoinWorkerThreadFactory(ForkJoinWorkerThreadFactory)
* @since 2.10.1
*/
@Nullable
public static ForkJoinWorkerThreadFactory getDefaultDisableInheritableForkJoinWorkerThreadFactory() {
return getDisableInheritableForkJoinWorkerThreadFactory(ForkJoinPool.defaultForkJoinWorkerThreadFactory);
}
@ -51,7 +50,7 @@ public class TtlForkJoinPoolHelper {
* @see DisableInheritableForkJoinWorkerThreadFactory
* @since 2.10.1
*/
public static boolean isDisableInheritableForkJoinWorkerThreadFactory(@Nullable ForkJoinWorkerThreadFactory threadFactory) {
public static boolean isDisableInheritableForkJoinWorkerThreadFactory( ForkJoinWorkerThreadFactory threadFactory) {
return threadFactory instanceof DisableInheritableForkJoinWorkerThreadFactory;
}
@ -61,8 +60,8 @@ public class TtlForkJoinPoolHelper {
* @see DisableInheritableForkJoinWorkerThreadFactory
* @since 2.10.1
*/
@Nullable
public static ForkJoinWorkerThreadFactory unwrap(@Nullable ForkJoinWorkerThreadFactory threadFactory) {
public static ForkJoinWorkerThreadFactory unwrap( ForkJoinWorkerThreadFactory threadFactory) {
if (!isDisableInheritableForkJoinWorkerThreadFactory(threadFactory)) return threadFactory;
return ((DisableInheritableForkJoinWorkerThreadFactory) threadFactory).unwrap();

39
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/TtlUtils.java

@ -0,0 +1,39 @@
package com.fr.third.alibaba.ttl.threadpool;
import com.fr.third.alibaba.ttl.TtlRunnable;
import com.fr.third.alibaba.ttl.spi.TtlAttachments;
import com.fr.third.alibaba.ttl.spi.TtlEnhanced;
import static com.fr.third.alibaba.ttl.TransmittableThreadLocal.Transmitter.capture;
public class TtlUtils {
public static Object doCaptureWhenNotTtlEnhanced( Object obj) {
if (obj instanceof TtlEnhanced) return null;
else return capture();
}
public static void setAutoWrapperAttachment( Object ttlAttachment) {
if (notTtlAttachments(ttlAttachment)) return;
((TtlAttachments) ttlAttachment).setTtlAttachment(TtlAttachments.KEY_IS_AUTO_WRAPPER, true);
}
public static Runnable unwrapIfIsAutoWrapper( Runnable runnable) {
if (isAutoWrapper(runnable)) return TtlRunnable.unwrap(runnable);
else return runnable;
}
private static boolean notTtlAttachments( Object ttlAttachment) {
return !(ttlAttachment instanceof TtlAttachments);
}
private static boolean isAutoWrapper( Runnable ttlAttachments) {
if (notTtlAttachments(ttlAttachments)) return false;
final Boolean value = ((TtlAttachments) ttlAttachments).getTtlAttachment(TtlAttachments.KEY_IS_AUTO_WRAPPER);
if (value == null) return false;
return value;
}
}

20
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/TtlAgent.java

@ -7,12 +7,6 @@ import com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.impl.TtlE
import com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.impl.TtlForkJoinTransformlet;
import com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.impl.TtlTimerTaskTransformlet;
import com.fr.third.alibaba.ttl.threadpool.DisableInheritableForkJoinWorkerThreadFactory;
import com.fr.third.alibaba.ttl.threadpool.DisableInheritableThreadFactory;
import com.fr.third.alibaba.ttl.threadpool.TtlExecutors;
import com.fr.third.alibaba.ttl.threadpool.TtlForkJoinPoolHelper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
import java.util.ArrayList;
@ -128,7 +122,7 @@ public final class TtlAgent {
* @see Logger#STDERR
* @see Logger#STDOUT
*/
public static void premain(String agentArgs, @NotNull Instrumentation inst) {
public static void premain(String agentArgs, Instrumentation inst) {
kvs = splitCommaColonStringToKV(agentArgs);
Logger.setLoggerImplType(getLogImplTypeFromAgentArgs(kvs));
@ -157,7 +151,7 @@ public final class TtlAgent {
}
}
private static String getLogImplTypeFromAgentArgs(@NotNull final Map<String, String> kvs) {
private static String getLogImplTypeFromAgentArgs(final Map<String, String> kvs) {
return kvs.get(Logger.TTL_AGENT_LOGGER_KEY);
}
@ -202,15 +196,15 @@ public final class TtlAgent {
return isOptionSetOrTrue(kvs, TTL_AGENT_ENABLE_TIMER_TASK_KEY);
}
private static boolean isOptionSetOrFalse(@Nullable final Map<String, String> kvs, @NotNull String key) {
private static boolean isOptionSetOrFalse( final Map<String, String> kvs, String key) {
return isOptionSetOrFalse(kvs, key, false);
}
private static boolean isOptionSetOrTrue(@Nullable final Map<String, String> kvs, @NotNull String key) {
private static boolean isOptionSetOrTrue( final Map<String, String> kvs, String key) {
return isOptionSetOrFalse(kvs, key, true);
}
private static boolean isOptionSetOrFalse(@Nullable final Map<String, String> kvs, @NotNull String key, boolean defaultValue) {
private static boolean isOptionSetOrFalse( final Map<String, String> kvs, String key, boolean defaultValue) {
if (null == kvs) return defaultValue;
final boolean hasEnableKey = kvs.containsKey(key);
@ -222,8 +216,8 @@ public final class TtlAgent {
/**
* Split to {@code json} like String({@code "k1:v1,k2:v2"}) to KV map({@code "k1"->"v1", "k2"->"v2"}).
*/
@NotNull
static Map<String, String> splitCommaColonStringToKV(@Nullable String commaColonString) {
static Map<String, String> splitCommaColonStringToKV( String commaColonString) {
Map<String, String> ret = new HashMap<String, String>();
if (commaColonString == null || commaColonString.trim().length() == 0) return ret;

10
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/TtlTransformer.java

@ -3,8 +3,6 @@ package com.fr.third.alibaba.ttl.threadpool.agent;
import com.fr.third.alibaba.ttl.threadpool.agent.internal.logging.Logger;
import com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.ClassInfo;
import com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.JavassistTransformlet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.instrument.ClassFileTransformer;
@ -29,7 +27,7 @@ public class TtlTransformer implements ClassFileTransformer {
* see {@code @return} of {@link ClassFileTransformer#transform(ClassLoader, String, Class, ProtectionDomain, byte[])}
*/
// [ERROR] com.alibaba.ttl.threadpool.agent.TtlTransformer.transform(ClassLoader, String, Class, ProtectionDomain, byte[])
// [ERROR] com.fr.third.alibaba.ttl.threadpool.agent.TtlTransformer.transform(ClassLoader, String, Class, ProtectionDomain, byte[])
// may expose internal representation by returning TtlTransformer.NO_TRANSFORM
// the value is null, so there is NO "EI_EXPOSE_REP" problem actually.
private static final byte[] NO_TRANSFORM = null;
@ -44,8 +42,8 @@ public class TtlTransformer implements ClassFileTransformer {
}
@Override
public final byte[] transform(@Nullable final ClassLoader loader, @Nullable final String classFile, final Class<?> classBeingRedefined,
final ProtectionDomain protectionDomain, @NotNull final byte[] classFileBuffer) {
public final byte[] transform( final ClassLoader loader, final String classFile, final Class<?> classBeingRedefined,
final ProtectionDomain protectionDomain, final byte[] classFileBuffer) {
try {
// Lambda has no class file, no need to transform, just return.
if (classFile == null) return NO_TRANSFORM;
@ -67,7 +65,7 @@ public class TtlTransformer implements ClassFileTransformer {
return NO_TRANSFORM;
}
private static String toClassName(@NotNull final String classFile) {
private static String toClassName(final String classFile) {
return classFile.replace('/', '.');
}
}

27
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/internal/transformlet/ClassInfo.java

@ -1,14 +1,14 @@
package com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet;
import com.fr.third.javassist.CtClass;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.fr.third.javassist.ClassPool;
import com.fr.third.javassist.CtClass;
import com.fr.third.javassist.LoaderClassPath;
import com.fr.third.javassist.NotFoundException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* @author Jerry Lee (oldratlee at gmail dot com)
@ -18,25 +18,26 @@ public class ClassInfo {
private final String className;
private final byte[] classFileBuffer;
private final ClassLoader loader;
private static Set<String> paths = new CopyOnWriteArraySet<>();
// SuppressFBWarnings for classFileBuffer parameter:
// [ERROR] new com.alibaba.ttl.threadpool.agent.internal.transformlet.ClassInfo(String, byte[], ClassLoader)
// [ERROR] new com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.ClassInfo(String, byte[], ClassLoader)
// may expose internal representation by storing an externally mutable object
// into ClassInfo.classFileBuffer
public ClassInfo(@NotNull String className, byte[] classFileBuffer, @Nullable ClassLoader loader) {
public ClassInfo(String className, byte[] classFileBuffer, ClassLoader loader) {
this.className = className;
this.classFileBuffer = classFileBuffer;
this.loader = loader;
}
@NotNull
public String getClassName() {
return className;
}
private CtClass ctClass;
@NotNull
public CtClass getCtClass() throws IOException {
if (ctClass != null) return ctClass;
@ -46,7 +47,13 @@ public class ClassInfo {
} else {
classPool.appendClassPath(new LoaderClassPath(loader));
}
for (String path: paths) {
try {
classPool.appendClassPath(path);
} catch (NotFoundException e) {
e.printStackTrace();
}
}
final CtClass clazz = classPool.makeClass(new ByteArrayInputStream(classFileBuffer), false);
clazz.defrost();
@ -63,4 +70,8 @@ public class ClassInfo {
public void setModified() {
this.modified = true;
}
public static void addPath(String path) {
paths.add(path);
}
}

3
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/internal/transformlet/JavassistTransformlet.java

@ -1,6 +1,5 @@
package com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet;
import org.jetbrains.annotations.NotNull;
import com.fr.third.javassist.CannotCompileException;
import com.fr.third.javassist.NotFoundException;
@ -13,5 +12,5 @@ import java.io.IOException;
* @since 2.5.1
*/
public interface JavassistTransformlet {
void doTransform(@NotNull ClassInfo classInfo) throws IOException, NotFoundException, CannotCompileException;
void doTransform(ClassInfo classInfo) throws IOException, NotFoundException, CannotCompileException;
}

28
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/internal/transformlet/impl/TtlExecutorTransformlet.java

@ -1,12 +1,8 @@
package com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.impl;
import com.fr.third.alibaba.ttl.threadpool.TtlExecutors;
import com.fr.third.alibaba.ttl.threadpool.agent.internal.logging.Logger;
import com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.ClassInfo;
import com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.JavassistTransformlet;
import com.fr.third.alibaba.ttl.TtlCallable;
import com.fr.third.alibaba.ttl.TtlRunnable;
import org.jetbrains.annotations.NotNull;
import com.fr.third.javassist.*;
import java.io.IOException;
@ -42,8 +38,8 @@ public class TtlExecutorTransformlet implements JavassistTransformlet {
EXECUTOR_CLASS_NAMES.add(THREAD_POOL_EXECUTOR_CLASS_NAME);
EXECUTOR_CLASS_NAMES.add("java.util.concurrent.ScheduledThreadPoolExecutor");
PARAM_TYPE_NAME_TO_DECORATE_METHOD_CLASS.put(RUNNABLE_CLASS_NAME, "com.alibaba.ttl.TtlRunnable");
PARAM_TYPE_NAME_TO_DECORATE_METHOD_CLASS.put("java.util.concurrent.Callable", "com.alibaba.ttl.TtlCallable");
PARAM_TYPE_NAME_TO_DECORATE_METHOD_CLASS.put(RUNNABLE_CLASS_NAME, "com.fr.third.alibaba.ttl.TtlRunnable");
PARAM_TYPE_NAME_TO_DECORATE_METHOD_CLASS.put("java.util.concurrent.Callable", "com.fr.third.alibaba.ttl.TtlCallable");
}
private static final String THREAD_FACTORY_CLASS_NAME = "java.util.concurrent.ThreadFactory";
@ -55,7 +51,7 @@ public class TtlExecutorTransformlet implements JavassistTransformlet {
}
@Override
public void doTransform(@NotNull final ClassInfo classInfo) throws IOException, NotFoundException, CannotCompileException {
public void doTransform(final ClassInfo classInfo) throws IOException, NotFoundException, CannotCompileException {
if (EXECUTOR_CLASS_NAMES.contains(classInfo.getClassName())) {
final CtClass clazz = classInfo.getCtClass();
@ -84,10 +80,10 @@ public class TtlExecutorTransformlet implements JavassistTransformlet {
/**
* @see TtlRunnable#get(Runnable, boolean, boolean)
* @see TtlCallable#get(Callable, boolean, boolean)
* @see Utils#setAutoWrapperAttachment(Object)
* @see com.fr.third.alibaba.ttl.threadpool.TtlUtils#setAutoWrapperAttachment(Object)
*/
// [ERROR] Format string should use %n rather than \n
private void updateSubmitMethodsOfExecutorClass_decorateToTtlWrapperAndSetAutoWrapperAttachment(@NotNull final CtMethod method) throws NotFoundException, CannotCompileException {
private void updateSubmitMethodsOfExecutorClass_decorateToTtlWrapperAndSetAutoWrapperAttachment(final CtMethod method) throws NotFoundException, CannotCompileException {
final int modifiers = method.getModifiers();
if (!Modifier.isPublic(modifiers) || Modifier.isStatic(modifiers)) return;
@ -100,7 +96,7 @@ public class TtlExecutorTransformlet implements JavassistTransformlet {
// decorate to TTL wrapper,
// and then set AutoWrapper attachment/Tag
"$%d = %s.get($%d, false, true);"
+ "\ncom.alibaba.ttl.threadpool.agent.internal.transformlet.impl.Utils.setAutoWrapperAttachment($%<d);",
+ "\ncom.fr.third.alibaba.ttl.threadpool.TtlUtils.setAutoWrapperAttachment($%<d);",
i + 1, PARAM_TYPE_NAME_TO_DECORATE_METHOD_CLASS.get(paramTypeName), i + 1);
logger.info("insert code before method " + Utils.signatureOfMethod(method) + " of class " + method.getDeclaringClass().getName() + ": " + code);
insertCode.append(code);
@ -112,14 +108,14 @@ public class TtlExecutorTransformlet implements JavassistTransformlet {
/**
* @see TtlExecutors#getDisableInheritableThreadFactory(java.util.concurrent.ThreadFactory)
*/
private void updateConstructorDisableInheritable(@NotNull final CtClass clazz) throws NotFoundException, CannotCompileException {
private void updateConstructorDisableInheritable(final CtClass clazz) throws NotFoundException, CannotCompileException {
for (CtConstructor constructor : clazz.getDeclaredConstructors()) {
final CtClass[] parameterTypes = constructor.getParameterTypes();
final StringBuilder insertCode = new StringBuilder();
for (int i = 0; i < parameterTypes.length; i++) {
final String paramTypeName = parameterTypes[i].getName();
if (THREAD_FACTORY_CLASS_NAME.equals(paramTypeName)) {
String code = String.format("$%d = com.alibaba.ttl.threadpool.TtlExecutors.getDisableInheritableThreadFactory($%<d);", i + 1);
String code = String.format("$%d = com.fr.third.alibaba.ttl.threadpool.TtlExecutors.getDisableInheritableThreadFactory($%<d);", i + 1);
logger.info("insert code before method " + Utils.signatureOfMethod(constructor) + " of class " + constructor.getDeclaringClass().getName() + ": " + code);
insertCode.append(code);
}
@ -129,9 +125,9 @@ public class TtlExecutorTransformlet implements JavassistTransformlet {
}
/**
* @see Utils#unwrapIfIsAutoWrapper(Runnable)
* @see com.fr.third.alibaba.ttl.threadpool.TtlUtils#unwrapIfIsAutoWrapper(Runnable)
*/
private boolean updateBeforeAndAfterExecuteMethodOfExecutorSubclass(@NotNull final CtClass clazz) throws NotFoundException, CannotCompileException {
private boolean updateBeforeAndAfterExecuteMethodOfExecutorSubclass(final CtClass clazz) throws NotFoundException, CannotCompileException {
final CtClass runnableClass = clazz.getClassPool().get(RUNNABLE_CLASS_NAME);
final CtClass threadClass = clazz.getClassPool().get("java.lang.Thread");
final CtClass throwableClass = clazz.getClassPool().get("java.lang.Throwable");
@ -140,7 +136,7 @@ public class TtlExecutorTransformlet implements JavassistTransformlet {
try {
final CtMethod beforeExecute = clazz.getDeclaredMethod("beforeExecute", new CtClass[]{threadClass, runnableClass});
// unwrap runnable if IsAutoWrapper
String code = "$2 = com.alibaba.ttl.threadpool.agent.internal.transformlet.impl.Utils.unwrapIfIsAutoWrapper($2);";
String code = "$2 = com.fr.third.alibaba.ttl.threadpool.TtlUtils.unwrapIfIsAutoWrapper($2);";
logger.info("insert code before method " + Utils.signatureOfMethod(beforeExecute) + " of class " + beforeExecute.getDeclaringClass().getName() + ": " + code);
beforeExecute.insertBefore(code);
modified = true;
@ -151,7 +147,7 @@ public class TtlExecutorTransformlet implements JavassistTransformlet {
try {
final CtMethod afterExecute = clazz.getDeclaredMethod("afterExecute", new CtClass[]{runnableClass, throwableClass});
// unwrap runnable if IsAutoWrapper
String code = "$1 = com.alibaba.ttl.threadpool.agent.internal.transformlet.impl.Utils.unwrapIfIsAutoWrapper($1);";
String code = "$1 = com.fr.third.alibaba.ttl.threadpool.TtlUtils.unwrapIfIsAutoWrapper($1);";
logger.info("insert code before method " + Utils.signatureOfMethod(afterExecute) + " of class " + afterExecute.getDeclaringClass().getName() + ": " + code);
afterExecute.insertBefore(code);
modified = true;

20
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/internal/transformlet/impl/TtlForkJoinTransformlet.java

@ -1,10 +1,8 @@
package com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.impl;
import com.fr.third.alibaba.ttl.spi.TtlEnhanced;
import com.fr.third.alibaba.ttl.threadpool.agent.internal.logging.Logger;
import com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.ClassInfo;
import com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.JavassistTransformlet;
import org.jetbrains.annotations.NotNull;
import com.fr.third.javassist.*;
import java.io.IOException;
@ -32,7 +30,7 @@ public class TtlForkJoinTransformlet implements JavassistTransformlet {
}
@Override
public void doTransform(@NotNull final ClassInfo classInfo) throws IOException, NotFoundException, CannotCompileException {
public void doTransform(final ClassInfo classInfo) throws IOException, NotFoundException, CannotCompileException {
if (FORK_JOIN_TASK_CLASS_NAME.equals(classInfo.getClassName())) {
updateForkJoinTaskClass(classInfo.getCtClass());
classInfo.setModified();
@ -43,38 +41,38 @@ public class TtlForkJoinTransformlet implements JavassistTransformlet {
}
/**
* @see Utils#doCaptureWhenNotTtlEnhanced(java.lang.Object)
* @see com.fr.third.alibaba.ttl.threadpool.TtlUtils#doCaptureWhenNotTtlEnhanced(java.lang.Object)
*/
private void updateForkJoinTaskClass(@NotNull final CtClass clazz) throws CannotCompileException, NotFoundException {
private void updateForkJoinTaskClass(final CtClass clazz) throws CannotCompileException, NotFoundException {
final String className = clazz.getName();
// add new field
final String capturedFieldName = "captured$field$added$by$ttl";
final CtField capturedField = CtField.make("private final Object " + capturedFieldName + ";", clazz);
clazz.addField(capturedField, "com.alibaba.ttl.threadpool.agent.internal.transformlet.impl.Utils.doCaptureWhenNotTtlEnhanced(this);");
clazz.addField(capturedField, "com.fr.third.alibaba.ttl.threadpool.TtlUtils.doCaptureWhenNotTtlEnhanced(this);");
logger.info("add new field " + capturedFieldName + " to class " + className);
final CtMethod doExecMethod = clazz.getDeclaredMethod("doExec", new CtClass[0]);
final String doExec_renamed_method_name = Utils.renamedMethodNameByTtl(doExecMethod);
final String beforeCode = "if (this instanceof " + TtlEnhanced.class.getName() + ") {\n" + // if the class is already TTL enhanced(eg: com.alibaba.ttl.TtlRecursiveTask)
final String beforeCode = "if (this instanceof " + "com.fr.third.alibaba.ttl.spi.TtlEnhanced" + ") {\n" + // if the class is already TTL enhanced(eg: com.fr.third.alibaba.ttl.TtlRecursiveTask)
" return " + doExec_renamed_method_name + "($$);\n" + // return directly/do nothing
"}\n" +
"Object backup = com.alibaba.ttl.TransmittableThreadLocal.Transmitter.replay(" + capturedFieldName + ");";
"Object backup = com.fr.third.alibaba.ttl.TransmittableThreadLocal.Transmitter.replay(" + capturedFieldName + ");";
final String finallyCode = "com.alibaba.ttl.TransmittableThreadLocal.Transmitter.restore(backup);";
final String finallyCode = "com.fr.third.alibaba.ttl.TransmittableThreadLocal.Transmitter.restore(backup);";
Utils.doTryFinallyForMethod(doExecMethod, doExec_renamed_method_name, beforeCode, finallyCode);
}
private void updateConstructorDisableInheritable(@NotNull final CtClass clazz) throws NotFoundException, CannotCompileException {
private void updateConstructorDisableInheritable(final CtClass clazz) throws NotFoundException, CannotCompileException {
for (CtConstructor constructor : clazz.getDeclaredConstructors()) {
final CtClass[] parameterTypes = constructor.getParameterTypes();
final StringBuilder insertCode = new StringBuilder();
for (int i = 0; i < parameterTypes.length; i++) {
final String paramTypeName = parameterTypes[i].getName();
if (FORK_JOIN_WORKER_THREAD_FACTORY_CLASS_NAME.equals(paramTypeName)) {
String code = String.format("$%d = com.alibaba.ttl.threadpool.TtlForkJoinPoolHelper.getDisableInheritableForkJoinWorkerThreadFactory($%<d);", i + 1);
String code = String.format("$%d = com.fr.third.alibaba.ttl.threadpool.TtlForkJoinPoolHelper.getDisableInheritableForkJoinWorkerThreadFactory($%<d);", i + 1);
logger.info("insert code before method " + Utils.signatureOfMethod(constructor) + " of class " + constructor.getDeclaringClass().getName() + ": " + code);
insertCode.append(code);
}

15
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/internal/transformlet/impl/TtlTimerTaskTransformlet.java

@ -3,13 +3,10 @@ package com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.impl;
import com.fr.third.alibaba.ttl.threadpool.agent.internal.logging.Logger;
import com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.ClassInfo;
import com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.JavassistTransformlet;
import org.jetbrains.annotations.NotNull;
import com.fr.third.javassist.*;
import java.io.IOException;
import static com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.impl.Utils.doTryFinallyForMethod;
/**
* TTL {@link JavassistTransformlet} for {@link java.util.TimerTask}.
*
@ -26,7 +23,7 @@ public class TtlTimerTaskTransformlet implements JavassistTransformlet {
private static final String RUN_METHOD_NAME = "run";
@Override
public void doTransform(@NotNull final ClassInfo classInfo) throws IOException, NotFoundException, CannotCompileException {
public void doTransform(final ClassInfo classInfo) throws IOException, NotFoundException, CannotCompileException {
if (TIMER_TASK_CLASS_NAME.equals(classInfo.getClassName())) return; // No need transform TimerTask class
final CtClass clazz = classInfo.getCtClass();
@ -50,21 +47,21 @@ public class TtlTimerTaskTransformlet implements JavassistTransformlet {
}
/**
* @see Utils#doCaptureWhenNotTtlEnhanced(java.lang.Object)
* @see com.fr.third.alibaba.ttl.threadpool.TtlUtils#doCaptureWhenNotTtlEnhanced(java.lang.Object)
*/
private void updateTimerTaskClass(@NotNull final CtClass clazz) throws CannotCompileException, NotFoundException {
private void updateTimerTaskClass(final CtClass clazz) throws CannotCompileException, NotFoundException {
final String className = clazz.getName();
// add new field
final String capturedFieldName = "captured$field$added$by$ttl";
final CtField capturedField = CtField.make("private final Object " + capturedFieldName + ";", clazz);
clazz.addField(capturedField, "com.alibaba.ttl.threadpool.agent.internal.transformlet.impl.Utils.doCaptureWhenNotTtlEnhanced(this);");
clazz.addField(capturedField, "com.fr.third.alibaba.ttl.threadpool.TtlUtils.doCaptureWhenNotTtlEnhanced(this);");
logger.info("add new field " + capturedFieldName + " to class " + className);
final CtMethod runMethod = clazz.getDeclaredMethod(RUN_METHOD_NAME, new CtClass[0]);
final String beforeCode = "Object backup = com.alibaba.ttl.TransmittableThreadLocal.Transmitter.replay(" + capturedFieldName + ");";
final String finallyCode = "com.alibaba.ttl.TransmittableThreadLocal.Transmitter.restore(backup);";
final String beforeCode = "Object backup = com.fr.third.alibaba.ttl.TransmittableThreadLocal.Transmitter.replay(" + capturedFieldName + ");";
final String finallyCode = "com.fr.third.alibaba.ttl.TransmittableThreadLocal.Transmitter.restore(backup);";
Utils.doTryFinallyForMethod(runMethod, beforeCode, finallyCode);
}

49
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/internal/transformlet/impl/Utils.java

@ -1,17 +1,10 @@
package com.fr.third.alibaba.ttl.threadpool.agent.internal.transformlet.impl;
import com.fr.third.alibaba.ttl.TtlRunnable;
import com.fr.third.alibaba.ttl.spi.TtlAttachments;
import com.fr.third.alibaba.ttl.spi.TtlEnhanced;
import com.fr.third.alibaba.ttl.threadpool.agent.internal.logging.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.fr.third.javassist.*;
import java.lang.reflect.Modifier;
import static com.fr.third.alibaba.ttl.TransmittableThreadLocal.Transmitter.capture;
/**
* @author Jerry Lee (oldratlee at gmail dot com)
* @since 2.6.0
@ -26,8 +19,8 @@ public class Utils {
* @param method method object
* @return method signature string
*/
@NotNull
static String signatureOfMethod(@NotNull final CtBehavior method) throws NotFoundException {
static String signatureOfMethod(final CtBehavior method) throws NotFoundException {
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(Modifier.toString(method.getModifiers()));
@ -48,16 +41,16 @@ public class Utils {
return stringBuilder.toString();
}
@NotNull
static String renamedMethodNameByTtl(@NotNull CtMethod method) {
static String renamedMethodNameByTtl(CtMethod method) {
return "original$" + method.getName() + "$method$renamed$by$ttl";
}
static void doTryFinallyForMethod(@NotNull CtMethod method, @NotNull String beforeCode, @NotNull String finallyCode) throws CannotCompileException, NotFoundException {
static void doTryFinallyForMethod(CtMethod method, String beforeCode, String finallyCode) throws CannotCompileException, NotFoundException {
doTryFinallyForMethod(method, renamedMethodNameByTtl(method), beforeCode, finallyCode);
}
static void doTryFinallyForMethod(@NotNull CtMethod method, @NotNull String renamedMethodName, @NotNull String beforeCode, @NotNull String finallyCode) throws CannotCompileException, NotFoundException {
static void doTryFinallyForMethod(CtMethod method, String renamedMethodName, String beforeCode, String finallyCode) throws CannotCompileException, NotFoundException {
final CtClass clazz = method.getDeclaringClass();
final CtMethod newMethod = CtNewMethod.copy(method, clazz, null);
@ -86,34 +79,4 @@ public class Utils {
clazz.addMethod(newMethod);
logger.info("insert code around method " + signatureOfMethod(newMethod) + " of class " + clazz.getName() + ": " + code);
}
@Nullable
public static Object doCaptureWhenNotTtlEnhanced(@Nullable Object obj) {
if (obj instanceof TtlEnhanced) return null;
else return capture();
}
public static void setAutoWrapperAttachment(@Nullable Object ttlAttachment) {
if (notTtlAttachments(ttlAttachment)) return;
((TtlAttachments) ttlAttachment).setTtlAttachment(TtlAttachments.KEY_IS_AUTO_WRAPPER, true);
}
@Nullable
public static Runnable unwrapIfIsAutoWrapper(@Nullable Runnable runnable) {
if (isAutoWrapper(runnable)) return TtlRunnable.unwrap(runnable);
else return runnable;
}
private static boolean notTtlAttachments(@Nullable Object ttlAttachment) {
return !(ttlAttachment instanceof TtlAttachments);
}
private static boolean isAutoWrapper(@Nullable Runnable ttlAttachments) {
if (notTtlAttachments(ttlAttachments)) return false;
final Boolean value = ((TtlAttachments) ttlAttachments).getTtlAttachment(TtlAttachments.KEY_IS_AUTO_WRAPPER);
if (value == null) return false;
return value;
}
}

2
fine-transmittable-thread-local/src/main/java/com/fr/third/alibaba/ttl/threadpool/agent/package-info.java

@ -4,5 +4,7 @@
* @author Jerry Lee (oldratlee at gmail dot com)
* @see com.fr.third.alibaba.ttl.threadpool.agent.TtlAgent
* @see <a href="https://docs.oracle.com/javase/10/docs/api/java/lang/instrument/package-summary.html">The mechanism for instrumentation</a>
*
* 此包中的不允许直接使用ttl中其他代码
*/
package com.fr.third.alibaba.ttl.threadpool.agent;

Loading…
Cancel
Save