Skip to content

Commit

Permalink
compiler: add delegated methods to proxy
Browse files Browse the repository at this point in the history
Add delegated methods addEventHandler and setCallHandler
from Connection to class generated with @Proxy to avoid
unneded interactions with the Connection intelf.

Closes: #21
  • Loading branch information
lundibundi committed May 27, 2017
1 parent f1a3235 commit 74abf59
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
Expand All @@ -27,6 +30,7 @@
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic.Kind;

/**
* Created by lundibundi on 5/25/17.
Expand All @@ -53,6 +57,7 @@ public class ProxyAnnotatedInterface {
private static final String SINGLETON_INSTANCE_NAME = "instance";

private TypeElement annotatedInterface;
private final Messager messager;
private String remoteInterfaceName;
private boolean singletonClass;

Expand All @@ -61,7 +66,10 @@ public class ProxyAnnotatedInterface {
private TypeName interfaceClassName;
private TypeName interfaceTypeName;

public ProxyAnnotatedInterface(TypeElement typeElement, Elements elements, Types types) {
public ProxyAnnotatedInterface(TypeElement typeElement,
Elements elements, Types types,
Messager messager) {
this.messager = messager;
annotatedInterface = typeElement;
typeUtils = new TypeUtils(types, elements);

Expand Down Expand Up @@ -126,6 +134,14 @@ public void generateCode(Filer filer) throws
}
}

addDelegateMethod(CONNECTION_FIELD_NAME, Connection.class, "setCallHandler",
Arrays.asList("interfaceName", "methodName", "handler"),
String.class, String.class, ManualHandler.class);

addDelegateMethod(CONNECTION_FIELD_NAME, Connection.class, "addEventHandler",
Arrays.asList("interfaceName", "eventName", "handler"),
String.class, String.class, ManualHandler.class);

// save to file
JavaFile javaFile = JavaFile.builder(
typeUtils.getElements()
Expand All @@ -138,6 +154,32 @@ public void generateCode(Filer filer) throws
javaFile.writeTo(filer);
}

private void addDelegateMethod(String caller, Class<?> clazz, String name,
List<String> parameterNames, Class<?>... parameterTypes) {
try {
final Method method = clazz.getDeclaredMethod(name, parameterTypes);
final MethodSpec methodDelegate = createDelegateMethod(caller, method, parameterNames);
classBuilder.addMethod(methodDelegate);
} catch (NoSuchMethodException e) {
messager.printMessage(Kind.WARNING,
"Cannot get " + name + " method from " + clazz.getCanonicalName()
+ ". Skipping delegate creation");
}
}

private MethodSpec createDelegateMethod(String caller, Method method, List<String> paramNames) {
MethodSpec.Builder delegateBuilder = MethodSpec.methodBuilder(method.getName())
.addModifiers(Modifier.PUBLIC)
.returns(method.getReturnType());
final Class<?>[] parameterTypes = method.getParameterTypes();
for (int i = 0; i < parameterTypes.length; i++) {
delegateBuilder.addParameter(parameterTypes[i], paramNames.get(i));
}
final CodeBlock originCall = composeMethodCall(caller, method.getName(), paramNames);
delegateBuilder.addStatement("$1L", originCall);
return delegateBuilder.build();
}

private MethodSpec createActionHandler(ExecutableElement method) {
if (method.getAnnotation(Call.class) != null) {
String[] names = getInterfaceAndMethod(method, remoteInterfaceName, Call.class, "value");
Expand Down Expand Up @@ -206,7 +248,7 @@ private CodeBlock composeParamMethodCall(String name, String methodName,
}

private CodeBlock composeMethodCall(String name, String methodName,
List<String> parameters) {
Collection<String> parameters) {
StringBuilder builder = new StringBuilder();
int i = 0;
for (String param : parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected void handleAnnotation(Element annotatedElement, Class<?> annotation)

TypeElement typeElement = (TypeElement) annotatedElement;
ProxyAnnotatedInterface jstpReceiver = new ProxyAnnotatedInterface(
typeElement, elementUtils, typeUtils);
typeElement, elementUtils, typeUtils, messager);

jstpReceiver.generateCode(filer);
}
Expand Down

0 comments on commit 74abf59

Please sign in to comment.