- Timestamp:
- 06/28/13 11:56:03 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/params/FramsClassBuilder.java
r86 r87 7 7 import java.lang.reflect.ParameterizedType; 8 8 import java.lang.reflect.Type; 9 import java.util.ArrayList; 10 import java.util.Collections; 11 import java.util.HashMap; 12 import java.util.LinkedList; 9 13 import java.util.List; 10 14 import java.util.Map; … … 12 16 import org.apache.log4j.Logger; 13 17 18 import com.framsticks.params.annotations.AutoAppendAnnotation; 14 19 import com.framsticks.params.annotations.FramsClassAnnotation; 15 20 import com.framsticks.params.annotations.ParamAnnotation; … … 18 23 import com.framsticks.util.lang.Strings; 19 24 25 @FramsClassAnnotation(id = "class", name = "class") 20 26 public class FramsClassBuilder { 21 27 private static final Logger log = 22 28 Logger.getLogger(FramsClassBuilder.class); 23 24 29 25 30 public static String getName(FramsClassAnnotation fca, Class<?> javaClass) { … … 63 68 } 64 69 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 70 if (type instanceof Class) { 71 72 Class<?> cl = (Class<?>) type; 73 74 // TODO: future support for enum 75 // if (cl.isEnum()) { 76 // Class<? extends Enum<?>> enumType = (Class<? extends Enum<?>>) cl; 77 // Enum<?>[] enums = enumType.getEnumConstants(); 78 // StringBuilder b = new StringBuilder(); 79 80 // b.append("d 0 ").append(enums.length - 1).append(" 0 "); 81 // for (Enum<?> e : enums) { 82 // b.append("~").append(e.name()); 83 // } 84 // return b.toString(); 85 // } 86 if (cl.equals(Integer.class) || cl.equals(int.class)) { 87 return "d"; 88 } 89 if (cl.equals(String.class)) { 90 return "s"; 91 } 92 if (cl.equals(Double.class) || cl.equals(double.class)) { 93 return "f"; 94 } 95 if (cl.equals(Boolean.class) || cl.equals(boolean.class)) { 96 return "d 0 1"; 97 } 98 if (cl.equals(Object.class)) { 99 return "x"; 100 } 101 81 102 return "o " + ((Class<?>) type).getCanonicalName(); 82 103 } 83 return null; 104 105 throw new ConstructionException().msg("failed to find framsticks for native type").arg("type", type); 84 106 } 85 107 … … 141 163 } 142 164 143 public static FramsClass buildForClass(Class<?> javaClass) throws ConstructionException { 165 public static Map<Class<?>, FramsClass> synchronizedCacheForBasedOnForJavaClass = Collections.synchronizedMap(new HashMap<Class<?>, FramsClass>()); 166 167 public FramsClass forClass(Class<?> javaClass) throws ConstructionException { 168 FramsClass result = synchronizedCacheForBasedOnForJavaClass.get(javaClass); 169 if (result != null) { 170 return result; 171 } 172 173 log.info("building for class " + javaClass); 144 174 145 175 FramsClassAnnotation fca = javaClass.getAnnotation(FramsClassAnnotation.class); 146 176 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), "");177 throw new ConstructionException().msg("java class is not annotated with FramsClassAnnotation").arg("java", javaClass); 178 } 179 180 id(getId(fca, javaClass)); 181 name(getName(fca, javaClass)); 152 182 153 183 Map<String, ParamCandidate> candidates = ParamCandidate.getAllCandidates(javaClass); 154 184 155 185 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 } 186 param(Param.build().id(pc.getId()).name(pc.getName()).type(getParamTypeForNativeType(pc.getType())).flags(pc.getFlags())); 187 } 188 189 result = finish(); 190 191 synchronizedCacheForBasedOnForJavaClass.put(javaClass, result); 192 193 return result; 194 } 195 196 197 protected String id; 198 199 protected String name; 200 201 protected String description; 202 203 protected final List<Param> params = new LinkedList<>(); 204 205 protected List<Group> groups = new ArrayList<Group>(); 206 207 @ParamAnnotation 208 public FramsClassBuilder id(String id) { 209 this.id = id; 210 return this; 211 } 212 213 @ParamAnnotation 214 public FramsClassBuilder name(String name) { 215 this.name = name; 216 return this; 217 } 218 219 public FramsClassBuilder idAndName(String v) { 220 this.id = v; 221 this.name = v; 222 return this; 223 } 224 225 @ParamAnnotation(id = "desc") 226 public FramsClassBuilder description(String description) { 227 this.description = description; 228 return this; 229 } 230 231 public FramsClassBuilder() { 232 } 233 234 public FramsClass finish() { 235 return new FramsClass(id, name, description, params, groups); 236 } 237 238 public FramsClassBuilder append(Param param) { 239 params.add(param); 240 return this; 241 } 242 243 @AutoAppendAnnotation 244 public FramsClassBuilder param(ParamBuilder builder) { 245 return append(builder.finish()); 246 } 247 248 @AutoAppendAnnotation 249 public FramsClassBuilder group(Group group) { 250 groups.add(group); 251 return this; 252 } 253 254 /** 255 * @return the id 256 */ 257 @ParamAnnotation 258 public String getId() { 259 return id; 260 } 261 262 /** 263 * @return the name 264 */ 265 @ParamAnnotation 266 public String getName() { 267 return name; 268 } 269 270 /** 271 * @return the description 272 */ 273 @ParamAnnotation(id = "desc") 274 public String getDescription() { 275 return description; 276 } 277 278 public FramsClassBuilder group(String group) { 279 return group(new Group(group)); 280 } 281 282 public ParamBuilder param(String id) { 283 return new ParamBuilder(this).id(id); 284 } 285 161 286 }
Note: See TracChangeset
for help on using the changeset viewer.