| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
package smetana.core; |
| 38 |
|
| 39 |
import java.util.Collection; |
| 40 |
import java.util.HashMap; |
| 41 |
import java.util.Iterator; |
| 42 |
import java.util.LinkedHashMap; |
| 43 |
import java.util.List; |
| 44 |
import java.util.Map; |
| 45 |
import java.util.Set; |
| 46 |
|
| 47 |
public class StructureDefinition { |
| 48 |
|
| 49 |
private static final Map<Class, StructureDefinition> all = new HashMap<Class, StructureDefinition>(); |
| 50 |
|
| 51 |
public static StructureDefinition from(Class cl) { |
| 52 |
if (cl == null) { |
| 53 |
throw new IllegalArgumentException(); |
| 54 |
} |
| 55 |
StructureDefinition result = all.get(cl); |
| 56 |
if (result == null) { |
| 57 |
result = new StructureDefinition(cl); |
| 58 |
all.put(cl, result); |
| 59 |
} |
| 60 |
return result; |
| 61 |
} |
| 62 |
|
| 63 |
private final Class cl; |
| 64 |
private Map<String, Bucket> buckets; |
| 65 |
|
| 66 |
private StructureDefinition(Class cl) { |
| 67 |
|
| 68 |
this.cl = cl; |
| 69 |
} |
| 70 |
|
| 71 |
public String toString() { |
| 72 |
return (cl == null ? "NO_CLASS" : cl.getName()) + " " + buckets; |
| 73 |
} |
| 74 |
|
| 75 |
private Map<String, Bucket> buckets() { |
| 76 |
if (buckets == null) { |
| 77 |
final List<String> definition = CType.getDefinition(cl); |
| 78 |
JUtils.LOG("StructureDefinition::run for " + cl); |
| 79 |
JUtils.LOG("def=" + definition); |
| 80 |
JUtils.LOG("first=" + definition.get(0)); |
| 81 |
|
| 82 |
buckets = new LinkedHashMap<String, Bucket>(); |
| 83 |
|
| 84 |
if (definition.get(0).equals("typedef enum")) { |
| 85 |
final String last = definition.get(definition.size() - 1); |
| 86 |
if (last.matches("\\w+") == false) { |
| 87 |
throw new UnsupportedOperationException(); |
| 88 |
} |
| 89 |
buckets.put(last, Bucket.buildEnum(last, definition)); |
| 90 |
return buckets; |
| 91 |
} |
| 92 |
|
| 93 |
if (definition.get(0).equals("typedef struct gvplugin_active_textlayout_s") == false |
| 94 |
&& definition.get(0).equals("typedef struct color_s") == false |
| 95 |
&& definition.get(0).equals("typedef struct") == false |
| 96 |
&& definition.get(0).equals("typedef struct pointf_s") == false |
| 97 |
&& definition.get(0).equals("typedef struct gvplugin_active_layout_s") == false |
| 98 |
&& definition.get(0).equals("typedef struct GVCOMMON_s") == false |
| 99 |
&& definition.get(0).equals("struct " + cl.getSimpleName()) == false |
| 100 |
&& definition.get(0).equals("typedef struct " + cl.getSimpleName()) == false |
| 101 |
&& definition.get(0).equals("typedef struct " + cl.getSimpleName().replaceFirst("_t", "_s")) == false |
| 102 |
&& definition.get(0).equals("typedef union " + cl.getSimpleName()) == false) { |
| 103 |
throw new IllegalStateException("<struct " + cl.getSimpleName() + "> VERSUS <" + definition.get(0) |
| 104 |
+ ">"); |
| 105 |
} |
| 106 |
if (definition.get(1).equals("{") == false) { |
| 107 |
throw new IllegalStateException(); |
| 108 |
} |
| 109 |
|
| 110 |
int last = definition.size() - 1; |
| 111 |
if (definition.get(definition.size() - 2).equals("}") |
| 112 |
&& definition.get(definition.size() - 1).equals(cl.getSimpleName())) { |
| 113 |
last--; |
| 114 |
} |
| 115 |
|
| 116 |
if (definition.get(last).equals("}") == false) { |
| 117 |
throw new IllegalStateException(); |
| 118 |
} |
| 119 |
|
| 120 |
for (Iterator<String> it = definition.subList(2, last).iterator(); it.hasNext();) { |
| 121 |
buckets.putAll(Bucket.buildSome(it)); |
| 122 |
} |
| 123 |
} |
| 124 |
return buckets; |
| 125 |
} |
| 126 |
|
| 127 |
public Set<String> getFields() { |
| 128 |
return buckets().keySet(); |
| 129 |
} |
| 130 |
|
| 131 |
public Collection<Bucket> getBuckets() { |
| 132 |
return buckets().values(); |
| 133 |
} |
| 134 |
|
| 135 |
public Map<String, Bucket> getBucketsMap() { |
| 136 |
return buckets(); |
| 137 |
} |
| 138 |
|
| 139 |
public Bucket getBucket(String field) { |
| 140 |
final Bucket result = buckets().get(field); |
| 141 |
if (result == null) { |
| 142 |
throw new IllegalArgumentException(field); |
| 143 |
} |
| 144 |
return result; |
| 145 |
} |
| 146 |
|
| 147 |
public Class getTheClass() { |
| 148 |
return cl; |
| 149 |
} |
| 150 |
|
| 151 |
public boolean containsFieldName(String fieldName) { |
| 152 |
return buckets().keySet().contains(fieldName); |
| 153 |
} |
| 154 |
|
| 155 |
} |