在组合关系中查找特定的调用方


class MyDate{
Calendar effectiveDate, cancelDate;
//Getter and setter
}
class Member{
private String fName, lName
private MyDate date;
//getter setters
}
class Policy{
private int policyId
private MyDate date;
//getter setters
}
class Address{
private int addressType
private MyDate date;
//getter setters
}

我有更多的类,如合同、付款、地址等,都参考了日期类

然后我有多个调用setEffectiveDate((的服务

Service1{
member.getDate().setEffectiveDate(effDate);
}
Service2{
policy.getDate().setEffectiveDate(effDate);
}
Service3{
contract.getDate().setEffectiveDate(effDate);
address.getDate().setEffectiveDate(addressEffDate);
}

我想在 setEffectiveDate(( 中设置一个调试指针,以便它仅在调用 setEffectiveDate(( 时暂停执行,例如地址对象。

我尝试使用 Thread.currentThread((.getStackTrace((,但它给了我调用服务类(在本例中为 Service3(的名称,但没有调用该方法的实际父对象(地址(。 注意:使用 Java 7、RSA/Eclipse

现在我正在使用一种非常繁琐的方法,如下所示

class MyDate{
Calendar effectiveDate, cancelDate;
//Getter and setter
public String source; //This is temporary variable I introduce just for debugging purpose
public setEffectiveDate(Calendar effectiveDate){
this.effectiveDate = effectiveDate;
/* This is temporary code for debugging */
if(this.source.equals("address"){
System.out.println("Called from address"); //Set a debug pointer on this line
}
}
}

然后稍后在地址类中,我设置源="地址">

class Address{
private int addressType
private MyDate date;
public getDate(){
this.date.source = "address"; //Temporary code just for debugging
return date;
}
}

任何帮助将不胜感激

首先,不鼓励使用名称 Date,因为任何开发人员都会认为它是标准的 Java Date 类,而不是您的自定义类。我建议您考虑重命名它。
其次,您可以为断点添加条件。在 Eclipse 中,勾选条件复选框,然后您可以添加一些返回布尔值的内容,例如 this.source.equals("address"(,并且只有在满足该条件时才会停止。

最新更新