Browse Source

support annotation @Json @ArrayJson at parameter with service method in
aptWeb

jiapeng 8 years ago
parent
commit
d16ef1af86

+ 13 - 0
jfwAptWeb/src/main/java/org/jfw/apt/web/annotation/param/ArrayJson.java

@ -0,0 +1,13 @@
1
package org.jfw.apt.web.annotation.param;
2

3
import java.lang.annotation.ElementType;
4
import java.lang.annotation.Retention;
5
import java.lang.annotation.RetentionPolicy;
6
import java.lang.annotation.Target;
7

8
@Target(ElementType.PARAMETER)
9
@Retention(RetentionPolicy.SOURCE)
10
public @interface ArrayJson {
11
	String value() default "";
12
	String targetTypeName() default "";
13
}

+ 13 - 0
jfwAptWeb/src/main/java/org/jfw/apt/web/annotation/param/Json.java

@ -0,0 +1,13 @@
1
package org.jfw.apt.web.annotation.param;
2

3
import java.lang.annotation.ElementType;
4
import java.lang.annotation.Retention;
5
import java.lang.annotation.RetentionPolicy;
6
import java.lang.annotation.Target;
7

8
@Target(ElementType.PARAMETER)
9
@Retention(RetentionPolicy.SOURCE)
10
public @interface Json {
11
	String value() default "";
12
	String targetTypeName() default "";
13
}

+ 3 - 1
jfwAptWeb/src/main/java/org/jfw/apt/web/handlers/BuildParamHandler.java

@ -6,9 +6,11 @@ import org.jfw.apt.exception.AptException;
6 6
import org.jfw.apt.model.MethodParamEntry;
7 7
import org.jfw.apt.out.ClassWriter;
8 8
import org.jfw.apt.web.RequestHandler;
9
import org.jfw.apt.web.handlers.buildParam.ArrayJsonHandler;
9 10
import org.jfw.apt.web.handlers.buildParam.BuildParameter;
10 11
import org.jfw.apt.web.handlers.buildParam.CookieHandler;
11 12
import org.jfw.apt.web.handlers.buildParam.DefineHandler;
13
import org.jfw.apt.web.handlers.buildParam.JsonHandler;
12 14
import org.jfw.apt.web.handlers.buildParam.NopHandler;
13 15
import org.jfw.apt.web.handlers.buildParam.PVarHandler;
14 16
import org.jfw.apt.web.handlers.buildParam.ParameterMapHandler;
@ -23,7 +25,7 @@ public class BuildParamHandler extends RequestHandler {
23 25
24 26
	private static final BuildParameter[] hanlders = new BuildParameter[] { DefineHandler.INS, NopHandler.INS,
25 27
			ParameterMapHandler.INS, PathVarHandler.INS,PVarHandler.INS ,RequestBodyHandler.INS,RequestHeaderHandler.INS,
26
			RequestParamHandler.INS,SessionValHandler.INS,UploadHandler.INS,CookieHandler.INS};
28
			RequestParamHandler.INS,SessionValHandler.INS,UploadHandler.INS,CookieHandler.INS,JsonHandler.INS,ArrayJsonHandler.INS};
27 29
28 30
	@Override
29 31
	public void init() {

+ 68 - 0
jfwAptWeb/src/main/java/org/jfw/apt/web/handlers/buildParam/ArrayJsonHandler.java

@ -0,0 +1,68 @@
1
package org.jfw.apt.web.handlers.buildParam;
2

3
import javax.lang.model.element.VariableElement;
4

5
import org.jfw.apt.Util;
6
import org.jfw.apt.annotation.Nullable;
7
import org.jfw.apt.exception.AptException;
8
import org.jfw.apt.model.MethodParamEntry;
9
import org.jfw.apt.out.ClassWriter;
10
import org.jfw.apt.web.AptWebHandler;
11
import org.jfw.apt.web.annotation.param.ArrayJson;
12

13
public class ArrayJsonHandler implements BuildParameter {
14
	public static final ArrayJsonHandler INS = new ArrayJsonHandler();
15

16
	private ArrayJson arrayJson;
17

18
	private ArrayJsonHandler() {
19
	}
20

21
	@Override
22
	public void build(ClassWriter cw, MethodParamEntry mpe, AptWebHandler aptWebHandler) throws AptException {
23
		String mpn = mpe.getTypeName();
24
		if (!mpn.endsWith("[]"))
25
			throw new AptException(mpe.getRef(), "@ArrayJson must with on Array");
26
		mpn = mpn.substring(0, mpn.length() - 2);
27
		if (Util.isPrimitive(mpn))
28
			throw new AptException(mpe.getRef(), "@ArrayJson not with on primitive array");
29

30
		String tn = Util.emptyToNull(arrayJson.targetTypeName());
31
		if (tn == null) {
32
			tn = mpn;
33
		}
34
		boolean parameterized = tn.indexOf("<") >= 0;
35
		String vn = Util.emptyToNull(arrayJson.value());
36
		if (vn == null) {
37
			vn = mpe.getName();
38
		}
39
		boolean nullable = mpe.getRef().getAnnotation(Nullable.class) != null;
40
		aptWebHandler.readParameters(vn);
41
		cw.bL(mpn).w("[] ").w(mpe.getName()).el(" =  null;");
42
		cw.l("if(null!=params && params.length>0){");
43
		if (parameterized) {
44
			cw.bL(mpe.getName()).w(" =(").w(tn).el("[]) new Object[params.length];");
45
		} else {
46
			cw.bL(mpe.getName()).w(" = new ").w(tn).el("[params.length];");
47
		}
48
		cw.l("for(int i = 0 ; i < params.length; ++i){");
49
		cw.l("if(null!= params[i] && params[i].length()>0){");
50
		cw.bL(mpe.getName()).w("[i] = ");
51
		if (!parameterized) {
52
			cw.w("org.jfw.util.json.JsonService.fromJson(params[i],").w(tn).el(".class);");
53
		} else {
54
			cw.w("org.jfw.util.json.JsonService.<").w(tn).w(">fromJson(params[i],(new org.jfw.util.reflect.TypeReference<").w(tn).el(">(){}).getType());");
55
		}
56
		cw.l("}").l("}");
57
		if (!nullable) {
58
			cw.l("}else{");
59
			cw.l("throw new IllegalArgumentException(\"invalid parameter:" + vn + "\");");
60
		}
61
		cw.l("}");
62
	}
63

64
	public boolean match(VariableElement ele) {
65
		this.arrayJson = ele.getAnnotation(ArrayJson.class);
66
		return null != this.arrayJson;
67
	}
68
}

+ 54 - 0
jfwAptWeb/src/main/java/org/jfw/apt/web/handlers/buildParam/JsonHandler.java

@ -0,0 +1,54 @@
1
package org.jfw.apt.web.handlers.buildParam;
2

3
import javax.lang.model.element.VariableElement;
4

5
import org.jfw.apt.Util;
6
import org.jfw.apt.annotation.Nullable;
7
import org.jfw.apt.exception.AptException;
8
import org.jfw.apt.model.MethodParamEntry;
9
import org.jfw.apt.out.ClassWriter;
10
import org.jfw.apt.web.AptWebHandler;
11
import org.jfw.apt.web.annotation.param.Json;
12

13
public class JsonHandler  implements BuildParameter{
14
	public static final JsonHandler INS = new JsonHandler();
15
	
16
	private Json json;
17
	private JsonHandler(){}
18
	@Override
19
	public void build(ClassWriter cw, MethodParamEntry mpe, AptWebHandler aptWebHandler) throws AptException {
20
		if (Util.isPrimitive(mpe.getTypeName()))
21
			throw new AptException(mpe.getRef(), "@Json not with on primitive");
22
		
23
		String tn = Util.emptyToNull(json.targetTypeName());
24
		if (tn == null) {
25
			tn = mpe.getTypeName();
26
		}
27
		boolean parameterized = tn.indexOf("<") >= 0;
28
		String vn = Util.emptyToNull(json.value());
29
		if(vn==null){
30
			vn = mpe.getName();
31
		}
32
		boolean nullable = mpe.getRef().getAnnotation(Nullable.class)!=null;
33
		cw.bL(mpe.getTypeName()).w(" ").w(mpe.getName()).el(" =  null;");
34
		aptWebHandler.readParameter(vn);
35
		cw.l("if(null!=param && param.length()>0){");
36
		cw.bL(mpe.getName()).w(" = ");
37
		if (!parameterized) {
38
			cw.w("org.jfw.util.json.JsonService.fromJson(param,").w(tn).el(".class);");
39
			
40
		} else {
41
			cw.w("org.jfw.util.json.JsonService.<").w(tn).w(">fromJson(param,(new org.jfw.util.reflect.TypeReference<").w(tn)
42
					.el(">(){}).getType());");
43
		}
44
		if(!nullable){
45
			cw.l("}else{");
46
			cw.l("throw new IllegalArgumentException(\"invalid parameter:" + vn + "\");");
47
		}
48
		cw.l("}");
49
	}
50
	public boolean match(VariableElement ele){
51
		this.json = ele.getAnnotation(Json.class);
52
		return null != this.json;
53
	}
54
}