-
Notifications
You must be signed in to change notification settings - Fork 872
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid looking up annotation types during type matching (#5906)
- Loading branch information
Showing
3 changed files
with
256 additions
and
7 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
64 changes: 64 additions & 0 deletions
64
...src/test/java/io/opentelemetry/javaagent/tooling/muzzle/AgentCachingPoolStrategyTest.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,64 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.tooling.muzzle; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.declaresMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import io.opentelemetry.test.AnnotatedTestClass; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.util.Enumeration; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.dynamic.ClassFileLocator; | ||
import net.bytebuddy.pool.TypePool; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class AgentCachingPoolStrategyTest { | ||
|
||
@Test | ||
void testSkipResourceLookupForAnnotations() { | ||
ClassLoader classLoader = | ||
new ClassLoader(AgentCachingPoolStrategyTest.class.getClassLoader()) { | ||
|
||
private void checkResource(String name) { | ||
if (name.contains("TestAnnotation")) { | ||
throw new IllegalStateException("Unexpected resource lookup for " + name); | ||
} | ||
} | ||
|
||
@Override | ||
public URL getResource(String name) { | ||
checkResource(name); | ||
return super.getResource(name); | ||
} | ||
|
||
@Override | ||
public InputStream getResourceAsStream(String name) { | ||
checkResource(name); | ||
return super.getResourceAsStream(name); | ||
} | ||
|
||
@Override | ||
public Enumeration<URL> getResources(String name) throws IOException { | ||
checkResource(name); | ||
return super.getResources(name); | ||
} | ||
}; | ||
|
||
ClassFileLocator locator = ClassFileLocator.ForClassLoader.of(classLoader); | ||
TypePool pool = AgentTooling.poolStrategy().typePool(locator, classLoader); | ||
TypePool.Resolution resolution = pool.describe(AnnotatedTestClass.class.getName()); | ||
TypeDescription typeDescription = resolution.resolve(); | ||
|
||
assertTrue(isAnnotatedWith(AnnotatedTestClass.TestAnnotation.class).matches(typeDescription)); | ||
assertTrue( | ||
declaresMethod(isAnnotatedWith(AnnotatedTestClass.TestAnnotation.class)) | ||
.matches(typeDescription)); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
muzzle/src/test/java/io/opentelemetry/test/AnnotatedTestClass.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,15 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.test; | ||
|
||
@AnnotatedTestClass.TestAnnotation | ||
public class AnnotatedTestClass { | ||
|
||
@AnnotatedTestClass.TestAnnotation | ||
void testMethod() {} | ||
|
||
public @interface TestAnnotation {} | ||
} |