How to work with the expected return type "java.lang.Class"?

It seemed to me that I would try to write an R-interface for Scribe (a mature OAuth library for Java by Pablo Fernandez) as a way to upgrade to Java (not used for 8 years), learn rJava and make better use of the Twitter API. But mainly because it was Friday afternoon, and I thought it would be fun. :)

Unfortunately, I'm not very far ...

I uploaded the file . jar for the scribe , as well as commons-condec (his only addiction, which I subsequently unzipped). I ran the Java code using netbeans and it works fine using the twitter example .

I was fine for the first few lines of code, just following the rJava pre-sale:

# load R packages
library(rJava)

# Initialise
.jinit()

# Add class paths
d1 <- "C:/Users/Tony/Documents/R/java/scribe-1.1.0.jar"
d2 <- "C:/Users/Tony/Documents/R/java/commons-codec-1.4/"
.jaddClassPath(path=c(d1, d2))

But then the short writing guide for the writer says the following:

// Java Code
OAuthService service = new ServiceBuilder()
                                .provider(TwitterApi.class)
                                .apiKey("6icbcAXyZx67r8uTAUM5Qw")
                                .apiSecret("SCCAdUUc6LXxiazxH3N0QfpNUvlUy84mZ2XZKiv39s")
                                .build();

I cannot figure out how to rewrite this in rJava language. A small search on the Internet suggests that I do this in parts, so first I did:

# Create object (back to R code again)
( service <- .jnew("org.scribe.builder.ServiceBuilder") )
[1] "Java-Object{org.scribe.builder.ServiceBuilder@58fe64b9}"

# Set up apiKey and apiSecret using "$" shortcut
service$apiKey("6icbcAXyZx67r8uTAUM5Qw")
service$apiSecret("SCCAdUUc6LXxiazxH3N0QfpNUvlUy84mZ2XZKiv39s")

Good. Then I need to find out what type of return is expected from the provider function:

# Inspect return type
.jmethods(service, "provider")
[1] "public org.scribe.builder.ServiceBuilder org.scribe.builder.ServiceBuilder.provider(java.lang.Class)"

He needs java.lang.Class. I'm embarrassed here. What does it mean? I think, looking at the source, it needs the return type "ServiceBuilder", but how to do it? This was my best guess by looking at .jcall (note: 'use.true.class = TRUE' did nothing):

> .jcall(obj = service, returnSig = "Lorg.scribe.builder.ServiceBuilder;", method = "org.scribe.builder.ServiceBuilder.provider", "org.scribe.builder.api.TwitterApi")

Error in .jcall(obj = service, returnSig = "Lorg.scribe.builder.ServiceBuilder;",  : 
  method org.scribe.builder.ServiceBuilder.provider with signature (Ljava/lang/String;)Lorg.scribe.builder.ServiceBuilder; not found

Any ideas?

+3
source share
1 answer

, ServiceBuilder .

Java, , .class, . , . , R, Java :

Class c = Class.forName("org.scribe.builder.api.TwitterApi");

c. :

service$provider(c);
+1

Source: https://habr.com/ru/post/1796296/


All Articles