English 中文(简体)
How-to programatically access private members trough privileged Aspect in AspectJ?
原标题:

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;
 }
问题回答

I had the same issue, and it seems that it s as simple as changing the

result += ", " + field.getName() + "=(PRIVATE)"; 

to

field.setAccessible(true);
result += ", " + field.getName() + "="+ field.get(this);

Hope this will help someone!





相关问题
c# reflection with dynamic class

I need to execute a method "FindAll" in my page. This method returns a list of the object. This is my method that I execute "FindAll". FindAll requires an int and returns an List of these class. ...

Performance overhead of using attributes in .NET

1.. Is there any performance overhead caused by the usage of attributes? Think for a class like: public class MyClass { int Count {get;set;} } where it has 10 attibutes (...

WPF GridView, Display GetType().Name in DisplayMemberBinding

I want include the Type name of each object in my collection from my GridView. I have a collection which has four different types in it that all derive from a base class. Call them, Foo, Bar, Fizz, ...

Testing private method of an abstract class using Reflection

How can I test a private method of an abstract class using reflection (using C#)? I am specifically interested in adapting the code found in this thread. I am aware of the discussion around the ...

Adding Items to ListBox, RadioList, Combobox using reflection

I m trying to add items to a listbox,combobox, radiolist using reflection. The code I have at the moment is as follows: public static Control ConfigureControl(Control control, ControlConfig ctrlconf)...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签