source: java/main/src/main/java/com/framsticks/params/FramsClass.java @ 78

Last change on this file since 78 was 78, checked in by psniegowski, 11 years ago

Add f0 parsing and f0->Model transformation.

File size: 9.1 KB
Line 
1package com.framsticks.params;
2
3import com.framsticks.params.types.CompositeParam;
4import com.framsticks.params.types.DecimalParam;
5import com.framsticks.params.types.FloatParam;
6import com.framsticks.params.types.StringParam;
7import com.framsticks.parsers.FileSource;
8import com.framsticks.parsers.Loaders;
9import com.framsticks.util.Casting;
10import org.apache.log4j.Logger;
11
12import javax.lang.model.element.TypeElement;
13import java.io.InputStream;
14import java.lang.reflect.*;
15import java.util.*;
16import java.util.List;
17
18/**
19 * The class FramsClass represents the class / schema of connected parameters
20 * (such as parameters within the class). It differs from C++ version by storing
21 * information about the class that parameters belong to.
22 *
23 * Based loosely on c++ class Param located in cpp/gdk/param.*
24 *
25 * @author Jarek Szymczak <name.surname@gmail.com>, Mateusz Jarus (please
26 *         replace name and surname with my personal data)
27 *
28 * @author Piotr Sniegowski
29 */
30public final class FramsClass {
31
32    private final static Logger LOGGER = Logger.getLogger(FramsClass.class.getName());
33
34    /**
35      * The Class which represents group.
36      */
37
38        /** The offset of the parameter (applied for newly added parameter). */
39        protected int fieldsNumber;
40
41        /** The groups. */
42        protected List<Group> groups = new ArrayList<Group>();
43
44        /**
45         * The param entry map <parameterId, param> (for fast accessing of parameters
46         * by their name)
47         */
48        protected Map<String, Param> paramEntryMap = new LinkedHashMap<String, Param>();
49
50        /** The param list (for accessing parameters by offset in O(1) time. */
51        protected List<Param> paramList = new ArrayList<Param>();
52
53        /** The param getId map (for fast lookup of offset based on name */
54        protected Map<String, Integer> paramIdMap = new HashMap<String, Integer>();
55
56        protected String id;
57
58        protected String name;
59
60        protected String description;
61
62        public Collection<Param> getParamEntries() {
63                return paramList;
64        }
65
66        public FramsClass() {
67        }
68
69        public FramsClass(String id, String name, String description) {
70                this.setId(id);
71                this.setName(name);
72                this.setDescription(description);
73        }
74
75        /**
76         * Adds new param entry.
77         *
78         * @param param
79         *            the new param entry
80         */
81        public FramsClass append(Param param) {
82                paramEntryMap.put(param.getId(), param);
83                //paramEntryMap.put(param.getInternalId(), param);
84                paramList.add(param);
85                try {
86                        Group group = groups.get(param.getGroup());
87                        if (group != null) {
88                                group.addProperty(param);
89                        }
90                } catch (IndexOutOfBoundsException ignored) {
91
92                }
93
94                return this;
95        }
96
97        /**
98         * Adds new group.
99         */
100        public FramsClass appendGroup(Group group) {
101                groups.add(group);
102                return this;
103        }
104
105        public String getDescription() {
106                return description;
107        }
108
109        public int getGroupCount() {
110                return groups.size();
111        }
112
113        /**
114         * Gets the group member.
115         *
116         * @param gi
117         *            the offset of group
118         * @param pi
119         *            the offset of member within a group
120         * @return the pi-th member of group gi
121         */
122        public Param getGroupMember(int gi, int pi) {
123                if (gi < 0 || pi < 0 || gi >= groups.size()) {
124                        return null;
125        }
126                Group group = groups.get(gi);
127                return (group != null ? group.getProperty(pi) : null);
128        }
129
130        /**
131         * Gets the group getName.
132         *
133         * @param gi
134         *            the offset of group
135         * @return the group getName
136         */
137        public String getGroupName(int gi) {
138                if (gi < 0 || gi >= groups.size())
139                        return null;
140                return groups.get(gi).name;
141        }
142
143        public String getId() {
144                return id;
145        }
146
147        public String getName() {
148                return name;
149        }
150
151    public String getNiceName() {
152        return name != null ? name : id;
153    }
154
155        /**
156         * Gets the param entry.
157         *
158         * @param i
159         *            the offset of parameter
160         * @return the param entry
161         */
162        public Param getParamEntry(int i) {
163                if (i < 0 || i >= paramList.size()) {
164                        return null;
165                }
166                return paramList.get(i);
167        }
168
169        /**
170         * Gets the param entry.
171         *
172         * @param id
173         *            the getId of parameter
174         * @return the param entry
175         */
176        public Param getParamEntry(String id) {
177                return paramEntryMap.get(id);
178        }
179
180        public int getParamCount() {
181                return paramList.size();
182        }
183
184        @Override
185        public String toString() {
186                return id;
187        }
188
189        public static FramsClass getFramsClass() {
190                return new FramsClass("class", "class", null)
191                        .append(new ParamBuilder().setId("name").setName("Name").setType(StringParam.class).build())
192                        .append(new ParamBuilder().setId("id").setName("id").setType(StringParam.class).build())
193                        .append(new ParamBuilder().setId("desc").setName("Description").setType(StringParam.class).build());
194        }
195
196        public void setId(String id) {
197                this.id = id;
198        }
199
200        public void setName(String name) {
201                this.name = name;
202        }
203
204        public void setDescription(String description) {
205                this.description = description;
206        }
207
208    public static String getParamTypeForNativeType(Type type) {
209        if (type instanceof ParameterizedType) {
210            ParameterizedType p = (ParameterizedType) type;
211            Type rawType = p.getRawType();
212            if (rawType.equals(Map.class)) {
213                Type containedType = p.getActualTypeArguments()[1];
214                //TODO uid should be passed along during construction
215                if (containedType instanceof Class) {
216                    return "l " + ((Class) containedType).getCanonicalName() + " name";
217                }
218            }
219                        if (rawType.equals(List.class)) {
220                                Type containedType = p.getActualTypeArguments()[0];
221                                if (containedType instanceof Class) {
222                                        return "l " + ((Class) containedType).getCanonicalName();
223                                }
224                        }
225            return null;
226        }
227
228        if (type.equals(Integer.class)) {
229            return "d";
230        }
231        if (type.equals(String.class)) {
232            return "s";
233        }
234        if (type.equals(Double.class)) {
235            return "f";
236        }
237        if (type instanceof Class) {
238            return "o " + ((Class) type).getCanonicalName();
239        }
240        return null;
241    }
242
243    public static final String GENERATE_HELP_PREFIX = "automatically generated from: ";
244
245        public static FramsClass readFromStream(InputStream stream) {
246                return Loaders.loadFramsClass(new FileSource(stream));
247        }
248
249        public static class Constructor {
250        protected final FramsClass result;
251        protected Class currentClass;
252        public Constructor(Class src, String name) {
253            result = new FramsClass(name, name, GENERATE_HELP_PREFIX + src.toString());
254            currentClass = src;
255            while (currentClass != null) {
256                try {
257                    currentClass.getMethod("constructFramsClass", Constructor.class).invoke(null, this);
258                } catch (Exception ignored) {
259                }
260                currentClass = currentClass.getSuperclass();
261            }
262                        currentClass = src;
263        }
264
265        public final FramsClass getResult() {
266            return result;
267        }
268
269                public Constructor allFields() {
270                        for (Field f : currentClass.getFields()) {
271                                field(f.getName());
272                        }
273                        return this;
274                }
275
276        public Constructor method(String name, Class<?> ... arguments) {
277            try {
278                Method method = currentClass.getMethod(name, arguments);
279                if (!Modifier.isPublic(method.getModifiers())) {
280                    return this;
281                }
282                String returnParamClass = getParamTypeForNativeType(method.getGenericReturnType());
283                if (returnParamClass == null) {
284                    return this;
285                }
286                Class[] args = method.getParameterTypes();
287                if (args.length == 0) {
288                    if (method.getName().startsWith("get")) {
289                        String fieldName = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4);
290                        Param param = new ParamBuilder().setType(returnParamClass).setName(fieldName).setId(fieldName).setHelp(GENERATE_HELP_PREFIX + method.toString()).build();
291                        assert param != null;
292                        result.append(param);
293                        return this;
294                    }
295                    return this;
296                }
297                return this;
298            } catch (NoSuchMethodException e) {
299                LOGGER.fatal("method " + name + " was not found in " + currentClass.toString());
300            }
301            return this;
302        }
303        public Constructor field(String name) {
304            try {
305                Field field = currentClass.getField(name);
306                if (!Modifier.isPublic(field.getModifiers())) {
307                    return this;
308                }
309                String paramClass = getParamTypeForNativeType(field.getGenericType());
310                if (paramClass == null) {
311                    return this;
312                }
313                Param param = new ParamBuilder().setType(paramClass).setName(field.getName()).setId(field.getName()).setHelp(GENERATE_HELP_PREFIX + field.toString()).build();
314                assert param != null;
315                result.append(param);
316                return this;
317            } catch (NoSuchFieldException e) {
318                LOGGER.fatal("field " + name + " was not found in " + currentClass.toString());
319            }
320            return this;
321        }
322    }
323}
Note: See TracBrowser for help on using the repository browser.