On Tue, 06 Jun 2006, Lukas Felber defenestrated me: > Thomas E Enebo wrote: > > On Mon, 29 May 2006, Lukas Felber defenestrated me: > > > >> I (Lukas Felber) am one of the three Swiss guys working on refactorings > >> for the RDT eclipse plugin. > >> > >> I have a Question concerning the common Classes of Ruby (like Object or > >> String). Is there a way to get to the JRuby Ast of those classes. I know > >> how I can create an Ast out of a file I have. Do I need to know where > >> the Rubyfile for a Class (e.g. String) lies to get to the Ast of the > >> Class or is there an other way? > > > > We do not directly link back to AST from an object (if this is what > > you are asking). That would not work well since multiple AST nodes could > > reference the same object. > > > Ok I think I expressed myself a bit bad. I'll make an example to to show > you what I'd like to have. > Here a simple Ruby file: > > class X < Dir > end > > What I need is the AST of the Class "Dir" (I want to get to its > constructor). > If I parse the example file I get a ClassNode. The method getSuperNode > gives me a node containing the > String "Dir". I would like to have way to get to the AST of the class > "Dir" over the String "Dir". > Of course "Dir" is only an example. It stands for any Built-in Class of > ruby. > I hope I expressed myself better this time.
Yeah I think so. Consider: class Dir def initialize end end class X < Dir end class Dir def foo end end X.new.foo In the above code snippet to find Dir class AST we would need two references. So I think this task is problematic. If you consider that builtin classes do not have an AST at all (they are implemented entirely in Java) then there may not even be an AST for a class. Also think of the scenario where I re-define initialize in Dir later on. Getting a reference to all Dir ClassNodes to find initialize would then also require know which order those AST were evaluated. So the problem to be solved is wanting to go to a super class and go to the source where a method is and do some refactoring? Something like that? Using the live instances of the classes is probably the only way to do this completely accurately. So as an oversimplification (and this is semi-incomplete pseudocode): 1. cls = IRuby.getClass("X") 2. cls = cls.getSuperClass() // More involved with classes which include modules. 3. method = cls.findMethod("initialize") 4. if method.isUndefined() goto 2 5. if method (is anything but AliasMethod or Defaultmethod) then no source access [native java code impl] 6. method.getBodyNode() [Contains location of method impl] getBodyNode does not currently exist.... The problem with my solution is it requires the jruby interpreter to parse and load these files which could have all sorts of unintended conqequences. My only other thought is you could create a visitor which walks all the ASTs you want to potentially refactor and then build your own set of references so you can ask a question like getAstNode(ClassName, MethodName) or getAstNode(ClassName) and get back what you want. My first example shows that this may not give back exactly what you want since you may have multiple versions of intialize for a particular class (as an example). -Tom -- + http://www.tc.umn.edu/~enebo +---- mailto:[EMAIL PROTECTED] ----+ | Thomas E Enebo, Protagonist | "Luck favors the prepared | | | mind." -Louis Pasteur | _______________________________________________ Jruby-devel mailing list Jruby-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jruby-devel