Skip to content

Commit

Permalink
Merge pull request #2172 from newrelic/detect-scala3-source
Browse files Browse the repository at this point in the history
Add scala3 source detection
  • Loading branch information
kanderson250 authored Dec 23, 2024
2 parents 80dff28 + bcf321e commit 4b929cf
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public boolean isDone() {
void detectSourceLanguages() {
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
detectJava();
detectScala3(systemClassLoader);
detectScala(systemClassLoader);
detectKotlin(systemClassLoader);
detectClojure(systemClassLoader);
Expand Down Expand Up @@ -89,9 +90,25 @@ private void detectJava() {
}
}

private void detectScala3(ClassLoader systemClassLoader) {
try {
//scala3-library removed all methods referencing the version 3 number, so we only capture the major version
//by checking for a class introduced in the scala 3 API.
Class<?> aClass = Class.forName("scala.deriving.Mirror", true, systemClassLoader);
if (aClass != null) {
recordSourceLanguageMetric("scala", "3.x");
}
} catch (ClassNotFoundException cnfe){
Agent.LOG.log(Level.FINEST, format("Class ''{0}'' was not found; scala3 is not present in the classpath", "scala.deriving.Mirror"));
} catch (Exception e) {
Agent.LOG.log(Level.FINEST, format("Exception occurred while trying to determine scala3 version", e.getMessage()));
}
}

private void detectScala(ClassLoader systemClassLoader) {
try {
// Scala has a static versionNumberString method that can be invoked to get the version number
// Scala has a static versionNumberString method that can be invoked to get the version number.
// A scala 3 app will also capture this metric, because scala 3 includes the scala 2.13 library.
Class<?> aClass = Class.forName(SCALA_VERSION_CLASS, true, systemClassLoader);
if (aClass != null) {
String version = (String) aClass.getMethod(SCALA_VERSION_METHOD).invoke(null);
Expand Down

0 comments on commit 4b929cf

Please sign in to comment.