Skip to content

Commit

Permalink
Data model
Browse files Browse the repository at this point in the history
  • Loading branch information
avantgardnerio committed Oct 30, 2018
1 parent edd0a57 commit 193249e
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 57 deletions.
15 changes: 15 additions & 0 deletions src/main/kotlin/net/squarelabs/depends/Resolver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.squarelabs.depends

import net.squarelabs.depends.models.Artifact
import org.jboss.shrinkwrap.resolver.api.maven.Maven

fun resolve(coordinate: String): Artifact {
val artifacts = Maven.resolver()
.resolve(coordinate)
.withoutTransitivity()
.asResolvedArtifact()
assert(artifacts.size == 1)
val artifact = artifacts[0]
val dependencies: List<Artifact> = artifact.dependencies.map { resolve(it.coordinate.toCanonicalForm()) }
return Artifact(coordinate, artifact.asFile(), dependencies)
}
10 changes: 10 additions & 0 deletions src/main/kotlin/net/squarelabs/depends/models/Artifact.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.squarelabs.depends.models

import java.io.File

data class Artifact(
val coordinate: String,
val file: File,
val dependencies: List<Artifact>
//val classes: List<Class>
)
6 changes: 6 additions & 0 deletions src/main/kotlin/net/squarelabs/depends/models/Class.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.squarelabs.depends.models

data class Class(
val name: String,
val methods: List<Method>
)
6 changes: 6 additions & 0 deletions src/main/kotlin/net/squarelabs/depends/models/Method.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.squarelabs.depends.models

data class Method(
val name: String,
val invocations: List<Method>
)
57 changes: 0 additions & 57 deletions src/test/kotlin/ResolverTest.kt

This file was deleted.

67 changes: 67 additions & 0 deletions src/test/kotlin/net/squarelabs/depends/ResolverTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package net.squarelabs.depends

import com.google.common.reflect.ClassPath
import net.squarelabs.depends.models.Artifact
import org.junit.Assert
import org.junit.Test
import org.jboss.shrinkwrap.resolver.api.maven.Maven
import java.net.URLClassLoader
import org.objectweb.asm.*
import org.objectweb.asm.commons.InstructionAdapter

class ResolverTest {
@Test
fun `should resolve`() {
// Resolve
val artifacts = Maven.resolver()
.resolve("io.dropwizard:dropwizard-core:1.3.7")
.withoutTransitivity()
.asResolvedArtifact()
Assert.assertEquals(1, artifacts.size)

// get file
val artifact = artifacts[0]
val file = artifact.asFile()
Assert.assertTrue(file.exists())

// load jar
URLClassLoader(arrayOf(file.toURL()), this.javaClass.classLoader).use { loader ->
val cp = ClassPath.from(loader)
Assert.assertTrue(cp.topLevelClasses.size > 0)
for (clazz in cp.topLevelClasses) {
val cl = object : ClassVisitor(Opcodes.ASM7) {
override fun visitMethod(access: Int, methodName: String?,
desc: String?, signature: String?, exceptions: Array<String>?): MethodVisitor? {
println("net.squarelabs.depends.models.Method: $methodName $desc")
System.out.println("\n" + methodName + desc)
val oriMv: MethodVisitor = object : MethodVisitor(Opcodes.ASM7) {}
val instMv = object : InstructionAdapter(Opcodes.ASM7, oriMv) {
override fun visitMethodInsn(opcode: Int, owner: String?, name: String?, descriptor: String?, isInterface: Boolean) {
println("invoke $owner.$name from ${file.name} ${clazz.name}.$methodName()")
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface)
}

}
return instMv
}

override fun visitEnd() {
println("net.squarelabs.depends.models.Method ends here")
super.visitEnd()
}
}

loader.getResourceAsStream(clazz.resourceName).use { stream ->
val classReader = ClassReader(stream)
classReader.accept(cl, 0)
}
}
}
}

@Test
fun `resolver should resolve`() {
val root: Artifact = resolve("io.dropwizard:dropwizard-core:1.3.7")
println("root=$root")
}
}

0 comments on commit 193249e

Please sign in to comment.