Werner Schuster (murphee) wrote:
Charles Oliver Nutter wrote:
JRuby doesn't support annotations because Ruby doesn't support
annotations. So what! We can extend Ruby to add something like
annotations:

class JPABean
  def self.inherited(clazz)
    @@annotations = {}
  end
  def self.anno(annotation)
    @@last_annotation = annotation
  end

  def self.method_added(sym)
    @@annotations[sym] = @@last_annotation
  end

  def self.get_annotation(sym)
    return @@annotations[sym]
  end
end

Quick thought: this doesn't seem to be thread safe - load two such
classes at the same time, and you might end up with confused annotations;
Thread A: anno :foo
Thread B: anno :bar
method_added(abc)
method_added(cde)

And now methods abc and cde have annotation :bar... or am I missing
something? Is class loading atomic?

Maybe you could do something with modules, and have the annotation be a
instance var of the metaclass that uses the annotations.

This would already be a problem with multiple threads loading the same class at the same time...it's nothing specific to the annotation code. Multiple threads making changes to a given class at the same time would step on each other, since classes are just another data structure in Ruby.

This would be a problem if you have many threads running at the same time in a given piece of class-defining code. While this sometimes happens, usually code is loaded via a require statement, which should only load a given file once. So in many or most cases, multiple threads won't try to do the initial creation of a class at the same time.

If it actually turned out to be a problem, the annotation could also be stored in a thread-local variable. And if we're talking about different classes, there's no overlap here either; each class has its own annotation state.

- Charlie

---------------------------------------------------------------------
To unsubscribe from this list please visit:

   http://xircles.codehaus.org/manage_email

Reply via email to