- Timestamp:
- 06/26/13 13:27:31 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/params/FramsClass.java
r85 r86 1 1 package com.framsticks.params; 2 2 3 import com.framsticks.params.types.StringParam; 4 import com.framsticks.parsers.FileSource; 5 import com.framsticks.parsers.Loaders; 6 import com.framsticks.util.lang.Casting; 7 8 import org.apache.log4j.Logger; 9 10 import java.io.InputStream; 11 import java.lang.reflect.*; 3 import com.framsticks.params.annotations.FramsClassAnnotation; 4 import com.framsticks.params.annotations.ParamAnnotation; 5 // import com.framsticks.util.FramsticksException; 6 12 7 import java.util.*; 8 9 import javax.annotation.Nonnull; 13 10 14 11 /** … … 24 21 * @author Piotr Sniegowski 25 22 */ 23 @FramsClassAnnotation(id = "class", name = "class") 26 24 public final class FramsClass { 27 28 private final static Logger log = Logger.getLogger(FramsClass.class29 .getName());30 31 /**32 * The Class which represents group.33 */34 25 35 26 /** The offset of the parameter (applied for newly added parameter). */ … … 62 53 63 54 public FramsClass() { 55 64 56 } 65 57 … … 77 69 */ 78 70 public FramsClass append(Param param) { 71 // if (param.hasFlag(Flags.USERHIDDEN)) { 72 // return this; 73 // } 79 74 paramEntryMap.put(param.getId(), param); 80 75 //paramEntryMap.put(param.getInternalId(), param); … … 104 99 } 105 100 101 @ParamAnnotation(id = "desc") 106 102 public String getDescription() { 107 103 return description; … … 130 126 131 127 /** 132 * Gets the group getName.128 * Gets the group name. 133 129 * 134 130 * @param gi 135 131 * the offset of group 136 * @return the group getName132 * @return the group name 137 133 */ 138 134 public String getGroupName(int gi) { … … 142 138 } 143 139 140 @ParamAnnotation 144 141 public String getId() { 145 142 return id; 146 143 } 147 144 145 @ParamAnnotation 148 146 public String getName() { 149 147 return name; … … 152 150 public String getNiceName() { 153 151 return name != null ? name : id; 152 } 153 154 public <T extends Param> T castedParam(@Nonnull final Param param, @Nonnull final Class<T> type, Object name) { 155 if (param == null) { 156 return null; 157 // throw new FramsticksException().msg("param is missing").arg("name", name).arg("in", this); 158 } 159 if (!type.isInstance(param)) { 160 return null; 161 // throw new FramsticksException().msg("wrong type of param").arg("actual", param.getClass()).arg("requested", type).arg("in", this); 162 } 163 return type.cast(param); 154 164 } 155 165 … … 161 171 * @return the param entry 162 172 */ 163 public <T extends Param> T getParamEntry(int i, Class<T> type) { 164 if (i < 0 || i >= paramList.size()) { 165 return null; 166 } 167 return Casting.tryCast(type, paramList.get(i)); 173 public <T extends Param> T getParamEntry(final int i, @Nonnull final Class<T> type) { 174 return castedParam(getParam(i), type, i); 168 175 } 169 176 … … 175 182 * @return the param entry 176 183 */ 177 public <T extends Param> T getParamEntry(String id, Class<T> type) { 178 return Casting.tryCast(type, paramEntryMap.get(id)); 184 public <T extends Param> T getParamEntry(@Nonnull final String id, @Nonnull final Class<T> type) { 185 return castedParam(getParam(id), type, id); 186 } 187 188 public Param getParam(int i) { 189 if (i < 0 || i >= paramList.size()) { 190 return null; 191 } 192 return paramList.get(i); 193 } 194 195 public Param getParam(String id) { 196 if (!paramEntryMap.containsKey(id)) { 197 return null; 198 } 199 return paramEntryMap.get(id); 179 200 } 180 201 … … 188 209 } 189 210 190 public static FramsClass getFramsClass() { 191 return new FramsClass("class", "class", null) 192 .append(Param.build().id("name").name("Name").type(StringParam.class)) 193 .append(Param.build().id("id").name("id").type(StringParam.class)) 194 .append(Param.build().id("desc").name("Description").type(StringParam.class)); 195 } 196 211 @ParamAnnotation 197 212 public void setId(String id) { 198 213 this.id = id; 199 214 } 200 215 216 @ParamAnnotation 201 217 public void setName(String name) { 202 218 this.name = name; 203 219 } 204 220 221 @ParamAnnotation(id = "desc") 205 222 public void setDescription(String description) { 206 223 this.description = description; 207 224 } 208 209 public static String getParamTypeForNativeType(Type type) {210 if (type instanceof ParameterizedType) {211 ParameterizedType p = (ParameterizedType) type;212 Type rawType = p.getRawType();213 if (rawType.equals(Map.class)) {214 Type containedType = p.getActualTypeArguments()[1];215 //TODO uid should be passed along during construction216 if (containedType instanceof Class) {217 return "l " + ((Class<?>) containedType).getCanonicalName()218 + " name";219 }220 }221 if (rawType.equals(List.class)) {222 Type containedType = p.getActualTypeArguments()[0];223 if (containedType instanceof Class) {224 return "l " + ((Class<?>) containedType).getCanonicalName();225 }226 }227 return null;228 }229 230 if (type.equals(Integer.class)) {231 return "d";232 }233 if (type.equals(String.class)) {234 return "s";235 }236 if (type.equals(Double.class)) {237 return "f";238 }239 if (type instanceof Class) {240 return "o " + ((Class<?>) type).getCanonicalName();241 }242 return null;243 }244 245 public static final String GENERATE_HELP_PREFIX = "automatically generated from: ";246 247 public static FramsClass readFromStream(InputStream stream) {248 return Loaders.loadFramsClass(new FileSource(stream));249 }250 251 public static class Constructor {252 protected final FramsClass result;253 protected Class<?> currentClass;254 255 public Constructor(Class<?> src, String name) {256 result = new FramsClass(name, name, GENERATE_HELP_PREFIX257 + src.toString());258 currentClass = src;259 while (currentClass != null) {260 try {261 currentClass.getMethod("constructFramsClass", Constructor.class).invoke(null, this);262 } catch (IllegalAccessException | IllegalArgumentException263 | InvocationTargetException | NoSuchMethodException264 | SecurityException e) {265 log.debug("failed to use constructFramsClass static method (skipping): ", e);266 }267 currentClass = currentClass.getSuperclass();268 }269 currentClass = src;270 }271 272 public final FramsClass getResult() {273 return result;274 }275 276 public Constructor allFields() {277 for (Field f : currentClass.getFields()) {278 field(f.getName());279 }280 return this;281 }282 283 public Constructor method(String name, Class<?> ... arguments) {284 try {285 Method method = currentClass.getMethod(name, arguments);286 if (!Modifier.isPublic(method.getModifiers())) {287 return this;288 }289 String returnParamClass = getParamTypeForNativeType(method.getGenericReturnType());290 if (returnParamClass == null) {291 return this;292 }293 Class<?>[] args = method.getParameterTypes();294 if (args.length == 0) {295 if (method.getName().startsWith("get")) {296 String fieldName = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4);297 Param param = Param.build().type(returnParamClass).name(fieldName).id(fieldName).help(GENERATE_HELP_PREFIX + method.toString()).finish();298 assert param != null;299 result.append(param);300 return this;301 }302 return this;303 }304 return this;305 } catch (NoSuchMethodException e) {306 log.fatal("method " + name + " was not found in " + currentClass.toString());307 }308 return this;309 }310 public Constructor field(String name) {311 try {312 Field field = currentClass.getField(name);313 if (!Modifier.isPublic(field.getModifiers())) {314 return this;315 }316 String paramClass = getParamTypeForNativeType(field.getGenericType());317 if (paramClass == null) {318 return this;319 }320 Param param = Param.build().type(paramClass).name(field.getName()).id(field.getName()).help(GENERATE_HELP_PREFIX + field.toString()).finish();321 assert param != null;322 result.append(param);323 return this;324 } catch (NoSuchFieldException e) {325 log.fatal("field " + name + " was not found in " + currentClass.toString());326 }327 return this;328 }329 }330 225 }
Note: See TracChangeset
for help on using the changeset viewer.