Add ActorMethodInfoMap (#47)

* Add ActorMethodInfoMap
This commit is contained in:
Leon Mai 2019-12-14 11:15:49 -08:00 committed by GitHub
parent 1ab0287817
commit 70f9f6e326
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}