| 1 | // License: GPL. For details, see LICENSE file. |
| 2 | import java.awt.Color; |
| 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | import java.lang.reflect.Executable; |
| 6 | import java.lang.reflect.Method; |
| 7 | import java.util.Collection; |
| 8 | import java.util.HashMap; |
| 9 | import java.util.List; |
| 10 | import java.util.Map; |
| 11 | import java.util.SortedMap; |
| 12 | import java.util.TreeMap; |
| 13 | |
| 14 | import org.openstreetmap.josm.Main; |
| 15 | import org.openstreetmap.josm.data.Preferences; |
| 16 | import org.openstreetmap.josm.data.preferences.BooleanProperty; |
| 17 | import org.openstreetmap.josm.data.preferences.CollectionProperty; |
| 18 | import org.openstreetmap.josm.data.preferences.ColorProperty; |
| 19 | import org.openstreetmap.josm.data.preferences.DoubleProperty; |
| 20 | import org.openstreetmap.josm.data.preferences.EnumProperty; |
| 21 | import org.openstreetmap.josm.data.preferences.IntegerProperty; |
| 22 | import org.openstreetmap.josm.data.preferences.ListListSetting; |
| 23 | import org.openstreetmap.josm.data.preferences.ListSetting; |
| 24 | import org.openstreetmap.josm.data.preferences.LongProperty; |
| 25 | import org.openstreetmap.josm.data.preferences.MapListSetting; |
| 26 | import org.openstreetmap.josm.data.preferences.Setting; |
| 27 | import org.openstreetmap.josm.data.preferences.StringProperty; |
| 28 | import org.openstreetmap.josm.data.preferences.StringSetting; |
| 29 | import org.openstreetmap.josm.data.preferences.StrokeProperty; |
| 30 | import org.openstreetmap.josm.tools.I18n; |
| 31 | |
| 32 | import spoon.Launcher; |
| 33 | import spoon.processing.AbstractProcessor; |
| 34 | import spoon.reflect.code.BinaryOperatorKind; |
| 35 | import spoon.reflect.code.CtAbstractInvocation; |
| 36 | import spoon.reflect.code.CtBinaryOperator; |
| 37 | import spoon.reflect.code.CtConstructorCall; |
| 38 | import spoon.reflect.code.CtExpression; |
| 39 | import spoon.reflect.code.CtFieldRead; |
| 40 | import spoon.reflect.code.CtInvocation; |
| 41 | import spoon.reflect.code.CtLiteral; |
| 42 | import spoon.reflect.code.CtUnaryOperator; |
| 43 | import spoon.reflect.code.CtVariableRead; |
| 44 | import spoon.reflect.code.UnaryOperatorKind; |
| 45 | import spoon.reflect.declaration.CtClass; |
| 46 | import spoon.reflect.declaration.CtElement; |
| 47 | import spoon.reflect.declaration.CtField; |
| 48 | import spoon.reflect.declaration.CtVariable; |
| 49 | import spoon.reflect.declaration.ModifierKind; |
| 50 | import spoon.reflect.eval.PartialEvaluator; |
| 51 | import spoon.reflect.reference.CtExecutableReference; |
| 52 | import spoon.reflect.reference.CtFieldReference; |
| 53 | import spoon.reflect.reference.CtVariableReference; |
| 54 | import spoon.reflect.visitor.filter.AbstractFilter; |
| 55 | import spoon.support.SpoonClassNotFoundException; |
| 56 | |
| 57 | public class PreferencesCollector extends AbstractProcessor<CtAbstractInvocation> { |
| 58 | |
| 59 | public SortedMap<String, Setting<?>> settingsMap = new TreeMap<>(); |
| 60 | public int numNull = 0; |
| 61 | |
| 62 | public static void main(String[] args) { |
| 63 | String baseDir = args[0]; |
| 64 | Launcher spoon = new Launcher(); |
| 65 | spoon.addInputResource(baseDir + "/src/org/openstreetmap/josm"); |
| 66 | spoon.buildModel(); |
| 67 | PreferencesCollector coll = new PreferencesCollector(); |
| 68 | for (CtAbstractInvocation inv : spoon.getFactory().Package().getRootPackage().getElements(new AbstractFilter<CtAbstractInvocation>() { |
| 69 | @Override |
| 70 | public boolean matches(CtAbstractInvocation element) { |
| 71 | return element instanceof CtInvocation || element instanceof CtConstructorCall; |
| 72 | } |
| 73 | })) { |
| 74 | coll.process(inv); |
| 75 | } |
| 76 | try { |
| 77 | System.err.println("number of unique recognized keys: " + coll.settingsMap.keySet().size()); |
| 78 | System.err.println("number of unrecognized keys (with duplicates): " + coll.numNull); |
| 79 | Main.pref.save(new File(baseDir + "/data/preferences-available.xml"), coll.settingsMap.entrySet().stream(), true, false); |
| 80 | } catch (IOException ex) { |
| 81 | throw new RuntimeException(ex); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void process(CtAbstractInvocation element) { |
| 87 | Map<Executable, Setting<?>> prefExecutables = new HashMap<>(); |
| 88 | try { |
| 89 | // don't include Preferences.getInteger(String key, String specName, int def) as it |
| 90 | // is usually called with non-constant parameter specName |
| 91 | prefExecutables.put(Preferences.class.getMethod("get", String.class), new StringSetting(null)); |
| 92 | prefExecutables.put(Preferences.class.getMethod("get", String.class, String.class), new StringSetting(null)); |
| 93 | prefExecutables.put(Preferences.class.getMethod("getCollection", String.class), new ListSetting(null)); |
| 94 | prefExecutables.put(Preferences.class.getMethod("getCollection", String.class, Collection.class), new ListSetting(null)); |
| 95 | prefExecutables.put(Preferences.class.getMethod("getArray", String.class), new ListListSetting(null)); |
| 96 | prefExecutables.put(Preferences.class.getMethod("getArray", String.class, Collection.class), new ListListSetting(null)); |
| 97 | prefExecutables.put(Preferences.class.getMethod("getBoolean", String.class), new StringSetting(null)); |
| 98 | prefExecutables.put(Preferences.class.getMethod("getBoolean", String.class, boolean.class), new StringSetting(null)); |
| 99 | prefExecutables.put(Preferences.class.getMethod("getInteger", String.class, int.class), new StringSetting(null)); |
| 100 | prefExecutables.put(Preferences.class.getMethod("getDouble", String.class, double.class), new StringSetting(null)); |
| 101 | prefExecutables.put(Preferences.class.getMethod("getListOfStructs", String.class, Collection.class), new MapListSetting(null)); |
| 102 | prefExecutables.put(Preferences.class.getMethod("getListOfStructs", String.class, Class.class), new MapListSetting(null)); |
| 103 | prefExecutables.put(Preferences.class.getMethod("getListOfStructs", String.class, Collection.class, Class.class), new MapListSetting(null)); |
| 104 | prefExecutables.put(Preferences.class.getMethod("getLong", String.class, long.class), new StringSetting(null)); |
| 105 | prefExecutables.put(StringProperty.class.getConstructor(String.class, String.class), new StringSetting(null)); |
| 106 | prefExecutables.put(IntegerProperty.class.getConstructor(String.class, int.class), new StringSetting(null)); |
| 107 | prefExecutables.put(LongProperty.class.getConstructor(String.class, long.class), new StringSetting(null)); |
| 108 | prefExecutables.put(BooleanProperty.class.getConstructor(String.class, boolean.class), new StringSetting(null)); |
| 109 | prefExecutables.put(DoubleProperty.class.getConstructor(String.class, double.class), new StringSetting(null)); |
| 110 | prefExecutables.put(CollectionProperty.class.getConstructor(String.class, Collection.class), new ListSetting(null)); |
| 111 | prefExecutables.put(StrokeProperty.class.getConstructor(String.class, String.class), new StringSetting(null)); |
| 112 | prefExecutables.put(EnumProperty.class.getConstructor(String.class, Class.class, Enum.class), new StringSetting(null)); |
| 113 | prefExecutables.put(ColorProperty.class.getConstructor(String.class, String.class), new StringSetting(null)); |
| 114 | prefExecutables.put(ColorProperty.class.getConstructor(String.class, Color.class), new StringSetting(null)); |
| 115 | } catch (NoSuchMethodException | SecurityException ex) { |
| 116 | throw new RuntimeException(ex); |
| 117 | } |
| 118 | CtExecutableReference exec = element.getExecutable(); |
| 119 | Executable realExec = null; |
| 120 | if (element instanceof CtInvocation) { |
| 121 | realExec = exec.getActualMethod(); |
| 122 | } else if (element instanceof CtConstructorCall) { |
| 123 | try { |
| 124 | realExec = exec.getActualConstructor(); |
| 125 | } catch (SpoonClassNotFoundException cnf) { |
| 126 | // it seems to have trouble with certain nested inner / anonymous classes |
| 127 | } |
| 128 | } else throw new AssertionError(); |
| 129 | |
| 130 | Setting setting = prefExecutables.get(realExec); |
| 131 | if (setting != null) { |
| 132 | List<CtExpression<?>> args = element.getArguments(); |
| 133 | CtExpression key = args.get(0); |
| 134 | StaticCodeEvaluator eval = new StaticCodeEvaluator(); |
| 135 | eval.evaluate(key); |
| 136 | String strKey = eval.getResult(); |
| 137 | if (strKey != null) { |
| 138 | // fix ColorProperty |
| 139 | Class klass = realExec.getDeclaringClass(); |
| 140 | if (klass.equals(ColorProperty.class)) { |
| 141 | strKey = ColorProperty.getColorKey(strKey); |
| 142 | } |
| 143 | |
| 144 | // add default value, if possilbe |
| 145 | if (setting instanceof StringSetting && args.size() >= 2) { |
| 146 | CtExpression defaultValue = args.get(args.size() - 1); |
| 147 | StaticCodeEvaluator eval2 = new StaticCodeEvaluator(); |
| 148 | eval2.evaluate(defaultValue); |
| 149 | String strDefault = eval2.getResult(); |
| 150 | if (strDefault != null) { |
| 151 | setting = new StringSetting(strDefault); |
| 152 | } else { |
| 153 | System.err.println("=== default value extraction failed!"); |
| 154 | System.err.println("file: "+element.getPosition().getLine()+":"+element.getPosition().getColumn() +element.getPosition().getFile()); |
| 155 | System.err.println("v: "+defaultValue.getClass()+ " "+ defaultValue); |
| 156 | System.err.println(eval.getErrorLog()); |
| 157 | } |
| 158 | } |
| 159 | settingsMap.put(strKey, setting); |
| 160 | } else { |
| 161 | System.err.println("=== key extraction failed!"); |
| 162 | System.err.println("file: "+element.getPosition().getLine()+":"+element.getPosition().getColumn() +element.getPosition().getFile()); |
| 163 | System.err.println("exec: "+element.getExecutable()); |
| 164 | System.err.println("args: "+element.getArguments()); |
| 165 | System.err.println(eval.getErrorLog()); |
| 166 | numNull++; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | public static Class determineClass(CtElement element) { |
| 172 | CtClass klass = element.getParent(CtClass.class); |
| 173 | if (klass == null) |
| 174 | return null; |
| 175 | return klass.getActualClass(); |
| 176 | } |
| 177 | |
| 178 | public static class StaticCodeEvaluator implements PartialEvaluator { |
| 179 | |
| 180 | private String result; |
| 181 | private StringBuilder sb = new StringBuilder(); |
| 182 | |
| 183 | private final Method marktr; |
| 184 | |
| 185 | public StaticCodeEvaluator() { |
| 186 | try { |
| 187 | marktr = I18n.class.getMethod("marktr", String.class); |
| 188 | } catch (NoSuchMethodException | SecurityException ex) { |
| 189 | throw new RuntimeException(); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public <R extends CtElement> R evaluate(R element) { |
| 195 | Object oResult = evalWrk(element, 0); |
| 196 | if (oResult != null) { |
| 197 | this.result = oResult.toString(); |
| 198 | } |
| 199 | return null; |
| 200 | } |
| 201 | |
| 202 | protected Object evalWrk(CtElement element, int lvl) { |
| 203 | lvl++; |
| 204 | if (lvl > 10) { |
| 205 | log("maximum recursion level exceeded!"); |
| 206 | return null; |
| 207 | } |
| 208 | if (element instanceof CtLiteral) { |
| 209 | return eval((CtLiteral) element, lvl); |
| 210 | } else if (element instanceof CtFieldRead) { |
| 211 | return eval((CtFieldRead) element, lvl); |
| 212 | } else if (element instanceof CtVariableRead) { |
| 213 | return eval((CtVariableRead) element, lvl); |
| 214 | } else if (element instanceof CtUnaryOperator) { |
| 215 | return eval((CtUnaryOperator) element, lvl); |
| 216 | } else if (element instanceof CtBinaryOperator) { |
| 217 | return eval((CtBinaryOperator) element, lvl); |
| 218 | } else if (element instanceof CtInvocation) { |
| 219 | return eval((CtInvocation) element, lvl); |
| 220 | } else { |
| 221 | log("cannot handle "+element.getClass()+ " "+element); |
| 222 | return null; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | public String getResult() { |
| 227 | return result; |
| 228 | } |
| 229 | |
| 230 | public String getErrorLog() { |
| 231 | return sb.toString(); |
| 232 | } |
| 233 | |
| 234 | protected Object eval(CtLiteral literal, int lvl) { |
| 235 | return literal.getValue(); |
| 236 | } |
| 237 | |
| 238 | protected Object eval(CtFieldRead frr, int lvl) { |
| 239 | CtFieldReference fr = frr.getVariable(); |
| 240 | String d = ""; |
| 241 | if (fr.isFinal()) { |
| 242 | d += "final "; |
| 243 | } |
| 244 | if (fr.isStatic()) { |
| 245 | d += "static "; |
| 246 | } |
| 247 | log(" getKey qualified name - "+fr.getQualifiedName()); |
| 248 | CtField fld = fr.getDeclaration(); |
| 249 | if (fr.isFinal() && fr.isStatic() && fld != null) { |
| 250 | CtExpression def = fld.getDefaultExpression(); |
| 251 | if (def == null) throw new AssertionError(); |
| 252 | log(" getKey default expression: "+def.getClass()); |
| 253 | return this.evalWrk(def, lvl); |
| 254 | } |
| 255 | log(" getKey result: "+d+" "+fld); |
| 256 | return null; |
| 257 | } |
| 258 | |
| 259 | protected Object eval(CtVariableRead vr, int lvl) { |
| 260 | CtVariableReference vref = vr.getVariable(); |
| 261 | CtVariable var = vref.getDeclaration(); |
| 262 | if (!var.getModifiers().contains(ModifierKind.FINAL)) { |
| 263 | log("non-final variable: "+var.getSimpleName()); |
| 264 | return null; |
| 265 | } |
| 266 | CtExpression def = var.getDefaultExpression(); |
| 267 | if (def == null) { |
| 268 | log("no initializer for variable: "+var.getSimpleName()); |
| 269 | return null; |
| 270 | } |
| 271 | return evalWrk(def, lvl); |
| 272 | } |
| 273 | |
| 274 | protected Object eval(CtUnaryOperator uo, int lvl) { |
| 275 | if (uo.getKind() != UnaryOperatorKind.NEG) { |
| 276 | log("cannot handle unary operator: "+uo.getKind()); |
| 277 | return null; |
| 278 | } |
| 279 | Object arg = evalWrk(uo.getOperand(), lvl); |
| 280 | if (arg == null) return null; |
| 281 | if (arg instanceof Integer) { |
| 282 | return - ((Integer) arg); |
| 283 | } else if (arg instanceof Long) { |
| 284 | return - ((Long) arg); |
| 285 | } else if (arg instanceof Float) { |
| 286 | return - ((Float) arg); |
| 287 | } else if (arg instanceof Double) { |
| 288 | return - ((Double) arg); |
| 289 | } else { |
| 290 | log("cannot handle type "+arg.getClass()+" of value "+arg); |
| 291 | return null; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | protected Object eval(CtBinaryOperator bo, int lvl) { |
| 296 | if (bo.getKind() != BinaryOperatorKind.PLUS) { |
| 297 | log("cannot handle binary operator: "+bo.getKind()); |
| 298 | return null; |
| 299 | } |
| 300 | Object left = evalWrk(bo.getLeftHandOperand(), lvl); |
| 301 | if (left != null && left instanceof String) { |
| 302 | Object right = evalWrk(bo.getRightHandOperand(), lvl); |
| 303 | if (right != null && right instanceof String) { |
| 304 | return ((String) left) + ((String) right); |
| 305 | } |
| 306 | } |
| 307 | return null; |
| 308 | } |
| 309 | |
| 310 | protected Object eval(CtInvocation inv, int lvl) { |
| 311 | if (inv.toString().equals("(getClass().getName())")) { |
| 312 | return determineClass(inv).getCanonicalName(); |
| 313 | } |
| 314 | CtExecutableReference eref = inv.getExecutable(); |
| 315 | Method m = eref.getActualMethod(); |
| 316 | if (marktr.equals(m)) { |
| 317 | List<CtExpression<?>> args = inv.getArguments(); |
| 318 | CtExpression arg = args.get(0); |
| 319 | return this.evalWrk(arg, lvl); |
| 320 | } |
| 321 | log("cannot handle CtInvocation: "+inv); |
| 322 | return null; |
| 323 | } |
| 324 | |
| 325 | protected void log(String msg) { |
| 326 | sb.append(msg + "\n"); |
| 327 | } |
| 328 | } |
| 329 | } |