diff --git a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/ConstructorSignatures.java b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/ConstructorSignatures.java index 17729915a..6da9a4ca8 100644 --- a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/ConstructorSignatures.java +++ b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/ConstructorSignatures.java @@ -17,6 +17,7 @@ package io.hotmoka.beans; import java.io.IOException; +import java.util.stream.Stream; import io.hotmoka.beans.api.signatures.ConstructorSignature; import io.hotmoka.beans.api.types.ClassType; @@ -53,7 +54,29 @@ public static ConstructorSignature of(ClassType definingClass, StorageType... fo * @return the signature of the constructor */ public static ConstructorSignature of(String definingClass, StorageType... formals) { - return new ConstructorSignatureImpl(definingClass, formals); + return new ConstructorSignatureImpl(StorageTypes.classNamed(definingClass), formals); + } + + /** + * Yields the signature of a constructor. + * + * @param definingClass the class defining the constructor + * @param formals the formal arguments of the constructor + * @return the signature of the constructor + */ + public static ConstructorSignature of(ClassType definingClass, Stream formals) { + return new ConstructorSignatureImpl(definingClass, formals.toArray(StorageType[]::new)); + } + + /** + * Yields the signature of a constructor. + * + * @param definingClass the name of the class defining the constructor + * @param formals the formal arguments of the constructor + * @return the signature of the constructor + */ + public static ConstructorSignature of(String definingClass, Stream formals) { + return new ConstructorSignatureImpl(StorageTypes.classNamed(definingClass), formals.toArray(StorageType[]::new)); } /** diff --git a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/MethodSignatures.java b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/MethodSignatures.java index e1ec9bcbc..ce84b26a5 100644 --- a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/MethodSignatures.java +++ b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/MethodSignatures.java @@ -17,12 +17,16 @@ package io.hotmoka.beans; import java.io.IOException; +import java.util.stream.Stream; import io.hotmoka.beans.api.signatures.MethodSignature; import io.hotmoka.beans.api.signatures.NonVoidMethodSignature; import io.hotmoka.beans.api.signatures.VoidMethodSignature; import io.hotmoka.beans.api.types.ClassType; import io.hotmoka.beans.api.types.StorageType; +import io.hotmoka.beans.internal.gson.MethodSignatureDecoder; +import io.hotmoka.beans.internal.gson.MethodSignatureEncoder; +import io.hotmoka.beans.internal.gson.MethodSignatureJson; import io.hotmoka.beans.internal.signatures.AbstractMethodSignature; import io.hotmoka.beans.internal.signatures.NonVoidMethodSignatureImpl; import io.hotmoka.beans.internal.signatures.VoidMethodSignatureImpl; @@ -58,7 +62,33 @@ public static NonVoidMethodSignature of(ClassType definingClass, String methodNa * @return the signature of the method */ public static NonVoidMethodSignature of(String definingClass, String methodName, StorageType returnType, StorageType... formals) { - return new NonVoidMethodSignatureImpl(definingClass, methodName, returnType, formals); + return new NonVoidMethodSignatureImpl(StorageTypes.classNamed(definingClass), methodName, returnType, formals); + } + + /** + * Yields the signature of a method, that returns a value. + * + * @param definingClass the class of the method + * @param methodName the name of the method + * @param returnType the type of the returned value + * @param formals the formal arguments of the method + * @return the signature of the method + */ + public static NonVoidMethodSignature of(ClassType definingClass, String methodName, StorageType returnType, Stream formals) { + return new NonVoidMethodSignatureImpl(definingClass, methodName, returnType, formals.toArray(StorageType[]::new)); + } + + /** + * Yields the signature of a method, that returns a value. + * + * @param definingClass the name of the class of the method + * @param methodName the name of the method + * @param returnType the type of the returned value + * @param formals the formal arguments of the method + * @return the signature of the method + */ + public static NonVoidMethodSignature of(String definingClass, String methodName, StorageType returnType, Stream formals) { + return new NonVoidMethodSignatureImpl(StorageTypes.classNamed(definingClass), methodName, returnType, formals.toArray(StorageType[]::new)); } /** @@ -82,7 +112,31 @@ public static VoidMethodSignature ofVoid(ClassType definingClass, String methodN * @return the signature of the method */ public static VoidMethodSignature ofVoid(String definingClass, String methodName, StorageType... formals) { - return new VoidMethodSignatureImpl(definingClass, methodName, formals); + return new VoidMethodSignatureImpl(StorageTypes.classNamed(definingClass), methodName, formals); + } + + /** + * Yields the signature of a method, that returns no value. + * + * @param definingClass the class of the method + * @param methodName the name of the method + * @param formals the formal arguments of the method + * @return the signature of the method + */ + public static VoidMethodSignature ofVoid(ClassType definingClass, String methodName, Stream formals) { + return new VoidMethodSignatureImpl(definingClass, methodName, formals.toArray(StorageType[]::new)); + } + + /** + * Yields the signature of a method, that returns no value. + * + * @param definingClass the name of the class of the method + * @param methodName the name of the method + * @param formals the formal arguments of the method + * @return the signature of the method + */ + public static VoidMethodSignature ofVoid(String definingClass, String methodName, Stream formals) { + return new VoidMethodSignatureImpl(StorageTypes.classNamed(definingClass), methodName, formals.toArray(StorageType[]::new)); } /** @@ -97,6 +151,43 @@ public static MethodSignature from(UnmarshallingContext context) throws IOExcept } /** + * Gson encoder. + */ + public static class Encoder extends MethodSignatureEncoder { + + /** + * Creates a new encoder. + */ + public Encoder() {} + } + + /** + * Gson decoder. + */ + public static class Decoder extends MethodSignatureDecoder { + + /** + * Creates a new decoder. + */ + public Decoder() {} + } + + /** + * Json representation. + */ + public static class Json extends MethodSignatureJson { + + /** + * Creates the Json representation for the given method signature. + * + * @param method the method signature + */ + public Json(MethodSignature method) { + super(method); + } + } + + /** * The method {@code balance} of a contract. */ public final static NonVoidMethodSignature BALANCE = AbstractMethodSignature.BALANCE; diff --git a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/gson/ConstructorSignatureJson.java b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/gson/ConstructorSignatureJson.java index fd761a508..a683d22e8 100644 --- a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/gson/ConstructorSignatureJson.java +++ b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/gson/ConstructorSignatureJson.java @@ -38,6 +38,6 @@ protected ConstructorSignatureJson(ConstructorSignature constructor) { @Override public ConstructorSignature unmap() { - return ConstructorSignatures.of(StorageTypes.classNamed(definingClass), Stream.of(formals).map(StorageTypes::named).toArray(StorageType[]::new)); + return ConstructorSignatures.of(StorageTypes.classNamed(definingClass), Stream.of(formals).map(StorageTypes::named)); } } \ No newline at end of file diff --git a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/gson/MethodSignatureDecoder.java b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/gson/MethodSignatureDecoder.java new file mode 100644 index 000000000..6f01ca114 --- /dev/null +++ b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/gson/MethodSignatureDecoder.java @@ -0,0 +1,31 @@ +/* +Copyright 2024 Fausto Spoto + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package io.hotmoka.beans.internal.gson; + +import io.hotmoka.beans.MethodSignatures; +import io.hotmoka.beans.api.signatures.MethodSignature; +import io.hotmoka.websockets.beans.MappedDecoder; + +/** + * A decoder for {@link MethodSignature}. + */ +public class MethodSignatureDecoder extends MappedDecoder { + + public MethodSignatureDecoder() { + super(MethodSignatures.Json.class); + } +} \ No newline at end of file diff --git a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/gson/MethodSignatureEncoder.java b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/gson/MethodSignatureEncoder.java new file mode 100644 index 000000000..a0131f01e --- /dev/null +++ b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/gson/MethodSignatureEncoder.java @@ -0,0 +1,31 @@ +/* +Copyright 2024 Fausto Spoto + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package io.hotmoka.beans.internal.gson; + +import io.hotmoka.beans.MethodSignatures; +import io.hotmoka.beans.api.signatures.MethodSignature; +import io.hotmoka.websockets.beans.MappedEncoder; + +/** + * An encoder for {@link MethodSignature}. + */ +public class MethodSignatureEncoder extends MappedEncoder { + + public MethodSignatureEncoder() { + super(MethodSignatures.Json::new); + } +} \ No newline at end of file diff --git a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/gson/MethodSignatureJson.java b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/gson/MethodSignatureJson.java new file mode 100644 index 000000000..87401da06 --- /dev/null +++ b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/gson/MethodSignatureJson.java @@ -0,0 +1,51 @@ +/* +Copyright 2024 Fausto Spoto + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package io.hotmoka.beans.internal.gson; + +import java.util.stream.Stream; + +import io.hotmoka.beans.MethodSignatures; +import io.hotmoka.beans.StorageTypes; +import io.hotmoka.beans.api.signatures.MethodSignature; +import io.hotmoka.beans.api.signatures.NonVoidMethodSignature; +import io.hotmoka.beans.api.types.StorageType; +import io.hotmoka.websockets.beans.api.JsonRepresentation; + +/** + * The JSON representation of a {@link MethodSignature}. + */ +public abstract class MethodSignatureJson implements JsonRepresentation { + private final String definingClass; + private final String name; + private final String[] formals; + private final String returnType; + + protected MethodSignatureJson(MethodSignature method) { + this.definingClass = method.getDefiningClass().getName(); + this.name = method.getMethodName(); + this.formals = method.getFormals().map(StorageType::getName).toArray(String[]::new); + this.returnType = method instanceof NonVoidMethodSignature nvms ? nvms.getReturnType().getName() : null; + } + + @Override + public MethodSignature unmap() { + if (returnType == null) + return MethodSignatures.ofVoid(StorageTypes.classNamed(definingClass), name, Stream.of(formals).map(StorageTypes::named)); + else + return MethodSignatures.of(StorageTypes.classNamed(definingClass), name, StorageTypes.named(returnType), Stream.of(formals).map(StorageTypes::named)); + } +} \ No newline at end of file diff --git a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/AbstractCodeSignature.java b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/AbstractCodeSignature.java index 27c643f05..f1c83f648 100644 --- a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/AbstractCodeSignature.java +++ b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/AbstractCodeSignature.java @@ -24,7 +24,6 @@ import java.util.stream.Stream; import io.hotmoka.annotations.Immutable; -import io.hotmoka.beans.StorageTypes; import io.hotmoka.beans.api.signatures.CodeSignature; import io.hotmoka.beans.api.types.ClassType; import io.hotmoka.beans.api.types.StorageType; @@ -47,7 +46,7 @@ public abstract class AbstractCodeSignature extends AbstractMarshallable impleme * The formal arguments of the method or constructor. */ private final StorageType[] formals; - + /** * Builds the signature of a method or constructor. * @@ -60,16 +59,6 @@ protected AbstractCodeSignature(ClassType definingClass, StorageType... formals) Stream.of(formals).forEach(formal -> Objects.requireNonNull(formal, "formals cannot hold null")); } - /** - * Builds the signature of a method or constructor. - * - * @param definingClass the name of the class of the method or constructor - * @param formals the formal arguments of the method or constructor - */ - public AbstractCodeSignature(String definingClass, StorageType... formals) { - this(StorageTypes.classNamed(definingClass), formals); - } - @Override public final ClassType getDefiningClass() { return definingClass; diff --git a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/ConstructorSignatureImpl.java b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/ConstructorSignatureImpl.java index 879f1c4c5..fe70d0639 100644 --- a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/ConstructorSignatureImpl.java +++ b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/ConstructorSignatureImpl.java @@ -43,16 +43,6 @@ public ConstructorSignatureImpl(ClassType definingClass, StorageType... formals) super(definingClass, formals); } - /** - * Builds the signature of a constructor. - * - * @param definingClass the name of the class of the constructor - * @param formals the formal arguments of the constructor - */ - public ConstructorSignatureImpl(String definingClass, StorageType... formals) { - super(definingClass, formals); - } - @Override public String toString() { return getDefiningClass() + commaSeparatedFormals(); @@ -93,5 +83,5 @@ public static ConstructorSignature from(UnmarshallingContext context) throws IOE /** * The constructor of an externally owned account with a big integer amount. */ - public final static ConstructorSignatureImpl EOA_CONSTRUCTOR = new ConstructorSignatureImpl(StorageTypes.EOA, StorageTypes.BIG_INTEGER, StorageTypes.STRING); + public final static ConstructorSignature EOA_CONSTRUCTOR = ConstructorSignatures.of(StorageTypes.EOA, StorageTypes.BIG_INTEGER, StorageTypes.STRING); } \ No newline at end of file diff --git a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/FieldSignatureImpl.java b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/FieldSignatureImpl.java index 3eff2c2c0..c8575a397 100644 --- a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/FieldSignatureImpl.java +++ b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/FieldSignatureImpl.java @@ -21,6 +21,7 @@ import java.util.Objects; import io.hotmoka.annotations.Immutable; +import io.hotmoka.beans.FieldSignatures; import io.hotmoka.beans.StorageTypes; import io.hotmoka.beans.api.signatures.FieldSignature; import io.hotmoka.beans.api.types.ClassType; @@ -40,123 +41,123 @@ public final class FieldSignatureImpl extends AbstractMarshallable implements Fi /** * The field that holds the balance in contracts. */ - public final static FieldSignatureImpl BALANCE_FIELD = new FieldSignatureImpl(StorageTypes.CONTRACT, "balance", StorageTypes.BIG_INTEGER); + public final static FieldSignature BALANCE_FIELD = FieldSignatures.of(StorageTypes.CONTRACT, "balance", StorageTypes.BIG_INTEGER); /** * The field that holds the red balance in contracts. */ - public final static FieldSignatureImpl RED_BALANCE_FIELD = new FieldSignatureImpl(StorageTypes.CONTRACT, "balanceRed", StorageTypes.BIG_INTEGER); + public final static FieldSignature RED_BALANCE_FIELD = FieldSignatures.of(StorageTypes.CONTRACT, "balanceRed", StorageTypes.BIG_INTEGER); /** * The field that holds the nonce in externally owned accounts. */ - public final static FieldSignatureImpl EOA_NONCE_FIELD = new FieldSignatureImpl(StorageTypes.EOA, "nonce", StorageTypes.BIG_INTEGER); + public final static FieldSignature EOA_NONCE_FIELD = FieldSignatures.of(StorageTypes.EOA, "nonce", StorageTypes.BIG_INTEGER); /** * The field that holds the public key in externally owned accounts. */ - public final static FieldSignatureImpl EOA_PUBLIC_KEY_FIELD = new FieldSignatureImpl(StorageTypes.EOA, "publicKey", StorageTypes.STRING); + public final static FieldSignature EOA_PUBLIC_KEY_FIELD = FieldSignatures.of(StorageTypes.EOA, "publicKey", StorageTypes.STRING); /** * The field of the manifest that holds the contract of the validators of the node. */ - public final static FieldSignatureImpl MANIFEST_VALIDATORS_FIELD = new FieldSignatureImpl(Constants.MANIFEST_NAME, "validators", StorageTypes.VALIDATORS); + public final static FieldSignature MANIFEST_VALIDATORS_FIELD = FieldSignatures.of(Constants.MANIFEST_NAME, "validators", StorageTypes.VALIDATORS); /** * The field of the manifest that holds the object that keeps track * of the versions of the modules of the node. */ - public final static FieldSignatureImpl MANIFEST_VERSIONS_FIELD = new FieldSignatureImpl(Constants.MANIFEST_NAME, "versions", StorageTypes.VERSIONS); + public final static FieldSignature MANIFEST_VERSIONS_FIELD = FieldSignatures.of(Constants.MANIFEST_NAME, "versions", StorageTypes.VERSIONS); /** * The field of the manifest that holds the gas station. */ - public final static FieldSignatureImpl MANIFEST_GAS_STATION_FIELD = new FieldSignatureImpl(StorageTypes.MANIFEST, "gasStation", StorageTypes.GAS_STATION); + public final static FieldSignature MANIFEST_GAS_STATION_FIELD = FieldSignatures.of(StorageTypes.MANIFEST, "gasStation", StorageTypes.GAS_STATION); /** * The field of the manifest that holds the gamete account of the node. */ - public final static FieldSignatureImpl MANIFEST_GAMETE_FIELD = new FieldSignatureImpl(Constants.MANIFEST_NAME, "gamete", StorageTypes.GAMETE); + public final static FieldSignature MANIFEST_GAMETE_FIELD = FieldSignatures.of(Constants.MANIFEST_NAME, "gamete", StorageTypes.GAMETE); /** * The field that holds the creator of an event. */ - public final static FieldSignatureImpl EVENT_CREATOR_FIELD = new FieldSignatureImpl(StorageTypes.EVENT, "creator", StorageTypes.CONTRACT); + public final static FieldSignature EVENT_CREATOR_FIELD = FieldSignatures.of(StorageTypes.EVENT, "creator", StorageTypes.CONTRACT); /** * The field that holds the gas price inside a {@code io.takamaka.code.governance.GenericGasStation}. */ - public final static FieldSignatureImpl GENERIC_GAS_STATION_GAS_PRICE_FIELD = new FieldSignatureImpl(StorageTypes.GENERIC_GAS_STATION, "gasPrice", StorageTypes.BIG_INTEGER); + public final static FieldSignature GENERIC_GAS_STATION_GAS_PRICE_FIELD = FieldSignatures.of(StorageTypes.GENERIC_GAS_STATION, "gasPrice", StorageTypes.BIG_INTEGER); /** * The field that holds the current supply inside a {@code io.takamaka.code.governance.AbstractValidators}. */ - public final static FieldSignatureImpl ABSTRACT_VALIDATORS_CURRENT_SUPPLY_FIELD = new FieldSignatureImpl(StorageTypes.ABSTRACT_VALIDATORS, "currentSupply", StorageTypes.BIG_INTEGER); + public final static FieldSignature ABSTRACT_VALIDATORS_CURRENT_SUPPLY_FIELD = FieldSignatures.of(StorageTypes.ABSTRACT_VALIDATORS, "currentSupply", StorageTypes.BIG_INTEGER); /** * The field that holds the gas price inside a {@code io.takamaka.code.math.UnsignedBigInteger.value}. */ - public final static FieldSignatureImpl UNSIGNED_BIG_INTEGER_VALUE_FIELD = new FieldSignatureImpl(StorageTypes.UNSIGNED_BIG_INTEGER, "value", StorageTypes.BIG_INTEGER); + public final static FieldSignature UNSIGNED_BIG_INTEGER_VALUE_FIELD = FieldSignatures.of(StorageTypes.UNSIGNED_BIG_INTEGER, "value", StorageTypes.BIG_INTEGER); /** * The field that holds the root of a {@code io.takamaka.code.util.StorageTreeMap}. */ - public final static FieldSignatureImpl STORAGE_TREE_MAP_ROOT_FIELD = new FieldSignatureImpl(StorageTypes.STORAGE_TREE_MAP, "root", StorageTypes.STORAGE_TREE_MAP_NODE); + public final static FieldSignature STORAGE_TREE_MAP_ROOT_FIELD = FieldSignatures.of(StorageTypes.STORAGE_TREE_MAP, "root", StorageTypes.STORAGE_TREE_MAP_NODE); /** * The field that holds the root of a {@code io.takamaka.code.util.StorageIntTreeMap}. */ - public final static FieldSignatureImpl STORAGE_TREE_INTMAP_ROOT_FIELD = new FieldSignatureImpl(StorageTypes.STORAGE_TREE_INTMAP, "root", StorageTypes.STORAGE_TREE_INTMAP_NODE); + public final static FieldSignature STORAGE_TREE_INTMAP_ROOT_FIELD = FieldSignatures.of(StorageTypes.STORAGE_TREE_INTMAP, "root", StorageTypes.STORAGE_TREE_INTMAP_NODE); /** * The field that holds the size of a {@code io.takamaka.code.util.StorageTreeMap.Node}. */ - public final static FieldSignatureImpl STORAGE_TREE_MAP_NODE_SIZE_FIELD = new FieldSignatureImpl(StorageTypes.STORAGE_TREE_MAP_NODE, "size", StorageTypes.INT); + public final static FieldSignature STORAGE_TREE_MAP_NODE_SIZE_FIELD = FieldSignatures.of(StorageTypes.STORAGE_TREE_MAP_NODE, "size", StorageTypes.INT); /** * The field that holds the size of a {@code io.takamaka.code.util.StorageTreeIntMap.Node}. */ - public final static FieldSignatureImpl STORAGE_TREE_INTMAP_NODE_SIZE_FIELD = new FieldSignatureImpl(StorageTypes.STORAGE_TREE_INTMAP_NODE, "size", StorageTypes.INT); + public final static FieldSignature STORAGE_TREE_INTMAP_NODE_SIZE_FIELD = FieldSignatures.of(StorageTypes.STORAGE_TREE_INTMAP_NODE, "size", StorageTypes.INT); /** * The field that holds the value of a {@code io.takamaka.code.util.StorageTreeIntMap.Node}. */ - public final static FieldSignatureImpl STORAGE_TREE_INTMAP_NODE_VALUE_FIELD = new FieldSignatureImpl(StorageTypes.STORAGE_TREE_INTMAP_NODE, "value", StorageTypes.OBJECT); + public final static FieldSignature STORAGE_TREE_INTMAP_NODE_VALUE_FIELD = FieldSignatures.of(StorageTypes.STORAGE_TREE_INTMAP_NODE, "value", StorageTypes.OBJECT); /** * The field that holds the left child of a {@code io.takamaka.code.util.StorageTreeIntMap.Node}. */ - public final static FieldSignatureImpl STORAGE_TREE_INTMAP_NODE_LEFT_FIELD = new FieldSignatureImpl(StorageTypes.STORAGE_TREE_INTMAP_NODE, "left", StorageTypes.STORAGE_TREE_INTMAP_NODE); + public final static FieldSignature STORAGE_TREE_INTMAP_NODE_LEFT_FIELD = FieldSignatures.of(StorageTypes.STORAGE_TREE_INTMAP_NODE, "left", StorageTypes.STORAGE_TREE_INTMAP_NODE); /** * The field that holds the right child of a {@code io.takamaka.code.util.StorageTreeIntMap.Node}. */ - public final static FieldSignatureImpl STORAGE_TREE_INTMAP_NODE_RIGHT_FIELD = new FieldSignatureImpl(StorageTypes.STORAGE_TREE_INTMAP_NODE, "right", StorageTypes.STORAGE_TREE_INTMAP_NODE); + public final static FieldSignature STORAGE_TREE_INTMAP_NODE_RIGHT_FIELD = FieldSignatures.of(StorageTypes.STORAGE_TREE_INTMAP_NODE, "right", StorageTypes.STORAGE_TREE_INTMAP_NODE); /** * The field that holds the key of a {@code io.takamaka.code.util.StorageTreeIntMap.Node}. */ - public final static FieldSignatureImpl STORAGE_TREE_INTMAP_NODE_KEY_FIELD = new FieldSignatureImpl(StorageTypes.STORAGE_TREE_INTMAP_NODE, "key", StorageTypes.INT); + public final static FieldSignature STORAGE_TREE_INTMAP_NODE_KEY_FIELD = FieldSignatures.of(StorageTypes.STORAGE_TREE_INTMAP_NODE, "key", StorageTypes.INT); /** * The field that holds the left tree of a {@code io.takamaka.code.util.StorageTreeMap.Node}. */ - public final static FieldSignatureImpl STORAGE_TREE_MAP_NODE_LEFT_FIELD = new FieldSignatureImpl(StorageTypes.STORAGE_TREE_MAP_NODE, "left", StorageTypes.STORAGE_TREE_MAP_NODE); + public final static FieldSignature STORAGE_TREE_MAP_NODE_LEFT_FIELD = FieldSignatures.of(StorageTypes.STORAGE_TREE_MAP_NODE, "left", StorageTypes.STORAGE_TREE_MAP_NODE); /** * The field that holds the right tree of a {@code io.takamaka.code.util.StorageTreeMap.Node}. */ - public final static FieldSignatureImpl STORAGE_TREE_MAP_NODE_RIGHT_FIELD = new FieldSignatureImpl(StorageTypes.STORAGE_TREE_MAP_NODE, "right", StorageTypes.STORAGE_TREE_MAP_NODE); + public final static FieldSignature STORAGE_TREE_MAP_NODE_RIGHT_FIELD = FieldSignatures.of(StorageTypes.STORAGE_TREE_MAP_NODE, "right", StorageTypes.STORAGE_TREE_MAP_NODE); /** * The field that holds the key of a {@code io.takamaka.code.util.StorageTreeMap.Node}. */ - public final static FieldSignatureImpl STORAGE_TREE_MAP_NODE_KEY_FIELD = new FieldSignatureImpl(StorageTypes.STORAGE_TREE_MAP_NODE, "key", StorageTypes.OBJECT); + public final static FieldSignature STORAGE_TREE_MAP_NODE_KEY_FIELD = FieldSignatures.of(StorageTypes.STORAGE_TREE_MAP_NODE, "key", StorageTypes.OBJECT); /** * The field that holds the value of a {@code io.takamaka.code.util.StorageTreeMap.Node}. */ - public final static FieldSignatureImpl STORAGE_TREE_MAP_NODE_VALUE_FIELD = new FieldSignatureImpl(StorageTypes.STORAGE_TREE_MAP_NODE, "value", StorageTypes.OBJECT); + public final static FieldSignature STORAGE_TREE_MAP_NODE_VALUE_FIELD = FieldSignatures.of(StorageTypes.STORAGE_TREE_MAP_NODE, "value", StorageTypes.OBJECT); /** * The class of the field. diff --git a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/NonVoidMethodSignatureImpl.java b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/NonVoidMethodSignatureImpl.java index 04681b8cd..bac49d770 100644 --- a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/NonVoidMethodSignatureImpl.java +++ b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/NonVoidMethodSignatureImpl.java @@ -20,7 +20,6 @@ import java.util.Objects; import io.hotmoka.annotations.Immutable; -import io.hotmoka.beans.StorageTypes; import io.hotmoka.beans.api.signatures.NonVoidMethodSignature; import io.hotmoka.beans.api.types.ClassType; import io.hotmoka.beans.api.types.StorageType; @@ -51,18 +50,6 @@ public NonVoidMethodSignatureImpl(ClassType definingClass, String methodName, St this.returnType = Objects.requireNonNull(returnType, "returnType cannot be null"); } - /** - * Builds the signature of a method, that returns a value. - * - * @param definingClass the name of the class of the method - * @param methodName the name of the method - * @param returnType the type of the returned value - * @param formals the formal arguments of the method - */ - public NonVoidMethodSignatureImpl(String definingClass, String methodName, StorageType returnType, StorageType... formals) { - this(StorageTypes.classNamed(definingClass), methodName, returnType, formals); - } - @Override public StorageType getReturnType() { return returnType; diff --git a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/VoidMethodSignatureImpl.java b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/VoidMethodSignatureImpl.java index 3280a3267..f7a82e6d7 100644 --- a/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/VoidMethodSignatureImpl.java +++ b/io-hotmoka-beans/src/main/java/io/hotmoka/beans/internal/signatures/VoidMethodSignatureImpl.java @@ -19,7 +19,6 @@ import java.io.IOException; import io.hotmoka.annotations.Immutable; -import io.hotmoka.beans.StorageTypes; import io.hotmoka.beans.api.signatures.VoidMethodSignature; import io.hotmoka.beans.api.types.ClassType; import io.hotmoka.beans.api.types.StorageType; @@ -42,17 +41,6 @@ public VoidMethodSignatureImpl(ClassType definingClass, String methodName, Stora super(definingClass, methodName, formals); } - /** - * Builds the signature of a method, that returns no value. - * - * @param definingClass the name of the class of the method - * @param methodName the name of the method - * @param formals the formal arguments of the method - */ - public VoidMethodSignatureImpl(String definingClass, String methodName, StorageType... formals) { - this(StorageTypes.classNamed(definingClass), methodName, formals); - } - @Override public String toString() { return "void " + getDefiningClass() + "." + getMethodName() + commaSeparatedFormals(); diff --git a/io-hotmoka-beans/src/test/java/io/hotmoka/beans/tests/MethodSignatureTests.java b/io-hotmoka-beans/src/test/java/io/hotmoka/beans/tests/MethodSignatureTests.java new file mode 100644 index 000000000..10788d828 --- /dev/null +++ b/io-hotmoka-beans/src/test/java/io/hotmoka/beans/tests/MethodSignatureTests.java @@ -0,0 +1,49 @@ +/* +Copyright 2024 Fausto Spoto + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package io.hotmoka.beans.tests; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import io.hotmoka.beans.MethodSignatures; +import io.hotmoka.beans.StorageTypes; +import io.hotmoka.testing.AbstractLoggedTests; +import jakarta.websocket.DecodeException; +import jakarta.websocket.EncodeException; + +public class MethodSignatureTests extends AbstractLoggedTests { + + @Test + @DisplayName("void method signatures are correctly encoded into Json and decoded from Json") + public void encodeDecodeWorksForVoidMethodSignature() throws EncodeException, DecodeException { + var method1 = MethodSignatures.ofVoid("io.hotmoka.MyClass", "m", StorageTypes.named("io.hotmoka.OtherClass"), StorageTypes.CHAR, StorageTypes.DOUBLE, StorageTypes.named("io.hotmoka.Something")); + String encoded = new MethodSignatures.Encoder().encode(method1); + var method2 = new MethodSignatures.Decoder().decode(encoded); + assertEquals(method1, method2); + } + + @Test + @DisplayName("non-void method signatures are correctly encoded into Json and decoded from Json") + public void encodeDecodeWorksForNonVoidMethodSignature() throws EncodeException, DecodeException { + var method1 = MethodSignatures.of("io.hotmoka.MyClass", "m", StorageTypes.FLOAT, StorageTypes.named("io.hotmoka.OtherClass"), StorageTypes.CHAR, StorageTypes.DOUBLE, StorageTypes.named("io.hotmoka.Something")); + String encoded = new MethodSignatures.Encoder().encode(method1); + var method2 = new MethodSignatures.Decoder().decode(encoded); + assertEquals(method1, method2); + } +} \ No newline at end of file