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

问题描述 投票:0回答:1
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()时才会挂起执行,比如说Address对象。

我尝试使用Thread.currentThread()。getStackTrace()但它给我调用Service类的名称(本例中为Service3),但不是调用该方法的实际父Object(Address)。注意:使用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
        }
    }
}

然后在Address类中我设置source =“address”

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

任何帮助将不胜感激

java debugging composition
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.