Dynamic in-line dependencie handling

Discussion in 'Plugin Development' started by V10lator, Jul 5, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    V10lator

    Hi,
    currently I'm trying to develop a stripped-down JSON library. This is for a class I want to share but as I don't want to force the users of the class to use this stipped-down library I'm trying to get some kind of dynamic class handling. For that I tried this:
    Code:java
    1. import org.json.JSONArray;
    2. import org.json.JSONException;
    3. import org.json.JSONObject;
    4. import org.json.StrippedJSONArray;
    5. import org.json.StrippedJSONObject;
    6.  
    7. private final boolean strippedJSON;
    8.  
    9. public theConstructor(...)
    10. {
    11. ...
    12. boolean sj = true;
    13. try
    14. {
    15. Class.forName("org.json.StrippedJSONObject");
    16. Class.forName("org.json.StrippedJSONArray");
    17. }
    18. {
    19. try
    20. {
    21. Class.forName("org.json.JSONObject");
    22. Class.forName("org.json.JSONArray");
    23. }
    24. {
    25. throw new Exception("No compatible JSON library found.");
    26. }
    27. sj = false;
    28. }
    29. strippedJSON = sj;
    30. ...
    31. }
    32.  
    33. someMethod()
    34. {
    35. ...
    36. try
    37. {
    38. if(strippedJSON)
    39. {
    40. StrippedJSONObject jo = new StrippedJSONObject(stringBuffer.toString());
    41. StrippedJSONArray ja = jo.getJSONArray("versions");
    42. pluginURL = jo.getString("bukkitdev_link");
    43. jo = ja.getJSONObject(0);
    44. nv = bukkitdevPrefix+jo.getString("name")+bukkitdevSuffix;
    45. turl = jo.getString("dl_link");
    46. tt = jo.getString("type");
    47. }
    48. else
    49. {
    50. JSONObject jo = new JSONObject(stringBuffer.toString());
    51. JSONArray ja = jo.getJSONArray("versions");
    52. pluginURL = jo.getString("bukkitdev_link");
    53. jo = ja.getJSONObject(0);
    54. nv = bukkitdevPrefix+jo.getString("name")+bukkitdevSuffix;
    55. turl = jo.getString("dl_link");
    56. tt = jo.getString("type");
    57. }
    58. }
    59. catch(JSONException e)
    60. {
    61. printStackTraceSync(e, true);
    62. return;
    63. }
    64. ....
    65. }

    Of course this code fails with "Unresolved compilation problems" if one of the libraries isn't present at build time, which is something I want to avoid. I want the class beeing able to build with one of the two libraries and to be able to use the one present at runtime...

    Of course I thought about simply providing my own org.json.*.class files instead of org.json.Stripped*.class but if one plugin would use the original org.json and another one use my org.json they would conflict.

    Any ideas?
     
  2. Offline

    md_5

    Always need both to be able to compile, no two ways about it.
     
Thread Status:
Not open for further replies.

Share This Page