use of com.dtflys.forest.reflection.ForestMethod in project forest by dromara.
the class InterfaceProxyHandler method invoke.
/**
* 调用 Forest 动态代理接口对象的方法
*
* @param proxy 动态代理对象
* @param method 所要调用的方法 {@link Method}对象
* @param args 所要调用方法的入参数组
* @return 方法调用返回结果
* @throws Throwable 方法调用过程中可能抛出的异常
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
if (method.isDefault()) {
return invokeDefaultMethod(proxy, method, args);
}
ForestMethod forestMethod = forestMethodMap.get(method);
if (forestMethod == null) {
if (args == null || args.length == 0) {
if ("toString".equals(methodName)) {
return "{Forest Proxy Object of " + interfaceClass.getName() + "}";
}
if ("getClass".equals(methodName)) {
return proxy.getClass();
}
if ("hashCode".equals(methodName)) {
return proxy.hashCode();
}
if ("notify".equals(methodName)) {
proxy.notify();
return null;
}
if ("notifyAll".equals(methodName)) {
proxy.notifyAll();
return null;
}
if ("wait".equals(methodName)) {
proxy.wait();
return null;
}
}
if (args != null && args.length == 1) {
if ("equals".equals(methodName)) {
Object obj = args[0];
if (Proxy.isProxyClass(obj.getClass())) {
InvocationHandler h1 = Proxy.getInvocationHandler(proxy);
InvocationHandler h2 = Proxy.getInvocationHandler(obj);
return h1.equals(h2);
}
return false;
}
if ("wait".equals(methodName) && args[0] instanceof Long) {
proxy.wait((Long) args[0]);
}
}
if (args != null && args.length == 2 && args[0] instanceof Long && args[1] instanceof Integer) {
if ("wait".equals(methodName)) {
proxy.wait((Long) args[0], (Integer) args[1]);
}
}
throw new NoSuchMethodError(method.getName());
}
return forestMethod.invoke(args);
}
use of com.dtflys.forest.reflection.ForestMethod in project forest by dromara.
the class InterfaceProxyHandler method initMethods.
private void initMethods(Class<?> clazz) {
Class<?>[] superClasses = clazz.getInterfaces();
for (Class<?> superClass : superClasses) {
initMethods(superClass);
}
Method[] methods = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if (method.isDefault()) {
continue;
}
ForestMethod forestMethod = new ForestMethod(this, configuration, method);
forestMethodMap.put(method, forestMethod);
}
}
use of com.dtflys.forest.reflection.ForestMethod in project forest by dromara.
the class ForestVariableUndefinedException method getErrorMessage.
private static String getErrorMessage(Class<? extends Annotation> annotationType, String attributeName, ForestMethod forestMethod, String variableName, String source) {
StringBuilder builder = new StringBuilder();
builder.append("[Forest] Cannot resolve variable '");
builder.append(variableName);
builder.append("'");
if (StringUtils.isNotBlank(source)) {
builder.append("\n\n\t[From Template]\n\t");
if (forestMethod != null) {
Method method = forestMethod.getMethod();
String typeName = method.getDeclaringClass().getTypeName();
String methodName = method.getName();
Class<?>[] paramTypes = method.getParameterTypes();
builder.append("method: ").append(typeName).append('.').append(methodName).append('(');
for (int i = 0; i < paramTypes.length; i++) {
Class<?> pType = paramTypes[i];
builder.append(pType.getName());
if (pType.isArray()) {
builder.append("[]");
}
if (i < paramTypes.length - 1) {
builder.append(", ");
}
}
builder.append(")\n\t");
}
if (annotationType != null) {
String annTypeName = annotationType.getSimpleName();
builder.append("annotation: ").append(annotationType.getPackage().getName()).append(".@").append(annTypeName).append("\n\t");
}
if (attributeName != null) {
builder.append("attribute: ").append(attributeName).append(" = ").append("\"").append(source).append("\"\n");
} else {
builder.append("template: ");
builder.append(source);
builder.append("\n");
}
}
return builder.toString();
}
use of com.dtflys.forest.reflection.ForestMethod in project forest by dromara.
the class TestMappingReference method testParameter.
@Test
public void testParameter() {
ForestMethod forestMethod = Mockito.mock(ForestMethod.class);
MappingVariable nameVar = new MappingVariable("name", String.class);
nameVar.setIndex(0);
MappingVariable ageVar = new MappingVariable("age", String.class);
ageVar.setIndex(1);
Mockito.when(forestMethod.getVariable("name")).thenReturn(nameVar);
Mockito.when(forestMethod.getVariable("age")).thenReturn(ageVar);
Mockito.when(forestMethod.getVariableValue("name", forestMethod)).thenReturn("Marry");
Mockito.when(forestMethod.getVariableValue("motherName", forestMethod)).thenReturn("Linda");
Mockito.when(forestMethod.getVariableValue("age", forestMethod)).thenReturn(12);
Mockito.when(forestMethod.isVariableDefined("motherName")).thenReturn(true);
MappingReference nameRef = new MappingReference(forestMethod, forestMethod, "name");
MappingReference ageRef = new MappingReference(forestMethod, forestMethod, "age");
MappingReference motherNameRef = new MappingReference(forestMethod, forestMethod, "motherName");
Assert.assertEquals("Peter", nameRef.render(new Object[] { "Peter", 15 }));
Mockito.verify(forestMethod).getVariable("name");
Mockito.verify(forestMethod, Mockito.never()).getVariableValue("name");
Assert.assertEquals(15, ageRef.render(new Object[] { "Peter", 15 }));
Mockito.verify(forestMethod, Mockito.never()).getVariableValue("motherName");
Assert.assertEquals("Linda", motherNameRef.render(new Object[] { "Peter", 15 }));
Mockito.verify(forestMethod).getVariableValue("motherName", forestMethod);
}
Aggregations