converting character to String when calling Java code

9 views
Skip to first unread message

Per Nyfelt

unread,
Oct 8, 2019, 7:40:40 AM10/8/19
to Renjin
Hi,

I am trying to make the google phoneNumber util from libphonenumber available as a Renjin package.

I am using
    <dependency>
     
<groupId>com.googlecode.libphonenumber</groupId>
     
<artifactId>libphonenumber</artifactId>
     
<version>8.10.19</version>
   
</dependency>


However invoking the google code is not working as expected here is a simplified example:


import(com.google.i18n.phonenumbers.PhoneNumberUtil)

phoneNumber <- "0701234566"
region <- ""

util <- PhoneNumberUtil$getInstance()
print(paste("phoneNumber type = ", typeof(phoneNumber), "; region type = ", typeof(region)))

phoneNum <- util$parseAndKeepRawInput(phoneNumber, region)
print(paste("phone number", phoneNumber, "is valid =", util$isValidNumber(phoneNum)))

It prints out
[1] "phoneNumber type =  character ; region type =  character"

and then I get the following exception
org.renjin.eval.EvalException: Cannot match arguments (character, character) to any JVM method overload:

        public com.google.i18n.phonenumbers.Phonenumber$PhoneNumber com.google.i18n.phonenumbers.PhoneNumberUtil.parseAndKeepRawInput(java.lang.CharSequence,java.lang.String) throws com.google.i18n.phonenumbers.NumberParseException
        public void com.google.i18n.phonenumbers.PhoneNumberUtil.parseAndKeepRawInput(java.lang.CharSequence,java.lang.String,com.google.i18n.phonenumbers.Phonenumber$PhoneNumber) throws com.google.i18n.phonenumbers.NumberParseException
        at org.renjin.invoke.reflection.FunctionBinding.invoke(FunctionBinding.java:176)
        at org.renjin.invoke.reflection.FunctionBinding.evaluateArgsAndInvoke(FunctionBinding.java:154)
        at org.renjin.invoke.reflection.MethodFunction.apply(MethodFunction.java:58)
        at org.renjin.sexp.FunctionCall.eval(FunctionCall.java:80)
        at org.renjin.primitives.special.AssignLeftFunction.assignLeft(AssignLeftFunction.java:58)
        at org.renjin.primitives.special.AssignLeftFunction.apply(AssignLeftFunction.java:42)
        at org.renjin.sexp.FunctionCall.eval(FunctionCall.java:80)
        at org.renjin.sexp.ExpressionVector.eval(ExpressionVector.java:85)
        at org.renjin.eval.Context.evaluate(Context.java:280)
        at org.renjin.script.RenjinScriptEngine.eval(RenjinScriptEngine.java:174)
        at org.renjin.script.RenjinScriptEngine.eval(RenjinScriptEngine.java:133)


I interpret this to mean that somehow the parameters are not converted to Strings so the correct method cannot be found.
Is there some way i can cast the character parameters (StringVectors) to java.lang.String so that the correct method that i want to call is found?

Best regards,
Per

Bertram, Alexander

unread,
Oct 8, 2019, 7:54:17 AM10/8/19
to renji...@googlegroups.com
Does the method have a CharSequence parameter instead of a java.lang.String? Renjin may not know how to convert a StringVector to a CharSequence. You could write a wrapper method that accepts two Strings instead and passes it onto the library method.

You can also correct the StringConverter class. It could be updated to "accept" a parameter of type CharSequence as well as java.lang.String:

Best,
Alex

--
You received this message because you are subscribed to the Google Groups "Renjin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to renjin-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/renjin-dev/499e9616-b2cd-4b70-a8af-561f40f4f4cd%40googlegroups.com.


--
Alexander Bertram
Technical Director
BeDataDriven BV

Web: http://bedatadriven.com
Email: al...@bedatadriven.com
Tel. Nederlands: +31(0)647205388
Skype: akbertram

Per Nyfelt

unread,
Oct 8, 2019, 8:08:07 AM10/8/19
to Renjin
Thanks Alex,

I give it a go and will submit a PR once i got it working!

Best regards,
Per


On Tuesday, October 8, 2019 at 1:54:17 PM UTC+2, Alexander Bertram wrote:
Does the method have a CharSequence parameter instead of a java.lang.String? Renjin may not know how to convert a StringVector to a CharSequence. You could write a wrapper method that accepts two Strings instead and passes it onto the library method.

You can also correct the StringConverter class. It could be updated to "accept" a parameter of type CharSequence as well as java.lang.String:

Best,
Alex

To unsubscribe from this group and stop receiving emails from it, send an email to renji...@googlegroups.com.

Per Nyfelt

unread,
Oct 8, 2019, 8:33:33 AM10/8/19
to Renjin
I can confirm that creating a wrapper worked like a charm btw:

Posting what I did here in case it would be helpful to someone else in the future:

import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;
import com.google.i18n.phonenumbers.Phonenumber;

/* this is temporary workaround until StringConverter is fixed */
public final class PhoneNumberWrapper {

  private final com.google.i18n.phonenumbers.PhoneNumberUtil numberUtil =
        com.google.i18n.phonenumbers.PhoneNumberUtil.getInstance();
        
  public Phonenumber.PhoneNumber parseAndKeepRawInput(String phoneNumber, String region) throws NumberParseException {
    return numberUtil.parseAndKeepRawInput(phoneNumber, region);
  }
}

and the i just call the wrapper instead for the problematic function (parseAndKeepRawInput):

validatePhoneNumber <- function(phoneNumber, defaultRegion = "SE") {
  import(com.google.i18n.phonenumbers.PhoneNumberUtil)
  import(se.alipsa.phonenumber.PhoneNumberWrapper)
 
  wrapper <- PhoneNumberWrapper$new()
  util <- PhoneNumberUtil$getInstance() 
  phoneNum <- wrapper$parseAndKeepRawInput(phoneNumber, defaultRegion)
  return(util$isValidNumber(phoneNum))
}

Best regards,
Per

Per Nyfelt

unread,
Oct 8, 2019, 4:46:03 PM10/8/19
to Renjin
Reply all
Reply to author
Forward
0 new messages