views:

425

answers:

1

Hi,

I was wondering if it was possible in AspectJ to do the following. I’m adding a method .inspect() to every object of class RubyObject. That method is supposed to spit a string like #(CompleteClassName, var1=val1, var2=val2, …)

So far so good, this.getClass().getFields() gets me all the visible fields I want and this.getClass().getDeclaredFields() coupled with Modifier.isPrivate(field.getModifiers())) gives me a list of all the private field..

Problem here is that I’m not able to retrieve the value of the privates fields like I do with the visible one. I guess it’s normal since the reflective API I’m using are not part of AspectJ so they are not aware I’m running a so-called privileged aspect here.

Since I’m in privileged mode, I can access all the private variables I want using this.privateVariableName but how can I do it programatically when the variable name is itself in a variable?

Thanks for your help..

public privileged aspect Reflect {
 private static HashMap<Class<? extends Object>,ArrayList<RubyObject>> oStore = new HashMap<Class<? extends Object>, ArrayList<RubyObject>>();

 private boolean RubyObject.hasBeenImported = false;
 declare parents: ca.concordia.* implements RubyObject;


 //inspect return a string of the format #<classname, var1=val, var2=val, ...>
 //Unfortunately, it doesn't get PRIVATE data since privileged aspect can't gelp with field.get(Object)
 public String RubyObject.inspect()
 { String result = "#<" + this.getClass().getName();
  for (Field field : this.getClass().getFields())
  { 
    try { result += ", " + field.getName() + "=" + field.get(this); }
    catch(Exception e) { result += ", " + field.getName() + "=(???)"; }  //should never occur;
  }
  for (Field field : this.getClass().getDeclaredFields())
  { if (Modifier.isPrivate(field.getModifiers()))
    result += ", " + field.getName() + "=(PRIVATE)"; 
  }
  result += ">";
  return result;
 }
A: 

In your code, what is the type of Field?

gulbrandr
It can be any type.
matdumsa