|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
help required in introducing new methodhi,
My problem is equivalent to implementing toString mehtod of a class. The output should be like: fieldVariable1= val1 | fieldVariable2= val2 |... etc. I could introduce the toString method but am unable to read the field variables and print them. Below is the approach i used. public aspect INAspect { // INTRODUCTION public String SomeInterface.toString(byte[] bytes) { String s=""; // s = 'field1='+ this.field1 + 'field2=' + this.field2 | ... etc return s; } // SYNTHETIC IMPLEMENTS declare parents: com.comp.attributes.* implements SomeInterface; } --- package com.comp.attributes; public class UserAttributes { private String field1 = "abc"; private int field2 = 100; ... getters and setters... //here toString() should be introduced that is like // "field1="+ this.field1+ "field2="+ this.field2 etc. } ------ when i decompile the code of user attibutes it is showing hte toString implemented. except for reflections (which is not prefered to be used) i want to know if there is a way to get this achieved. I tried with AJType; but no luck. Any ideas/ pointers please. ravi _______________________________________________ aspectj-users mailing list aspectj-users@... https://dev.eclipse.org/mailman/listinfo/aspectj-users |
|
|
Re: help required in introducing new methodHi Ravi,
you don't specify which error is blocking you, so I'll assume two possibilities. The first one is that fields you are trying to access are private, so not visible from the aspect that is declaring the method. This can be solved using "public privileged aspect". The "privileged" keyword gives the aspect rights to access private members. The second one is that you are declaring the toString on "SomeInterface" and then using field1, field2 etc.. Make sure those fields are declared on the class, AspectJ is subject to the same checks a normal class is, so if you declare a method on an interface, that method will be able to access other methods/fields in that interface or super interfaces. Hope this helps, Simone Ravi Chandra wrote: > hi, > > My problem is equivalent to implementing toString mehtod of a class. > *The output should be like: fieldVariable1= val1 | fieldVariable2= > val2 |... etc. I could introduce the toString method but am unable to > read the field variables and print them. *Below is the approach i used. > > public aspect INAspect *{* > // INTRODUCTION > public String SomeInterface.toString(byte[] bytes) { > String s=""; > *// s = 'field1='+ this.field1 + 'field2=' + this.field2 | ... etc* > return s; > } > > // SYNTHETIC IMPLEMENTS > declare parents: com.comp.attributes.* implements SomeInterface; > *}* > > --- > > package com.comp.attributes; > public class UserAttributes *{* > private String field1 = "abc"; > private int field2 = 100; > ... getters and setters... > > * //here toString() should be introduced that is like > // "field1="+ this.field1+ "field2="+ this.field2 etc. > }* > > ------ > > when i decompile the code of user attibutes it is showing hte toString > implemented. except for reflections (which is not prefered to be used) > i want to know if there is a way to get this achieved. I tried with > AJType; but no luck. > > Any ideas/ pointers please. > ravi > ------------------------------------------------------------------------ > > _______________________________________________ > aspectj-users mailing list > aspectj-users@... > https://dev.eclipse.org/mailman/listinfo/aspectj-users > -- Simone Gianni CEO Semeru s.r.l. Apache Committer http://www.simonegianni.it/ _______________________________________________ aspectj-users mailing list aspectj-users@... https://dev.eclipse.org/mailman/listinfo/aspectj-users |
|
|
Re: help required in introducing new methodhi,
Thank you for replaying. "The second one is that you are declaring the toString on "SomeInterface" and then using field1, field2 etc.. Make sure those fields are declared on the class," I require the AspectJ to read all the declared fields(ideally only annotated fields) and generate the toString for it or read the public getters and generate the same. The field1 field2 etc are taken as an example. There can be fields other than this also. i need all of them printed in the toString. I added SomeInterface as parent because, thats the only way i figured the method can be added to all classes in com.comp.dto.*; i am not sure if there is a better way to do this. i suppose i am clear. regards, ravi On Tue, Sep 15, 2009 at 1:56 PM, Simone Gianni <simoneg@...> wrote: Hi Ravi, _______________________________________________ aspectj-users mailing list aspectj-users@... https://dev.eclipse.org/mailman/listinfo/aspectj-users |
|
|
Re: help required in introducing new methodHi Ravi,
if you need to access fields/getters dynamically then reflection is the only way to go. Suppose I have these classes : public class User { private String name; private String surname; // Getters/setters etc.. } public class Order { private String orderId; private String address; // Getters/setters etc.. } Then I cannot write a single method that creates a to-string of these two classes if not using reflection. AspectJ can help writing that method only once and then introducing it in all your (and not your) classes. Simone Ravi Chandra wrote: > hi, > > Thank you for replaying. > > "The second one is that you are declaring the toString on > "SomeInterface" and then using field1, field2 etc.. Make sure those > fields are declared on the class," > > I require the AspectJ to read all the declared fields(ideally only > annotated fields) and generate the toString for it or read the public > getters and generate the same. The field1 field2 etc are taken as an > example. There can be fields other than this also. i need all of them > printed in the toString. > > I added SomeInterface as parent because, thats the only way i figured > the method can be added to all classes in com.comp.dto.*; i am not > sure if there is a better way to do this. > > i suppose i am clear. > > regards, > ravi > > > On Tue, Sep 15, 2009 at 1:56 PM, Simone Gianni <simoneg@... > <mailto:simoneg@...>> wrote: > > Hi Ravi, > you don't specify which error is blocking you, so I'll assume two > possibilities. > > The first one is that fields you are trying to access are private, > so not visible from the aspect that is declaring the method. This > can be solved using "public privileged aspect". The "privileged" > keyword gives the aspect rights to access private members. > > The second one is that you are declaring the toString on > "SomeInterface" and then using field1, field2 etc.. Make sure > those fields are declared on the class, AspectJ is subject to the > same checks a normal class is, so if you declare a method on an > interface, that method will be able to access other methods/fields > in that interface or super interfaces. > > Hope this helps, > Simone > > Ravi Chandra wrote: > > hi, > > My problem is equivalent to implementing toString mehtod of a > class. *The output should be like: fieldVariable1= val1 | > fieldVariable2= val2 |... etc. I could introduce the toString > method but am unable to read the field variables and print > them. *Below is the approach i used. > > public aspect INAspect *{* > // INTRODUCTION > public String SomeInterface.toString(byte[] bytes) { > String s=""; > *// s = 'field1='+ this.field1 + 'field2=' + this.field2 > | ... etc* > return s; > } > > // SYNTHETIC IMPLEMENTS > declare parents: com.comp.attributes.* implements > SomeInterface; > *}* > > --- > > package com.comp.attributes; > public class UserAttributes *{* > private String field1 = "abc"; > private int field2 = 100; > ... getters and setters... > > * //here toString() should be introduced that is like > // "field1="+ this.field1+ "field2="+ this.field2 etc. > }* > > ------ > > when i decompile the code of user attibutes it is showing hte > toString implemented. except for reflections (which is not > prefered to be used) i want to know if there is a way to get > this achieved. I tried with AJType; but no luck. > > Any ideas/ pointers please. > ravi > ------------------------------------------------------------------------ > > _______________________________________________ > aspectj-users mailing list > aspectj-users@... <mailto:aspectj-users@...> > https://dev.eclipse.org/mailman/listinfo/aspectj-users > > > > > -- > Simone Gianni CEO Semeru s.r.l. Apache Committer > http://www.simonegianni.it/ > > _______________________________________________ > aspectj-users mailing list > aspectj-users@... <mailto:aspectj-users@...> > https://dev.eclipse.org/mailman/listinfo/aspectj-users > > > ------------------------------------------------------------------------ > > _______________________________________________ > aspectj-users mailing list > aspectj-users@... > https://dev.eclipse.org/mailman/listinfo/aspectj-users > -- Simone Gianni CEO Semeru s.r.l. Apache Committer http://www.simonegianni.it/ _______________________________________________ aspectj-users mailing list aspectj-users@... https://dev.eclipse.org/mailman/listinfo/aspectj-users |
| Free embeddable forum powered by Nabble | Forum Help |