1 | package com.framsticks.params; |
---|
2 | |
---|
3 | import java.io.InputStream; |
---|
4 | import java.lang.reflect.Field; |
---|
5 | import java.lang.reflect.Member; |
---|
6 | import java.lang.reflect.Method; |
---|
7 | import java.lang.reflect.ParameterizedType; |
---|
8 | import java.lang.reflect.Type; |
---|
9 | import java.util.List; |
---|
10 | import java.util.Map; |
---|
11 | |
---|
12 | import org.apache.log4j.Logger; |
---|
13 | |
---|
14 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
15 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
16 | import com.framsticks.parsers.FileSource; |
---|
17 | import com.framsticks.parsers.Loaders; |
---|
18 | import com.framsticks.util.lang.Strings; |
---|
19 | |
---|
20 | public class FramsClassBuilder { |
---|
21 | private static final Logger log = |
---|
22 | Logger.getLogger(FramsClassBuilder.class); |
---|
23 | |
---|
24 | |
---|
25 | public static String getName(FramsClassAnnotation fca, Class<?> javaClass) { |
---|
26 | return fca.name().equals("") ? javaClass.getSimpleName() : fca.name(); |
---|
27 | } |
---|
28 | |
---|
29 | public static String getId(FramsClassAnnotation fca, Class<?> javaClass) { |
---|
30 | return fca.id().equals("") ? javaClass.getSimpleName() : fca.id(); |
---|
31 | } |
---|
32 | |
---|
33 | public static String getParamTypeForNativeType(Type type) { |
---|
34 | if (type instanceof ParameterizedType) { |
---|
35 | ParameterizedType p = (ParameterizedType) type; |
---|
36 | Type rawType = p.getRawType(); |
---|
37 | Type containedType = null; |
---|
38 | //TODO make implementation here |
---|
39 | boolean map = false; |
---|
40 | if (rawType.equals(Map.class)) { |
---|
41 | containedType = p.getActualTypeArguments()[1]; |
---|
42 | map = true; |
---|
43 | } else if (rawType.equals(List.class)) { |
---|
44 | containedType = p.getActualTypeArguments()[0]; |
---|
45 | } |
---|
46 | if (!(containedType instanceof Class)) { |
---|
47 | return null; |
---|
48 | } |
---|
49 | Class<?> containedClass = (Class<?>) containedType; |
---|
50 | StringBuilder b = new StringBuilder(); |
---|
51 | b.append("l "); |
---|
52 | FramsClassAnnotation fca = containedClass.getAnnotation(FramsClassAnnotation.class); |
---|
53 | if (fca == null) { |
---|
54 | log.error("the class is not annotated: " + containedClass); |
---|
55 | return null; |
---|
56 | } |
---|
57 | b.append(getName(fca, containedClass)); |
---|
58 | if (map) { |
---|
59 | b.append(" name"); |
---|
60 | } |
---|
61 | |
---|
62 | return b.toString(); |
---|
63 | } |
---|
64 | |
---|
65 | if (type.equals(Integer.class) || type.equals(int.class)) { |
---|
66 | return "d"; |
---|
67 | } |
---|
68 | if (type.equals(String.class)) { |
---|
69 | return "s"; |
---|
70 | } |
---|
71 | if (type.equals(Double.class) || type.equals(double.class)) { |
---|
72 | return "f"; |
---|
73 | } |
---|
74 | if (type.equals(Boolean.class) || type.equals(boolean.class)) { |
---|
75 | return "d 0 1"; |
---|
76 | } |
---|
77 | if (type.equals(Object.class)) { |
---|
78 | return "x"; |
---|
79 | } |
---|
80 | if (type instanceof Class) { |
---|
81 | return "o " + ((Class<?>) type).getCanonicalName(); |
---|
82 | } |
---|
83 | return null; |
---|
84 | } |
---|
85 | |
---|
86 | public static final String GENERATE_HELP_PREFIX = "automatically generated from: "; |
---|
87 | |
---|
88 | public static FramsClass readFromStream(InputStream stream) { |
---|
89 | return Loaders.loadFramsClass(new FileSource(stream)); |
---|
90 | } |
---|
91 | |
---|
92 | // public static Class<? extends Param> getParamType(@Nonnull Class<?> c) { |
---|
93 | // if (c.equals(Integer.class)) { |
---|
94 | // return DecimalParam.class; |
---|
95 | // } |
---|
96 | // if (c.equals(Double.class)) { |
---|
97 | // return FloatParam.class; |
---|
98 | // } |
---|
99 | // if (c.equals(String.class)) { |
---|
100 | // return StringParam.class; |
---|
101 | // } |
---|
102 | // if (c.equals(Object.class)) { |
---|
103 | // return UniversalParam.class; |
---|
104 | // } |
---|
105 | // return null; |
---|
106 | // } |
---|
107 | |
---|
108 | public static String extractIdOf(Member member) { |
---|
109 | if (member instanceof Field) { |
---|
110 | return member.getName(); |
---|
111 | } |
---|
112 | if (member instanceof Method) { |
---|
113 | Method m = (Method) member; |
---|
114 | String n = m.getName(); |
---|
115 | int argsNum = m.getParameterTypes().length; |
---|
116 | if (argsNum == 0) { |
---|
117 | return n.startsWith("get") ? Strings.uncapitalize(n.substring(3)) : n; |
---|
118 | } |
---|
119 | if (argsNum == 1) { |
---|
120 | return n.startsWith("set") ? Strings.uncapitalize(n.substring(3)) : n; |
---|
121 | } |
---|
122 | log.error("invalid number of arguments"); |
---|
123 | return null; |
---|
124 | } |
---|
125 | log.error("invalid kind of member"); |
---|
126 | return null; |
---|
127 | } |
---|
128 | public static String getName(ParamAnnotation annotation, Member member) { |
---|
129 | return annotation.name().equals("") ? Strings.capitalize(extractIdOf(member)) : annotation.name(); |
---|
130 | } |
---|
131 | |
---|
132 | public static String getId(ParamAnnotation annotation, Member member) { |
---|
133 | return annotation.id().equals("") ? extractIdOf(member) : annotation.id(); |
---|
134 | } |
---|
135 | |
---|
136 | public static ParamBuilder fill(ParamBuilder builder, Member member, ParamAnnotation annotation) { |
---|
137 | return builder |
---|
138 | .id(getId(annotation, member)) |
---|
139 | .name(getName(annotation, member)); |
---|
140 | |
---|
141 | } |
---|
142 | |
---|
143 | public static FramsClass buildForClass(Class<?> javaClass) throws ConstructionException { |
---|
144 | |
---|
145 | FramsClassAnnotation fca = javaClass.getAnnotation(FramsClassAnnotation.class); |
---|
146 | if (fca == null) { |
---|
147 | log.error(javaClass.getName() + " is not annotated with FramsClassAnnotation"); |
---|
148 | return null; |
---|
149 | } |
---|
150 | |
---|
151 | FramsClass framsClass = new FramsClass(getId(fca, javaClass), getName(fca, javaClass), ""); |
---|
152 | |
---|
153 | Map<String, ParamCandidate> candidates = ParamCandidate.getAllCandidates(javaClass); |
---|
154 | |
---|
155 | for (ParamCandidate pc : candidates.values()) { |
---|
156 | framsClass.append(Param.build().id(pc.getId()).name(pc.getName()).type(getParamTypeForNativeType(pc.getType())).flags(pc.getFlags())); |
---|
157 | } |
---|
158 | |
---|
159 | return framsClass; |
---|
160 | } |
---|
161 | } |
---|