2

I wonder if there are another ways to find attributes in specific class are non-referenced by other classes (I mean, non used attributes).

My way is like that, for example I have a class like:

public class EABHeaderInformation implements Serializable{
/**
 * 
 */
private static final long serialVersionUID = -4986763088497593972L;

//BargainFinder - AlternateBooking
private int multiTicketSequencdNmbr;
private String resBookDesigCode;
private LocalDateTime departureDate;
private LocalDateTime lastTicketingDate;
private List<String> text;
private String validatingCarrierCode;

public String getValidatingCarrierCode() {
    return validatingCarrierCode;
}
public void setValidatingCarrierCode(String validatingCarrierCode) {
    this.validatingCarrierCode = validatingCarrierCode;
}
public int getMultiTicketSequencdNmbr() {
    return multiTicketSequencdNmbr;
}
public void setMultiTicketSequencdNmbr(int multiTicketSequencdNmbr) {
    this.multiTicketSequencdNmbr = multiTicketSequencdNmbr;
}
public String getResBookDesigCode() {
    return resBookDesigCode;
}
public void setResBookDesigCode(String resBookDesigCode) {
    this.resBookDesigCode = resBookDesigCode;
}
public LocalDateTime getDepartureDate() {
    return departureDate;
}
public void setDepartureDate(LocalDateTime departureDate) {
    this.departureDate = departureDate;
}
public LocalDateTime getLastTicketingDate() {
    return lastTicketingDate;
}
public void setLastTicketingDate(LocalDateTime lastTicketingDate) {
    this.lastTicketingDate = lastTicketingDate;
}
public List<String> getText() {
    return text;
}
public void setText(List<String> text) {
    this.text = text;
}}

It's a simple POJO with getter and setters. I check every getter and setter with 'Open Call Hierarchy' in Eclipse, to find out if the attribute is used by others or not. But it takes a lot of time when I work on bigger classes than this.

So, is there a faster way to do this? Thanks for replies.

  • You can `Ctrl`-`Shift`-`G` over any field or method to find the usage in your workspace, but it's likely going to take the same amount of time. – Mena Aug 09 '16 at 13:50
  • 1
    The problem is that if a field or its setter/getter has higher visibility than private Eclipse doesn't know reliably whether it is used or not (a using project might not have been imported or closed). Thus "find usages" or "open call hierarchy" won't return 100% reliable information (unless _you know_). The next question would be: why do you have that many potentially unused fields in the first place? If you don't need them don't add them. If you don't use them anymore then check for usages when you think they might not be used anymore. – Thomas Aug 09 '16 at 13:50
  • Besides what I wrote above, a simple "hack" would be to comment the getter/setters (and probably the fields as well) and watch for compiler errors (you might need a clean build, i.e. recompile everything, though) ;) – Thomas Aug 09 '16 at 13:54
  • There are a lot of class in this project for mapping xml data. But as I mentioned, plenty of them are not even used. And also I believe that the best solution would be not adding them too, but this code was written before by someone and I'm responsible for refactoring. Thanks for reply :) – Fırat Çağlar Akbulut Aug 10 '16 at 06:18

1 Answers1

1

Eclipse can already create a warning or error for unused private members, but for public ones the Eclipse stance has always been that it's not a valuable feature. I tend to disagree, because many users have a limited scope that would be useful (specifically, all, or a subset of, the projects in the workspace). See this feature request, this one, and this one.

There are some third party options, such as UCDetector and this simple plug-in example.

See also this SO question and the answers.

Community
  • 1
  • 1
E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • With reading all of those links, I've learned that even 'Open Call Hierarchy' is not %100 safe to find unused fields. But I found that little plugin useful, it'll really make my job easier. Thanks! – Fırat Çağlar Akbulut Aug 10 '16 at 06:32