User1291
User1291

Reputation: 8182

java - what happens to instances when I change the class?

I will be required to come up with a "adaptive" class layout, i.e. in my case, one that can inline certain array elements as fields of their own or change their sizes at runtime.

I have no idea how to do that (yet?), but let's assume for a moment, I had.

What happens to instances of class A when I change the bytecode of said class?

In particular, how are field / method offsets handled?

For example, let's say I add a field to the class ... will that affect created instances or just newly created ones?

EDIT: since people were asking for details, my task is to

"Conduct a survey of previous work regarding class layout modifications at runtime and based on the knowledge gained in the survey, implement such a class layout modification for [technical details]"

If you are interested in these "technical details", I'll be working on the

Synchronized-by-Default framework, which uses - atm - an array of fixed-size locks, each lock consisting of a numerical value, the last N bytes of which are used as transactional IDs.

Since using an array of locks reduces data locality, we are interested in determining a heuristic that will tell us when it makes sense to inline the locks - i.e. such that the locks and the object they correspond to (hopefully) end up in the same cache line.

Also, using a fixed-size IDs artificially limits the amount of transactions that can be concurrently active at any one point in time. So we want to be able to "grow" them on demand.

I'm supposed to come up with a dynamic class layout that will support both, inlining the locks and growing them, as needed.

Upvotes: 0

Views: 150

Answers (2)

talex
talex

Reputation: 20455

It's impossible. At least without VERY DARK MAGIC. What's happened if you decide to apply some of it is up to you,

EDIT:

It is hard to prove absence of something, but I try to give some arguments:

  • There is no such option in language spec and public API.
  • None of implementations I know support that feature.
  • It is against idea of static typing to change type definition in runtime.

Upvotes: 1

wumpz
wumpz

Reputation: 9131

You are looking for some bytecode modification frameworks like:

http://asm.ow2.org/

All kinds of frameworks uses some of these to build e.g. proxy objects. Maybe this is a bad example due to it is constructing a new enriched class, which is assignable. But as already mentioned, you really need to know what you are doing.

At least asm provides you with a validator to get some kind of hints of eventually introduced problems. Dont know how other frameworks handle this.

Saying all this, again a warning!!! Better try to improve your application architecture or try something like Apaches DynaBean (https://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/DynaBean.html).

Upvotes: 0

Related Questions