Skip to content

Commit 6e96c8e

Browse files
Amending flows to remove generics + testing type argument implementation.
1 parent 5959d84 commit 6e96c8e

File tree

14 files changed

+132
-60
lines changed

14 files changed

+132
-60
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ buildscript {
1414
junit_version = '5.3.1'
1515

1616
onixlabs_group = 'io.onixlabs'
17-
onixlabs_corda_core_release_version = '1.0.0'
17+
onixlabs_corda_core_release_version = '1.1.0'
1818

1919
cordapp_platform_version = 8
2020
cordapp_contract_name = 'ONIXLabs Corda Identity Framework Contract'
@@ -45,7 +45,7 @@ buildscript {
4545
}
4646

4747
group 'io.onixlabs'
48-
version '1.0.0'
48+
version '2.0.0'
4949

5050
subprojects {
5151
repositories {

onixlabs-corda-identity-framework-integration/src/main/kotlin/io/onixlabs/corda/identityframework/integration/AttestationQueryService.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class AttestationQueryService(rpc: CordaRPCOps) : RPCService(rpc) {
8484
): StateAndRef<T>? {
8585
return rpc.startFlowDynamic(
8686
FindAttestationFlow::class.java,
87+
T::class.java,
8788
linearId,
8889
externalId,
8990
attestor,
@@ -141,6 +142,7 @@ class AttestationQueryService(rpc: CordaRPCOps) : RPCService(rpc) {
141142
): List<StateAndRef<T>> {
142143
return rpc.startFlowDynamic(
143144
FindAttestationsFlow::class.java,
145+
T::class.java,
144146
linearId,
145147
externalId,
146148
attestor,
@@ -155,6 +157,6 @@ class AttestationQueryService(rpc: CordaRPCOps) : RPCService(rpc) {
155157
stateStatus,
156158
relevancyStatus,
157159
pageSpecification
158-
).returnValue.getOrThrow(flowTimeout).map { it.cast<T>() }
160+
).returnValue.getOrThrow(flowTimeout).cast()
159161
}
160162
}

onixlabs-corda-identity-framework-integration/src/main/kotlin/io/onixlabs/corda/identityframework/integration/ClaimQueryService.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.onixlabs.corda.identityframework.integration
1818

19+
import io.onixlabs.corda.core.contract.cast
1920
import io.onixlabs.corda.core.integration.RPCService
2021
import io.onixlabs.corda.core.workflow.DEFAULT_PAGE_SPECIFICATION
2122
import io.onixlabs.corda.identityframework.contract.CordaClaim
@@ -53,8 +54,7 @@ class ClaimQueryService(rpc: CordaRPCOps) : RPCService(rpc) {
5354
* @param flowTimeout The amount of time that the flow will be allowed to execute before failing.
5455
* @return Returns a claim that matches the query, or null if no claim was found.
5556
*/
56-
fun findClaim(
57-
claimClass: Class<out CordaClaim<*>> = CordaClaim::class.java,
57+
inline fun <reified T : CordaClaim<*>> findClaim(
5858
valueClass: Class<*>? = null,
5959
linearId: UniqueIdentifier? = null,
6060
externalId: String? = null,
@@ -72,7 +72,7 @@ class ClaimQueryService(rpc: CordaRPCOps) : RPCService(rpc) {
7272
): StateAndRef<CordaClaim<*>>? {
7373
return rpc.startFlowDynamic(
7474
FindClaimFlow::class.java,
75-
claimClass,
75+
T::class.java,
7676
valueClass,
7777
linearId,
7878
externalId,
@@ -86,7 +86,7 @@ class ClaimQueryService(rpc: CordaRPCOps) : RPCService(rpc) {
8686
stateStatus,
8787
relevancyStatus,
8888
pageSpecification
89-
).returnValue.getOrThrow(flowTimeout)
89+
).returnValue.getOrThrow(flowTimeout)?.cast()
9090
}
9191

9292
/**

onixlabs-corda-identity-framework-workflow/src/main/kotlin/io/onixlabs/corda/identityframework/workflow/FindAttestationFlow.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import net.corda.core.node.services.vault.QueryCriteria.VaultQueryCriteria
3939
/**
4040
* Represents the flow for finding an attestation in the vault.
4141
*
42+
* @param attestationClass The class of the underlying attestation.
4243
* @param linearId The linear ID to include in the query.
4344
* @param externalId The external ID to include in the query.
4445
* @param attestor The attestor to include in the query.
@@ -56,7 +57,8 @@ import net.corda.core.node.services.vault.QueryCriteria.VaultQueryCriteria
5657
*/
5758
@StartableByRPC
5859
@StartableByService
59-
class FindAttestationFlow<T : Attestation<*>>(
60+
class FindAttestationFlow(
61+
attestationClass: Class<out Attestation<*>> = Attestation::class.java,
6062
linearId: UniqueIdentifier? = null,
6163
externalId: String? = null,
6264
attestor: AbstractParty? = null,
@@ -71,9 +73,9 @@ class FindAttestationFlow<T : Attestation<*>>(
7173
stateStatus: Vault.StateStatus = Vault.StateStatus.UNCONSUMED,
7274
relevancyStatus: Vault.RelevancyStatus = Vault.RelevancyStatus.ALL,
7375
override val pageSpecification: PageSpecification = DEFAULT_PAGE_SPECIFICATION
74-
) : FindStateFlow<T>() {
76+
) : FindStateFlow<Attestation<*>>() {
7577
override val criteria: QueryCriteria = VaultQueryCriteria(
76-
contractStateTypes = setOf(contractStateType),
78+
contractStateTypes = setOf(attestationClass),
7779
relevancyStatus = relevancyStatus,
7880
status = stateStatus
7981
).andWithExpressions(

onixlabs-corda-identity-framework-workflow/src/main/kotlin/io/onixlabs/corda/identityframework/workflow/FindAttestationsFlow.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import net.corda.core.node.services.vault.Sort
4141
/**
4242
* Represents the flow for finding attestations in the vault.
4343
*
44+
* @param attestationClass The class of the underlying attestation.
4445
* @param linearId The linear ID to include in the query.
4546
* @param externalId The external ID to include in the query.
4647
* @param attestor The attestor to include in the query.
@@ -58,7 +59,8 @@ import net.corda.core.node.services.vault.Sort
5859
*/
5960
@StartableByRPC
6061
@StartableByService
61-
class FindAttestationsFlow<T : Attestation<*>>(
62+
class FindAttestationsFlow(
63+
attestationClass: Class<out Attestation<*>> = Attestation::class.java,
6264
linearId: UniqueIdentifier? = null,
6365
externalId: String? = null,
6466
attestor: AbstractParty? = null,
@@ -74,9 +76,9 @@ class FindAttestationsFlow<T : Attestation<*>>(
7476
relevancyStatus: Vault.RelevancyStatus = Vault.RelevancyStatus.ALL,
7577
override val pageSpecification: PageSpecification = DEFAULT_PAGE_SPECIFICATION,
7678
override val sorting: Sort = DEFAULT_SORTING
77-
) : FindStatesFlow<T>() {
79+
) : FindStatesFlow<Attestation<*>>() {
7880
override val criteria: QueryCriteria = VaultQueryCriteria(
79-
contractStateTypes = setOf(contractStateType),
81+
contractStateTypes = setOf(attestationClass),
8082
relevancyStatus = relevancyStatus,
8183
status = stateStatus
8284
).andWithExpressions(

onixlabs-corda-identity-framework-workflow/src/main/kotlin/io/onixlabs/corda/identityframework/workflow/FindClaimFlow.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import net.corda.core.node.services.vault.QueryCriteria.VaultQueryCriteria
3737
/**
3838
* Represents the flow for finding a claim in the vault.
3939
*
40+
* @param claimClass The class of the underlying corda claim.
41+
* @param valueClass The class of the underlying corda claim value.
4042
* @param linearId The linear ID to include in the query.
4143
* @param externalId The external ID to include in the query.
4244
* @param issuer The issuer to include in the query.

onixlabs-corda-identity-framework-workflow/src/main/kotlin/io/onixlabs/corda/identityframework/workflow/FindClaimsFlow.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import net.corda.core.node.services.vault.Sort
3838
/**
3939
* Represents the flow for finding claims in the vault.
4040
*
41+
* @param claimClass The class of the underlying corda claim.
42+
* @param valueClass The class of the underlying corda claim value.
4143
* @param linearId The linear ID to include in the query.
4244
* @param externalId The external ID to include in the query.
4345
* @param issuer The issuer to include in the query.

onixlabs-corda-identity-framework-workflow/src/main/kotlin/io/onixlabs/corda/identityframework/workflow/FlowLogicExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fun FlowLogic<*>.checkClaimExists(claim: CordaClaim<*>) {
7272
* @throws FlowException if the claim already exists.
7373
*/
7474
fun FlowLogic<*>.checkAttestationExists(attestation: Attestation<*>) {
75-
if (subFlow(FindAttestationsFlow<Attestation<*>>(hash = attestation.hash)).isNotEmpty()) {
75+
if (subFlow(FindAttestationsFlow(hash = attestation.hash)).isNotEmpty()) {
7676
throw FlowException("An attestation with the specified hash already exists: ${attestation.hash}.")
7777
}
7878
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.onixlabs.corda.identityframework.workflow
2+
3+
import java.lang.reflect.ParameterizedType
4+
import java.lang.reflect.Type
5+
6+
internal abstract class TypeReference<T> : Comparable<T> {
7+
8+
val type: Type = getGenericType()
9+
val typeName: String = type.typeName
10+
val arguments: List<Type> = getGenericTypeArguments()
11+
12+
fun toClass(): Class<T> {
13+
return Class.forName(typeName) as Class<T>
14+
}
15+
16+
private fun getGenericType(): Type {
17+
val superClass = javaClass.genericSuperclass
18+
19+
if (superClass is Class<*>) {
20+
throw IllegalArgumentException("TypeReference constructed without actual type information.")
21+
}
22+
23+
return (superClass as ParameterizedType).actualTypeArguments[0]
24+
}
25+
26+
private fun getGenericTypeArguments(): List<Type> {
27+
return if (type is ParameterizedType) {
28+
type.actualTypeArguments.toList()
29+
} else emptyList()
30+
}
31+
32+
override fun compareTo(other: T): Int = 0
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.onixlabs.corda.identityframework.workflow
2+
3+
import io.onixlabs.corda.identityframework.contract.CordaClaim
4+
import org.junit.jupiter.api.Test
5+
import kotlin.test.assertEquals
6+
7+
class TypeReferenceTests {
8+
9+
@Test
10+
fun `TypeReference should resolve type of Integer`() {
11+
val actual = object : TypeReference<Int>() {}.type.typeName
12+
val expected = "java.lang.Integer"
13+
assertEquals(expected, actual)
14+
}
15+
16+
@Test
17+
fun `TypeReference should resolve type of CordaClaim`() {
18+
val actual = object : TypeReference<CordaClaim<*>>() {}.type.typeName
19+
val expected = "io.onixlabs.corda.identityframework.contract.CordaClaim"
20+
assertEquals(expected, actual)
21+
}
22+
23+
@Test
24+
fun `TypeReference should resolve type arguments of CordaClaim`() {
25+
val actual = object : TypeReference<CordaClaim<String>>() {}.arguments[0].typeName
26+
val expected = "java.lang.String"
27+
assertEquals(expected, actual)
28+
}
29+
}

onixlabs-corda-identity-framework-workflow/src/test/kotlin/io/onixlabs/corda/identityframework/workflow/attestation/FindAttestationFlowTests.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class FindAttestationFlowTests : FlowTest() {
5757
Pipeline
5858
.create(network)
5959
.run(it) {
60-
FindAttestationFlow<Attestation<CordaClaim<String>>>(
60+
FindAttestationFlow(
6161
linearId = attestation.state.data.linearId,
6262
stateStatus = Vault.StateStatus.UNCONSUMED
6363
)
@@ -74,7 +74,7 @@ class FindAttestationFlowTests : FlowTest() {
7474
Pipeline
7575
.create(network)
7676
.run(it) {
77-
FindAttestationFlow<Attestation<CordaClaim<String>>>(
77+
FindAttestationFlow(
7878
externalId = attestation.state.data.linearId.externalId,
7979
stateStatus = Vault.StateStatus.UNCONSUMED
8080
)
@@ -91,7 +91,7 @@ class FindAttestationFlowTests : FlowTest() {
9191
Pipeline
9292
.create(network)
9393
.run(it) {
94-
FindAttestationFlow<Attestation<CordaClaim<String>>>(
94+
FindAttestationFlow(
9595
attestor = attestation.state.data.attestor,
9696
stateStatus = Vault.StateStatus.UNCONSUMED
9797
)
@@ -108,7 +108,7 @@ class FindAttestationFlowTests : FlowTest() {
108108
Pipeline
109109
.create(network)
110110
.run(it) {
111-
FindAttestationFlow<Attestation<CordaClaim<String>>>(
111+
FindAttestationFlow(
112112
pointer = attestation.state.data.pointer,
113113
stateStatus = Vault.StateStatus.UNCONSUMED
114114
)
@@ -125,7 +125,7 @@ class FindAttestationFlowTests : FlowTest() {
125125
Pipeline
126126
.create(network)
127127
.run(it) {
128-
FindAttestationFlow<Attestation<CordaClaim<String>>>(
128+
FindAttestationFlow(
129129
pointerStateRef = claim.ref,
130130
stateStatus = Vault.StateStatus.UNCONSUMED
131131
)
@@ -142,7 +142,7 @@ class FindAttestationFlowTests : FlowTest() {
142142
Pipeline
143143
.create(network)
144144
.run(it) {
145-
FindAttestationFlow<Attestation<CordaClaim<String>>>(
145+
FindAttestationFlow(
146146
pointerStateClass = claim.state.data.javaClass,
147147
stateStatus = Vault.StateStatus.UNCONSUMED
148148
)
@@ -159,7 +159,7 @@ class FindAttestationFlowTests : FlowTest() {
159159
Pipeline
160160
.create(network)
161161
.run(it) {
162-
FindAttestationFlow<Attestation<CordaClaim<String>>>(
162+
FindAttestationFlow(
163163
pointerStateLinearId = claim.state.data.linearId,
164164
stateStatus = Vault.StateStatus.UNCONSUMED
165165
)
@@ -176,7 +176,7 @@ class FindAttestationFlowTests : FlowTest() {
176176
Pipeline
177177
.create(network)
178178
.run(it) {
179-
FindAttestationFlow<Attestation<CordaClaim<String>>>(
179+
FindAttestationFlow(
180180
pointerHash = attestation.state.data.pointer.hash,
181181
stateStatus = Vault.StateStatus.UNCONSUMED
182182
)
@@ -193,7 +193,7 @@ class FindAttestationFlowTests : FlowTest() {
193193
Pipeline
194194
.create(network)
195195
.run(it) {
196-
FindAttestationFlow<Attestation<CordaClaim<String>>>(
196+
FindAttestationFlow(
197197
status = attestation.state.data.status,
198198
stateStatus = Vault.StateStatus.UNCONSUMED
199199
)
@@ -210,7 +210,7 @@ class FindAttestationFlowTests : FlowTest() {
210210
Pipeline
211211
.create(network)
212212
.run(it) {
213-
FindAttestationFlow<Attestation<CordaClaim<String>>>(
213+
FindAttestationFlow(
214214
previousStateRef = attestation.state.data.previousStateRef,
215215
stateStatus = Vault.StateStatus.UNCONSUMED
216216
)
@@ -227,7 +227,7 @@ class FindAttestationFlowTests : FlowTest() {
227227
Pipeline
228228
.create(network)
229229
.run(it) {
230-
FindAttestationFlow<Attestation<CordaClaim<String>>>(
230+
FindAttestationFlow(
231231
hash = attestation.state.data.hash,
232232
stateStatus = Vault.StateStatus.UNCONSUMED
233233
)

0 commit comments

Comments
 (0)