Groovyc: [Static type checking] - Cannot find matching method io.swagger.client.util.EmUtil#addLobList(java.lang.Object, java.lang.Object). Please check if the declared type is correct and if the method exists.
I am calling this function in a static block in groovy, as shown below:
static {
Arrays.asList(LOBEnum.values()).forEach {lob -> EmUtil.getInstance().addLobList(lob.name(), lob.getLob())}
EmUtil.getInstance().setPrefix("CCB_Reference_Data_")
EmUtil.getInstance().init()
}
This calls a java function, as shown below:
public void addLobList(String lob, String licenseLob) {
lobList.add(lob);
lobLicenseList.add(licenseLob);
}
It looks like lob.name() and lob.getLob() each have a return type of Object. If you want to use the return values of those methods as parameters to your addLobList method, the static type checker needs to make sure those are String instance. You can cast those with (String). Alternatively, you could change the return value of those methods to String.
Related
I have a method that parses a String and converts it to a boolean.
The legal values are "true" and "false".
boolean convertStringToBoolean(String text) {
if (text.equals("true") {
return true;
} else if (text.equals("false")) {
return false;
} else {
throw new IllegalArgumentException(text);
}
}
When I use this variable, I get a naming problem.
void doSomething(String isSpecificReadString) {
boolean isSpecificRead = convertStringToBoolean(isSpecificReadString);
...
}
The problem is that the parameter carries a meaning that I want to keep in its name.
The meaning doesn't change just because the type is changed.
So far, my solution has been to just suffix the type to the parameter.
But I don't like this solution.
What should I call the variables to solve this problem?
So far, my solution has been to just suffix the type to the parameter.
But I don't like this solution.
Strongly typed language as Java works in this way.
Variables cannot change their type at runtime.
You have to accept the language constraints.
Converted a data from a type into another type within a method may create this issue.
But your actual solution is rather fine.
The intention is clear and you kept a natural name for the target variable.
You indeed added the type suffix only for the input that will be converted (temporary/intermediary variable), not for the output that will be used in the next statement(s).
As inspired by Andy Turner, I can make two doSomething methods.
A method that takes a String and another that takes a boolean.
The String method can then call the boolean method.
As follows:
void doSomething(String isSpecificRead) {
doSomething(convertStringToBoolean(isSpecificReadString));
}
void doSomething(boolean isSpecificRead) {
...
}
I'm basically trying to create a static method that will serve as a wrapper for any method I pass and will execute something before and after the actual execution of the method itself. I'd prefer to do it using Java 8 new coding style. So far I have a class that has a static method, but I'm not sure what the parameter type should be so it can take any method with any type of parameter and then execute it. Like i mentioned I want to do some stuff before and after the method executes.
For example: executeAndProcess(anyMethod(anyParam));
Your method can accept a Supplier instance and return its result:
static <T> T executeAndProcess(Supplier<T> s) {
preExecute();
T result = s.get();
postExecute();
return result;
}
Call it like this:
AnyClass result = executeAndProcess(() -> anyMethod(anyParam));
How can I call a method that is passed on through a parameter in a method in Java 8?
Small example I have a method like that:
void output(String text) {
System.out.println(text)
}
Now I want to pass that method to another class which should call output and set something for text. Is that possible?
The class you want to pass the function to must take a parameter of type Consumer<String>. This class represents a function that takes a parameter of some type (String in this case), and has return type void. A Consumer has a method accept that takes the parameter and calls the function.
You can create you class like this:
class Test {
Test(Consumer<String> consumer) {
consumer.accept("This is a string!");
}
}
Now, when you want to instantiate this class, you need to pass your function to it like this:
Test t = new Test(this::output);
The :: notation is called a method reference. The this (before the colons) means that the method is located in the object you're in. It can be changed to, for example, MyClass::output if it is a static method on MyClass, or myObject::output if it is a method on the object myObject.
public class Curiosity {
public void toString()//error because of this specific method name
{
System.out.println("method is successfully implemented");
}
}
How can i use a method of the same name "toString()" if i want to ?
Do I have to give its return type as String if not what should i do to change its return type like suppose if i want to use a void return type for toString does java allow that ?
toString() method must return a String. That's the only way to override Object's toString().
public String toString()
{
return "method is successfully implemented";
}
If you wish to use the same name but not override Object's toString, you can overload the name toString by adding arguments, thus changing the signature of your method.
Example :
public void toString (String something)
{
System.out.println("method is successfully implemented " + something);
}
You are trying to overload toString() method in a wrong manner
Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample, draw(String s) and draw(int i) are distinct and unique methods because they require different argument types.
You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
The only way to use toString() in your class is by keeping the return type as String
public String toString()
{
//your code here
}
That is how it is defined in Objectclass and if you wish to override it you will have to use the exact signature
or if you still wish to use the method name as toString what you can do is change the method's signature.
A method's signature includes method's name and the parameters.
Remember that return type is not a part of a method's signature
You can look at the source code of the java.lang.Object.
The toString method have a return value in String type. You can't have another method which's name is toString but return type is not String.
Actually, it's forbidden in Java in any inheritance relationship. When you call the method, the compiler only cares the name and the parameters. So how can it distinguishes the methods of the same name but with the different return type?
This does not work
public String getjLabel4text(void){...
But this does
public String getjLabel4text(){...
Why so? I am not taking any arguments so shouldn't I write void there? Why is it causing an error?
More generally, method declarations have six components, in order:
Modifiers—such as public, private, etc.
The return type—the data type of the value returned by the method, or void if the method does not return a value.
The method name—the rules for field names apply to method names as well, but the convention is a little different.
The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
An exception list—to be discussed later.
The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.
Oracle Documentation
Java is not like C/C++ You do not need to put void parameter on a function with no parameters, and it throws an error because it is not a valid parameter definition as in java you can not have parameters of type void.
In C and C++ we can take void as an method argument because void is a datatype in both c and c++ . But Java is different, Here void is not a datatype rather its a keyword that is used just to signify method will not return any argument. so taking void as argument is technically invalid and is of no use, this () itself is sufficient.
Ex.
public String Name() { return "Hello";}
and
public String Name(void){ return "Hello";}
In C : Name() will take unspecified no of argument for unspecified type and Name(void) will take no argument.
In C++ : Both Name() and Name(void) will take no argument.
In Java : Name() will work while Name(void) will return compilation error.
There is no need to explicitly mark method as no argument in Java.
You just omit any argument definition and there it is, a method with no arguments.
There is, though a need for return type for any method. It's where void gets in.
void is just for specifying a return value. Declaration of a return type is required for the syntax to work correctly. Since the argument list length is variable, having none is acceptable. void is a keyword, and is not allowed in the argument list.
A method can return a value or not.
public void doSomthing(String text) {
print text;
}
This don't return any value but executes and returns.
public String doSomthing(String text) {
return text;
}
This method returns the text.
You can't input a void.