Frederic Leitenberger
Frederic Leitenberger

Reputation: 2019

Rhino Method Shutter/Interceptor

i am looking for a way to intercept all method calls from JavaScript (Rhino/Java) to any and all Java-Objects (Java-Methods).

I want to:

Is there a way to this inside the Rhino-Engine?

I know there are other options like Aspect-Oriented-Programming-Libraries, but i want to avoid those.

I also tried to replace the NativeJavaMethod implementation in the Rhino engine, but is tied very deeply into the system and impossible to replace.

If there was only a factory for those (like with the ContextFactory) that would be great.

Or a method call interceptor like the ContextFactory.doTopCall(...), but for all JS-2-Java calls.

Upvotes: 0

Views: 326

Answers (2)

Jawad
Jawad

Reputation: 327

If you just want to intercept calls to methods of Java Objects then that is possible. Override creation of Context objects by overriding the makeContext method of the context factory while initializing the context factory (most of the time its the global context factory).

Override the wrap factory in the context and override the wrapAsJavaObject method and return an anonymous implementation of NativeJavaObject that overrides the get() and put() method. Every time something is put (like a property or method), check if its a FieldAndMethods. Create a new class that extends from FieldAndMethods and also wraps it (contains), create an instance of this class by passing the original object to the constructor and put this instance instead of the original instance.

The new class should override the call() method for interception. and just call the contained objects call method.

If you need sample code let me know.

Upvotes: 0

Frederic Leitenberger
Frederic Leitenberger

Reputation: 2019

I found Rhino ClassShutter replacement: SandboxShutter which looks promising.

Upvotes: 0

Related Questions