can JAXB parse "" to double? - jaxb

I am getting this error when unmarshalling an xml
java.lang.NumberFormatException:
at com.sun.xml.bind.DatatypeConverterImpl._parseDouble(DatatypeConverterImpl.java:232)
while checking on the xml and my classes for double values, I've found one of the elements of the xml that has a null value for its double attribute (I know it is double because the others of the same element have a double attribute)
<data num = "">...</data>
data class
public class Data implements Serializable {
...
#XmlAttribute(name = "num")
private double num;
//private Double num; //same problem as above
...}
can the num attribute be parsed to double if it was null? or is it fine?

Related

Type mismatch. Required: Double Found: String kotlin in android studio

i'm new to kotlin android studio,trying to update the textView with editText from data class and i used in editText "afterTextChanged" , created a setter function in data class with editable paramete, i can't convert double to string with editable, anyone help me out ?
here is function in data class
fun setSize(editable: Editable) {size = editable.toString()}
Your size variable is (apparently) a Double, so you have to assign a Double value to it. You're doing editable.toString() which just gives you a String representation of your Editable's contents.
You need to try and parse that as a Double with toDouble() (which throws an exception if it's not a valid number) or toDoubleOrNull (which returns null if it's invalid).
Since it's (I assume) user-edited content you're parsing, and there's a good possibility they'll enter something that's not a valid number representation, you need to handle that possibility. I'd go with toDoubleOrNull since it's easier in Kotlin (there are lots of these *OrNull variants on functions for that reason):
fun setSize(editable: Editable) {
// only runs the let block if the value is parsed
editable.toString().toDoubleOrNull()?.let { size = it }
}
or if you're not familiar with let:
fun setSize(editable: Editable) {
val double = editable.toString().toDoubleOrNull()
if (double != null) size = double
}

Cast a object to class 'double' through vars.putObject

In Jmeter assertion main trying to compare values of 2 variables with double data type. following script I'm using to cast a value in double
double actual = vars.getObject("Merticvalue");
log.info ("Actual MetricValue is found to be " + actual);
double expected=153.60
vars.putObject("expected",expected);
if (vars.getObject("expected") != vars.getObject("actual")) {
props.put("testcaseExecutionStatus",5);
String Status = props.get("testcaseExecutionStatus").toString();
log.info("Status:"+ Status)
return;
}
props.put("testcaseExecutionStatus",1);
String Status = props.get("testcaseExecutionStatus").toString();
log.info("Status:"+ Status)
I'm getting this error:
GroovyCastException: Cannot cast object '153.60'
with class 'java.lang.String' to class 'double'
The issue is getting Merticvalue value with is saved as String, you can cast it:
double actual = Double.valueOf(vars.getObject("Merticvalue"));

Groovy primitive double arithmetic

This yields 127
double middle = 255 / 2
While this yields 127.5
Double middle = 255 / 2
Meanwhile this yields 127.5 as well
double middle = (255 / 2) as double
I know that Groovy operates with BigDecimal per default, but to me this is a Huuge bug! How can this be?
This actually has nothing to do with BigDecimals, but rather with the type coercion from primitive integer to the primitive double. This problem is caused by the Groovy compiler and the (most probably) incorrect bytecode it produces. Take a look at the following bytecode representation of the first case. The following Groovy code:
void ex1() {
double x = 255 / 2
println x
}
gets compiled to a bytecode that can be represented as:
public void ex1() {
CallSite[] var1 = $getCallSiteArray();
double x = 0.0D;
if (BytecodeInterface8.isOrigInt() && BytecodeInterface8.isOrigD() && !__$stMC && !BytecodeInterface8.disabledStandardMetaClass()) {
int var5 = 255 / 2;
x = (double)var5;
} else {
Object var4 = var1[5].call(255, 2);
x = DefaultTypeTransformation.doubleUnbox(var4);
}
var1[6].callCurrent(this, x);
}
It shows that in this case, it is not possible to get 127.5 as a result, because the result of 255 / 2 expression is stored in the variable of type int. It feels like this is an example of inconsistent behavior because here is what the bytecode of the method that uses Double looks like:
public void ex2() {
CallSite[] var1 = $getCallSiteArray();
Double x = null;
if (BytecodeInterface8.isOrigInt() && !__$stMC && !BytecodeInterface8.disabledStandardMetaClass()) {
Object var4 = var1[8].call(255, 2);
x = (Double)ScriptBytecodeAdapter.castToType(var4, Double.class);
} else {
Object var3 = var1[7].call(255, 2);
x = (Double)ScriptBytecodeAdapter.castToType(var3, Double.class);
}
var1[9].callCurrent(this, x);
}
The main problem with this use case is that adding #TypeChecked does not prevent you from making this mistake - compilation passes and the incorrect result is returned. However, when we add #TypeChecked annotation to the method that uses Double the compilation error is thrown. Adding #CompileStatic solves the problem.
I've run some tests and I can confirm that this problem exists in the recent 2.5.6, as well as 3.0.0-alpha-4 versions. I've created a bug report in the Groovy JIRA project. Thanks for finding and reporting the problem!
UPDATE: Java does the same
It seems like this is not a Groovy bug - this is how Java does things as well. In Java, you can store a result of a division of two ints in the double variable, but you will get nothing else than an integer cast to the double. With {{Double}} type things are different in terms of the syntax but pretty similar in terms of the bytecode. With {{Double}} you need to explicitly cast at least one part of the equation to the {{double}} type, which results in the bytecode that casts both integers to the {{double}}. Consider the following example in Java:
final class IntDivEx {
static double div(int a, int b) {
return a / b;
}
static Double div2(int a, int b) {
return a / (double) b;
}
public static void main(String[] args) {
System.out.println(div(255,2));
System.out.println(div2(255,2));
}
}
When you run it you get:
127.0
127.5
Now, if you take a look at the bytecode it creates, you will see something like this:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
final class IntDivEx {
IntDivEx() {
}
static double div(int a, int b) {
return (double)(a / b);
}
static Double div2(int a, int b) {
return (double)a / (double)b;
}
public static void main(String[] args) {
System.out.println(div(255, 2));
System.out.println(div2(255, 2));
}
}
The only difference (in terms of the syntax) between Groovy and Java is that Groovy allows you to implicitly cast an integer to Double, and that is why
Double x = 255 / 2
is the correct statement in Groovy, while Java, in this case, fails during the compilation with the following error:
Error:(10, 18) java: incompatible types: int cannot be converted to java.lang.Double
That is why in Java you need to use casting when you assign from integer to Double.

java.lang.IllegalArgumentException: wrong number of arguments at javax.faces.component.UIComponentBase.isRendered [duplicate]

I'm using JSF 2.
I have a method that checks for matching values from a list of values:
#ManagedBean(name="webUtilMB")
#ApplicationScoped
public class WebUtilManagedBean implements Serializable{ ...
public static boolean isValueIn(Integer value, Integer ... options){
if(value != null){
for(Integer option: options){
if(option.equals(value)){
return true;
}
}
}
return false;
}
...
}
To call this method in EL I tried:
#{webUtilMB.isValueIn(OtherBean.category.id, 2,3,5)}
But it gave me a:
SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (http-localhost/127.0.0.1:8080-5) java.lang.IllegalArgumentException: wrong number of arguments
Is there a way to execute such a method from EL?
No, it is not possible to use variable arguments in EL method expressions, let alone EL functions.
Your best bet is to create multiple different named methods with a different amount of fixed arguments.
public static boolean isValueIn2(Integer value, Integer option1, Integer option2) {}
public static boolean isValueIn3(Integer value, Integer option1, Integer option2, Integer option3) {}
public static boolean isValueIn4(Integer value, Integer option1, Integer option2, Integer option3, Integer option4) {}
// ...
As a dubious alternative, you could pass a commaseparated string and split it inside the method
#{webUtilMB.isValueIn(OtherBean.category.id, '2,3,5')}
or even a string array which is created by fn:split() on a commaseparated string
#{webUtilMB.isValueIn(OtherBean.category.id, fn:split('2,3,5', ','))}
but either way, you'd still need to parse them as integer, or to convert the passed-in integer to string.
In case you're already on EL 3.0, you could also use the new EL 3.0 collection syntax without the need for the whole EL function.
#{[2,3,5].contains(OtherBean.category.id)}

JSR-303 bean validation with Groovy traits (Hibernate Validator)

I run into a problem with JSR-303 bean validation with a Groovy class that implements Traits with additional fields (plus their constraints). Let me show you a simple example. I have a class called ClientInfo which looks like this:
class ClientInfo implements AddressInfo, ContactInfo, BusinessEntity { }
It implements 3 traits that provides additional information. AddressInfo may look like this:
trait AddressInfo {
#Size(max = 128)
#SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE)
String contactName
#Size(max = 255)
#SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE)
String addressLine1
#Size(max = 255)
#SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE)
String addressLine2
#Size(max = 32)
#SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE)
String postalCode
#Size(max = 64)
#SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE)
String city
#Size(max = 3)
#SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE)
String countryCode
}
So far everything looks good. The problem shows up when the constraint validation fails. For example, if countryCode field fails during ClientInfo instance validation, it holds error message under the AddressInfo__countryCode field name:
{"AddressInfo__countryCode":"size must be between 0 and 3"}
It is caused by the fact that the fields that come from the trait are compiled into e.g. AddressInfo__countryCode, AddressInfo__addressLine1 and so one, providing getters and setters for those fields.
I tried to use Jackson annotations like #JsonProperty('countryCode') over the countryCode field in the AddressInfo trait, but it didn't help (the annotation was added to compiled class).
My question is: is it possible to use e.g. Jackson property mappers to render field names provided in #JsonProperty('...') annotation for the fields provided by implemented trait? I will really appreciate your help.
PS: I'm using 'org.hibernate:hibernate-validator:5.1.3.Final' bean validation implementation (maybe this one should be change).

Resources