Skip to content

Commit

Permalink
Replace Java 8 call - BigInteger.intValueExact() (#54)
Browse files Browse the repository at this point in the history
Switch to default target compatibility (Java 1.6)
  • Loading branch information
fmrsabino authored Jan 22, 2019
1 parent 5508023 commit ca9b64a
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 58 deletions.
12 changes: 1 addition & 11 deletions bivrost-abi-parser/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'maven'

sourceCompatibility = 1.8

dependencies {

implementation project(':bivrost-utils')
implementation project(':bivrost-solidity-types')

Expand All @@ -19,13 +16,6 @@ dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

uploadArchives {
repositories {
mavenDeployer {
Expand All @@ -34,7 +24,7 @@ uploadArchives {
}
}

task sourcesJar(type: Jar, dependsOn:classes) {
task sourcesJar(type: Jar, dependsOn: classes) {
from sourceSets.main.allSource
classifier = 'sources'
}
Expand Down
7 changes: 5 additions & 2 deletions bivrost-abi-parser/src/main/kotlin/pm/gnosis/AbiParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import pm.gnosis.model.*
import pm.gnosis.utils.BigIntegerUtils
import pm.gnosis.utils.generateSolidityMethodId
import java.io.File
import java.math.BigInteger
Expand Down Expand Up @@ -268,8 +269,10 @@ object AbiParser {
val source = if (isSolidityDynamicType(className)) {
val dynamicValOffsetName = "$dynamicValName$DECODER_VAR_ARG_OFFSET_SUFFIX"
function.addStatement(
"val·$dynamicValOffsetName·=·%T(%L.consume(),·16).intValueExact()",
BigInteger::class.asClassName(), DECODER_VAR_PARTITIONS_NAME
"val·$dynamicValOffsetName·=·%T.exact(%T(%L.consume(),·16))",
BigIntegerUtils::class.asClassName(),
BigInteger::class.asClassName(),
DECODER_VAR_PARTITIONS_NAME
)
"%L.subData(%L)" to mutableListOf(DECODER_VAR_PARTITIONS_NAME, dynamicValOffsetName)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.math.BigInteger
import kotlin.String
import pm.gnosis.model.Solidity
import pm.gnosis.model.SolidityBase
import pm.gnosis.utils.BigIntegerUtils

class Abi7 {
object Function {
Expand All @@ -17,7 +18,7 @@ class Abi7 {
val source = SolidityBase.PartitionData.of(data)

// Add decoders
val arg0Offset = BigInteger(source.consume(), 16).intValueExact()
val arg0Offset = BigIntegerUtils.exact(BigInteger(source.consume(), 16))
val arg0 = Solidity.Bytes.DECODER.decode(source.subData(arg0Offset))

return Return(arg0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.math.BigInteger
import kotlin.String
import pm.gnosis.model.Solidity
import pm.gnosis.model.SolidityBase
import pm.gnosis.utils.BigIntegerUtils

class Abi8 {
object Function {
Expand All @@ -17,7 +18,7 @@ class Abi8 {
val source = SolidityBase.PartitionData.of(data)

// Add decoders
val arg0Offset = BigInteger(source.consume(), 16).intValueExact()
val arg0Offset = BigIntegerUtils.exact(BigInteger(source.consume(), 16))
val arg0 = Solidity.Bytes.DECODER.decode(source.subData(arg0Offset))

return Return(arg0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlin.Boolean
import kotlin.String
import pm.gnosis.model.Solidity
import pm.gnosis.model.SolidityBase
import pm.gnosis.utils.BigIntegerUtils

class Abi9 {
object Owners {
Expand All @@ -23,7 +24,7 @@ class Abi9 {

// Add decoders
val arg0 = TupleB.DECODER.decode(source)
val arg1Offset = BigInteger(source.consume(), 16).intValueExact()
val arg1Offset = BigIntegerUtils.exact(BigInteger(source.consume(), 16))
val arg1 = SolidityBase.Vector.Decoder(TupleB.DECODER).decode(source.subData(arg1Offset))

return Return(arg0, arg1)
Expand All @@ -33,9 +34,9 @@ class Abi9 {
val source = SolidityBase.PartitionData.of(data)

// Add decoders
val arg0Offset = BigInteger(source.consume(), 16).intValueExact()
val arg0Offset = BigIntegerUtils.exact(BigInteger(source.consume(), 16))
val arg0 = SolidityBase.Vector.Decoder(TupleA.DECODER).decode(source.subData(arg0Offset))
val arg1Offset = BigInteger(source.consume(), 16).intValueExact()
val arg1Offset = BigIntegerUtils.exact(BigInteger(source.consume(), 16))
val arg1 = SolidityBase.Vector.Decoder(SolidityBase.Vector.Decoder(Array7.Decoder(Array5.Decoder(Solidity.UInt256.DECODER)))).decode(source.subData(arg1Offset))

return Arguments(arg0, arg1)
Expand All @@ -61,7 +62,7 @@ class Abi9 {
override fun decode(source: SolidityBase.PartitionData): TupleA {
val arg0 = Solidity.UInt256.DECODER.decode(source)
val arg1 = Solidity.UInt256.DECODER.decode(source)
val arg2Offset = BigInteger(source.consume(), 16).intValueExact()
val arg2Offset = BigIntegerUtils.exact(BigInteger(source.consume(), 16))
val arg2 = SolidityBase.Vector.Decoder(SolidityBase.Vector.Decoder(Array7.Decoder(Array5.Decoder(Solidity.UInt256.DECODER)))).decode(source.subData(arg2Offset))
return TupleA(arg0, arg1, arg2)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlin.String
import kotlin.collections.List
import pm.gnosis.model.Solidity
import pm.gnosis.model.SolidityBase
import pm.gnosis.utils.BigIntegerUtils

class Abi14 {
object Events {
Expand All @@ -20,9 +21,9 @@ class Abi14 {

// Decode data
val source = SolidityBase.PartitionData.of(data)
val arg0Offset = BigInteger(source.consume(), 16).intValueExact()
val arg0Offset = BigIntegerUtils.exact(BigInteger(source.consume(), 16))
val arg0 = Solidity.Bytes.DECODER.decode(source.subData(arg0Offset))
val arg1Offset = BigInteger(source.consume(), 16).intValueExact()
val arg1Offset = BigIntegerUtils.exact(BigInteger(source.consume(), 16))
val arg1 = Solidity.String.DECODER.decode(source.subData(arg1Offset))
val arg2 = TupleA.DECODER.decode(source)
return Arguments(arg0, arg1, arg2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import kotlin.Boolean
import kotlin.String
import pm.gnosis.model.Solidity
import pm.gnosis.model.SolidityBase
import pm.gnosis.utils.BigIntegerUtils

class Abi16 {
object Malformed {
Expand All @@ -18,7 +19,7 @@ class Abi16 {
val source = SolidityBase.PartitionData.of(data)

// Add decoders
val arg0Offset = BigInteger(source.consume(), 16).intValueExact()
val arg0Offset = BigIntegerUtils.exact(BigInteger(source.consume(), 16))
val arg0 = TupleA.DECODER.decode(source.subData(arg0Offset))

return Arguments(arg0)
Expand All @@ -36,9 +37,9 @@ class Abi16 {
class Decoder : SolidityBase.TypeDecoder<TupleA> {
override fun isDynamic(): Boolean = true
override fun decode(source: SolidityBase.PartitionData): TupleA {
val arg0Offset = BigInteger(source.consume(), 16).intValueExact()
val arg0Offset = BigIntegerUtils.exact(BigInteger(source.consume(), 16))
val arg0 = Solidity.Bytes.DECODER.decode(source.subData(arg0Offset))
val arg1Offset = BigInteger(source.consume(), 16).intValueExact()
val arg1Offset = BigIntegerUtils.exact(BigInteger(source.consume(), 16))
val arg1 = Solidity.String.DECODER.decode(source.subData(arg1Offset))
return TupleA(arg0, arg1)
}
Expand Down
3 changes: 0 additions & 3 deletions bivrost-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ apply plugin: 'java-library'
apply plugin: 'kotlin'
apply plugin: 'maven'

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

dependencies {
compileOnly gradleApi()

Expand Down
10 changes: 0 additions & 10 deletions bivrost-solidity-types-generator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'maven'

sourceCompatibility = 1.8

task runSolidityTypeGenerator(type: JavaExec) {
def targetProject = project.parent.childProjects.get("bivrost-solidity-types") ?: project
def srcDirs = targetProject.sourceSets.main.kotlin.getSrcDirs()
Expand All @@ -28,14 +26,6 @@ dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}

compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

task sourcesJar(type: Jar, dependsOn:classes) {
from sourceSets.main.allSource
classifier = 'sources'
Expand Down
9 changes: 0 additions & 9 deletions bivrost-solidity-types/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,12 @@ apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'maven'

sourceCompatibility = 1.8

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"

testImplementation group: 'junit', name: 'junit', version: '4.12'
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

uploadArchives {
repositories {
mavenDeployer {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package pm.gnosis.utils

import java.math.BigInteger
import kotlin.experimental.and

fun String.padStartMultiple(multiple: Int, padChar: Char = ' ') =
this.padStart(if (this.length % multiple != 0) this.length + multiple - this.length % multiple else 0, padChar)
this.padStart(if (this.length % multiple != 0) this.length + multiple - this.length % multiple else 0, padChar)

fun String.padEndMultiple(multiple: Int, padChar: Char = ' ') =
this.padEnd(if (this.length % multiple != 0) this.length + multiple - this.length % multiple else 0, padChar)
this.padEnd(if (this.length % multiple != 0) this.length + multiple - this.length % multiple else 0, padChar)


private val hexArray = "0123456789abcdef".toCharArray()
Expand All @@ -32,3 +33,10 @@ fun String.hexToByteArray(): ByteArray {
}
return data
}

// Compatibility method for pre Java8
object BigIntegerUtils {
fun exact(bigInteger: BigInteger): Int =
if (bigInteger.bitLength() <= 31) bigInteger.toInt()
else throw ArithmeticException("BigInteger out of int range")
}
10 changes: 0 additions & 10 deletions bivrost-utils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'maven'

sourceCompatibility = 1.8

dependencies {

implementation project(':bivrost-solidity-types')

implementation "org.jetbrains.kotlin:kotlin-stdlib"
Expand All @@ -16,13 +13,6 @@ dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

uploadArchives {
repositories {
mavenDeployer {
Expand Down

0 comments on commit ca9b64a

Please sign in to comment.