Skip to content

Commit

Permalink
Add interfaces support
Browse files Browse the repository at this point in the history
  • Loading branch information
Vacxe committed Sep 22, 2018
1 parent 5dc0435 commit f48f730
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 6 deletions.
Binary file modified .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/library/.gradle
/library/build
/build
/.idea
/.idea
/.gradle
Binary file removed .gradle/4.9/fileChanges/last-build.bin
Binary file not shown.
Binary file removed .gradle/4.9/fileContent/fileContent.lock
Binary file not shown.
Binary file removed .gradle/4.9/fileHashes/fileHashes.bin
Binary file not shown.
Binary file removed .gradle/4.9/fileHashes/fileHashes.lock
Binary file not shown.
Binary file removed .gradle/4.9/taskHistory/taskHistory.bin
Binary file not shown.
Binary file removed .gradle/4.9/taskHistory/taskHistory.lock
Binary file not shown.
Binary file removed .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
2 changes: 0 additions & 2 deletions .gradle/buildOutputCleanup/cache.properties

This file was deleted.

Binary file removed .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
Empty file.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'com.jfrog.bintray'
apply plugin: 'jacoco'

buildscript {
ext.kotlin_version = '1.2.60'
ext.kotlin_version = '1.2.41'
repositories {
mavenCentral()
jcenter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package konveyor.generate

import com.github.vacxe.konveyor.generate.EnumGenerator
import com.github.vacxe.konveyor.generate.ImmutableCollectionGenerator
import com.github.vacxe.konveyor.generate.InterfaceGenerator
import konveyor.exceptions.KonveyorException
import java.lang.reflect.Constructor

Expand All @@ -11,6 +12,7 @@ internal class Generator(private val customObjectResolver: ObjectResolver = Obje
private val randomPrimitiveGenerator = PrimitiveGenerator()
private val randomCollectionsGenerator = ImmutableCollectionGenerator()
private val enumGenerator = EnumGenerator()
private val interfaceGenerator = InterfaceGenerator()

fun <T> build(clazz: Class<T>, constructorNumber: Int = 0, nestedLevel: Int = 0): T {
if (nestedLevel > parameters.nesting) {
Expand Down Expand Up @@ -49,6 +51,7 @@ internal class Generator(private val customObjectResolver: ObjectResolver = Obje
randomPrimitiveGenerator.isPrimivite(parameterType) -> randomPrimitiveGenerator.generatePrimitive(parameterType)
randomCollectionsGenerator.isImmutableCollection(parameterType) -> randomCollectionsGenerator.generateCollection(parameterType)
enumGenerator.isEnum(parameterType) -> enumGenerator.generateEnum(parameterType)
interfaceGenerator.inInterface(parameterType) -> interfaceGenerator.generateMock(parameterType)
else -> generateNestedClass(parameterType, nestedLevel + 1)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.vacxe.konveyor.generate

import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method
import java.lang.reflect.Proxy

class InterfaceGenerator {
fun inInterface(parameterType: Class<*>) = parameterType.isInterface

fun generateMock(parameterType: Class<*>)
= Proxy.newProxyInstance(InterfaceGenerator::class.java.classLoader, arrayOf(parameterType), object : InvocationHandler {
override fun invoke(p0: Any?, method: Method, p2: Array<out Any>?): Any {
val name = method.name
if (name == "toString") {
return "It's a mock interface for ${parameterType.name} class"
}
throw RuntimeException("no method found")
}
})
}
9 changes: 7 additions & 2 deletions src/test/kotlin/GenerationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,16 @@ class GenerationTest {
}

@Test
fun interfaceGeneration() {
fun interfaceGenerationWithImplimentation() {
val objectResolver = ObjectResolver()
objectResolver.addCustomType(MyInterface::class.java, { MyInterfaceImpl() })
val customParameters = CustomParameters(customObjectResolver = objectResolver)

val immutableCollectionDataClass: NestedInterfaceDataClass = randomBuild(customParameters = customParameters)
val nestedInterfaceDataClass: NestedInterfaceDataClass = randomBuild(customParameters = customParameters)
}

@Test
fun interfaceGenerationWithoutImplimentation() {
val nestedInterfaceDataClass: NestedInterfaceDataClass = randomBuild()
}
}

0 comments on commit f48f730

Please sign in to comment.