JSONObject deserialization cann call methods now

Currently the only supported parameter is String
This commit is contained in:
7u83 2018-12-01 09:30:36 +01:00
parent abee1a8d36
commit bdb7ae4992
6 changed files with 45 additions and 47 deletions

View File

@ -1,4 +1,4 @@
#Sat, 01 Dec 2018 03:52:02 +0100
#Sat, 01 Dec 2018 09:27:21 +0100
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=

View File

@ -192,51 +192,16 @@ public class AssetEditorDialog extends EscDialog {
}//GEN-LAST:event_cancelButtonActionPerformed
static public boolean runDialog(Window parent, JSONObject o) {
//JSONObject jo = new org.json.JSONObject(parent, new String[]{"getMyName"});
//System.out.printf("PARENT: %s", jo.toString());
// JSONObject.
static public boolean runDialog(Window parent, JSONObject o, JSONObject all) {
AssetEditorDialog d = new AssetEditorDialog(parent);
Json.put(d.assetEditorPanel, o);
if (o!=null)
Json.put(d.assetEditorPanel, o);
d.setLocationRelativeTo(parent);
d.setVisible(true);
d.dispose();
d.assetEditorPanel.symField.setText("Hallo");
//d.assetEditorPanel.symField.setText("Herr");
JSONObject jo = Json.get(d.assetEditorPanel);
System.out.printf("Resulting JSONN %s\n", jo.toString(5));
//Class aClass = d.assetEditorPanel.getClass().getDeclaredFields();
Field[] fields = d.assetEditorPanel.getClass().getFields();
for (Field f : fields) {
Export ex = f.getAnnotation(Export.class);
System.out.printf("Fieldname: %s\n",f.getName());
if (ex == null){
continue;
}
System.out.printf("EX: %s\n", ex.getClass().getName());
}
return true;
}

View File

@ -126,6 +126,11 @@ public class AssetEditorPanel extends javax.swing.JPanel {
@Export
public String hallo = "hello";
@Import("type")
public void putType(String type){
System.out.printf("Here we have a type: %s\n", type);
}
public JDialog dialog;

View File

@ -202,7 +202,7 @@ public class AssetListDialog extends EscDialog {
private void doEdit() {
JSONObject o = assetListPanel.getSelectedObject();
System.out.printf("JON: %s",o.toString(4));
AssetEditorDialog.runDialog(this, o);
AssetEditorDialog.runDialog(this, o,null);
}
private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed

View File

@ -52,7 +52,7 @@ public class AssetListPanel extends javax.swing.JPanel implements GuiSelectionLi
return;
}
json_set = new JSONObject(Globals.prefs.get("myassets", "{EUR:{name:Euro,decimals:8}}"));
json_set = new JSONObject(Globals.prefs.get("myassets", "{EUR:{name:Euro,decimals:8,type:Curreny}}"));
reload();
assetTable.setRowSelectionAllowed(true);
@ -102,7 +102,8 @@ public class AssetListPanel extends javax.swing.JPanel implements GuiSelectionLi
m.addRow(new Object[]{
o.opt("id"),
symbol,
o.opt("name")
o.opt("name"),
o.opt("type")
});
}

View File

@ -30,6 +30,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextField;
@ -89,19 +91,18 @@ public class Json {
/**
* Inverse to get
*
* @param o Object
* @param jo JSONObject
*/
public static void put(Object o, JSONObject jo) {
Field[] fields = o.getClass().getFields();
for (Field f : fields) {
System.out.printf("ANNOT: %s\n",f.getName());
Import imp = f.getAnnotation(Import.class);
if (imp == null) {
continue;
}
Class cls = f.getType();
if (JTextField.class.isAssignableFrom(cls)) {
try {
@ -111,6 +112,32 @@ public class Json {
} catch (IllegalArgumentException | IllegalAccessException ex1) {
Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, ex1);
}
continue;
}
}
Method[] methods = o.getClass().getMethods();
for (Method m : methods) {
Import imp = m.getAnnotation(Import.class);
if (imp == null) {
continue;
}
if (m.getParameterCount() != 1) {
Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, "Wrong pcouunt");
continue;
}
String name = null == imp.value() ? m.getName() : imp.value();
Class p0 = m.getParameterTypes()[0];
if (String.class.isAssignableFrom(p0)){
String param = jo.optString(name, "");
try {
m.invoke(o, param);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, ex);
}
}
}