Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Accessing private fields

Alain,

Remember that getDeclaredFields() will only return the fields declared on the class itself, not on its superclasses. If you want all the fields for an instance, you need to walk up the inheritance hierarchy using Class.getSuperclass() and get the declared fields on each.

Ron

Ron Bodkin
Chief Technology Officer
New Aspects of Security
m: (415) 509-2895

> ------------Original Message-------------
> From: "Alain van den Hove" <A_vandenHove@xxxxxxxxxx>
> To: <aspectj-users@xxxxxxxxxxx>
> Date: Tue, Aug-12-2003 1:06 AM
> Subject: [aspectj-users] Accessing private fields
> 
> 
> 
> Hello everybody!
> 
> I'm trying to access private fields by reflection to get a class's
> contents, it doesn't work unless i had the code directly into the class
> that i want to inspect
> 
> In this aspect, I had the function showClassContent() to the class
> ExampleMain that I want to inspect :
> 
> public aspect TraceMyClasses {
> 
> public void ExampleMain.showClassContent() {
> 	Field[] fieldTab = this.getClass().getDeclaredFields();
> 	for(int i=0;i<fieldTab.length-1;i++) {
> 		String fieldName = fieldTab[i].getName();
> 		String fieldValue;
> 		try {
> 			fieldValue=fieldTab[i].get(this).toString();
> 		} catch (IllegalAccessException e) {
> 			fieldValue="Unable to access";
> 		}
> 	}
> 
> void around (ExampleMain myClass, Object arg): execution(void *.*(..))
> 	   && this(myClass) && args(arg) {
> 		try { proceed(myClass,arg); }
> 		catch (Exception e) {
> 			myClass.showClassContent();
> 		}	
> 	}
> }
> 
> But I can't get the value of private fields.. How can I do ?
> 
> Thanks in advance
> Alain
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-users
> 


Back to the top