Skip to content

Commit

Permalink
Added basic support for Proxy classes
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzo committed Jan 19, 2023
1 parent d9521e0 commit d4ba111
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 31 deletions.
19 changes: 0 additions & 19 deletions .github/actions/host-setup/action.yaml

This file was deleted.

17 changes: 15 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ on:
branches:
- main

permissions:
checks: write
pull-requests: write

jobs:
build:
name: Build
Expand All @@ -17,9 +21,18 @@ jobs:
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Host setup
uses: ./.github/actions/host-setup
- name: Setup JDK11
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '11'
cache: 'gradle'
- name: Test & Build
run: ./gradlew -s build
- name: Publish Test Report
uses: mikepenz/action-junit-report@v3
if: always()
with:
report_paths: '**/build/test-results/*/TEST-*.xml'
- name: Coverage report
uses: codecov/codecov-action@v3
8 changes: 6 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ jobs:
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Host setup
uses: ./.github/actions/host-setup
- name: Setup JDK11
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '11'
cache: 'gradle'
- name: Publish
env:
ORG_GRADLE_PROJECT_signingKey: ${{ secrets.SIGNING_KEY }}
Expand Down
17 changes: 11 additions & 6 deletions core/src/main/kotlin/io/github/gmazzo/codeowners/CodeOwners.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package io.github.gmazzo.codeowners

import java.io.Reader
import java.lang.reflect.Proxy
import kotlin.reflect.KClass

inline fun <reified Type> codeOwnersOf() =
Expand All @@ -12,17 +13,21 @@ val KClass<*>.codeOwners
get() = java.codeOwners

val Class<*>.codeOwners: Set<String>?
get() = with(topLevelClass()) { classLoader.getCodeOwners(`package`.name, simpleName) }
get() = with(topLevelClass()) { classLoader.getCodeOwners(`package`?.name, simpleName) }

val Throwable.codeOwners
get() = stackTrace.asSequence().map { it.codeOwners }.firstOrNull()
get() = stackTrace.asSequence().mapNotNull { it.codeOwners }.firstOrNull()

private tailrec fun Class<*>.topLevelClass(): Class<*> = when (val enclosing = enclosingClass) {
null -> this
null -> when {
Proxy.isProxyClass(this) -> interfaces.firstOrNull() ?: this
else -> this
}

else -> enclosing.topLevelClass()
}

private val StackTraceElement.codeOwners: Set<String>?
val StackTraceElement.codeOwners: Set<String>?
get() {
val clazz = runCatching { Class.forName(className) }.getOrNull() ?: return null

Expand All @@ -32,8 +37,8 @@ private val StackTraceElement.codeOwners: Set<String>?
}

@JvmOverloads
tailrec fun ClassLoader.getCodeOwners(packageName: String, className: String? = null): Set<String>? {
val packagePath = packageName.replace('.', '/')
tailrec fun ClassLoader.getCodeOwners(packageName: String?, className: String? = null): Set<String>? {
val packagePath = packageName?.replace('.', '/') ?: return null
val path = "$packagePath/${className.orEmpty()}.codeowners"
val owners = getResources(path).asSequence()
.flatMap { it.openStream()?.reader()?.use(Reader::readLines).orEmpty() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import foo.bar.impl.FooBarImpl
import foo.impl.FooImpl
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.lang.reflect.Proxy

class CodeOwnersTest {

Expand Down Expand Up @@ -77,4 +78,11 @@ class CodeOwnersTest {
assertEquals(null, codeOwnersOf<CodeOwnersTest>())
}

@Test
fun `given proxy class of Foo, returns owners correctly`() {
val proxy = Proxy.newProxyInstance(javaClass.classLoader, arrayOf(Foo::class.java)) { _, _, _ -> }

assertEquals(setOf("foo"), proxy::class.codeOwners)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertIterableEquals
import org.junit.jupiter.api.Test
import java.io.File
import java.lang.UnsupportedOperationException
import java.nio.file.Files

class CodeOwnersTransformTest {
Expand All @@ -33,13 +32,14 @@ class CodeOwnersTransformTest {

val outputFiles = outputsDir.walkTopDown()
.filter { it.isFile }
.sorted()
.map { it.toRelativeString(outputsDir) to it.readText().trim() }
.toList()

assertIterableEquals(
listOf(
"org/test/utils/.codeowners" to "kotlin-devs",
"org/test/lib/.codeowners" to "kotlin-devs",
"org/test/utils/.codeowners" to "kotlin-devs",
),
outputFiles
)
Expand Down

0 comments on commit d4ba111

Please sign in to comment.