IntelliJ compile warning for Groovy code is wrong - intellij-idea

Optional<BigDecimal> maybeConvertStringToBigDecimal(Optional<String> string) {
string.flatMap {
try {
return Optional.of(new BigDecimal(it))
} catch (Exception e) {
log.error "Exception: ${e.message}\n\n${e.stackTrace}"
return Optional.<BigDecimal> empty()
}
}
}
This code compiles just fine but IntelliJ shows a red warning that
Cannot return 'Optional<U>' from method returning
'Optional<BigDecimal>'
I think it is clear that there is always Optional<BigDecimal> returned. What can be done to get rid of the warning? Is this an IntelliJ issue or is there an issue with the code?

Related

Getting a warning when use objectmapper in flux inappropriate blocking method call in java reactor

i am new to reactor, i tried to create a flux from Iterable. then i want to convert my object into string by using object mapper. then the ide warns a message like this in this part of the code new ObjectMapper().writeValueAsString(event). the message Inappropriate blocking method call. there is no compile error. could you suggest a solution.
Flux.fromIterable(Arrays.asList(new Event(), new Event()))
.flatMap(event -> {
try {
return Mono.just(new ObjectMapper().writeValueAsString(event));
} catch (JsonProcessingException e) {
return Mono.error(e);
}
})
.subscribe(jsonStrin -> {
System.out.println("jsonStrin = " + jsonStrin);
});
I will give you an answer, but I don't pretty sure this is what you want. it seems like block the thread. so then you can't get the exact benefits of reactive if you block the thread. that's why the IDE warns you. you can create the mono with monoSink. like below.
AtomicReference<ObjectMapper> objectMapper = new AtomicReference<>(new ObjectMapper());
Flux.fromIterable(Arrays.asList(new Event(), new Event()))
.flatMap(event -> {
return Mono.create(monoSink -> {
try {
monoSink.success(objectMapper .writeValueAsString(event));
} catch (JsonProcessingException e) {
monoSink.error(e);
}
});
})
.cast(String.class) // this cast will help you to axact data type that you want to continue the pipeline
.subscribe(jsonString -> {
System.out.println("jsonString = " + jsonString);
});
please try out this method and check that error will be gone.
it doesn't matter if objectMapper is be a normal java object as you did. (if you don't change). it is not necessary for your case.
You need to do it like this:
Flux.fromIterable(Arrays.asList(new Event(), new Event()))
.flatMap(event -> {
try {
return Mono.just(new ObjectMapper().writeValueAsString(event));
} catch (JsonProcessingException e) {
return Mono.error(e);
}
})
.subscribe(jsonStrin -> {
System.out.println("jsonStrin = " + jsonStrin);
});

Failure failing in CATCH

I'm probably overlooking something simple, but I do not expect the below code to fail. It is behaving as if I wrote die instead of fail in the catch block.
The Failure does not get properly handled and the code dies.
sub foo()
{
try {
say 1 / 0;
CATCH { default { fail "FAIL" } }
}
return True;
}
with foo() {
say "done";
}
else
{
say "handled {.exception.message}"
}
Output:
FAIL
in block at d:\tmp\x.pl line 5
in any at d:\tmp\x.pl line 5
in sub foo at d:\tmp\x.pl line 4
in block <unit> at d:\tmp\x.pl line 11
To bring home to later readers the full force of what Yoda said in their comment, the simplest solution is to unlearn the notion that you have to try in order to CATCH. You don't:
sub foo()
{
say 1 / 0;
CATCH { default { fail "FAIL" } }
return True;
}
with foo() {
say "done";
}
else
{
say "handled {.exception.message}"
}
correctly displays:
handled FAIL
According to the Failure documentation this seems to be the defined behavior.
Sink (void) context causes a Failure to throw, i.e. turn into a normal exception. The use fatal pragma causes this to happen in all contexts within the pragma's scope. Inside try blocks, use fatal is automatically set, and you can disable it with no fatal.
You can try to use the no fatal pragma.
sub foo() {
try {
no fatal;
say 1 / 0;
CATCH { default { fail "FAIL" } }
}
}
unless foo() {
say "handled"
}

How to use espresso matcher as a condition for If else statement?

The snippet of my code looks like this:
private void SelectOnline(String env) {
onView(withText("Some Text")).perform(click());
if (onView(withText(env)).check(matches(isChecked()))) {
onView(withId(R.id.dialogCancel)).perform(click());
}else {
onView(withText(env)).perform(click());
}
}
I got an error with message
required: boolean
found: ViewInteraction
This means that you cannot use espresso View Matchers as a condition for if..else. Is there some other way to implement if..else statement ?
Espresso was designed in a way to discourage devs from using conditionals, so there's no officially supported way to do this.
However, there are hacks you can try. I use try/catch statements. In your case, it would be something along the lines of:
private void SelectTransitBackendOnline(String env) {
onView(withText("Some Text")).perform(click());
try {
onView(withText(env)).check(matches(isChecked())))
onView(withId(R.id.dialogCancel)).perform(click());
} catch (AssertionFailedError e) {
onView(withText(env)).perform(click());
}
}
Depending on what you are doing in the try block, change the catch block's exception. I wanted to click on cancel_button if it exists, so I changed it to:
try {
onView(withId(R.id.cancel_button)).perform(click());
} catch (NoMatchingViewException ignore) {
}

Error handling in the cycle for()

How to make the cycle for() to restart or skip when there is an error?
My code moves on web pages, but fails on a non-existent page and returns an error and stops.
I tried try-catch, but I did not understand how to do a re-start or skip.
It should be a simple solution.
function A() {
// do something
}
function B() {
try {
// A();
}
catch(Exception e){
log ( e );
A();
}
finally {
// whatever you want whether theres an exception or not
}
}
I don't know if this might work, but it worths a try.

Unknown NullPointerException when creating simple player

I am trying to write a simple player but on running this code, it throws a NullPointerException.
try {
player = Manager.createPlayer("C:\\Users\\Admin\\Desktop\\Movie_0001.3gp");
player.realize();
VideoControl vc;
vc=(VideoControl) player.getControl("VideoControl");
if(vc!=null){
Item video=(Item) vc.initDisplayMode(vc.USE_GUI_PRIMITIVE, null);
playerFrm=new Form("playing video");
playerFrm.append(video);
display=display.getDisplay(this);
display.setCurrent(playerFrm);
}
player.prefetch();
player.start();
}
catch(IOException ex) {
ex.printStackTrace();
} catch (MediaException ex) {
ex.printStackTrace();
}
display=display.getDisplay seems odd! Was display initialized before and are you overwriting it with a function from its own?
getDisplay probably should be some sort of static function
you need to change in code Where you get cureent display,
display=Display.getDisplay(this);