PolMine / bignlp

Tools to process large corpora line-by-line and in parallel mode
1 stars 1 forks source link

AnnotationList$as.data.table() results in Illegal reflective access warning #25

Closed ablaette closed 2 years ago

ablaette commented 3 years ago

When calling AnnotationList$as.data.table() you may (almost certainly) see the following warning:

WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by RJavaTools to method java.util.Arrays$ArrayList.size() WARNING: Please consider reporting this to the maintainers of RJavaTool

To reproduce this, use the following example for the AnnotationList class:

library(bignlp)
docs <- c("This is a first document.", "This is another document!")
annoli <- AnnotationList$new(docs)
Pipe <- AnnotationPipeline$new()
Pipe$annotate(annoli)
annoli$as.data.table()

The warning is issued the first time you access the enriched ArrayList with the annotations, see the following minimal example.

library(bignlp)
annoli <- AnnotationList$new(c("This is a first document.", "This is another document!"))
AnnotationPipeline$new()$annotate(annoli)
annoli$obj$size()

The warning results from Java and is a frequent matter of discussions. As I understand it, the concept of modules introduced with Java 9 is less permissive to access classes/methods across modules if they are not exposed/imported properly. You can control the behavior of reflective access using java parameters, but you cannot turn off the warning. No matter what setting you choose for "--illegal-access" (for options, see this), the warning is issued at least once:

options(java.parameters = c("-Xmx4g", "--illegal-accesss=permit"))
library(bignlp)
annoli <- AnnotationList$new(c("This is a first document.", "This is another document!"))
AnnotationPipeline$new()$annotate(annoli)
annoli$obj$size()

As we know that this warning does not necessarily indicate a serious problem, we might consider suppressing it. But my experiments with traditional R functions such as capture.output(), sink(), suppressWarnings() have not been successful.

So at this stage, it seems to me that we have to live with this warning and that we might have to leave it with explaining in the documentation that users will see this warning which can be safely ignored.

To conclude, there are two things I have not tried yet:

ablaette commented 2 years ago

One year later, we now get this error (not warning), that cannot be muted:

java.lang.IllegalAccessException: class RJavaTools cannot access a member of class java.util.Arrays$ArrayList (in module java.base) with modifiers "public"

One reason may be that the ArrayList we instantiate is notthe kind of ArrayLiost we think it is: https://stackoverflow.com/questions/28851652/java-lang-classcastexception-java-util-arraysarraylist-cannot-be-cast-to-java

ablaette commented 2 years ago

The Stackoverflow discussion pointed to the relevant direction. I instantiated the wrong ArrayList. This is what works now:

self$obj <- .jnew("java.util.ArrayList")
.jnew("java.util.Collections")$addAll(self$obj, anno_array)