2016-02-26 45 views
0

我需要你的幫助來理解爲什麼在我的方法中,對象分配返回堆棧溢出錯誤。Java - 爲什麼我的JSON方法中的stackoverflow錯誤?

這裏是方法:

public String toJson(){ 

     JSONObject json; 
     json = new JSONObject(this); //error happens here... 

     return json.toString(); 
    } 

在這裏從這個類的所有的代碼:

package com.neocloud.model; 

import java.io.Serializable; 

import com.amazonaws.util.json.JSONObject; 
import com.neocloud.amazon.AwsSns; 
import com.neocloud.model.dao.DeviceDAO; 
import com.neocloud.model.dao.GroupDAO; 
import com.neocloud.model.dao.SubscriptionDAO; 

public class Subscription implements Serializable{ 

    private static final long serialVersionUID = -3901714994276495605L; 
    private long id; 
    private Device dispositivo; 
    private Group grupo; 
    private String subscription; 


    public Subscription(){ 

     clear(); 

    } 

    public void clear(){ 

     id = 0; 
     subscription = ""; 
     grupo = new Group(); 
     dispositivo = new Device(); 

    } 

    public String toJson(){ 

     JSONObject json; 
     json = new JSONObject(this); //error happens here... 

     return json.toString(); 
    } 


    public String registerDB(boolean registrarSNS){ 

     GroupDAO gDAO = new GroupDAO(); 
     DeviceDAO dDAO = new DeviceDAO(); 

     gDAO.selectById(this.grupo); 
     dDAO.selectById(this.dispositivo); 

     if (registrarSNS) 
      registerSNS(this.dispositivo.getEndPointArn(), this.grupo.getTopicArn()); 

     SubscriptionDAO sDAO = new SubscriptionDAO(); 
     return sDAO.insert(this); 
    } 

    public String registerSNS(String endpointArn, String topicArn){ 

     String resposta; 
     AwsSns sns = AwsSns.getInstance(); 
     resposta = ""+sns.addSubscriptionToTopic(endpointArn, topicArn); 
     setSubscription(resposta); 

     return resposta;   
    } 

    public String delete(){ 

     AwsSns sns = AwsSns.getInstance(); 
     SubscriptionDAO sDAO = new SubscriptionDAO(); 
     sDAO.selectById(this); 

     sns.deleteSubscriptionFromTopic(this.subscription); 

     return sDAO.delete(this); 

    } 


    public long getId() { 
     return id; 
    } 

    public void setId(long id) { 
     if (this.id != id) 
      this.id = id; 
    } 

    public String getSubscription() { 
     return subscription; 
    } 

    public void setSubscription(String subscription) { 
     if (subscription != null){ 
      if (!this.subscription.equals(subscription)) 
       this.subscription = subscription; 
     }else 
      this.subscription = new String(); 
    } 

    public Device getDispositivo() { 
     return dispositivo; 
    } 

    public void setDispositivo(Device dispositivo) { 
     if (dispositivo != null){ 
      if (dispositivo.getId() != this.dispositivo.getId()) 
       this.dispositivo = dispositivo; 
     }else 
      this.dispositivo = new Device(); 
    } 

    public Group getGrupo() { 
     return grupo; 
    } 

    public void setGrupo(Group grupo) { 
     if (grupo != null){ 
      if (grupo.getId() != this.grupo.getId()) 
       this.grupo = grupo; 
     }else 
      this.grupo = new Group(); 
    } 

} 

這裏是設備類:

package com.neocloud.model; 

import java.io.Serializable; 
import java.util.ArrayList; 

import com.amazonaws.util.json.JSONException; 
import com.amazonaws.util.json.JSONObject; 
import com.neocloud.amazon.AwsSns; 
import com.neocloud.model.dao.DeviceDAO; 
import com.neocloud.model.enums.EEnvironment; 
import com.neocloud.model.enums.EProduct; 
import com.neocloud.model.enums.ESystem; 

public class Device implements Serializable{ 


    private static final long serialVersionUID = 8515474788917476721L; 
    private long id; 
    private String development; 
    private String SSL = "tls://gateway.push.apple.com:2195"; 
    private String feedback = "tls://feedback.push.apple.com:2196"; 
    private String sandboxSSL = "tls://gateway.sandbox.push.apple.com:2195"; 
    private String sandboxFeedback = "tls://feedback.sandbox.push.apple.com:2196"; 
    private String message; 
    private String endPointArn; 
    private String deviceToken; 
    private EProduct appID; 
    private String appVersion; 
    private String deviceUID; 
    private String deviceName; 
    private String deviceModel; 
    private String deviceVersion; 
    private boolean pushBadge; 
    private boolean pushAlert; 
    private boolean pushSound; 
    private ESystem system; 
    private EEnvironment environment; 
    private ArrayList<Subscription> subscriptions; 
    private String deviceUser; 


    public Device(){ 

     clear(); 

    } 

    public String deleteToken(){ 
     /* 
     * 
     * terminar aqui ainda... 
     * 
     DeviceDAO dDAO = new DeviceDAO(); 

     AwsSns sns = AwsSns.getInstance(); 

     sns.deleteDeviceEndpoint(getEndPointArn()); 

     ArrayList<String> subscriptions = dDAO.getSubscriptionsFromEndpointArn(this); 

     for (int i=0; i<subscriptions.size();i++){ 

      sns.deleteSubscriptionFromTopic(subscriptions.get(i)); 

     } 

     return dDAO.delete(this); 
     */ return ""; 
    } 

    public String updateToken(){ 

     if ((system == ESystem.IOS) && (deviceToken.length() != 64)) 
      return "deviceTokenlen64"; 

     AwsSns sns = AwsSns.getInstance(); 
     String jsonResposta = ""+sns.updateDeviceEndpoint(getEndPointArn(), deviceToken); 

     DeviceDAO dDAO = new DeviceDAO(); 
     dDAO.update(this); 

     return jsonResposta; 

    } 

    public String registerDevice(){ 

     if (appVersion.length() == 0) 
      return "applen0"; 
     if (deviceUID.length() > 40) 
      return "deviceUID40"; 
     if ((system == ESystem.IOS) && (deviceToken.length() != 64)) 
      return "deviceTokenlen64"; 
     if (deviceName.length() == 0) 
      return "deviceNamelen0"; 
     if (deviceModel.length() == 0) 
      return "deviceModellen0"; 
     if (deviceVersion.length() == 0) 
      return "deviceVersionlen0"; 

     AwsSns sns = AwsSns.getInstance(); 
     String jsonResposta = null; 

     switch (getAppID()) { 
      case MODULE: { 
       if (system == ESystem.IOS) 
        jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTMODULEIOS); 
       else if (system == ESystem.ANDROID) 
        jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTMODULEANDROID); 
       break; 

      } 
      case HOSTPRO: { 
       if (system == ESystem.IOS) 
        jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTHOSTPROIOS); 
       else if (system == ESystem.ANDROID) 
        jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTHOSTPROANDROID);     
       break; 

      } 
      case CONNEXOON: { 
       if (system == ESystem.IOS) 
        jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTCONNEXOONIOS); 
       else if (system == ESystem.ANDROID) 
        jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTCONNEXOONANDROID); 
       break; 

      } 
      case TAHOMA: { 

       break; 
      } 
      case MINIBOX: { 

       break; 
      } 
      case NONE: { 

      } 
      default: { 

      } 
     } 


     if (jsonResposta != null){ 

      JSONObject json; 
      try { 
       jsonResposta = jsonResposta.replace("}", "\"}").replace("arn", "\"arn"); 
       jsonResposta = jsonResposta.replace("{EndPointArn:", "{\"EndPointArn\":"); 
       json = new JSONObject(jsonResposta); 
       setEndPointArn(json.getString("EndpointArn")); 
      } catch (JSONException e) { 

       e.printStackTrace(); 
      } 

     } 

     Subscription subscription; 

     switch (getAppID()){ 
      case MODULE: { 

       subscription = new Subscription(); 
       subscription.getGrupo().setId(19); 
       subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICMODULEGERAL); 
       subscriptions.add(subscription); 


       if (system == ESystem.IOS){ 

        subscription = new Subscription(); 
        subscription.getGrupo().setId(25); 
        subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICMODULEIOS); 
        subscriptions.add(subscription); 

       } 
       else if (system == ESystem.ANDROID){ 

        subscription = new Subscription(); 
        subscription.getGrupo().setId(22); 
        subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICMODULEANDROID); 
        subscriptions.add(subscription); 

       } 
       break; 

      } 
      case HOSTPRO: { 

       subscription = new Subscription(); 
       subscription.getGrupo().setId(10); 
       subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICHOSTPROGERAL); 
       subscriptions.add(subscription); 

       if (system == ESystem.IOS){ 

        subscription = new Subscription(); 
        subscription.getGrupo().setId(16); 
        subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICHOSTPROIOS); 
        subscriptions.add(subscription); 

       } 
       else if (system == ESystem.ANDROID){ 

        subscription = new Subscription(); 
        subscription.getGrupo().setId(13); 
        subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICHOSTPROANDROID); 
        subscriptions.add(subscription); 

       } 
       break; 

      } 
      case CONNEXOON: { 

       subscription = new Subscription(); 
       subscription.getGrupo().setId(1); 
       subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICCONNEXOONGERAL); 
       subscriptions.add(subscription); 

       if (system == ESystem.IOS){ 

        subscription = new Subscription(); 
        subscription.getGrupo().setId(7); 
        subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICCONNEXOONIOS); 
        subscriptions.add(subscription); 

       } 
       else if (system == ESystem.ANDROID){ 

        subscription = new Subscription(); 
        subscription.getGrupo().setId(4); 
        subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICCONNEXOONANDROID); 
        subscriptions.add(subscription); 

       } 
       break; 

      } 
      case TAHOMA: { 

       break; 
      } 
      case MINIBOX: { 

       break; 
      } 
      case NONE: { 

      } 
      default: { 

      } 
     } 

     DeviceDAO dDAO = new DeviceDAO(); 
     return dDAO.insert(this); 

    } 


    public void clear(){ 

     development = ""; 
     message = ""; 
     endPointArn = ""; 
     deviceToken = ""; 
     appID = EProduct.NONE; 
     appVersion = ""; 
     deviceUID = ""; 
     deviceName = ""; 
     deviceModel = ""; 
     deviceVersion = ""; 
     pushBadge = false; 
     pushAlert = false; 
     pushSound = false; 
     system = ESystem.NONE; 
     environment = EEnvironment.HOMOLOGATION; 
     subscriptions = new ArrayList<Subscription>(); 
     deviceUser = ""; 

    } 

    public String toJson(){ 

     JSONObject json; 
     json = new JSONObject(this); 

     return json.toString(); 

    } 


    public String getDevelopment() { 
     return development; 
    } 

    public void setDevelopment(String development) { 
     if (development != null){ 
      if (!development.equals(this.development)) 
       this.development = development; 
     } 
     this.development = new String(); 
    } 

    public String getSSL() { 
     return SSL; 
    } 

    public void setSSL(String sSL) { 
     if (sSL != null){ 
      if (!sSL.equals(this.SSL)) 
       this.SSL = sSL; 
     } 
     else 
      this.SSL = new String(); 
    } 

    public String getFeedback() { 
     return feedback; 
    } 

    public void setFeedback(String feedback) { 
     if (feedback != null){ 
      if (!feedback.equals(this.feedback)) 
       this.feedback = feedback; 
     } 
     this.feedback = new String(); 
    } 

    public String getSandboxSSL() { 
     return sandboxSSL; 
    } 

    public void setSandboxSSL(String sandboxSSL) { 

     if (sandboxSSL != null){ 
      if (!sandboxSSL.equals(this.sandboxSSL)) 
       this.sandboxSSL = sandboxSSL; 
     }else  
      this.sandboxSSL = new String(); 
    } 

    public String getSandboxFeedback() { 
     return sandboxFeedback; 
    } 

    public void setSandboxFeedback(String sandboxFeedback) { 
     if (sandboxFeedback != null){ 
      if (!sandboxFeedback.equals(this.sandboxFeedback)) 
       this.sandboxFeedback = sandboxFeedback; 
     }else 
      this.sandboxFeedback = new String(); 
    } 

    public String getMessage() { 
     return message; 
    } 

    public void setMessage(String message) { 
     if (message != null){ 
      if (!message.equals(this.message)) 
       this.message = message; 
     }else 
      this.message = new String(); 
    } 

    public String getEndPointArn() { 
     return endPointArn; 
    } 

    public void setEndPointArn(String createPlatformEndpointResult) { 
     if (createPlatformEndpointResult != null){ 
      if (!createPlatformEndpointResult.equals(this.endPointArn)) 
       this.endPointArn = createPlatformEndpointResult; 
     }else 
      this.endPointArn = new String(); 
    } 

    public String getDeviceToken() { 
     return deviceToken; 
    } 

    public void setDeviceToken(String deviceToken) { 
     if (deviceToken != null){ 
      if (!deviceToken.equals(this.deviceToken)) 
       this.deviceToken = deviceToken; 
     }else 
      this.deviceToken = new String(); 
    } 

    public EProduct getAppID() { 
     return appID ; 
    } 

    public void setAppID(EProduct appID){ 
     if (appID != this.appID) 
      this.appID = appID; 
    } 

    public String getAppVersion() { 
     return appVersion; 
    } 

    public void setAppVersion(String appVersion) { 
     if (appVersion != null){ 
      if (!appVersion.equals(this.appVersion)) 
       this.appVersion = appVersion; 
     }else 
      this.appVersion = new String(); 
    } 

    public String getDeviceUID() { 
     return deviceUID; 
    } 

    public void setDeviceUID(String deviceUID) { 
     if (deviceUID != null){ 
      if (!deviceUID.equals(this.deviceUID)) 
       this.deviceUID = deviceUID; 
     }else 
      this.deviceUID = new String(); 
    } 

    public String getDeviceName() { 
     return deviceName; 
    } 

    public void setDeviceName(String deviceName) { 
     if (deviceName != null){ 
      if (!deviceName.equals(this.deviceName)) 
       this.deviceName = deviceName; 
     }else 
      this.deviceName = new String(); 
    } 

    public String getDeviceModel() { 
     return deviceModel; 
    } 

    public void setDeviceModel(String deviceModel) { 
     if (deviceModel != null){ 
      if (!deviceModel.equals(this.deviceModel)) 
       this.deviceModel = deviceModel; 
     }else 
      this.deviceModel = new String(); 
    } 

    public String getDeviceVersion() { 
     return deviceVersion; 
    } 

    public void setDeviceVersion(String deviceVersion) { 
     if (deviceVersion != null){ 
      if (!deviceVersion.equals(this.deviceVersion)) 
       this.deviceVersion = deviceVersion; 
     }else 
      this.deviceVersion = new String(); 
    } 

    public boolean isPushBadge() { 
     return pushBadge; 
    } 

    public void setPushBadge(boolean pushBadge) { 
     if (pushBadge != this.pushBadge) 
      this.pushBadge = pushBadge; 
    } 

    public boolean isPushAlert() { 
     return pushAlert; 
    } 

    public void setPushAlert(boolean pushAlert) { 
     if (pushAlert != this.pushAlert) 
      this.pushAlert = pushAlert; 
    } 

    public boolean isPushSound() { 
     return pushSound; 
    } 

    public void setPushSound(boolean pushSound) { 
     if (this.pushSound != pushSound) 
      this.pushSound = pushSound; 
    } 

    public ESystem getSystem() { 
     return system; 
    } 

    public void setTipoSmartphone(ESystem system) { 
     if (system != this.system) 
      this.system = system; 
    } 

    public EEnvironment getEnvironment() { 
     return environment; 
    } 

    public void setEnvironment(EEnvironment environment) { 
     if (environment != this.environment) 
      this.environment = environment; 
    } 

    public long getId() { 
     return id; 
    } 

    public void setId(long id) { 
     if (this.id != id) 
      this.id = id; 
    } 

    public void setSystem(ESystem system) { 
     if (system != this.system) 
    this.system = system; 
    } 

    public ArrayList<Subscription> getSubscriptions() { 
     return subscriptions; 
    } 

    public void setSubscriptions(ArrayList<Subscription> subscriptions) { 
     if (subscriptions != null) 
      this.subscriptions = subscriptions; 
     else 
      this.subscriptions = new ArrayList<Subscription>(); 
    } 

    public String getDeviceUser() { 
     return deviceUser; 
    } 

    public void setDeviceUser(String deviceUser) { 
     if (deviceUser != null){ 
      if (!deviceUser.equals(this.deviceUser)) 
       this.deviceUser = deviceUser; 
     }else 
      this.deviceUser = new String(); 
    } 



} 

這裏是Group類:

package com.neocloud.model; 

import java.io.Serializable; 
import java.util.ArrayList; 

import com.amazonaws.util.json.JSONException; 
import com.amazonaws.util.json.JSONObject; 
import com.neocloud.amazon.AwsSns; 
import com.neocloud.model.dao.GroupDAO; 

public class Group implements Serializable{ 


    private static final long serialVersionUID = -5032857327241801763L; 
    private long id; 
    private String nome; 
    private String topicArn; 

    public Group(){ 
     this.clear(); 
    } 

    public void clear(){ 
     this.id = 0; 
     this.nome = new String(); 
     this.topicArn = new String(); 
    } 

    public String registerGrupo(){ 

     AwsSns snsClient = AwsSns.getInstance(); 

     String topic = ""+snsClient.createTopic(this.nome); 

     JSONObject json; 
     String jsonResposta = topic; 
     try { 
      jsonResposta = jsonResposta.replace("}", "\"}").replace("arn", "\"arn"); 
      jsonResposta = jsonResposta.replace("{TopicArn:", "{\"TopicArn\":"); 
      json = new JSONObject(jsonResposta); 
      setTopicArn(json.getString("TopicArn")); 
     } catch (JSONException e) { 

      e.printStackTrace(); 
     } 

     GroupDAO grupoDAO = new GroupDAO(); 
     return grupoDAO.insert(this); 

    } 

    public String deleteGrupo(){ 
     AwsSns snsClient = AwsSns.getInstance(); 

     GroupDAO grupoDAO = new GroupDAO(); 
     ArrayList<Group> grupos = grupoDAO.select(this); 

     if (grupos.size() > 0){ 

      for (int i = 0; i<grupos.size(); i++){ 
       this.topicArn = grupos.get(i).getTopicArn(); 
       this.nome = grupos.get(i).getNome(); 
       this.id = grupos.get(i).getId(); 
       snsClient.deleteTopic(this.topicArn); 

      } 

      return grupoDAO.delete(this); 

     } 

     return "-1"; 
    } 

    public String toJson(){ 

     JSONObject json; 
     json = new JSONObject(this); 

     return json.toString(); 

    } 

    public long getId() { 
     return id; 
    } 
    public void setId(long id) { 
     this.id = id; 
    } 
    public String getNome() { 
     return nome; 
    } 
    public void setNome(String nome) { 
     this.nome = nome; 
    } 
    public String getTopicArn() { 
     return topicArn; 
    } 
    public void setTopicArn(String topicArn) { 
     this.topicArn = topicArn; 
    } 

} 

這裏是Tomcat的日誌:

在 java.lang.reflect.Executable(來源不明)異常在線程 「HTTP-BIO-8080-EXEC-5」 java.lang.StackOverflowError的在 java.lang.reflect.Method。(Unknown Source)at java.lang.reflect.Method.copy(Unknown Source)at java.lang.reflect.ReflectAccess.copyMethod(Unknown Source)at sun.reflect.ReflectionFactory .copyMethod(Unknown Source) java.lang.Class.copyMethods(Unknown Source)at java.lang.Class.getMethods(Unknown Source)at(com.amazonaws.util.json.JSONObject.JSONObject.java:285) com.amazonaws.util.json.JSONObject。包裹(JSONObject.java:1540)在 com.amazonaws.util.json.JSONObject.populateMap(JSONObject.java:960)

感謝所有幫助!

+2

你嘗試過調試應用程序? –

+0

你可以看到更多的堆棧跟蹤,你可以看到正在重複的部分? –

回答

1
json = new JSONObject(this); 

你試圖JSONify這個

id = 0; 
subscription = ""; 
grupo = new Group(); 
dispositivo = new Device(); 

,我認爲在某種程度上,它不能JSONify GroupDevice。你能告訴我們這些類的代碼嗎?

更新:在您的Subscription.clear()方法中,您會影響new Group() & a new Device()。創建new Device()時,將調用方法Device.clear()並創建new ArrayList<Subscription>(),這將調用方法Subscription.clear(),這將影響new Group() & a new Device()等等。你無限循環,這就是爲什麼你得到StackOverflow錯誤。這是奇怪的,你訂閱是由一個由訂閱的ArrayList組成的,也由組等組成的。

+0

嗨Jorel。我已經發布了你想要的課。 –

+0

@ pedro.olimpio更新,檢查你是否可以解決你的模型 –

+0

感謝您的幫助@Jorel我在這裏更改循環引用...非常感謝。 –

1

也許在您的對象的循環引用。

0

也許你有一個循環引用...

Object a = X; 
Object c = Y; 

a.prop = Y; 
c.prop2 = X; 

那麼,你的JSON(a)是

{ 
    prop : { 
     prop2 : { 
     prop : Y (Y have prop2 again) 
     /*infinite..*/ 
     } 
    } 
} 
相關問題