-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix language detection logic (#7245)
- Loading branch information
1 parent
dddda34
commit 97b5fe7
Showing
27 changed files
with
463 additions
and
330 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
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
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
43 changes: 43 additions & 0 deletions
43
...t/agent-ci-visibility/src/main/java/datadog/trace/civisibility/source/index/Language.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,43 @@ | ||
package datadog.trace.civisibility.source.index; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
enum Language { | ||
JAVA(".java", false), | ||
GROOVY(".groovy", false), | ||
KOTLIN(".kt", false), | ||
SCALA(".scala", false), | ||
GHERKIN(".feature", true); | ||
|
||
private static final Language[] UNIVERSE = Language.values(); | ||
|
||
private final String extension; | ||
private final boolean nonCode; | ||
|
||
Language(String extension, boolean nonCode) { | ||
this.extension = extension; | ||
this.nonCode = nonCode; | ||
} | ||
|
||
public String getExtension() { | ||
return extension; | ||
} | ||
|
||
public boolean isNonCode() { | ||
return nonCode; | ||
} | ||
|
||
@Nullable | ||
static Language getByFileName(String fileName) { | ||
for (Language language : UNIVERSE) { | ||
if (fileName.endsWith(language.extension)) { | ||
return language; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
static Language getByOrdinal(int ordinal) { | ||
return UNIVERSE[ordinal]; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...ci-visibility/src/main/java/datadog/trace/civisibility/source/index/LanguageDetector.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,54 @@ | ||
package datadog.trace.civisibility.source.index; | ||
|
||
import datadog.trace.civisibility.source.Utils; | ||
import java.io.IOException; | ||
import java.lang.annotation.Annotation; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
class LanguageDetector { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(LanguageDetector.class); | ||
|
||
@Nullable | ||
public Language detect(@Nonnull Class<?> clazz) { | ||
Class<?> c = clazz; | ||
while (c != null) { | ||
Class<?>[] interfaces = c.getInterfaces(); | ||
for (Class<?> anInterface : interfaces) { | ||
String interfaceName = anInterface.getName(); | ||
if ("groovy.lang.GroovyObject".equals(interfaceName)) { | ||
return Language.GROOVY; | ||
} | ||
} | ||
|
||
Annotation[] annotations = c.getAnnotations(); | ||
for (Annotation annotation : annotations) { | ||
Class<? extends Annotation> annotationType = annotation.annotationType(); | ||
if ("kotlin.Metadata".equals(annotationType.getName())) { | ||
return Language.KOTLIN; | ||
} | ||
if ("scala.reflect.ScalaSignature".equals(annotationType.getName())) { | ||
return Language.SCALA; | ||
} | ||
} | ||
|
||
c = c.getSuperclass(); | ||
} | ||
|
||
try { | ||
// try to parse the class file to see if it contains filename attribute | ||
String fileName = Utils.getFileName(clazz); | ||
if (fileName != null) { | ||
return Language.getByFileName(fileName); | ||
} | ||
} catch (IOException e) { | ||
log.debug("Error while trying to read filename from class {}", clazz.getName(), e); | ||
} | ||
|
||
// assuming Java | ||
return Language.JAVA; | ||
} | ||
} |
Oops, something went wrong.