Skip to content

Commit 265e159

Browse files
committed
Cleanups
1 parent 38f0592 commit 265e159

File tree

7 files changed

+40
-98
lines changed

7 files changed

+40
-98
lines changed

lint-baseline.xml

+4-48
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
9797
<location
9898
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt"
99-
line="34"
99+
line="33"
100100
column="1"/>
101101
</issue>
102102

@@ -107,7 +107,7 @@
107107
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
108108
<location
109109
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt"
110-
line="35"
110+
line="34"
111111
column="1"/>
112112
</issue>
113113

@@ -118,7 +118,7 @@
118118
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
119119
<location
120120
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt"
121-
line="36"
121+
line="35"
122122
column="1"/>
123123
</issue>
124124

@@ -129,51 +129,7 @@
129129
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
130130
<location
131131
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt"
132-
line="37"
133-
column="1"/>
134-
</issue>
135-
136-
<issue
137-
id="InternalGradleApiUsage"
138-
message="Avoid using internal Gradle APIs"
139-
errorLine1="import org.gradle.api.internal.file.DefaultFileTreeElement"
140-
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
141-
<location
142-
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt"
143-
line="16"
144-
column="1"/>
145-
</issue>
146-
147-
<issue
148-
id="InternalGradleApiUsage"
149-
message="Avoid using internal Gradle APIs"
150-
errorLine1="import org.gradle.internal.file.Chmod"
151-
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
152-
<location
153-
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt"
154-
line="17"
155-
column="1"/>
156-
</issue>
157-
158-
<issue
159-
id="InternalGradleApiUsage"
160-
message="Avoid using internal Gradle APIs"
161-
errorLine1="import org.gradle.internal.file.FileMetadata"
162-
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
163-
<location
164-
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt"
165-
line="18"
166-
column="1"/>
167-
</issue>
168-
169-
<issue
170-
id="InternalGradleApiUsage"
171-
message="Avoid using internal Gradle APIs"
172-
errorLine1="import org.gradle.internal.file.Stat"
173-
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
174-
<location
175-
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt"
176-
line="19"
132+
line="36"
177133
column="1"/>
178134
</issue>
179135

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/RelocatorRemapper.kt

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.github.jengelman.gradle.plugins.shadow.internal
22

3-
import com.github.jengelman.gradle.plugins.shadow.relocation.RelocateClassContext
4-
import com.github.jengelman.gradle.plugins.shadow.relocation.RelocatePathContext
53
import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
64
import java.util.regex.Pattern
75
import org.objectweb.asm.commons.Remapper
@@ -39,11 +37,9 @@ internal class RelocatorRemapper(
3937

4038
for (relocator in relocators) {
4139
if (relocator.canRelocateClass(newName)) {
42-
val classContext = RelocateClassContext(newName)
43-
return prefix + relocator.relocateClass(classContext) + suffix
40+
return prefix + relocator.relocateClass(newName) + suffix
4441
} else if (relocator.canRelocatePath(newName)) {
45-
val pathContext = RelocatePathContext(newName)
46-
return prefix + relocator.relocatePath(pathContext) + suffix
42+
return prefix + relocator.relocatePath(newName) + suffix
4743
}
4844
}
4945

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt

+9-24
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.github.jengelman.gradle.plugins.shadow.internal
22

3+
import com.github.jengelman.gradle.plugins.shadow.relocation.RelocateClassContext
4+
import com.github.jengelman.gradle.plugins.shadow.relocation.RelocatePathContext
5+
import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
36
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction
47
import java.io.ByteArrayInputStream
58
import java.io.ByteArrayOutputStream
6-
import java.io.File
79
import java.io.InputStream
810
import java.nio.charset.Charset
911
import java.nio.file.NoSuchFileException
@@ -12,11 +14,6 @@ import java.util.Properties
1214
import java.util.jar.Attributes.Name as JarAttributeName
1315
import kotlin.io.path.toPath
1416
import org.apache.tools.zip.ZipEntry
15-
import org.gradle.api.file.RelativePath
16-
import org.gradle.api.internal.file.DefaultFileTreeElement
17-
import org.gradle.internal.file.Chmod
18-
import org.gradle.internal.file.FileMetadata
19-
import org.gradle.internal.file.Stat
2017

2118
/**
2219
* Known as `Main-Class` in the manifest file.
@@ -49,17 +46,12 @@ internal inline fun zipEntry(
4946
block()
5047
}
5148

52-
/**
53-
* This is used for creating a [DefaultFileTreeElement] with default values.
54-
* [file], [chmod], and [stat] should be non-null, so they are set to dummy values here.
55-
*/
56-
internal fun createDefaultFileTreeElement(
57-
file: File = DummyFile,
58-
relativePath: RelativePath,
59-
chmod: Chmod = DummyChmod,
60-
stat: Stat = DummyStat,
61-
): DefaultFileTreeElement {
62-
return DefaultFileTreeElement(file, relativePath, chmod, stat)
49+
internal fun Relocator.relocatePath(path: String): String {
50+
return relocatePath(RelocatePathContext(path))
51+
}
52+
53+
internal fun Relocator.relocateClass(className: String): String {
54+
return relocateClass(RelocateClassContext(className))
6355
}
6456

6557
internal fun Properties.inputStream(
@@ -88,11 +80,4 @@ internal fun requireResourceAsPath(name: String): Path {
8880
return resource.toURI().toPath()
8981
}
9082

91-
private val DummyFile = File("dummy")
92-
private val DummyChmod = Chmod { _, _ -> error("This is a dummy implementation.") }
93-
private val DummyStat = object : Stat {
94-
override fun getUnixMode(f: File): Int = error("This is a dummy implementation.")
95-
override fun stat(f: File): FileMetadata = error("This is a dummy implementation.")
96-
}
97-
9883
private object Utils

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.kt

+8-9
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ public open class ShadowCopyAction(
108108
remapClass(stream, fileDetails.path, fileDetails.lastModified)
109109
}
110110
} else {
111-
if (!transform(fileDetails)) {
112-
fileDetails.writeToZip(remapper.map(relativePath))
113-
}
111+
val mapped = remapper.map(relativePath)
112+
if (transform(fileDetails, mapped)) return
113+
fileDetails.writeToZip(mapped)
114114
}
115115
} catch (e: Exception) {
116116
throw GradleException("Could not add $fileDetails to ZIP '$zipFile'.", e)
@@ -119,20 +119,19 @@ public open class ShadowCopyAction(
119119

120120
private fun visitDir(dirDetails: FileCopyDetails) {
121121
try {
122-
val mappedPath = if (relocators.isEmpty()) {
122+
val mapped = if (relocators.isEmpty()) {
123123
dirDetails.relativePath.pathString
124124
} else {
125125
remapper.map(dirDetails.relativePath.pathString)
126126
}
127-
dirDetails.writeToZip("$mappedPath/")
127+
dirDetails.writeToZip("$mapped/")
128128
} catch (e: Exception) {
129129
throw GradleException("Could not add $dirDetails to ZIP '$zipFile'.", e)
130130
}
131131
}
132132

133133
private fun isUnused(classPath: String): Boolean {
134-
val classPathWithoutExtension = classPath.substringBeforeLast(".")
135-
val className = classPathWithoutExtension.replace('/', '.')
134+
val className = classPath.substringBeforeLast(".").replace('/', '.')
136135
return unusedClasses.contains(className).also {
137136
if (it) {
138137
logger.debug("Dropping unused class: $className")
@@ -176,12 +175,12 @@ public open class ShadowCopyAction(
176175
}
177176
}
178177

179-
private fun transform(fileDetails: FileCopyDetails): Boolean {
178+
private fun transform(fileDetails: FileCopyDetails, mapped: String): Boolean {
180179
val transformer = transformers.find { it.canTransformResource(fileDetails) } ?: return false
181180
fileDetails.file.inputStream().use { steam ->
182181
transformer.transform(
183182
TransformerContext(
184-
path = remapper.map(fileDetails.relativePath.pathString),
183+
path = mapped,
185184
inputStream = steam,
186185
relocators = relocators,
187186
),

src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocatorTest.kt

+2-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import assertk.assertThat
44
import assertk.assertions.isEqualTo
55
import assertk.assertions.isFalse
66
import assertk.assertions.isTrue
7+
import com.github.jengelman.gradle.plugins.shadow.internal.relocateClass
8+
import com.github.jengelman.gradle.plugins.shadow.internal.relocatePath
79
import org.junit.jupiter.api.Test
810

911
/**
@@ -383,13 +385,5 @@ class SimpleRelocatorTest {
383385
}
384386
}
385387
""".trimIndent()
386-
387-
fun SimpleRelocator.relocatePath(path: String): String {
388-
return relocatePath(RelocatePathContext(path))
389-
}
390-
391-
fun SimpleRelocator.relocateClass(className: String): String {
392-
return relocateClass(RelocateClassContext(className))
393-
}
394388
}
395389
}

src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/BaseTransformerTest.kt

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.github.jengelman.gradle.plugins.shadow.transformers
22

3-
import com.github.jengelman.gradle.plugins.shadow.internal.createDefaultFileTreeElement
43
import com.github.jengelman.gradle.plugins.shadow.internal.requireResourceAsStream
54
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer.Companion.create
5+
import com.github.jengelman.gradle.plugins.shadow.util.noOpDelegate
66
import com.github.jengelman.gradle.plugins.shadow.util.testObjectFactory
77
import java.lang.reflect.ParameterizedType
88
import java.nio.file.Path
@@ -11,6 +11,7 @@ import java.util.zip.ZipFile
1111
import kotlin.io.path.createTempFile
1212
import kotlin.io.path.outputStream
1313
import org.apache.tools.zip.ZipOutputStream
14+
import org.gradle.api.file.FileTreeElement
1415
import org.gradle.api.file.RelativePath
1516
import org.junit.jupiter.api.BeforeEach
1617

@@ -31,8 +32,10 @@ abstract class BaseTransformerTest<T : Transformer> {
3132
protected companion object {
3233
const val MANIFEST_NAME: String = "META-INF/MANIFEST.MF"
3334

34-
fun Transformer.canTransformResource(path: String): Boolean {
35-
val element = createDefaultFileTreeElement(relativePath = RelativePath.parse(true, path))
35+
fun Transformer.canTransformResource(path: String, isFile: Boolean = true): Boolean {
36+
val element = object : FileTreeElement by noOpDelegate() {
37+
override fun getRelativePath(): RelativePath = RelativePath.parse(isFile, path)
38+
}
3639
return canTransformResource(element)
3740
}
3841

Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
package com.github.jengelman.gradle.plugins.shadow.util
22

33
import java.io.OutputStream
4+
import java.lang.reflect.InvocationHandler
5+
import java.lang.reflect.Proxy
46
import org.apache.tools.zip.ZipOutputStream
57
import org.gradle.api.model.ObjectFactory
68
import org.gradle.testfixtures.ProjectBuilder
79

810
val testObjectFactory: ObjectFactory = ProjectBuilder.builder().build().objects
911

12+
val NO_OP_HANDLER = InvocationHandler { _, _, _ -> }
13+
14+
inline fun <reified T : Any> noOpDelegate(): T {
15+
val javaClass = T::class.java
16+
return Proxy.newProxyInstance(javaClass.classLoader, arrayOf(javaClass), NO_OP_HANDLER) as T
17+
}
18+
1019
fun OutputStream.zipOutputStream(): ZipOutputStream {
1120
return this as? ZipOutputStream ?: ZipOutputStream(this)
1221
}

0 commit comments

Comments
 (0)