-
Notifications
You must be signed in to change notification settings - Fork 325
enhance class name detection #3746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jackshirazi
merged 7 commits into
elastic:main
from
wolframhaussig:feature/class-name-detection
Sep 2, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7a475e1
enhance class name detection
wolframhaussig efc249f
added fix and test for nested scala objects
wolframhaussig b5f2406
added changelog entry
wolframhaussig 211a043
remove regex
wolframhaussig 62d96d7
fix anonymous class names
wolframhaussig 4b4ab46
fix unit tests
wolframhaussig 84dd423
Merge branch 'main' into feature/class-name-detection
wolframhaussig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
102 changes: 102 additions & 0 deletions
102
apm-agent-plugin-sdk/src/main/java/co/elastic/apm/agent/sdk/bytebuddy/ClassNameParser.java
This file contains hidden or 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,102 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.apm.agent.sdk.bytebuddy; | ||
|
|
||
| /** | ||
| * Utility class to extract the correct simple class name | ||
| */ | ||
| public class ClassNameParser { | ||
| /** | ||
| * Utility class, do not instantiate | ||
| */ | ||
| private ClassNameParser() {} | ||
| /** | ||
| * returns true if the string only contains digits | ||
| * @param str the string to check | ||
| * @return | ||
| */ | ||
| private static boolean isNumeric(String str) { | ||
| for(int i = 0; i< str.length(); i++) { | ||
| if(!Character.isDigit(str.charAt(i))) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| /** | ||
| * this method parses the anonymous class name from the full class name | ||
| * @param className | ||
| * @return | ||
| */ | ||
| private static String getAnonymousClassName(String className) { | ||
| int lastDollarIndex = className.lastIndexOf('$'); | ||
| int currentDollarIndex; | ||
| String nestedClassName; | ||
| // we do not want to show just a number for anonymous classes, so we walk back until we find a named (normal or nested) class | ||
| do { | ||
| currentDollarIndex = className.lastIndexOf('$', lastDollarIndex -1); | ||
| nestedClassName = className.substring(currentDollarIndex + 1, lastDollarIndex); | ||
| lastDollarIndex = currentDollarIndex; | ||
| } while(currentDollarIndex != -1 && isNumeric(nestedClassName)); | ||
| return className.substring(currentDollarIndex + 1); | ||
| } | ||
| /** | ||
| * Parses the class name from nested and anonymous classes (containing a $ sign) | ||
| * <p> | ||
| * Note: We cannot know if the $ sign is part of the class name or denotes a nested/anonymous class. As $ signs should not be used in class names anyway, | ||
| * we handle them always as a class separator | ||
| * </p> | ||
| * @param className | ||
| * @return | ||
| */ | ||
| private static String getNestedClassName(String className) { | ||
| int dollarIndex = className.lastIndexOf('$'); | ||
| String innerClassName = className.substring(dollarIndex + 1); | ||
| if(isNumeric(innerClassName)) { | ||
| // this is an anonymous inner class | ||
| className = getAnonymousClassName(className); | ||
| } else { | ||
| className = innerClassName; | ||
| } | ||
| return className; | ||
| } | ||
| /** | ||
| * Parses the simple class name from a class name | ||
| * @param className | ||
| * @return | ||
| */ | ||
| public static String parse(String className) { | ||
| //remove package name | ||
| className = className.substring(className.lastIndexOf('.') + 1); | ||
| if(className.contains("$")) { | ||
| if(className.endsWith("$")) { | ||
| // this can happen if the source code was in Scala and the object keyword was used | ||
| // https://www.toptal.com/scala/scala-bytecode-and-the-jvm | ||
| className = className.substring(0, className.length() - 1); | ||
| if(className.contains("$")) | ||
| { | ||
| className = getNestedClassName(className); | ||
| } | ||
| } else { | ||
| className = getNestedClassName(className); | ||
| } | ||
| } | ||
| return className; | ||
| } | ||
| } | ||
This file contains hidden or 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
83 changes: 83 additions & 0 deletions
83
...gent-plugin-sdk/src/test/java/co/elastic/apm/agent/sdk/bytebuddy/ClassNameParserTest.java
This file contains hidden or 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,83 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.apm.agent.sdk.bytebuddy; | ||
|
|
||
| import static org.assertj.core.api.Assertions.*; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import co.elastic.apm.agent.sdk.bytebuddy.clazzes.ParentClass.InnerClass; | ||
| import co.elastic.apm.agent.sdk.bytebuddy.clazzes.ParentClass.InnerClass.NestedInnerClass; | ||
| import co.elastic.apm.agent.sdk.bytebuddy.clazzes.ParentObject.ChildScalaObject$; | ||
| import co.elastic.apm.agent.sdk.bytebuddy.clazzes.SimpleClass; | ||
| import co.elastic.apm.agent.sdk.bytebuddy.clazzes.AnonymousClass; | ||
| import co.elastic.apm.agent.sdk.bytebuddy.clazzes.AnonymousNestedClass; | ||
| import co.elastic.apm.agent.sdk.bytebuddy.clazzes.Dollar$Class; | ||
| import co.elastic.apm.agent.sdk.bytebuddy.clazzes.ScalaObject$; | ||
|
|
||
| class ClassNameParserTest { | ||
|
|
||
| @Test | ||
| void testSimpleClassName() { | ||
| String className = ClassNameParser.parse(SimpleClass.class.getName()); | ||
| assertThat(className).isEqualTo("SimpleClass"); | ||
| } | ||
| @Test | ||
| void testNestedClassName() { | ||
| String className = ClassNameParser.parse(InnerClass.class.getName()); | ||
| assertThat(className).isEqualTo("InnerClass"); | ||
| } | ||
| @Test | ||
| void testDoubleNestedClassName() { | ||
| String className = ClassNameParser.parse(NestedInnerClass.class.getName()); | ||
| assertThat(className).isEqualTo("NestedInnerClass"); | ||
| } | ||
| @Test | ||
| void testDollarClassName() { | ||
| String className = ClassNameParser.parse(Dollar$Class.class.getName()); | ||
| //We have no way to know if the class name is Dollar$Class or Class is a nested class of Dollar | ||
| //As dollar signs should not be used in names, it should be fine to detect Class instead of Dollar$Class | ||
| assertThat(className).describedAs("Dollar signs should not be used in names").isEqualTo("Class"); | ||
| } | ||
| @Test | ||
| void testAnonymousClassName() { | ||
| String className = ClassNameParser.parse(AnonymousClass.getAnonymousClass().getName()); | ||
| assertThat(className).isEqualTo("AnonymousClass$1"); | ||
| } | ||
| @Test | ||
| void testAnonymousNestedClassName() { | ||
| String className = ClassNameParser.parse(InnerClass.getAnonymousClass().getName()); | ||
| assertThat(className).isEqualTo("InnerClass$1"); | ||
| } | ||
| @Test | ||
| void testAnonymousInAnonymousClassName() { | ||
| String className = ClassNameParser.parse(AnonymousNestedClass.getAnonymousClass().getName()); | ||
| assertThat(className).isEqualTo("AnonymousNestedClass$1$1"); | ||
| } | ||
| @Test | ||
| void testScalaObjectClassName() { | ||
| String className = ClassNameParser.parse(ScalaObject$.class.getName()); | ||
| assertThat(className).isEqualTo("ScalaObject"); | ||
| } | ||
| @Test | ||
| void testNestedScalaObjectClassName() { | ||
| String className = ClassNameParser.parse(ChildScalaObject$.class.getName()); | ||
| assertThat(className).isEqualTo("ChildScalaObject"); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
...t-plugin-sdk/src/test/java/co/elastic/apm/agent/sdk/bytebuddy/clazzes/AnonymousClass.java
This file contains hidden or 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,30 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.apm.agent.sdk.bytebuddy.clazzes; | ||
|
|
||
| public class AnonymousClass { | ||
| public static Class<?> getAnonymousClass() { | ||
| return new Object() { | ||
| @Override | ||
| public String toString() { | ||
| return "foo"; | ||
| } | ||
| }.getClass(); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
...in-sdk/src/test/java/co/elastic/apm/agent/sdk/bytebuddy/clazzes/AnonymousNestedClass.java
This file contains hidden or 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,39 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.apm.agent.sdk.bytebuddy.clazzes; | ||
|
|
||
| public class AnonymousNestedClass { | ||
| public static Class<?> getAnonymousClass() { | ||
| Factory f = new Factory() { | ||
| @Override | ||
| public Object getObject() { | ||
| return new Object() { | ||
| @Override | ||
| public String toString() { | ||
| return "foo"; | ||
| } | ||
| }; | ||
| } | ||
| }; | ||
| return f.getObject().getClass(); | ||
| } | ||
| public static interface Factory { | ||
| Object getObject(); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
...ent-plugin-sdk/src/test/java/co/elastic/apm/agent/sdk/bytebuddy/clazzes/Dollar$Class.java
This file contains hidden or 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,23 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.apm.agent.sdk.bytebuddy.clazzes; | ||
|
|
||
| public class Dollar$Class { | ||
|
|
||
| } |
36 changes: 36 additions & 0 deletions
36
...gent-plugin-sdk/src/test/java/co/elastic/apm/agent/sdk/bytebuddy/clazzes/ParentClass.java
This file contains hidden or 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,36 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.apm.agent.sdk.bytebuddy.clazzes; | ||
|
|
||
| public class ParentClass { | ||
|
|
||
| public static class InnerClass { | ||
| public static Class<?> getAnonymousClass() { | ||
| return new Object() { | ||
| @Override | ||
| public String toString() { | ||
| return "foo"; | ||
| } | ||
| }.getClass(); | ||
| } | ||
| public static class NestedInnerClass { | ||
|
|
||
| } | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
...ent-plugin-sdk/src/test/java/co/elastic/apm/agent/sdk/bytebuddy/clazzes/ParentObject.java
This file contains hidden or 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,29 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.apm.agent.sdk.bytebuddy.clazzes; | ||
|
|
||
| public class ParentObject { | ||
| /** | ||
| * Scala objects generate a class with a dollar at the end | ||
| * https://www.toptal.com/scala/scala-bytecode-and-the-jvm | ||
| */ | ||
| public class ChildScalaObject$ { | ||
|
|
||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
...ent-plugin-sdk/src/test/java/co/elastic/apm/agent/sdk/bytebuddy/clazzes/ScalaObject$.java
This file contains hidden or 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,27 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.apm.agent.sdk.bytebuddy.clazzes; | ||
|
|
||
| /** | ||
| * Scala objects generate a class with a dollar at the end | ||
| * https://www.toptal.com/scala/scala-bytecode-and-the-jvm | ||
| */ | ||
| public class ScalaObject$ { | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.