Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7822439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:55:29+00:00 2026-06-02T07:55:29+00:00

I was hoping to use groovy’s invokeMethod to do this, but it turns out

  • 0

I was hoping to use groovy’s invokeMethod to do this, but it turns out that when you call from Java to Groovy, invokeMethod isn’t called, but otherwise it would have worked perefectly.

I have a case where I’m submitting a Groovy class to a Java class (Which I can’t edit). The Groovy class is annotated and the Java class scans for the annotations and saves the annotated methods as listeners for it’s events.

When the event is issued I’d like to grab some information from the event object, use it to retrieve data and inject that data into the event handler in the script (Via annotated variables inside that method).

The things I have control over–I instantiate the scripts, set a base class for them, and pass them to the other system to be registered. The scripts will be written by others–I have control over the script’s design but my goal is simplicity.

I could probably create an adapter class, but that seems quite difficult and fragile since I’d have to manually register all those methods instead of using the annotations like it does now–there are a lot of different events to listen to.

I’m wondering if there are groovy tricks I’m not considering. I’m still pretty new to groovy meta-programming. Perhaps there is a way to create the adapter class automatically, or when I compile the scripts, replace the methods with forwarding methods that forward to my code before calling their real method–anything like that possible?

Requested source code:

Source code–well let’s see, this process is spread across a few classes…

This is how I set up the Groovy Class Loader with a ScriptBase

cconfig.setScriptBaseClass("tv.kress.bill.minecraft.ezplugin.ScriptBase");
GroovyClassLoader gcl = new GroovyClassLoader(getClass().getClassLoader(), cconfig);

Then I pass it to the Groovy Scripting Engine (I’m leaving out some stuff here)

gse = new GroovyScriptEngine(cpString, gcl);

Then I instantiate the script

scriptClass = gse.loadScriptByName(file.getAbsolutePath());
instance = (GroovyObject) scriptClass.newInstance();

Then, if it’s a “Listener” which is the marker interface that the “canned” java library uses to identify java classes it should scan for annotations, I pass it off to that class so that any annotated methods can be registered (Somewhere along the line “instance” became “script”, same object though:

 if (script instanceof Listener)
     pm.registerEvents((Listener) script, this);

The interesting part of the script itself looks like this:

@EventHandler
public void userEvent(UserInteractEvent event) {

What I’d like to add is the ability to, inside the userEvent, add an annotated local variable like this:

    @Persist int persistedPerUserData // Or @PersistPerUser? or @Persist(User=true)?

so that just before userEvent is called, I can intercept it. I’d grab the user name from the UserInteractionEvent, combine it with the script, variable and method name to get a unique signature like “MyScript:UserEvent:Bill:persistedPerUserData” and use that to retrieve an int I can place into persistedPerUserData.

Later after the method returns grab the value from persistedPerUserData and store it back into “MyScript:UserEvent:Bill:persistedPerUserData” (Currently a hash but I expect to make it a database eventually).

In this way, the script never has to consider the fact that it’s dealing with different users, it just has to have a single set of variables and all the persistence just works.

There are other events this will work for, but I believe they all extend the same event and that root event has the “user” field.

EDIT: Just as another thing NOT to try, I tried to use the ProxyMetaClass/interceptor like this:

// Attempt (and fail) to intercept calls to an instance of clazz
class Slicer {
    public static Object slice(Class clazz) {
        Object instance;
        def proxy = ProxyMetaClass.getInstance(clazz);
        proxy.interceptor = new MyInterceptor();
        proxy.use {
            instance = clazz.newInstance();
        }
        return instance;
    }       
}

With the same results, every call from a groovy class was instrumented fine, but no calls from Java were intercepted. Back to the drawing board. I guess this is why Aspects use bytecode manipulation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:55:31+00:00Added an answer on June 2, 2026 at 7:55 am

    I really haven’t figured out an answer to this, but I came up with something that I think will work–I suppose nobody mentioned it because it was so obvious, but I’m still “Thinking in Java” More than groovy.

    Okay, where I was hoping for the script implementation to look something like this:

    @EventHandler
    public void userEvent(UserInteractEvent event) {
        @Persist int persisteData
        // At this point persistedData contains data different depending on which user was passed in
    ...
    

    I think that if I use a closure I think I can do something close:

    @EventHandler
    public void userEvent(UserInteractEvent event) {
        persistScope(event.user) {
            @Persist int persistedPerUserData // Or @PersistPerUser? or @Persist(User=true)?
            ...
    

    and that way within persistScope I can scan the closure for @Persist annotations and do my thing. This may not work exactly because that int hasn’t been created until the closure starts, but I think I can fix that using the methods I mentioned in the question as long as I’m calling from groovy to groovy. Either that or I’ll just make “it” a hash with the persisted user data.

    It’s slightly more awkward but I think it will work, and I like the fact that it’s a little more explicit (In fact before I was just assuming that the “event” passed in had a .getUser() method, now I can scope persistence to anything I want).

    I’ll go try to implement this and give it a few days to see if anyone comes up with an answer to the original question I asked before accepting this.

    EDIT: I’m unhappy with this solution. Since the variables are declared inside that scope I couldn’t use the @Persist annotation, so i passed in a hash that the module can use as a data container, then I persist it after the closure returns.

    Still looking for better answers…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't have any experience with SVG but I was hoping to use this
I am hoping to use Groovy more as a functional language than I can
I don't use Excel very often, but I'm hoping there is a fairly straightforward
I was hoping to use this with MvcSiteMapProvider to hide / show menu items
I've been hoping to use inheritance in Meteor, but I couldn't find anything about
I'm hoping to use jqGrid for a current web project that I'm working on.
Was hoping to use Django / db-api's built in string excaping, but it looks
I was hoping to use OrderBy on a SPFolderCollection object but after I include
I'm hoping to use a Google Map in this way: User zooms in on
I've got a wrapper powershell script that I'm hoping to use to automate a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.