mirror of https://github.com/dapr/java-sdk.git
parent
1ab0287817
commit
70f9f6e326
|
@ -0,0 +1,35 @@
|
||||||
|
package io.dapr.actors.runtime;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Actor method dispatcher map. Holds method_name -> Method for methods defined in Actor interfaces.
|
||||||
|
*/
|
||||||
|
class ActorMethodInfoMap {
|
||||||
|
private final HashMap<String, Method> methods;
|
||||||
|
|
||||||
|
public <T> ActorMethodInfoMap(Iterable<Class<T>> interfaceTypes)
|
||||||
|
{
|
||||||
|
this.methods = new HashMap<String, Method>();
|
||||||
|
|
||||||
|
// Find methods which are defined in Actor interface.
|
||||||
|
for (Class<T> actorInterface : interfaceTypes)
|
||||||
|
{
|
||||||
|
for (Method methodInfo : actorInterface.getMethods())
|
||||||
|
{
|
||||||
|
this.methods.put(methodInfo.getName(), methodInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Method LookupActorMethodInfo(String methodName) throws NoSuchMethodException
|
||||||
|
{
|
||||||
|
Method methodInfo = this.methods.get(methodName);
|
||||||
|
if (methodInfo == null) {
|
||||||
|
throw new NoSuchMethodException("Actor type doesn't contain method " + methodName);
|
||||||
|
}
|
||||||
|
|
||||||
|
return methodInfo;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) Microsoft Corporation.
|
||||||
|
* Licensed under the MIT License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.dapr.actors.runtime;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Parameter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for ActorMethodInfoMap.
|
||||||
|
*/
|
||||||
|
public class ActorMethodInfoMapTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void normalUsage() {
|
||||||
|
ArrayList<Class<TestActor>> interfaceTypes = new ArrayList<Class<TestActor>>();
|
||||||
|
interfaceTypes.add(TestActor.class);
|
||||||
|
ActorMethodInfoMap m = new ActorMethodInfoMap(interfaceTypes);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Method m1 = m.LookupActorMethodInfo("getData");
|
||||||
|
Assert.assertEquals("getData", m1.getName());
|
||||||
|
Class c = m1.getReturnType();
|
||||||
|
Assert.assertEquals(c.getClass(), String.class.getClass());
|
||||||
|
Parameter[] p = m1.getParameters();
|
||||||
|
Assert.assertEquals(p[0].getType().getClass(), String.class.getClass());
|
||||||
|
}
|
||||||
|
catch(Exception e) {
|
||||||
|
Assert.fail("Exception not expected.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = NoSuchMethodException.class)
|
||||||
|
public void lookUpNonExistingMethod() throws NoSuchMethodException {
|
||||||
|
ArrayList<Class<TestActor>> interfaceTypes = new ArrayList<Class<TestActor>>();
|
||||||
|
interfaceTypes.add(TestActor.class);
|
||||||
|
ActorMethodInfoMap m = new ActorMethodInfoMap(interfaceTypes);
|
||||||
|
|
||||||
|
m.LookupActorMethodInfo("thisMethodDoesNotExist");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Only used for this test.
|
||||||
|
*/
|
||||||
|
public interface TestActor extends Actor {
|
||||||
|
String getData(String key);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue