Skip to content

Commit

Permalink
fix: fix interface error issues
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jul 14, 2023
1 parent 0f17226 commit 9f04d13
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/main/kotlin/cc/unitmesh/devti/flow/AutoDevFlow.kt
Expand Up @@ -178,6 +178,10 @@ class AutoDevFlow(
processor.createDto(code)
}

processor.isRepository(code) -> {
processor.createRepository(code)
}

else -> {
processor.createClass(code, null)
}
Expand Down
11 changes: 8 additions & 3 deletions src/main/kotlin/cc/unitmesh/devti/flow/JavaSpringBaseCrud.kt
Expand Up @@ -39,6 +39,7 @@ class JavaSpringBaseCrud(val project: Project) : SpringBaseCrud {
override fun getAllEntityFiles(): List<PsiFile> = filterFilesByFunc(::entityFilter)
override fun getAllDtoFiles(): List<PsiFile> = filterFilesByFunc(::dtoFilter)
override fun getAllServiceFiles(): List<PsiFile> = filterFilesByFunc(::serviceFilter)
override fun getAllRepositoryFiles(): List<PsiFile> = filterFilesByFunc(::repositoryFilter)

private fun filterFilesByFunc(filter: KFunction1<PsiClass, Boolean>): List<PsiFile> {
return runReadAction {
Expand Down Expand Up @@ -141,6 +142,10 @@ class JavaSpringBaseCrud(val project: Project) : SpringBaseCrud {
return createClassByCode(code, getAllDtoFiles())
}

override fun createRepository(code: String): DtClass? {
return createClassByCode(code, getAllRepositoryFiles())
}

override fun createService(code: String): DtClass? {
return createClassByCode(code, getAllServiceFiles())
}
Expand Down Expand Up @@ -171,11 +176,11 @@ class JavaSpringBaseCrud(val project: Project) : SpringBaseCrud {
runWriteAction {
val newClass = psiElementFactory.createClassFromText(code, null)

val regex = Regex("public\\s+class\\s+(\\w+)")
val regex = Regex("public\\s+(class|interface)\\s+(\\w+)")
val matchResult = regex.find(code)

val className = if (matchResult?.groupValues?.get(1) != null) {
matchResult.groupValues[1]
val className = if (matchResult?.groupValues?.get(2) != null) {
matchResult.groupValues[2]
} else if (newClass.identifyingElement?.text != null) {
newClass.identifyingElement?.text
} else {
Expand Down
20 changes: 20 additions & 0 deletions src/main/kotlin/cc/unitmesh/devti/flow/SpringBaseCrud.kt
Expand Up @@ -17,13 +17,15 @@ interface SpringBaseCrud {
fun getAllEntityFiles(): List<PsiFile>
fun getAllDtoFiles(): List<PsiFile>
fun getAllServiceFiles(): List<PsiFile>
fun getAllRepositoryFiles(): List<PsiFile>


fun createControllerOrUpdateMethod(targetController: String, code: String, isControllerExist: Boolean)
fun createController(endpoint: String, code: String): DtClass?
fun createEntity(code: String): DtClass?
fun createService(code: String): DtClass?
fun createDto(code: String): DtClass?
fun createRepository(code: String): DtClass?
fun createClass(code: String, packageName: String?): DtClass?

fun dtoFilter(clazz: PsiClass): Boolean {
Expand All @@ -46,6 +48,11 @@ interface SpringBaseCrud {
it == "org.springframework.stereotype.Service"
}

fun repositoryFilter(clazz: PsiClass): Boolean = clazz.annotations
.map { it.qualifiedName }.any {
it == "org.springframework.stereotype.Repository"
}

fun entityFilter(clazz: PsiClass): Boolean = clazz.annotations
.map { it.qualifiedName }.any {
it == "javax.persistence.Entity"
Expand Down Expand Up @@ -103,4 +110,17 @@ interface SpringBaseCrud {
return regex.containsMatchIn(code)
}

fun isRepository(code: String): Boolean {
if (code.contains("@Repository")) {
return true
}

if (code.contains("import org.springframework.stereotype.Repository")) {
return true
}

// regex to match `public class xxRepository`
val regex = Regex("public\\s+class\\s+\\w+Repository")
return regex.containsMatchIn(code)
}
}
Expand Up @@ -22,7 +22,8 @@ public class {serviceName} {
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

public class {xxxRepository} extends JpaRepository<{xxx}, Long> {
@Repository
public interface {xxxRepository} extends JpaRepository<{xxx}, Long> {

}
```
Expand Down

0 comments on commit 9f04d13

Please sign in to comment.