Ignore:
Timestamp:
06/26/13 13:27:31 (11 years ago)
Author:
psniegowski
Message:

HIGHLIGHTS:

  • use java annotations to mark classes and fields to be used when:
    • using java classes with ReflectionAccess? to represent remote objects with FramsClass? description found by "info ..." requests
    • to build up FramsClass? representation of objects not present at remote server
  • allow using primitive types (instead of wraping counterparts) in reflected classes
  • rework FramsClass? creation process (add FramsClassBuilder?)
  • add more tests

CHANGELOG:
Prepare model.World class.

Minor change.

Use primitive types for Genotype and Creature classes.

Use primitive types in model.Neuro* classes.

Use primitive types in model.Joint* classes.

Use primitive types in model.Part* classes.

Fix primitive values.

Extract FramsClassBuilder?.

Add tests of Model classes.

More fixes.

Refactorize out ParamCandidate?.

Several fixes.

Fix all regressions after introducing annotations.

Use annotations throughout the project.

Add exception classes.

Improve creation of FramsClass?.

More changes.

Many changes regarding annotations.

Annotate classes in com.framsticks.model package.

Remove manual FramsClass? constructor.

Construct FramsClass? for Creature. Add test.

Add default values to the ParamAnnotation?.

Add ParamBuilderTest? and ParamAnnotation?.

Add FramsClassAnnotation?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • java/main/src/main/java/com/framsticks/params/FramsClass.java

    r85 r86  
    11package com.framsticks.params;
    22
    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.*;
     3import com.framsticks.params.annotations.FramsClassAnnotation;
     4import com.framsticks.params.annotations.ParamAnnotation;
     5// import com.framsticks.util.FramsticksException;
     6
    127import java.util.*;
     8
     9import javax.annotation.Nonnull;
    1310
    1411/**
     
    2421 * @author Piotr Sniegowski
    2522 */
     23@FramsClassAnnotation(id = "class", name = "class")
    2624public final class FramsClass {
    27 
    28         private final static Logger log = Logger.getLogger(FramsClass.class
    29                         .getName());
    30 
    31         /**
    32          * The Class which represents group.
    33          */
    3425
    3526        /** The offset of the parameter (applied for newly added parameter). */
     
    6253
    6354        public FramsClass() {
     55
    6456        }
    6557
     
    7769         */
    7870        public FramsClass append(Param param) {
     71                // if (param.hasFlag(Flags.USERHIDDEN)) {
     72                //      return this;
     73                // }
    7974                paramEntryMap.put(param.getId(), param);
    8075                //paramEntryMap.put(param.getInternalId(), param);
     
    10499        }
    105100
     101        @ParamAnnotation(id = "desc")
    106102        public String getDescription() {
    107103                return description;
     
    130126
    131127        /**
    132          * Gets the group getName.
     128         * Gets the group name.
    133129         *
    134130         * @param gi
    135131         *            the offset of group
    136          * @return the group getName
     132         * @return the group name
    137133         */
    138134        public String getGroupName(int gi) {
     
    142138        }
    143139
     140        @ParamAnnotation
    144141        public String getId() {
    145142                return id;
    146143        }
    147144
     145        @ParamAnnotation
    148146        public String getName() {
    149147                return name;
     
    152150        public String getNiceName() {
    153151                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);
    154164        }
    155165
     
    161171         * @return the param entry
    162172         */
    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);
    168175        }
    169176
     
    175182         * @return the param entry
    176183         */
    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);
    179200        }
    180201
     
    188209        }
    189210
    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
    197212        public void setId(String id) {
    198213                this.id = id;
    199214        }
    200215
     216        @ParamAnnotation
    201217        public void setName(String name) {
    202218                this.name = name;
    203219        }
    204220
     221        @ParamAnnotation(id = "desc")
    205222        public void setDescription(String description) {
    206223                this.description = description;
    207224        }
    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 construction
    216                                 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_PREFIX
    257                                         + src.toString());
    258                         currentClass = src;
    259                         while (currentClass != null) {
    260                                 try {
    261                                         currentClass.getMethod("constructFramsClass", Constructor.class).invoke(null, this);
    262                                 } catch (IllegalAccessException | IllegalArgumentException
    263                                                 | InvocationTargetException | NoSuchMethodException
    264                                                 | 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         }
    330225}
Note: See TracChangeset for help on using the changeset viewer.