1

I have two functions like this:

String getMessage(String code, Object... objects);
String getMessage(Locale locale, String code, Object... objects);

And I try to call the first function without varags argument:

myClass.getMessage("online.order");

I get this compiler error:

Error: java: incompatible types: java.lang.String cannot be converted to java.util.Locale

It tries to use the second function and I do not understand why?

Update: Context of the code

This is the interface that define those two functions:

public interface MyMessageProvider {
    String getMessage(String code, Object... objects);
    String getMessage(Locale locale, String code, Object... objects);
}

This is the actual class that implements that interface and also extends Spring ReloadableResourceBundleMessageSource

public class CustomMessageSource extends ReloadableResourceBundleMessageSource implements MyMessageProvider {
    public static final String BEAN_NAME = "messageSource";

    public static CustomMessageSource getInstance() {
        return (CustomMessageSource) SpringApplicationContext.getBean(BEAN_NAME);
    }

    @Override
    public String getMessage(String code, Object[] objects) {
        return getMessage(getCurrentLocale(), code, objects);
    }

    @Override
    public String getMessage(Locale locale, String code, Object... objects) {
        return getMessage(code, objects, code, locale);
    }
}

And I call getMessage in a class like this:

public class MyService {
    @Autowired
    private CustomMessageSource messageSource;

    public String createSomeMessage(){
        messageSource.getMessage("online.order");
    }

}

I can solve the issue by adding String getMessage(String code) to the interface/implementation. The compile error does not makes sence to me same as you guys. But, I am getting the compile error!

6
  • 1
    Are you sure your build is up to date? Try doing Project -> Clean, if the same error still appears, edit in some more context. The above code will not produce the error described. Commented Nov 18, 2016 at 18:21
  • 1
    @MattClark, I cleaned the project but still I am getting same compile error. The only solution I could find so far is define a new function String getMessage(String code) and call the first function inside it with null as varags. Commented Nov 18, 2016 at 18:30
  • looks strange, the first method should get invoked, are there other overloaded methods? Commented Nov 18, 2016 at 18:34
  • Whelp, you are going to have to edit in some additional context, I just tried this in my IDE, and am unable to reproduce the issue. Commented Nov 18, 2016 at 18:38
  • I added additional contexts to the question. The code runs in Spring boot framework. Commented Nov 18, 2016 at 19:06

2 Answers 2

3

The signature of the overridden method slightly changes a parent method signature from

public String getMessage(String code, Object... objects)

to

public String getMessage(String code, Object[] objects)

For the compiler, it doesn't matter, it considers Object... as a thing that should be transformed to an Object[] and will make that conversion (after the compile stage is completed, there is no any Object... stuff).

Whereas for us, it does matter. We have to follow a method signature exactly. To invoke the method, you have to pass an array or null there:

instance.getMessage("code", new Object[]{});
instance.getMessage("code", null);
Sign up to request clarification or add additional context in comments.

4 Comments

yes, I found the issue at the same time :) But the compiler did not tell me anything about this issue/
@Arashsoft, It considers Object... and Object[] equal, so the overriding passed without issues.
But this post shows that they are not equal. It is compiler bug :)
@Arashsoft, they are not equal only for us in a syntax light
0

OMG! I found the issue myself. The issue is in the implementation class where I overrided

String getMessage(String code, Object... objects);

with

String getMessage(String code, Object[] objects)

It works fine without any issue but when I want to call getMessage without any varags argument, the override cannot handle it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.