Skip to content

Commit a0da213

Browse files
committed
Check if target method is external
1 parent 51defce commit a0da213

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

unittest/src/main/java/com/iconloop/score/test/Score.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.iconloop.score.test;
1818

1919
import score.Address;
20+
import score.annotation.External;
2021
import score.annotation.Optional;
2122
import score.UserRevertedException;
2223

@@ -116,13 +117,12 @@ private Object[] convertParameters(Method method, Object[] params) {
116117

117118
private Method getMethodByName(String name) throws NoSuchMethodException {
118119
Class<?> clazz = instance.getClass();
119-
Method[] m = clazz.getMethods();
120-
for (Method method : m) {
121-
if (method.getName().equals(name)) {
122-
return method;
120+
Method[] methods = clazz.getMethods();
121+
for (Method m : methods) {
122+
if (m.getName().equals(name) && isExternal(m)) {
123+
return m;
123124
}
124125
}
125-
126126
throw new NoSuchMethodException();
127127
}
128128

@@ -161,6 +161,10 @@ private Object getDefault(Class<?> type) {
161161
}
162162
}
163163

164+
private boolean isExternal(Method method) {
165+
return method.getAnnotation(External.class) != null;
166+
}
167+
164168
private boolean isOptional(Parameter parameter) {
165169
return parameter.getAnnotation(Optional.class) != null;
166170
}

unittest/src/main/java/score/annotation/External.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
package score.annotation;
1818

1919
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
2022
import java.lang.annotation.Target;
2123

24+
@Retention(RetentionPolicy.RUNTIME)
2225
@Target(ElementType.METHOD)
2326
public @interface External {
2427
boolean readonly() default false;

unittest/src/test/java/score/CallTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@
3030
import java.util.List;
3131

3232
import static org.junit.jupiter.api.Assertions.assertEquals;
33+
import static org.junit.jupiter.api.Assertions.assertThrows;
3334

3435
public class CallTest extends TestBase {
3536
private static final ServiceManager sm = getServiceManager();
3637
private static final Account owner = sm.createAccount();
3738
private static Score echoScore;
3839

3940
public static class Echo {
41+
public int nonExternal() {
42+
return 0;
43+
}
44+
4045
@External(readonly=true)
4146
public String echo(@Optional String message) {
4247
return message;
@@ -113,6 +118,12 @@ static void setUp() throws Exception {
113118
echoScore = sm.deploy(owner, Echo.class);
114119
}
115120

121+
@Test
122+
void callNonExternal() {
123+
assertThrows(RuntimeException.class, () ->
124+
echoScore.call("nonExternal"));
125+
}
126+
116127
@Test
117128
void callCasted() {
118129
String echoMessage = "test";

0 commit comments

Comments
 (0)