/
AspectJTransform.kt
235 lines (188 loc) · 8.88 KB
/
AspectJTransform.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package com.archinamon.api.transform
import com.android.build.api.transform.*
import com.android.build.gradle.internal.pipeline.TransformInvocationBuilder
import com.android.build.gradle.internal.pipeline.TransformManager
import com.android.build.gradle.internal.pipeline.TransformTask
import com.android.build.gradle.internal.variant.BaseVariantData
import com.android.utils.FileUtils
import com.archinamon.AndroidConfig
import com.archinamon.api.AspectJMergeJars
import com.archinamon.api.AspectJWeaver
import com.archinamon.plugin.ConfigScope
import com.archinamon.utils.*
import com.archinamon.utils.DependencyFilter.isExcludeFilterMatched
import com.archinamon.utils.DependencyFilter.isIncludeFilterMatched
import com.google.common.collect.Sets
import org.aspectj.util.FileUtil
import org.gradle.api.GradleException
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import java.io.File
internal abstract class AspectJTransform(val project: Project, private val policy: BuildPolicy): Transform() {
private lateinit var config: AndroidConfig
private val aspectJWeaver: AspectJWeaver = AspectJWeaver(project)
private val aspectJMerger: AspectJMergeJars = AspectJMergeJars()
fun withConfig(config: AndroidConfig): AspectJTransform {
this.config = config
return this
}
open fun prepareProject(): AspectJTransform {
project.afterEvaluate {
getVariantDataList(config.plugin).forEach(this::setupVariant)
with(config.aspectj()) {
aspectJWeaver.weaveInfo = weaveInfo
aspectJWeaver.debugInfo = debugInfo
aspectJWeaver.addSerialVUID = addSerialVersionUID
aspectJWeaver.noInlineAround = noInlineAround
aspectJWeaver.ignoreErrors = ignoreErrors
aspectJWeaver.transformLogFile = transformLogFile
aspectJWeaver.breakOnError = breakOnError
aspectJWeaver.experimental = experimental
aspectJWeaver.ajcArgs from ajcArgs
}
}
return this
}
private fun <T: BaseVariantData> setupVariant(variantData: T) {
if (variantData.scope.instantRunBuildContext.isInInstantRunMode) {
if (modeComplex()) {
throw GradleException(SLICER_DETECTED_ERROR)
}
}
val javaTask = getJavaTask(variantData)
getAjSourceAndExcludeFromJavac(project, variantData)
aspectJWeaver.encoding = javaTask!!.options.encoding
aspectJWeaver.sourceCompatibility = JavaVersion.VERSION_1_7.toString()
aspectJWeaver.targetCompatibility = JavaVersion.VERSION_1_7.toString()
}
/* External API */
override fun getName(): String {
return TRANSFORM_NAME
}
override fun getInputTypes(): Set<QualifiedContent.ContentType> {
return Sets.immutableEnumSet(QualifiedContent.DefaultContentType.CLASSES)
}
override fun getOutputTypes(): Set<QualifiedContent.ContentType> {
return TransformManager.CONTENT_CLASS
}
override fun getScopes(): MutableSet<in QualifiedContent.Scope> {
return if (modeComplex()) TransformManager.SCOPE_FULL_PROJECT else Sets.immutableEnumSet(QualifiedContent.Scope.PROJECT)
}
override fun getReferencedScopes(): MutableSet<in QualifiedContent.Scope> {
return if (modeComplex()) super.getReferencedScopes() else TransformManager.SCOPE_FULL_PROJECT
}
override fun isIncremental(): Boolean {
return false
}
@Suppress("OverridingDeprecatedMember")
override fun transform(context: Context, inputs: Collection<TransformInput>, referencedInputs: Collection<TransformInput>, outputProvider: TransformOutputProvider, isIncremental: Boolean) {
this.transform(TransformInvocationBuilder(context)
.addInputs(inputs)
.addReferencedInputs(referencedInputs)
.addOutputProvider(outputProvider)
.setIncrementalMode(isIncremental).build())
}
override fun transform(transformInvocation: TransformInvocation) {
// bypassing transformer for non-test variant data in ConfigScope.TEST
if (!verifyBypassInTestScope(transformInvocation.context)) {
logBypassTransformation()
return
}
val outputProvider = transformInvocation.outputProvider
val includeJars = config.aspectj().includeJar
val excludeJars = config.aspectj().excludeJar
val includeAspects = config.aspectj().includeAspectsFromJar
if (!transformInvocation.isIncremental) {
outputProvider.deleteAll()
}
val outputDir = outputProvider.getContentLocation(TRANSFORM_NAME, outputTypes, scopes, Format.DIRECTORY)
if (outputDir.isDirectory) FileUtils.deleteDirectoryContents(outputDir)
FileUtils.mkdirs(outputDir)
aspectJWeaver.destinationDir = outputDir.absolutePath
aspectJWeaver.bootClasspath = config.getBootClasspath().joinToString(separator = File.pathSeparator)
// clear weaver input, so each transformation can have its own configuration
// (e.g. different build types / variants)
// thanks to @philippkumar
aspectJWeaver.inPath.clear()
aspectJWeaver.aspectPath.clear()
logAugmentationStart()
// attaching source classes compiled by compile${variantName}AspectJ task
includeCompiledAspects(transformInvocation, outputDir)
val inputs = if (modeComplex()) transformInvocation.inputs else transformInvocation.referencedInputs
inputs.forEach proceedInputs@ { input ->
if (input.directoryInputs.isEmpty() && input.jarInputs.isEmpty())
return@proceedInputs //if no inputs so nothing to proceed
input.directoryInputs.forEach { dir ->
aspectJWeaver.inPath shl dir.file
aspectJWeaver.classPath shl dir.file
}
input.jarInputs.forEach { jar ->
aspectJWeaver.classPath shl jar.file
if (modeComplex()) {
val includeAllJars = config.aspectj().includeAllJars
val includeFilterMatched = includeJars.isNotEmpty() && isIncludeFilterMatched(jar.file, includeJars)
val excludeFilterMatched = excludeJars.isNotEmpty() && isExcludeFilterMatched(jar.file, excludeJars)
if (excludeFilterMatched) {
logJarInpathRemoved(jar)
}
if (!excludeFilterMatched && (includeAllJars || includeFilterMatched)) {
logJarInpathAdded(jar)
aspectJWeaver.inPath shl jar.file
} else {
copyJar(outputProvider, jar)
}
} else {
if (includeJars.isNotEmpty() || excludeJars.isNotEmpty()) logIgnoreInpathJars()
}
val includeAspectsFilterMatched = includeAspects.isNotEmpty() && isIncludeFilterMatched(jar.file, includeAspects)
if (includeAspectsFilterMatched) {
logJarAspectAdded(jar)
aspectJWeaver.aspectPath shl jar.file
}
}
}
val hasAjRt = aspectJWeaver.classPath.any { it.name.contains(AJRUNTIME); }
if (hasAjRt) {
logWeaverBuildPolicy(policy)
aspectJWeaver.doWeave()
if (modeComplex()) {
aspectJMerger.doMerge(this, transformInvocation, outputDir)
}
logAugmentationFinish()
} else {
logEnvInvalid()
logNoAugmentation()
}
}
private fun modeComplex(): Boolean {
return policy == BuildPolicy.COMPLEX
}
/* Internal */
private fun verifyBypassInTestScope(ctx: Context): Boolean {
val variant = (ctx as TransformTask).variantName
return when (config.scope) {
ConfigScope.TEST -> variant.contains("androidtest", true)
else -> true
}
}
private fun includeCompiledAspects(transformInvocation: TransformInvocation, outputDir: File) {
val compiledAj = project.file("${project.buildDir}/aspectj/${(transformInvocation.context as TransformTask).variantName}")
if (compiledAj.exists()) {
aspectJWeaver.aspectPath shl compiledAj
//copy compiled .class files to output directory
FileUtil.copyDir(compiledAj, outputDir)
}
}
private fun copyJar(outputProvider: TransformOutputProvider, jarInput: JarInput?): Boolean {
if (jarInput === null) {
return false
}
var jarName = jarInput.name
if (jarName.endsWith(".jar")) {
jarName = jarName.substring(0, jarName.length - 4)
}
val dest: File = outputProvider.getContentLocation(jarName, jarInput.contentTypes, jarInput.scopes, Format.JAR)
FileUtil.copyFile(jarInput.file, dest)
return true
}
}