|
11 | 11 | import io.jooby.MediaType; |
12 | 12 | import io.jooby.Reified; |
13 | 13 | import io.jooby.Route; |
| 14 | +import io.jooby.annotations.Dispatch; |
14 | 15 | import io.jooby.internal.apt.asm.ArrayWriter; |
15 | 16 | import io.jooby.internal.apt.asm.ConstructorWriter; |
16 | 17 | import io.jooby.internal.apt.asm.RouteAttributesWriter; |
|
20 | 21 | import org.objectweb.asm.Type; |
21 | 22 |
|
22 | 23 | import javax.annotation.processing.ProcessingEnvironment; |
| 24 | +import javax.lang.model.element.AnnotationMirror; |
| 25 | +import javax.lang.model.element.AnnotationValue; |
| 26 | +import javax.lang.model.element.ExecutableElement; |
23 | 27 | import java.lang.reflect.Method; |
24 | 28 | import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Optional; |
25 | 31 |
|
26 | 32 | import static org.objectweb.asm.Opcodes.ACC_PUBLIC; |
27 | 33 | import static org.objectweb.asm.Opcodes.ACC_SUPER; |
@@ -122,15 +128,65 @@ private void install(ClassWriter writer, List<HandlerCompiler> handlers) throws |
122 | 128 | setContentType(visitor, "setProduces", handler.getProduces()); |
123 | 129 |
|
124 | 130 | /** |
| 131 | + * ****************************************************************************************** |
125 | 132 | * Annotations as route attributes |
| 133 | + * ****************************************************************************************** |
126 | 134 | */ |
127 | 135 | routeAttributes.process(handler.getExecutable()); |
| 136 | + |
| 137 | + /** |
| 138 | + * ****************************************************************************************** |
| 139 | + * Dispatch |
| 140 | + * ****************************************************************************************** |
| 141 | + */ |
| 142 | + setDispatch(visitor, handler.getExecutable()); |
128 | 143 | } |
129 | 144 | visitor.visitInsn(RETURN); |
130 | 145 | visitor.visitMaxs(0, 0); |
131 | 146 | visitor.visitEnd(); |
132 | 147 | } |
133 | 148 |
|
| 149 | + private void setDispatch(MethodVisitor visitor, ExecutableElement executable) |
| 150 | + throws NoSuchMethodException { |
| 151 | + String executorKey = findAnnotation(executable.getAnnotationMirrors(), Dispatch.class.getName()) |
| 152 | + .map(it -> annotationAttribute(it, "value").toString()) |
| 153 | + .orElseGet(() -> |
| 154 | + findAnnotation(executable.getEnclosingElement().getAnnotationMirrors(), |
| 155 | + Dispatch.class.getName()) |
| 156 | + .map(it -> annotationAttribute(it, "value").toString()) |
| 157 | + .orElse(null) |
| 158 | + ); |
| 159 | + |
| 160 | + if (executorKey != null) { |
| 161 | + Method setExecutor = Route.class.getDeclaredMethod("setExecutorKey", String.class); |
| 162 | + visitor.visitVarInsn(ALOAD, 2); |
| 163 | + visitor.visitLdcInsn(executorKey); |
| 164 | + visitor.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(setExecutor.getDeclaringClass()), |
| 165 | + setExecutor.getName(), |
| 166 | + Type.getMethodDescriptor(setExecutor), false); |
| 167 | + visitor.visitInsn(POP); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + private Object annotationAttribute(AnnotationMirror annotationMirror, String method) { |
| 172 | + Map<? extends ExecutableElement, ? extends AnnotationValue> map = env |
| 173 | + .getElementUtils().getElementValuesWithDefaults(annotationMirror); |
| 174 | + for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : map.entrySet()) { |
| 175 | + if (entry.getKey().getSimpleName().toString().equals(method)) { |
| 176 | + return entry.getValue().getValue(); |
| 177 | + } |
| 178 | + } |
| 179 | + throw new IllegalArgumentException( |
| 180 | + "Missing: " + annotationMirror.getAnnotationType().toString() + "." + method); |
| 181 | + } |
| 182 | + |
| 183 | + private Optional<? extends AnnotationMirror> findAnnotation( |
| 184 | + List<? extends AnnotationMirror> annotationMirrors, String name) { |
| 185 | + return annotationMirrors.stream() |
| 186 | + .filter(it -> it.getAnnotationType().toString().equals(name)) |
| 187 | + .findFirst(); |
| 188 | + } |
| 189 | + |
134 | 190 | private void setReturnType(MethodVisitor visitor, HandlerCompiler handler) |
135 | 191 | throws NoSuchMethodException { |
136 | 192 | TypeDefinition returnType = handler.getReturnType(); |
|
0 commit comments