-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance class name detection (#3746)
* enhance class name detection * added fix and test for nested scala objects * added changelog entry * remove regex * fix anonymous class names - anonymous classes are now shown as "ParentClass$1" instead of "ParentClass" - added more testcases * fix unit tests
- Loading branch information
1 parent
803f6da
commit 753bc86
Showing
12 changed files
with
395 additions
and
3 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
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 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 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 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 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 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 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 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 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 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.