Reformat files in node-api

This commit is contained in:
Tommy Lillehagen 2017-10-06 15:27:39 +01:00
parent cb9e27a84a
commit f98942d7ad
36 changed files with 256 additions and 212 deletions

View File

@ -1,4 +1,5 @@
@file:JvmName("ConfigUtilities")
package net.corda.nodeapi.config
import com.typesafe.config.Config

View File

@ -1,4 +1,5 @@
@file:JvmName("ClientContexts")
package net.corda.nodeapi.internal.serialization
import net.corda.core.serialization.SerializationContext

View File

@ -140,6 +140,7 @@ object DefaultKryoCustomizer {
// Use this to allow construction of objects using a JVM backdoor that skips invoking the constructors, if there
// is no no-arg constructor available.
private val defaultStrategy = Kryo.DefaultInstantiatorStrategy(fallbackStrategy)
override fun <T> newInstantiatorOf(type: Class<T>): ObjectInstantiator<T> {
// However this doesn't work for non-public classes in the java. namespace
val strat = if (type.name.startsWith("java.") && !isPublic(type.modifiers)) fallbackStrategy else defaultStrategy
@ -151,6 +152,7 @@ object DefaultKryoCustomizer {
override fun write(kryo: Kryo, output: Output, obj: PartyAndCertificate) {
kryo.writeClassAndObject(output, obj.certPath)
}
override fun read(kryo: Kryo, input: Input, type: Class<PartyAndCertificate>): PartyAndCertificate {
return PartyAndCertificate(kryo.readClassAndObject(input) as CertPath)
}

View File

@ -1,4 +1,5 @@
@file:JvmName("ServerContexts")
package net.corda.nodeapi.internal.serialization
import net.corda.core.serialization.ClassWhitelist

View File

@ -26,8 +26,7 @@ open class ArraySerializer(override val type: Type, factory: SerializerFactory)
private fun calcTypeName(type: Type): String =
if (type.componentType().isArray()) {
val typeName = calcTypeName(type.componentType()); "$typeName[]"
}
else {
} else {
val arrayType = if (type.asClass()!!.componentType.isPrimitive) "[p]" else "[]"
"${type.componentType().typeName}$arrayType"
}

View File

@ -38,8 +38,7 @@ class CollectionSerializer(val declaredType: ParameterizedType, factory: Seriali
if (supportedTypes.containsKey(declaredClass)) {
// Simple case - it is already known to be a collection.
return deriveParametrizedType(declaredType, uncheckedCast(declaredClass))
}
else if (actualClass != null && Collection::class.java.isAssignableFrom(actualClass)) {
} else if (actualClass != null && Collection::class.java.isAssignableFrom(actualClass)) {
// Declared class is not collection, but [actualClass] is - represent it accordingly.
val collectionClass = findMostSuitableCollectionType(actualClass)
return deriveParametrizedType(declaredType, collectionClass)

View File

@ -151,8 +151,7 @@ abstract class CustomSerializer<T : Any> : AMQPSerializer<T> {
* @param unmake A lambda that extracts the string value for an instance, that defaults to the [toString] method.
*/
abstract class ToString<T : Any>(clazz: Class<T>, withInheritance: Boolean = false,
private val maker: (String) -> T = clazz.getConstructor(String::class.java).let {
`constructor` ->
private val maker: (String) -> T = clazz.getConstructor(String::class.java).let { `constructor` ->
{ string -> `constructor`.newInstance(string) }
},
private val unmaker: (T) -> String = { obj -> obj.toString() })

View File

@ -45,8 +45,7 @@ class MapSerializer(private val declaredType: ParameterizedType, factory: Serial
if (supportedTypes.containsKey(declaredClass)) {
// Simple case - it is already known to be a map.
return deriveParametrizedType(declaredType, uncheckedCast(declaredClass))
}
else if (actualClass != null && Map::class.java.isAssignableFrom(actualClass)) {
} else if (actualClass != null && Map::class.java.isAssignableFrom(actualClass)) {
// Declared class is not map, but [actualClass] is - represent it accordingly.
val mapClass = findMostSuitableMapType(actualClass)
return deriveParametrizedType(declaredType, mapClass)
@ -108,12 +107,10 @@ internal fun Class<*>.checkSupportedMapType() {
if (HashMap::class.java.isAssignableFrom(this) && !LinkedHashMap::class.java.isAssignableFrom(this)) {
throw IllegalArgumentException(
"Map type $this is unstable under iteration. Suggested fix: use java.util.LinkedHashMap instead.")
}
else if (WeakHashMap::class.java.isAssignableFrom(this)) {
} else if (WeakHashMap::class.java.isAssignableFrom(this)) {
throw IllegalArgumentException("Weak references with map types not supported. Suggested fix: "
+ "use java.util.LinkedHashMap instead.")
}
else if (Dictionary::class.java.isAssignableFrom(this)) {
} else if (Dictionary::class.java.isAssignableFrom(this)) {
throw IllegalArgumentException(
"Unable to serialise deprecated type $this. Suggested fix: prefer java.util.map implementations")
}

View File

@ -88,8 +88,7 @@ open class SerializationOutput(internal val serializerFactory: SerializerFactory
// assigned to them first as they will be first read from the stream on receiving end.
// Skip for primitive types as they are too small and overhead of referencing them will be much higher than their content
if (suitableForObjectReference(obj.javaClass)) objectHistory.put(obj, objectHistory.size)
}
else {
} else {
data.writeReferencedObject(ReferencedObject(retrievedRefCount))
}
}

View File

@ -12,10 +12,12 @@ class ZonedDateTimeSerializer(factory: SerializerFactory) : CustomSerializer.Pro
// so that any change to internals of `ZonedDateTime` is detected early.
companion object {
val ofLenient = ZonedDateTime::class.java.getDeclaredMethod("ofLenient", LocalDateTime::class.java, ZoneOffset::class.java, ZoneId::class.java)
init {
ofLenient.isAccessible = true
}
}
override val additionalSerializers: Iterable<CustomSerializer<out Any>> = listOf(LocalDateTimeSerializer(factory), ZoneIdSerializer(factory))
override fun toProxy(obj: ZonedDateTime): ZonedDateTimeProxy = ZonedDateTimeProxy(obj.toLocalDateTime(), obj.offset, obj.zone)

View File

@ -132,5 +132,10 @@ fun AMQPField.validateType(classloader: ClassLoader) = when (type) {
}
private fun ClassLoader.exists(clazz: String) = run {
try { this.loadClass(clazz); true } catch (e: ClassNotFoundException) { false } }
try {
this.loadClass(clazz); true
} catch (e: ClassNotFoundException) {
false
}
}

View File

@ -12,7 +12,8 @@ import java.util.List;
public class ListsSerializationJavaTest {
@CordaSerializable
interface Parent {}
interface Parent {
}
public static class Child implements Parent {
private final int value;

View File

@ -45,6 +45,7 @@ class AttachmentsClassLoaderTests : TestDependencyInjectionBase() {
whenever(serviceHub.attachments).thenReturn(attachmentStorage)
return this.withServiceHub(serviceHub)
}
private fun SerializationContext.withServiceHub(serviceHub: ServiceHub): SerializationContext {
return this.withTokenContext(SerializeAsTokenContextImpl(serviceHub) {}).withProperty(attachmentsClassLoaderEnabledPropertyName, true)
}

View File

@ -271,6 +271,7 @@ class CordaClassResolverTests {
}
open class SubHashSet<E> : HashSet<E>()
@Test
fun `Check blacklisted subclass`() {
expectedEx.expect(IllegalStateException::class.java)
@ -281,6 +282,7 @@ class CordaClassResolverTests {
}
class SubSubHashSet<E> : SubHashSet<E>()
@Test
fun `Check blacklisted subsubclass`() {
expectedEx.expect(IllegalStateException::class.java)
@ -291,6 +293,7 @@ class CordaClassResolverTests {
}
class ConnectionImpl(private val connection: Connection) : Connection by connection
@Test
fun `Check blacklisted interface impl`() {
expectedEx.expect(IllegalStateException::class.java)
@ -302,6 +305,7 @@ class CordaClassResolverTests {
interface SubConnection : Connection
class SubConnectionImpl(private val subConnection: SubConnection) : SubConnection by subConnection
@Test
fun `Check blacklisted super-interface impl`() {
expectedEx.expect(IllegalStateException::class.java)
@ -320,6 +324,7 @@ class CordaClassResolverTests {
@CordaSerializable
class CordaSerializableHashSet<E> : HashSet<E>()
@Test
fun `Check blacklist precedes CordaSerializable`() {
expectedEx.expect(IllegalStateException::class.java)

View File

@ -105,6 +105,7 @@ class SerializationTokenTest : TestDependencyInjectionBase() {
object UnitSerializationToken : SerializationToken {
override fun fromToken(context: SerializeAsTokenContext): Any = UnitSerializeAsToken()
}
override fun toToken(context: SerializeAsTokenContext): SerializationToken = UnitSerializationToken
}

View File

@ -17,6 +17,7 @@ class DeserializeMapTests {
@Test
fun mapTest() {
data class C(val c: Map<String, Int>)
val c = C(mapOf("A" to 1, "B" to 2))
val serialisedBytes = TestSerializationOutput(VERBOSE, sf).serialize(c)
@ -26,6 +27,7 @@ class DeserializeMapTests {
@Test(expected = java.io.NotSerializableException::class)
fun abstractMapFromMapOf() {
data class C(val c: AbstractMap<String, Int>)
val c = C(mapOf("A" to 1, "B" to 2) as AbstractMap)
val serialisedBytes = TestSerializationOutput(VERBOSE, sf).serialize(c)
@ -35,6 +37,7 @@ class DeserializeMapTests {
@Test(expected = java.io.NotSerializableException::class)
fun abstractMapFromTreeMap() {
data class C(val c: AbstractMap<String, Int>)
val c = C(TreeMap(mapOf("A" to 1, "B" to 2)))
val serialisedBytes = TestSerializationOutput(VERBOSE, sf).serialize(c)
@ -44,6 +47,7 @@ class DeserializeMapTests {
@Test
fun sortedMapTest() {
data class C(val c: SortedMap<String, Int>)
val c = C(sortedMapOf("A" to 1, "B" to 2))
val serialisedBytes = TestSerializationOutput(VERBOSE, sf).serialize(c)
DeserializationInput(sf).deserialize(serialisedBytes)
@ -52,6 +56,7 @@ class DeserializeMapTests {
@Test
fun navigableMapTest() {
data class C(val c: NavigableMap<String, Int>)
val c = C(TreeMap(mapOf("A" to 1, "B" to 2)).descendingMap())
val serialisedBytes = TestSerializationOutput(VERBOSE, sf).serialize(c)
@ -61,6 +66,7 @@ class DeserializeMapTests {
@Test
fun dictionaryTest() {
data class C(val c: Dictionary<String, Int>)
val v: Hashtable<String, Int> = Hashtable()
v.put("a", 1)
v.put("b", 2)
@ -74,6 +80,7 @@ class DeserializeMapTests {
@Test
fun hashtableTest() {
data class C(val c: Hashtable<String, Int>)
val v: Hashtable<String, Int> = Hashtable()
v.put("a", 1)
v.put("b", 2)
@ -87,6 +94,7 @@ class DeserializeMapTests {
@Test
fun hashMapTest() {
data class C(val c: HashMap<String, Int>)
val c = C(HashMap(mapOf("A" to 1, "B" to 2)))
// expect this to throw
@ -97,6 +105,7 @@ class DeserializeMapTests {
@Test
fun weakHashMapTest() {
data class C(val c: WeakHashMap<String, Int>)
val c = C(WeakHashMap(mapOf("A" to 1, "B" to 2)))
Assertions.assertThatThrownBy { TestSerializationOutput(VERBOSE, sf).serialize(c) }
@ -106,6 +115,7 @@ class DeserializeMapTests {
@Test
fun concreteTreeMapTest() {
data class C(val c: TreeMap<String, Int>)
val c = C(TreeMap(mapOf("A" to 1, "B" to 3)))
val serialisedBytes = TestSerializationOutput(VERBOSE, sf).serialize(c)
@ -115,6 +125,7 @@ class DeserializeMapTests {
@Test
fun concreteLinkedHashMapTest() {
data class C(val c: LinkedHashMap<String, Int>)
val c = C(LinkedHashMap(mapOf("A" to 1, "B" to 2)))
val serialisedBytes = TestSerializationOutput(VERBOSE, sf).serialize(c)

View File

@ -402,6 +402,7 @@ class DeserializeSimpleTypesTests {
@Test
fun arrayOfArrayOfInt() {
class C(val c: Array<Array<Int>>)
val c = C(arrayOf(arrayOf(1, 2, 3), arrayOf(4, 5, 6)))
val serialisedC = TestSerializationOutput(VERBOSE, sf1).serialize(c)
@ -421,6 +422,7 @@ class DeserializeSimpleTypesTests {
@Test
fun arrayOfIntArray() {
class C(val c: Array<IntArray>)
val c = C(arrayOf(IntArray(3), IntArray(3)))
c.c[0][0] = 1; c.c[0][1] = 2; c.c[0][2] = 3
c.c[1][0] = 4; c.c[1][1] = 5; c.c[1][2] = 6
@ -447,14 +449,24 @@ class DeserializeSimpleTypesTests {
arrayOf(IntArray(3), IntArray(3), IntArray(3)),
arrayOf(IntArray(3), IntArray(3), IntArray(3))))
for (i in 0..2) { for (j in 0..2) { for (k in 0..2) { c.c[i][j][k] = i + j + k } } }
for (i in 0..2) {
for (j in 0..2) {
for (k in 0..2) {
c.c[i][j][k] = i + j + k
}
}
}
val serialisedC = TestSerializationOutput(VERBOSE, sf1).serialize(c)
val deserializedC = DeserializationInput(sf1).deserialize(serialisedC)
for (i in 0..2) { for (j in 0..2) { for (k in 0..2) {
for (i in 0..2) {
for (j in 0..2) {
for (k in 0..2) {
assertEquals(c.c[i][j][k], deserializedC.c[i][j][k])
}}}
}
}
}
}
@Test

View File

@ -325,8 +325,10 @@ class EvolvabilityTests {
data class C(val e: Int, val c: Int, val b: Int, val a: Int, val d: Int) {
@DeprecatedConstructorForDeserialization(1)
constructor (b: Int, a: Int) : this(-1, -1, b, a, -1)
@DeprecatedConstructorForDeserialization(2)
constructor (a: Int, c: Int, b: Int) : this(-1, c, b, a, -1)
@DeprecatedConstructorForDeserialization(3)
constructor (a: Int, b: Int, c: Int, d: Int) : this(-1, c, b, a, d)
}
@ -377,6 +379,7 @@ class EvolvabilityTests {
// Add a parameter to inner but keep outer unchanged
data class Inner(val a: Int, val b: String?)
data class Outer(val a: Int, val b: Inner)
val sc2 = f.readBytes()
@ -419,10 +422,13 @@ class EvolvabilityTests {
data class C(val b: Int, val c: Int, val d: Int, val e: Int, val f: Int, val g: Int) {
@DeprecatedConstructorForDeserialization(1)
constructor (b: Int, c: Int) : this(b, c, -1, -1, -1, -1)
@DeprecatedConstructorForDeserialization(2)
constructor (b: Int, c: Int, d: Int) : this(b, c, d, -1, -1, -1)
@DeprecatedConstructorForDeserialization(3)
constructor (b: Int, c: Int, d: Int, e: Int) : this(b, c, d, e, -1, -1)
@DeprecatedConstructorForDeserialization(4)
constructor (b: Int, c: Int, d: Int, e: Int, f: Int) : this(b, c, d, e, f, -1)
}

View File

@ -535,6 +535,7 @@ class SerializationOutputTests {
}
val FOO_PROGRAM_ID = "net.corda.nodeapi.internal.serialization.amqp.SerializationOutputTests.FooContract"
class FooState : ContractState {
override val participants: List<AbstractParty> = emptyList()
}

View File

@ -19,6 +19,7 @@ class SerializeAndReturnSchemaTest {
@Test
fun getSchema() {
data class C(val a: Int, val b: Int)
val a = 1
val b = 2