Skip to content

Commit

Permalink
Added JSON representation for the mthod signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
spoto committed Jan 17, 2024
1 parent 30c089f commit 38a48c9
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<StorageType> 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<StorageType> formals) {
return new ConstructorSignatureImpl(StorageTypes.classNamed(definingClass), formals.toArray(StorageType[]::new));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<StorageType> 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<StorageType> formals) {
return new NonVoidMethodSignatureImpl(StorageTypes.classNamed(definingClass), methodName, returnType, formals.toArray(StorageType[]::new));
}

/**
Expand All @@ -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<StorageType> 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<StorageType> formals) {
return new VoidMethodSignatureImpl(StorageTypes.classNamed(definingClass), methodName, formals.toArray(StorageType[]::new));
}

/**
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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<MethodSignature, MethodSignatures.Json> {

public MethodSignatureDecoder() {
super(MethodSignatures.Json.class);
}
}
Original file line number Diff line number Diff line change
@@ -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<MethodSignature, MethodSignatures.Json> {

public MethodSignatureEncoder() {
super(MethodSignatures.Json::new);
}
}
Original file line number Diff line number Diff line change
@@ -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<MethodSignature> {
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Loading

0 comments on commit 38a48c9

Please sign in to comment.