문제

I need some understanding of what I'm running into with an issue. I have a method with the signature addTerm(enum, String, String...) but when use it I get an error that says The method addTerm(Comparison, String, String...) in the type FTSearchBuilder is not applicable for the arguments (Comparison, String, String, String) - I also get the same error if I pass only a single string. Initially, I thought this was a problem with my vararg, but through testing I've found that if I change the initial argument from a Comparison to a String, my method works just fine. However, I have not had issues with passing an enum in other methods. Can anyone explain what is going on and how to fix this?

Code below in case it is useful.

Error

FTSearchBuilder sb = new FTSearchBuilder();
sb.addTerm(Comparison.EQ, "Form", "frmClaim", "frmClaimstub");

Works (with slight modification to addTerm to use String instead of enum)

FTSearchBuilder sb = new FTSearchBuilder();
sb.addTerm("=", "Form", "frmClaim", "frmClaimstub");

Comparison

public enum Comparison {
    LT("<"), LTE("<="), EQ("="), GTE(">="), GT(">");

    private final String text;

    private Comparison(String text) {
        this.text = text;
    }

    public String getText() {
        return this.text;
    }
}

addTerm() (within FTSearchBuilder class)

public class FTSearchBuilder {
private String query;

public FTSearchBuilder() {
    query = "";
}

public String getQuery() {
    return this.query;
}

public void addTerm(Comparison operator, String fieldName, String... values) {
    // Validate input
    String queryTerm = "";
    if (0 == values.length) {
        throw new ArrayIndexOutOfBoundsException("No comparison values given");
    }
    for (int x = 0; x < values.length; x++) {
        if (!StringUtil.isEmpty(values[x])) {
            if (queryTerm.equals("")) {
                queryTerm += "([" + fieldName + "]" + operator.getText() + enquote(values[x]);
            } else {
                queryTerm += " | [" + fieldName + "]" + operator.getText() + enquote(values[x]);
            }
            queryTerm += ")";
        }
    }

    if (!query.equals("")) {
        this.query += "&";
    }
    this.query += queryTerm;
}

public void addDateTerm(Comparison operator, String fieldName, String... values) {
    // Validate input
    String queryTerm = "";
    if (0 == values.length) {
        throw new ArrayIndexOutOfBoundsException("No comparison values given");
    }
    for (int x = 0; x < values.length; x++) {
        if (!StringUtil.isEmpty(values[x])) {
            if (queryTerm.equals("")) {
                queryTerm += "([" + fieldName + "]" + operator.getText() + values[x];
            } else {
                queryTerm += " | [" + fieldName + "]" + operator.getText() + values[x];
            }
            queryTerm += ")";
        }
    }

    if (!query.equals("")) {
        this.query += "&";
    }
    this.query += queryTerm;
}

private String enquote(String s) {
    return "\"" + s + "\"";
}
}
도움이 되었습니까?

해결책

Just for future troubleshooters - the problem isn't in the code. I had been packaging up reusable code into plug-ins for use in other applications, and that included everything here. I still had the local copy of the package and the plug-in had been added to the buildpath so there were two identical copies of the package and classes.

When I changed the signature of the FTSearchBuilder, the compiler was able to tell which one I was using because then they had different signatures and it started working, but when I changed it back it became ambiguous again and flagged an error.

I removed the local copy and everything started working just fine.

다른 팁

I did the following test and worked fine:

public class Test {
    enum Comparison {
        LT("<"), LTE("<="), EQ("="), GTE(">="), GT(">");

        private final String text;

        private Comparison(String text) {
            this.text = text;
        }

        public String getText() {
            return this.text;
        }
    }

    public static void main(String[] args) {
        addTerm(Comparison.EQ, "Form", "frmClaim", "frmClaimstub");
    }

    private static void addTerm(Comparison operator, String fieldName, String... values) {
        System.out.println(operator.toString());
        System.out.println(fieldName);

        if (values != null && values.length > 0){
            for (String v : values) {
                System.out.print(v + " ");
            }
        }

        System.out.println();
        System.out.println();
    }
}

Outputs:

EQ
Form
frmClaim frmClaimstub 

Could you please send us the stacktrace?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top