Skip to content

Commit

Permalink
feat: ComponentTracker file location of Kotlin files (#20073) (#20087)
Browse files Browse the repository at this point in the history
* ComponentTracker file location of Kotlin files

* rename and deprecate

Co-authored-by: marcin <[email protected]>
  • Loading branch information
vaadin-bot and MarcinVaadin authored Sep 30, 2024
1 parent e3d797f commit 0afec75
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,21 @@ public int hashCode() {
}

/**
* Finds the Java file this location refers to.
* Finds the source file this location refers to.
*
* @param configuration
* the application configuration
* @return the Java file the location refers to, or {@code null}
* @return the source file the location refers to, or {@code null}
*/
public File findJavaFile(AbstractConfiguration configuration) {
public File findSourceFile(AbstractConfiguration configuration) {
String cls = className();
String filenameNoExt = filename().replace(".java", "");
int indexOfExt = filename().lastIndexOf(".");
String ext = filename().substring(indexOfExt);
if (!ext.equals(".java") && !ext.equals(".kt")) {
return null;
}

String filenameNoExt = filename().substring(0, indexOfExt);

if (!cls.endsWith(filenameNoExt)) {
// Check for inner class
Expand All @@ -141,11 +147,28 @@ public File findJavaFile(AbstractConfiguration configuration) {
}

File src = configuration.getJavaSourceFolder();
if (ext.equals(".kt") && src.getPath().endsWith("/java")) {
src = new File(src.getPath().substring(0,
src.getPath().lastIndexOf("/java")) + "/kotlin");
}
File javaFile = new File(src,
cls.replace(".", File.separator) + ".java");
cls.replace(".", File.separator) + ext);
return javaFile;
}

/**
* Finds the Java file this location refers to.
*
* @param configuration
* the application configuration
* @return the Java file the location refers to, or {@code null}
* @deprecated use findSourceFile
*/
@Deprecated
public File findJavaFile(AbstractConfiguration configuration) {
return findSourceFile(configuration);
}

@Override
public String toString() {
return "Component '" + className + "' at '" + filename + "' ("
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void findJavaFile_simpleClass() {
.resolve(Path.of("com", "example", "app", "MyClass.java"))
.toFile();

File javaFile = location.findJavaFile(configuration);
File javaFile = location.findSourceFile(configuration);
Assert.assertEquals(expectedFile, javaFile);
}

Expand All @@ -60,7 +60,7 @@ public void findJavaFile_simpleClass_dollarInPackage() {
.resolve(Path.of("com", "exa$mple", "app", "MyClass.java"))
.toFile();

File javaFile = location.findJavaFile(configuration);
File javaFile = location.findSourceFile(configuration);
Assert.assertEquals(expectedFile, javaFile);
}

Expand All @@ -79,7 +79,7 @@ public void findJavaFile_simpleClass_dollarInName() {
Path.of("com", "example", "app", "MyClass$NotInner.java"))
.toFile();

File javaFile = location.findJavaFile(configuration);
File javaFile = location.findSourceFile(configuration);
Assert.assertEquals(expectedFile, javaFile);
}

Expand All @@ -98,7 +98,7 @@ public void findJavaFile_innerClass() {
.resolve(Path.of("com", "example", "app", "MyClass.java"))
.toFile();

File javaFile = location.findJavaFile(configuration);
File javaFile = location.findSourceFile(configuration);
Assert.assertEquals(expectedFile, javaFile);
}

Expand All @@ -117,7 +117,26 @@ public void findJavaFile_nestedInnerClass() {
.resolve(Path.of("com", "example", "app", "MyClass.java"))
.toFile();

File javaFile = location.findJavaFile(configuration);
File javaFile = location.findSourceFile(configuration);
Assert.assertEquals(expectedFile, javaFile);
}

@Test
public void findKotlinFile_simpleClass() {
File defaultJavaSrcDir = new File("src/main/java");
File kotlinExpectedSrcDir = new File("src/main/kotlin");
AbstractConfiguration configuration = Mockito
.mock(AbstractConfiguration.class);
Mockito.when(configuration.getJavaSourceFolder())
.thenReturn(defaultJavaSrcDir);

ComponentTracker.Location location = new ComponentTracker.Location(
"com.example.app.MyClass", "MyClass.kt", "whoCares", 99);
File expectedFile = kotlinExpectedSrcDir.toPath()
.resolve(Path.of("com", "example", "app", "MyClass.kt"))
.toFile();

File javaFile = location.findSourceFile(configuration);
Assert.assertEquals(expectedFile, javaFile);
}

Expand Down

0 comments on commit 0afec75

Please sign in to comment.