Skip to content

Commit

Permalink
Give everything an unique natural key
Browse files Browse the repository at this point in the history
  • Loading branch information
avantgardnerio committed Oct 30, 2018
1 parent 157c712 commit 37585d2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ compileKotlin {
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

test {
maxHeapSize = "12g"
}
21 changes: 13 additions & 8 deletions src/main/kotlin/net/squarelabs/depends/Resolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,23 @@ fun resolve(coordinate: String, state: State): Artifact {
}
}

fun classesFromFile(file: File): List<Class> {
fun classesFromFile(file: File): Map<String,Class> {
return URLClassLoader(arrayOf(file.toURL())).use { it ->
val cp = ClassPath.from(it)
val size = cp.topLevelClasses.size
cp.topLevelClasses.mapIndexed { index, clazz ->
val classes = mutableMapOf<String,Class>()
cp.topLevelClasses.forEachIndexed { index, clazz ->
val methods = methodsFromClass(clazz, it)
if (index % 1000 == 0) println("$methodCount methods and $invocationCount invocations class $index / $size is ${clazz.name}")
Class(clazz.name, methods)
// assert(classes.get(clazz.name) == null) // TODO: handle duplicates /foo/bar/class vs /foo.bar/class
classes.put(clazz.name, Class(clazz.name, methods))
}
classes.toMap()
}
}

fun methodsFromClass(clazz: ClassPath.ClassInfo, loader: URLClassLoader): List<Method> {
val methods = mutableListOf<Method>()
fun methodsFromClass(clazz: ClassPath.ClassInfo, loader: URLClassLoader): Map<String, Method> {
val methods = mutableMapOf<String, Method>()
val cl = object : ClassVisitor(Opcodes.ASM7) {
override fun visitMethod(access: Int, methodName: String,
desc: String, signature: String?, exceptions: Array<String>?): MethodVisitor? {
Expand All @@ -58,14 +61,16 @@ fun methodsFromClass(clazz: ClassPath.ClassInfo, loader: URLClassLoader): List<M
val instMv = object : InstructionAdapter(Opcodes.ASM7, oriMv) {
override fun visitMethodInsn(opcode: Int, owner: String, name: String, descriptor: String, isInterface: Boolean) {
invocationCount++
invocations.add(Invocation(owner, name, descriptor))
//invocations.add(Invocation(owner, name, descriptor))
//println("invoke $owner.$name from ${clazz.name}.$methodName()")
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface)
}
}
methodCount++
val method = Method(methodName, desc, invocations.toList())
methods.add(method)
val methodId = "$methodName$desc"
assert(methods.get(methodId) == null)
methods.put(methodId, method)
return instMv
}
}
Expand All @@ -75,5 +80,5 @@ fun methodsFromClass(clazz: ClassPath.ClassInfo, loader: URLClassLoader): List<M
classReader.accept(cl, 0)
}

return methods.toList()
return methods.toMap()
}
2 changes: 1 addition & 1 deletion src/main/kotlin/net/squarelabs/depends/models/Artifact.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ data class Artifact(
val coordinate: String,
val file: File,
val dependencies: List<Artifact>,
val classes: List<Class>
val classes: Map<String,Class>
)
2 changes: 1 addition & 1 deletion src/main/kotlin/net/squarelabs/depends/models/Class.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package net.squarelabs.depends.models

data class Class(
val name: String,
val methods: List<Method>
val methods: Map<String,Method>
)

0 comments on commit 37585d2

Please sign in to comment.