-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edd0a57
commit 193249e
Showing
6 changed files
with
104 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |