-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add detection of ThreadLocal.initialValue() calls
- Loading branch information
Showing
5 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
feline/src/main/java/com/spotify/feline/FelineThreadLocalTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) 2022 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.spotify.feline; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
import net.bytebuddy.agent.builder.AgentBuilder; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.NamedElement; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.dynamic.DynamicType; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import net.bytebuddy.utility.JavaModule; | ||
|
||
class FelineThreadLocalTransformer implements AgentBuilder.Transformer { | ||
|
||
private final ElementMatcher.Junction<NamedElement> matcher; | ||
|
||
public static AgentBuilder.Transformer forThreadLocal() { | ||
return new FelineThreadLocalTransformer(named("initialValue")); | ||
} | ||
|
||
private FelineThreadLocalTransformer(final ElementMatcher.Junction<NamedElement> matcher) { | ||
this.matcher = matcher; | ||
} | ||
|
||
@Override | ||
public DynamicType.Builder<?> transform( | ||
final DynamicType.Builder<?> builder, | ||
final TypeDescription typeDescription, | ||
final ClassLoader classLoader, | ||
final JavaModule module) { | ||
|
||
return builder.visit(Advice.to(FutureCallAdvice.class).on(matcher)); | ||
} | ||
|
||
static class FutureCallAdvice { | ||
|
||
@Advice.OnMethodEnter | ||
static void onEnter() { | ||
FelineRuntime.acceptThreadLocalInitialValue(); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
feline/src/test/java/com/spotify/feline/ThreadLocalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) 2022 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.spotify.feline; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ThreadLocalTest { | ||
|
||
private static final AtomicInteger COUNTER = new AtomicInteger(); | ||
private static final Runnable RUNNABLE = COUNTER::incrementAndGet; | ||
|
||
@BeforeAll | ||
public static void classSetUp() { | ||
Feline.addThreadLocalInitialValueConsumer(RUNNABLE); | ||
} | ||
|
||
@AfterAll | ||
public static void teardown() { | ||
Feline.removeThreadLocalInitialValueConsumer(RUNNABLE); | ||
} | ||
|
||
@Test | ||
void testThreadLocal() { | ||
final int before = COUNTER.get(); | ||
final ThreadLocal<String> threadLocal = ThreadLocal.withInitial(() -> ""); | ||
threadLocal.get(); | ||
final int after = COUNTER.get(); | ||
assertEquals(before + 1, after); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters