Intercept array access using Bytebuddy - byte-buddy

I have been using Bytebuddy to monitor application behaviours, and I would like to check whether an array field of one of application classes is updated before executing a particular method. I have read Bytebuddy documentation and stack overflow questions and have found some useful documentation of how to intercept field accesses using MemberSubstitution.
However, because the field I'm interested in is an array, onWrite and onRead events in MemberSubstitution seem irrelevant.
Is it possible to track updates on an array field using Bytebuddy?

No, unfortunately no. Arrays are read onto the stack. Then only afterwards elements are accessed by index. This can be preceded by arbitrary instructions and is not interceptable individually.

Related

What is the use case of atomFamily in recoil?

I did my first experiment with recoil, building an editable table. Each cell has an atom which stores its row, column, and text value.
The way I built this was by
initializing each cell's atom into a dictionary (just a plain object), with keys in a format of [column]x[row]
I then iterate over these keys in the Table component, and pass only the key to each Cell component
The Cell component uses useRecoilState and find its specific Atom by accessing the main dictionary using the key it got passed as a prop.
Now, it seems to me that this use case (creating thousands of related atoms with the same shape) is what atomFamily is meant to make easier, but I don't understand how to use it in this way, where you initialize each atom with a specific value.
And, besides that, I don't understand what is the advantage of using atomFamily over storing a collection of atoms. I understand there is memoization involved, but I don't understand what is getting memoized other than, if I am reading correctly, the ability to recall a specific atom by calling the function again with the same id, which would get you pretty much the same behavior I'm getting with a dictionary.
There is very little difference: if you want to manually memoize and manage your own collection of atoms, then you certainly can. atomFamily is essentially just sugar for that: it handles the memoization for you, so all that you have to do is use a unique key to access each atom. Verbatim from the documentation for atomFamily:
An atom represents a piece of state with Recoil. An atom is created and registered per <RecoilRoot> by your app. But, what if your state isn’t global? What if your state is associated with a particular instance of a control, or with a particular element? For example, maybe your app is a UI prototyping tool where the user can dynamically add elements and each element has state, such as its position. Ideally, each element would get its own atom of state. You could implement this yourself via a memoization pattern. But, Recoil provides this pattern for you with the atomFamily() utility. An Atom Family represents a collection of atoms. When you call atomFamily() it will return a function which provides the RecoilState atom based on the parameters you pass in.
As far as examples for how to use atomFamily: beyond the documentation linked above, there are lots of existing questions and answers on Stack Overflow which already cover exactly that. Here are a couple which I have answered previously:
Dynamic atom keys in Recoil
Looking for a pattern to normalize state in Recoil without losing the benefit of Suspense

Sending pointers to another process using GVariant

I am currently developing a simple browser using webkit2gtk. In webkit2gtk, all DOM related operations are done in separate process called WebProcess.
I have created a GTKWidget in UI Process. I need to pass GTKWidget structure to WebProcess. Can we use GVariant for it?
I have read GVariant apis. I am able to send string, integer and other basic types using GVariant. But can we send objects using GVariant?
So, we have to distinguish between your title and your question.
First your title:
Sending pointers to another process
yes, you can do that (pointers are usually convertible to an integer type), but pointers are always specific to one's process memory – the receiver process doesn't have access to that, so the pointer shows into nothingness (you get a segmentation fault), or worse, to something else (your program runs with wrong data).
But can we send objects using GVariant?
Generally, no. You can't look inside objects, so they might contain pointers. See my comment above.
However, many objects in fact work without pointers, so the type information and the memory that belongs to an object is sufficient to "recreate" it on the receiver side, just by casting the received memory into the shape of the object you know it is. However, that is very object-specific.
You will probably like to read about serialization.

Unique vertices with Frames

Is there a thread safe way to ensure unique vertices are created by a framed graph? Consider the following:
Node n = framedGraph.addVertex(1, Node.class);
Node m = framedGraph.addVertex(1, Node.class);
System.out.println(n.equals(framedGraph.getVertex(1, Node.class)));
System.out.println(m.equals(framedGraph.getVertex(1, Node.class)));
prints true, false.
I'm looking for functionality similar to the get or create unique node functionality provided by Neo4j (which is the backing graph in this case).
As an aside - is there a way to use non-numeric ids?
Node m = framedGraph.addVertex("http://example.org", Node.class);
System.out.println(n.equals(framedGraph.getVertex("http://example.org", Node.class)));
prints false
Neo4j and most Graph implementations of Blueprints ignore the ID parameter. Aside from TinkerGraph, they generally all assign their own IDs without methods to create your own. You could always use IdGraph to help simulate your own ID.
Blueprints doesn't maintain the notion of "get or create". You have to implement that yourself or I suppose it's possible to reach down into Neo4j code to do it, with the expectation that your code is no longer portable from one graph to another. In that way, perhaps you could build a graph wrapper implementation similar to IdGraph that exposed a getOrCreate() method. At least that way, you still get to work with the Graph interface and the such logic is encapsulated within that. Of course, that won't help with enabling such functionality directly in Frames.

Name of this design pattern?

I'm trying to figure out the name behind this design pattern. Basically, you have some arbitrary data that needs to be processed, and any arbitrary number of "handler" objects that may be capable of handling the data. The data gets passed to these handlers until something processes it.
For example, in Qt, QImage reads images via QImageReader. QImageReader queries QImageIOHandler objects to see if the given file format can be read by that QImageIOHandler. If so, it uses that handler to read the image.
Is there a name for this delegation of responsibility?
Chain of Responsibility

The process which Lucene tokenizes text

This can be considered as a general Java question but for better understanding I'm using Lucene as example.
You can use different Tokenizers in Lucene to tokenize text. There's the main abstract Tokenizer class and then many different classes that extend it. Same thing for TokenFilter.
Now, it seems that each time you want to index a document, a new Tokenizer is created. The question is, since Tokeinzer is just a utility class, why not make it static? for example, a Tokenizer that converts all letters to lower case can have a static method that does just that for every input it gets. What's the point of creating a new object for every piece of text we want to index?
One thing to mention - Tokeinzer has a private field that contains the input it receives to tokenize. I just don't see why we need to store it this way because the object is destroyed right after the tokenization process is over and the new tokenized text is returned. The only thing I can think of is multi-threaded access maybe?
Thank you!
Now, it seems that each time you want to index a document, a new Tokenizer is created
This is not true, the Analyzer.reusableTokenStream method is called, which re-uses
not just a Tokenizer, but also the entire chain (TokenFilters, etc).
See http://lucene.apache.org/java/3_0_0/api/core/org/apache/lucene/analysis/Analyzer.html#reusableTokenStream(java.lang.String, java.io.Reader)
One thing to mention - Tokeinzer has a private field that contains the input it receives to tokenize. I just don't see why we need to store it this way because the object is destroyed right after the tokenization process is over and the new tokenized text is returned. The only thing I can think of is multi-threaded access maybe?
As mentioned earlier, the entire Chain of tokenizers and tokenfilters is reused across documents. So all of their Attributes are reused, but also its important to note that attributes are shared across the chain (e.g. all Tokenizers and TokenFilters' Attribute references point to the same instances). This is why it is crucial to call clearAttributes() in your tokenizer to reset all attributes.
As an example, a Whitespace tokenizer adds a reference to a TermAttribute in its ctor, and its wrapped by a LowerCaseFilter which adds a reference to a TermAttribute in its ctor, too. Both these TermAttributes point to the same underlying char[]. When a new document is processed, Analyzer.reusableTokenStream is invoked, which returns the same TokenStream chain (in this case Whitespace wrapped with LowerCaseFilter) used in the previous document. The reset(Reader) method is called, resetting the tokenizer's input to the new document contents. Finally reset() is called on the entire stream, which resets any internal state from the previous document, and the contents are processed until incrementToken() returns false.
Dont worry about creating an instance here and there of a class when doing something complex like indexing a document w/ Lucene. There is probably going to be lots and lots of objects created inside the tokenizing and indexing process. One more tokeniser instance is literally nothing when one compares the left over garbage from thrown away objects when the process completes. If you dont believe me get a profile and watch object creation counts.