Skip to content

Commit ae799df

Browse files
committed
APT: add flash + form + header param
1 parent 652efdd commit ae799df

14 files changed

Lines changed: 217 additions & 68 deletions

File tree

‎jooby/src/main/java/io/jooby/Context.java‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public interface Context extends Registry {
9898
*
9999
* @return Flash map.
100100
*/
101-
@Nonnull FlashMap flashMap();
101+
@Nonnull FlashMap flash();
102102

103103
/**
104104
* Get a flash attribute.
@@ -108,15 +108,6 @@ public interface Context extends Registry {
108108
*/
109109
@Nonnull Value flash(@Nonnull String name);
110110

111-
/**
112-
* Set a flash attribute.
113-
*
114-
* @param name Attribute's name.
115-
* @param value Attribute's value.
116-
* @return This context.
117-
*/
118-
@Nonnull Context flash(@Nonnull String name, @Nonnull String value);
119-
120111
/**
121112
* Find a session or creates a new session.
122113
*
@@ -334,7 +325,7 @@ public interface Context extends Registry {
334325
*
335326
* @return Request headers as {@link Value}.
336327
*/
337-
@Nonnull Value headers();
328+
@Nonnull Value header();
338329

339330
/**
340331
* Get a header that matches the given name.

‎jooby/src/main/java/io/jooby/DefaultContext.java‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public interface DefaultContext extends Context {
7070
return this;
7171
}
7272

73-
@Override default @Nonnull FlashMap flashMap() {
73+
@Override default @Nonnull FlashMap flash() {
7474
return (FlashMap) getAttributes()
7575
.computeIfAbsent(FlashMap.NAME, key -> FlashMap
7676
.create(this, new Cookie(getRouter().getFlashCookie()).setHttpOnly(true)));
@@ -83,12 +83,7 @@ public interface DefaultContext extends Context {
8383
* @return Flash attribute.
8484
*/
8585
@Override default @Nonnull Value flash(@Nonnull String name) {
86-
return Value.create(name, flashMap().get(name));
87-
}
88-
89-
@Override default @Nonnull Context flash(@Nonnull String name, @Nonnull String value) {
90-
flashMap().put(name, value);
91-
return this;
86+
return Value.create(name, flash().get(name));
9287
}
9388

9489
@Override default @Nonnull Session session() {
@@ -177,15 +172,15 @@ public interface DefaultContext extends Context {
177172
}
178173

179174
@Override @Nonnull default Value header(@Nonnull String name) {
180-
return headers().get(name);
175+
return header().get(name);
181176
}
182177

183178
@Override @Nonnull default Map<String, String> headerMap() {
184-
return headers().toMap();
179+
return header().toMap();
185180
}
186181

187182
@Override @Nonnull default Map<String, List<String>> headerMultimap() {
188-
return headers().toMultimap();
183+
return header().toMultimap();
189184
}
190185

191186
@Override default boolean accept(@Nonnull MediaType contentType) {

‎jooby/src/main/java/io/jooby/ForwardingContext.java‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,14 @@ public ForwardingContext(@Nonnull Context context) {
5858
return context.getRouter();
5959
}
6060

61-
@Nonnull @Override public FlashMap flashMap() {
62-
return context.flashMap();
61+
@Nonnull @Override public FlashMap flash() {
62+
return context.flash();
6363
}
6464

6565
@Nonnull @Override public Value flash(@Nonnull String name) {
6666
return context.flash(name);
6767
}
6868

69-
@Nonnull @Override public Context flash(@Nonnull String name, @Nonnull String value) {
70-
return context.flash(name, value);
71-
}
72-
7369
@Nonnull @Override public Session session() {
7470
return context.session();
7571
}
@@ -155,8 +151,8 @@ public ForwardingContext(@Nonnull Context context) {
155151
return context.queryMultimap();
156152
}
157153

158-
@Override @Nonnull public Value headers() {
159-
return context.headers();
154+
@Override @Nonnull public Value header() {
155+
return context.header();
160156
}
161157

162158
@Nonnull @Override public Value header(@Nonnull String name) {

‎jooby/src/main/java/io/jooby/internal/ValueInjector.java‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.math.BigInteger;
3030
import java.nio.charset.Charset;
3131
import java.nio.file.Path;
32+
import java.time.Instant;
3233
import java.util.ArrayList;
3334
import java.util.Collection;
3435
import java.util.Collections;
@@ -105,9 +106,7 @@ private Constructor selectConstructor(Constructor[] constructors) {
105106
return result;
106107
}
107108

108-
private <T> T setters(T newInstance, Value object, Set<Value> skip)
109-
throws IllegalAccessException, InstantiationException, InvocationTargetException,
110-
NoSuchMethodException {
109+
private <T> T setters(T newInstance, Value object, Set<Value> skip) {
111110
Method[] methods = newInstance.getClass().getMethods();
112111
for (Value value : object) {
113112
if (!skip.contains(value)) {
@@ -123,6 +122,8 @@ private <T> T setters(T newInstance, Value object, Set<Value> skip)
123122
Object arg = value(value, parameter.getType(),
124123
parameter.getParameterizedType());
125124
method.invoke(newInstance, arg);
125+
} catch (InvocationTargetException x) {
126+
throw new ProvisioningException(parameter, x.getCause());
126127
} catch (Exception x) {
127128
throw new ProvisioningException(parameter, x);
128129
}
@@ -227,6 +228,9 @@ public static boolean isSimple(Class rawType, Type type) {
227228
if (UUID.class == rawType) {
228229
return true;
229230
}
231+
if (Instant.class == rawType) {
232+
return true;
233+
}
230234
if (BigDecimal.class == rawType) {
231235
return true;
232236
}
@@ -301,6 +305,9 @@ private Object value(Value value, Class rawType, Type type)
301305
if (UUID.class == rawType) {
302306
return UUID.fromString(value.get(0).value());
303307
}
308+
if (Instant.class == rawType) {
309+
return Instant.ofEpochMilli(value.get(0).longValue());
310+
}
304311
if (BigDecimal.class == rawType) {
305312
return new BigDecimal(value.get(0).value());
306313
}

‎modules/jooby-jetty/src/main/java/io/jooby/internal/jetty/JettyContext.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public JettyContext(Request request, Router router, int bufferSize, long maxRequ
200200
return multipart;
201201
}
202202

203-
@Nonnull @Override public Value headers() {
203+
@Nonnull @Override public Value header() {
204204
if (headers == null) {
205205
Enumeration<String> names = request.getHeaderNames();
206206
Map<String, Collection<String>> headerMap = new LinkedHashMap<>();

‎modules/jooby-mvc-compiler/src/main/java/io/jooby/compiler/Annotations.java‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,25 @@ public interface Annotations {
2222

2323
String PATH_PARAM = "io.jooby.annotations.PathParam";
2424

25+
String QUERY_PARAM = "io.jooby.annotations.QueryParam";
26+
27+
String COOKIE_PARAM = "io.jooby.annotations.CookieParam";
28+
29+
String HEADER_PARAM = "io.jooby.annotations.HeaderParam";
30+
31+
String FLASH_PARAM = "io.jooby.annotations.FlashParam";
32+
33+
String FORM_PARAM = "io.jooby.annotations.FormParam";
34+
2535
Set<String> PATH_PARAMS = unmodifiableSet(new LinkedHashSet<>(asList(PATH_PARAM)));
2636

27-
static boolean isPathParam(String type) {
28-
return PATH_PARAMS.contains(type);
29-
}
37+
Set<String> QUERY_PARAMS = unmodifiableSet(new LinkedHashSet<>(asList(QUERY_PARAM)));
38+
39+
Set<String> COOKIE_PARAMS = unmodifiableSet(new LinkedHashSet<>(asList(COOKIE_PARAM)));
40+
41+
Set<String> HEADER_PARAMS = unmodifiableSet(new LinkedHashSet<>(asList(HEADER_PARAM)));
42+
43+
Set<String> FLASH_PARAMS = unmodifiableSet(new LinkedHashSet<>(asList(FLASH_PARAM)));
44+
45+
Set<String> FORM_PARAMS = unmodifiableSet(new LinkedHashSet<>(asList(FORM_PARAM)));
3046
}

‎modules/jooby-mvc-compiler/src/main/java/io/jooby/compiler/MvcHandlerCompiler.java‎

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package io.jooby.compiler;
22

3+
import com.squareup.javapoet.JavaFile;
4+
import com.squareup.javapoet.TypeSpec;
35
import io.jooby.Context;
46
import io.jooby.FlashMap;
57
import io.jooby.Formdata;
68
import io.jooby.Multipart;
9+
import io.jooby.ProvisioningException;
710
import io.jooby.QueryString;
811
import io.jooby.Reified;
912
import io.jooby.Route;
@@ -34,6 +37,7 @@
3437
import java.io.PrintWriter;
3538
import java.lang.reflect.Method;
3639
import java.util.Collections;
40+
import java.util.HashMap;
3741
import java.util.List;
3842
import java.util.Map;
3943
import java.util.Optional;
@@ -117,6 +121,17 @@ public TypeMirror arg0(TypeMirror mirror) {
117121
}
118122
}
119123

124+
private static final Map<String, Set<String>> PARAMS = new HashMap<>();
125+
126+
static {
127+
PARAMS.put("path", Annotations.PATH_PARAMS);
128+
PARAMS.put("query", Annotations.QUERY_PARAMS);
129+
PARAMS.put("cookie", Annotations.COOKIE_PARAMS);
130+
PARAMS.put("header", Annotations.HEADER_PARAMS);
131+
PARAMS.put("flash", Annotations.FLASH_PARAMS);
132+
PARAMS.put("multipart", Annotations.FORM_PARAMS);
133+
}
134+
120135
private static final Type OBJ = getType(Object.class);
121136
private static final Type HANDLER = getType(Route.Handler.class);
122137

@@ -132,8 +147,11 @@ public TypeMirror arg0(TypeMirror mirror) {
132147
private final TypeElement owner;
133148
private final ExecutableElement executable;
134149
private final ProcessingEnvironment environment;
150+
private final String httpMethod;
135151

136-
public MvcHandlerCompiler(ProcessingEnvironment environment, ExecutableElement executable) {
152+
public MvcHandlerCompiler(ProcessingEnvironment environment, String httpMethod,
153+
ExecutableElement executable) {
154+
this.httpMethod = httpMethod.toLowerCase();
137155
this.environment = environment;
138156
this.executable = executable;
139157
this.owner = (TypeElement) executable.getEnclosingElement();
@@ -203,14 +221,16 @@ private void apply(ClassWriter writer) throws Exception {
203221
apply.visitMethodInsn(INVOKESTATIC, "java/util/Optional", "ofNullable",
204222
"(Ljava/lang/Object;)Ljava/util/Optional;", false);
205223
}
206-
} else if (isPathParam(parameter)) {
207-
String parameterName = parameterName(parameter, Annotations.PATH_PARAMS);
224+
} else {
225+
Map.Entry<String, Set<String>> strategy = paramStrategy(parameter);
226+
227+
String parameterName = parameterName(parameter, strategy.getValue());
208228

209229
Method paramValue = paramValue(parameter, paramType);
210230
boolean dynamic =
211231
paramValue.getName().equals("to") && !isSimpleType(paramType.arg0(parameter.asType()));
212232
if (dynamic) {
213-
Method pathParam = Context.class.getDeclaredMethod("path");
233+
Method pathParam = Context.class.getDeclaredMethod(strategy.getKey());
214234
apply.visitMethodInsn(INVOKEINTERFACE, CTX.getInternalName(), pathParam.getName(),
215235
getMethodDescriptor(pathParam), true);
216236
apply.visitLdcInsn(asmType(rawType));
@@ -228,7 +248,7 @@ private void apply(ClassWriter writer) throws Exception {
228248
} else {
229249
apply.visitLdcInsn(parameterName);
230250

231-
Method pathParam = Context.class.getDeclaredMethod("path", String.class);
251+
Method pathParam = Context.class.getDeclaredMethod(strategy.getKey(), String.class);
232252
apply.visitMethodInsn(INVOKEINTERFACE, CTX.getInternalName(), pathParam.getName(),
233253
getMethodDescriptor(pathParam), true);
234254
// to(Class)
@@ -259,6 +279,15 @@ private void apply(ClassWriter writer) throws Exception {
259279
apply.visitEnd();
260280
}
261281

282+
private Map.Entry<String, Set<String>> paramStrategy(VariableElement parameter) {
283+
return PARAMS.entrySet().stream()
284+
.filter(it -> isParam(parameter, it.getValue()))
285+
.findFirst()
286+
.orElseThrow(() -> new ProvisioningException(
287+
"Unable to provision parameter: '" + parameter + ": " + parameter.asType() + "'",
288+
null));
289+
}
290+
262291
private ParamType paramType(VariableElement parameter) {
263292
TypeMirror typeMirror = parameter.asType();
264293
if (isSimpleType(typeMirror)) {
@@ -337,10 +366,10 @@ private Method paramValue(VariableElement param, ParamType paramType)
337366
}
338367

339368
private boolean isSimpleType(TypeMirror mirror) {
340-
return eq(String.class, mirror) || mirror.getKind().isPrimitive() || isPrimitiveWrapper(mirror);
369+
return eq(String.class, mirror) || mirror.getKind().isPrimitive() || isBasic(mirror);
341370
}
342371

343-
private boolean isPrimitiveWrapper(TypeMirror mirror) {
372+
private boolean isBasic(TypeMirror mirror) {
344373
switch (rawType(mirror)) {
345374
case "java.lang.Byte":
346375
case "java.lang.Character":
@@ -349,6 +378,7 @@ private boolean isPrimitiveWrapper(TypeMirror mirror) {
349378
case "java.lang.Long":
350379
case "java.lang.Float":
351380
case "java.lang.Double":
381+
case "java.time.Instant":
352382
return true;
353383
default:
354384
return false;
@@ -410,12 +440,8 @@ private String cleanString(String value) {
410440
return value;
411441
}
412442

413-
private boolean isPathParam(VariableElement parameter) {
414-
return isPathParam(parameter.getAnnotationMirrors());
415-
}
416-
417-
private boolean isPathParam(List<? extends AnnotationMirror> annotationMirrors) {
418-
return annotations(annotationMirrors, Annotations.PATH_PARAMS).size() > 0;
443+
private boolean isParam(VariableElement parameter, Set<String> annotations) {
444+
return annotations(parameter.getAnnotationMirrors(), annotations).size() > 0;
419445
}
420446

421447
private List<AnnotationMirror> annotations(List<? extends AnnotationMirror> annotationMirrors,
@@ -439,10 +465,10 @@ private Method typeInjection(String rawType, ParamType paramType) throws Excepti
439465
return Context.class.getDeclaredMethod("multipart");
440466
}
441467
if (FlashMap.class.getName().equals(rawType)) {
442-
return Context.class.getDeclaredMethod("flashMap");
468+
return Context.class.getDeclaredMethod("flash");
443469
}
444470
if (Session.class.getName().equals(rawType)) {
445-
if (paramType == ParamType.OPTIONAL) {
471+
if (paramType.isOptional()) {
446472
return Context.class.getDeclaredMethod("sessionOrNull");
447473
} else {
448474
return Context.class.getDeclaredMethod("session");
@@ -507,7 +533,7 @@ public String getSourceFileName() {
507533
}
508534

509535
public String getHandlerName() {
510-
return getOwner() + "$" + executable.getSimpleName();
536+
return getOwner() + "$" + httpMethod.toUpperCase() + "$" + executable.getSimpleName();
511537
}
512538

513539
public String getHandlerInternal() {

‎modules/jooby-mvc-compiler/src/main/java/io/jooby/compiler/MvcProcessor.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public boolean process(Set<? extends TypeElement> annotations,
5858
Set<? extends Element> methods = roundEnvironment.getElementsAnnotatedWith(httpMethod);
5959
for (Element e : methods) {
6060
ExecutableElement method = (ExecutableElement) e;
61-
MvcHandlerCompiler compiler = new MvcHandlerCompiler(processingEnvironment, method);
61+
MvcHandlerCompiler compiler = new MvcHandlerCompiler(processingEnvironment, httpMethod.getSimpleName().toString(), method);
6262
String key = compiler.getKey();
6363
result.put(key, compiler);
6464
}

0 commit comments

Comments
 (0)