Calling method from a component by its name in a string [duplicate] - java

This question already has answers here:
How do I invoke a Java method when given the method name as a string?
(23 answers)
Closed 10 years ago.
I just want to know if there is any way to call a method from on an object by its name in a String.
Something like this
setLocation(int,int) it's a method from jLabel1, how can i call this
method(setLocation()) by using its name in string ?
String component = jLabel1.getName();
component.setLocation(x,y);

Sure, you can use the Java reflection API
This SO question may helps you.

Related

Why is it returning address instead of object? [duplicate]

This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 5 months ago.
Comparator<Employee> comparator =Comparator.comparing(Employee::getIncome);
Employee minObj= Employee.persons().stream()
.min(comparator)
.get();
This is returning me address instead of an object.
toMap.Employee#66133adc
What is the problem and how to write it correctly?
It probably isn't—you have forgotten to override toString().

Null pointer exception inside Optional.ofNullable() [duplicate]

This question already has answers here:
How to convert Optional<Object> to Optional<String> [closed]
(3 answers)
Closed 2 years ago.
I am trying to convert a set to string inside Optional.OfNullable method like:
test.setAbc(Optional.ofNullable(rule.getSampleSet().toString()).orElse(null));
but if sampleSet is null it will give me a NullPointerException.
can anyone tell me how to resolve this issue using .map method with Optional?
I know one traditional way of doing it by checking nullability beforehand:
if(rule.getSampeSet != null)
but I am much interested in knowing if we can do it in one line.
Instead of calling toString() inside the ofNullable, you could map the optional to it:
test.setAbc(Optional.ofNullable(rule.getSampleSet()).map(Object::toString).orElse(null));

Store a method's name in a variable and call that method using that variable [duplicate]

This question already has answers here:
How do I invoke a Java method when given the method name as a string?
(23 answers)
Closed 6 years ago.
I want to call a method whose name (with or without parentheses and parameters) is stored in a string variable and call this method using this variable.
Until now I have found that in Java if you do this:
String str = "func();";
System.out.println(str);
This would call method func(). But I don't think this is recommended.
So, my question is:
Is it okay to do this?
Is there any other way to do this?
Thanks.
Method m = YourClass.class.getMethod("methodName", parametersOfYourMethods);

Java - Passing and executing a function [duplicate]

This question already has answers here:
Java Pass Method as Parameter
(17 answers)
How to pass a function as a parameter in Java? [duplicate]
(8 answers)
Closed 7 years ago.
Is there a way of passing a function to another function and then executing it?
functionCaller(functionToBeCalled());
In java 8 you can use a method reference or lambda
functionCaller(this::functionToBeCalled);
or
functionCaller(() -> functionToBeCalled());
I don't know if I understand very well your question, but effectively you can call a function in param of another function.
You can do this (I suppose your current language is Java):
// if write(...) and getValue() are static method of Writer class
Writer.write(getValue());
// if write(...) and getValue() can just be used by instanciate an object
Writer writer = new Writer();
String val = writer.getValue();
writer.write(val);
There are basic Java programming lesson.
Thanks

Java / Get all arguments in reflection [duplicate]

This question already has answers here:
Can I obtain method parameter name using Java reflection?
(15 answers)
Closed 9 years ago.
I search way to get all the names of a function in java. for example:
public String doSomething(String user, boolean tree, String[] arr){
// HERE I want to get all the names of the params, meaning: user, tree and arr, and do something with them
}
How can I do it?
You can, but only if you have debugging information in the code. The names of these variables exist mainly for you, not for the processor.
Details here:
Can I obtain method parameter name using Java reflection?

Categories

Resources