Add inter-type public static method to classes
in my project I have a set of classes (whose names and number I don't apriori) which I want to add a public static method to.
For simplicity, let assume that I want to add a static method which returns the static logger instance of the class. The only requirement is that the static function should be called as follows:
public class Foo {
private static final Logger LOG = LoggerFactor.getLogger(Foo.class);
public static void main(String args) {
Foo.getLogger().info("works!");
}
}
Since I think this a cross cutting concern I think solving my issue with AspectJ here, but I didn't find any information about my scenario.
My questions are:
- if this is even possible?
- if so, how can you achieve it?
I'm aware of the possibility of dyamically declare the parent of my classes implementing an interface, but then I'm stuck because I cannot generate static methods on interface:
public aspect StaticMethodAspect {
public interface HasStaticMethod {}
declare parents: ... implements HasStaticMethod;
public static Logger HasStaticMethod.getLogger() { //aspect error
...
}
}
And I'm also aware of this solution, but it doesn't meet my requirement about the way of call the method.
Thanks for any kind reply
java static aop aspectj aspect
add a comment |
in my project I have a set of classes (whose names and number I don't apriori) which I want to add a public static method to.
For simplicity, let assume that I want to add a static method which returns the static logger instance of the class. The only requirement is that the static function should be called as follows:
public class Foo {
private static final Logger LOG = LoggerFactor.getLogger(Foo.class);
public static void main(String args) {
Foo.getLogger().info("works!");
}
}
Since I think this a cross cutting concern I think solving my issue with AspectJ here, but I didn't find any information about my scenario.
My questions are:
- if this is even possible?
- if so, how can you achieve it?
I'm aware of the possibility of dyamically declare the parent of my classes implementing an interface, but then I'm stuck because I cannot generate static methods on interface:
public aspect StaticMethodAspect {
public interface HasStaticMethod {}
declare parents: ... implements HasStaticMethod;
public static Logger HasStaticMethod.getLogger() { //aspect error
...
}
}
And I'm also aware of this solution, but it doesn't meet my requirement about the way of call the method.
Thanks for any kind reply
java static aop aspectj aspect
add a comment |
in my project I have a set of classes (whose names and number I don't apriori) which I want to add a public static method to.
For simplicity, let assume that I want to add a static method which returns the static logger instance of the class. The only requirement is that the static function should be called as follows:
public class Foo {
private static final Logger LOG = LoggerFactor.getLogger(Foo.class);
public static void main(String args) {
Foo.getLogger().info("works!");
}
}
Since I think this a cross cutting concern I think solving my issue with AspectJ here, but I didn't find any information about my scenario.
My questions are:
- if this is even possible?
- if so, how can you achieve it?
I'm aware of the possibility of dyamically declare the parent of my classes implementing an interface, but then I'm stuck because I cannot generate static methods on interface:
public aspect StaticMethodAspect {
public interface HasStaticMethod {}
declare parents: ... implements HasStaticMethod;
public static Logger HasStaticMethod.getLogger() { //aspect error
...
}
}
And I'm also aware of this solution, but it doesn't meet my requirement about the way of call the method.
Thanks for any kind reply
java static aop aspectj aspect
in my project I have a set of classes (whose names and number I don't apriori) which I want to add a public static method to.
For simplicity, let assume that I want to add a static method which returns the static logger instance of the class. The only requirement is that the static function should be called as follows:
public class Foo {
private static final Logger LOG = LoggerFactor.getLogger(Foo.class);
public static void main(String args) {
Foo.getLogger().info("works!");
}
}
Since I think this a cross cutting concern I think solving my issue with AspectJ here, but I didn't find any information about my scenario.
My questions are:
- if this is even possible?
- if so, how can you achieve it?
I'm aware of the possibility of dyamically declare the parent of my classes implementing an interface, but then I'm stuck because I cannot generate static methods on interface:
public aspect StaticMethodAspect {
public interface HasStaticMethod {}
declare parents: ... implements HasStaticMethod;
public static Logger HasStaticMethod.getLogger() { //aspect error
...
}
}
And I'm also aware of this solution, but it doesn't meet my requirement about the way of call the method.
Thanks for any kind reply
java static aop aspectj aspect
java static aop aspectj aspect
edited Jan 19 at 11:37
Koldar
asked Jan 19 at 11:21
KoldarKoldar
496314
496314
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
If you want to declare static methods or members via ITD you need to know the class name, which is not the case for you. So you are stuck with doing things similar to what you already found, see also my answers here:
- https://stackoverflow.com/a/29060099/1082681
- https://stackoverflow.com/a/12127220/1082681
- https://stackoverflow.com/a/7393364/1082681
These examples also show how to log directly from another aspect because usually logging is also a cross-cutting concern. So if you can avoid logging manually, just use my approach.
But if you definitely do want to have a static logger for each target class and indeed use it the way your sample code shows, use the AspectJ integration with annotation processing briefly described and linked to from my first answer on the above list. Feel free to ask follow-up questions in case you do not understand the sample code from both links I provided there.
Hello @kriegaex, I was actually hoping you would answer me! Together with the aspect documentation, I read several answers of yours while learning aspectj (included the second and the third link you've provided). That being said, I'm intrigued on the first link, in particular on the Annotation processing: basically it allows the dynamically generates aspects given a specific annotation? in the readmeajcseems to require an annotation, aside as processor. Is my intuition correct?
– Koldar
Jan 20 at 10:18
Yes, if I understand your question correctly. You annotate the target classes with an (probably a self-defined) annotation, something like@Log. Then your processor is triggered whenever an annotated class would is found during the build's annotation processing phase. The processor creates the corresponding aspect source code files doing the ITD, one per target class. Then compilation proceeds normally and all your classes and aspects (including the APT-generated ones) get compiled and woven. You can do that manually or via AspectJ Maven plugin.
– kriegaex
Jan 20 at 14:52
1
The only thing I do not like so much about this approach is that you actually have to "pollute" your own application classes with annotations serving the sole purpose of being picked up by the AOP-related annotation processor. I like to keep my own aspect out of my application source code as much as possible. But currently ITD cannot use type name patterns like normal pointcuts, which is an AspectJ limitation. I just started discussing this issue with a the lead AspectJ maintainer on the mailing list, let's see what he says.
– kriegaex
Jan 20 at 14:55
Good to hear that this question lead to something good. In the meantime, I will accept your answer. I think at the moment my problem can indeed be solved with annotation processing.
– Koldar
Jan 21 at 8:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54266555%2fadd-inter-type-public-static-method-to-classes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to declare static methods or members via ITD you need to know the class name, which is not the case for you. So you are stuck with doing things similar to what you already found, see also my answers here:
- https://stackoverflow.com/a/29060099/1082681
- https://stackoverflow.com/a/12127220/1082681
- https://stackoverflow.com/a/7393364/1082681
These examples also show how to log directly from another aspect because usually logging is also a cross-cutting concern. So if you can avoid logging manually, just use my approach.
But if you definitely do want to have a static logger for each target class and indeed use it the way your sample code shows, use the AspectJ integration with annotation processing briefly described and linked to from my first answer on the above list. Feel free to ask follow-up questions in case you do not understand the sample code from both links I provided there.
Hello @kriegaex, I was actually hoping you would answer me! Together with the aspect documentation, I read several answers of yours while learning aspectj (included the second and the third link you've provided). That being said, I'm intrigued on the first link, in particular on the Annotation processing: basically it allows the dynamically generates aspects given a specific annotation? in the readmeajcseems to require an annotation, aside as processor. Is my intuition correct?
– Koldar
Jan 20 at 10:18
Yes, if I understand your question correctly. You annotate the target classes with an (probably a self-defined) annotation, something like@Log. Then your processor is triggered whenever an annotated class would is found during the build's annotation processing phase. The processor creates the corresponding aspect source code files doing the ITD, one per target class. Then compilation proceeds normally and all your classes and aspects (including the APT-generated ones) get compiled and woven. You can do that manually or via AspectJ Maven plugin.
– kriegaex
Jan 20 at 14:52
1
The only thing I do not like so much about this approach is that you actually have to "pollute" your own application classes with annotations serving the sole purpose of being picked up by the AOP-related annotation processor. I like to keep my own aspect out of my application source code as much as possible. But currently ITD cannot use type name patterns like normal pointcuts, which is an AspectJ limitation. I just started discussing this issue with a the lead AspectJ maintainer on the mailing list, let's see what he says.
– kriegaex
Jan 20 at 14:55
Good to hear that this question lead to something good. In the meantime, I will accept your answer. I think at the moment my problem can indeed be solved with annotation processing.
– Koldar
Jan 21 at 8:55
add a comment |
If you want to declare static methods or members via ITD you need to know the class name, which is not the case for you. So you are stuck with doing things similar to what you already found, see also my answers here:
- https://stackoverflow.com/a/29060099/1082681
- https://stackoverflow.com/a/12127220/1082681
- https://stackoverflow.com/a/7393364/1082681
These examples also show how to log directly from another aspect because usually logging is also a cross-cutting concern. So if you can avoid logging manually, just use my approach.
But if you definitely do want to have a static logger for each target class and indeed use it the way your sample code shows, use the AspectJ integration with annotation processing briefly described and linked to from my first answer on the above list. Feel free to ask follow-up questions in case you do not understand the sample code from both links I provided there.
Hello @kriegaex, I was actually hoping you would answer me! Together with the aspect documentation, I read several answers of yours while learning aspectj (included the second and the third link you've provided). That being said, I'm intrigued on the first link, in particular on the Annotation processing: basically it allows the dynamically generates aspects given a specific annotation? in the readmeajcseems to require an annotation, aside as processor. Is my intuition correct?
– Koldar
Jan 20 at 10:18
Yes, if I understand your question correctly. You annotate the target classes with an (probably a self-defined) annotation, something like@Log. Then your processor is triggered whenever an annotated class would is found during the build's annotation processing phase. The processor creates the corresponding aspect source code files doing the ITD, one per target class. Then compilation proceeds normally and all your classes and aspects (including the APT-generated ones) get compiled and woven. You can do that manually or via AspectJ Maven plugin.
– kriegaex
Jan 20 at 14:52
1
The only thing I do not like so much about this approach is that you actually have to "pollute" your own application classes with annotations serving the sole purpose of being picked up by the AOP-related annotation processor. I like to keep my own aspect out of my application source code as much as possible. But currently ITD cannot use type name patterns like normal pointcuts, which is an AspectJ limitation. I just started discussing this issue with a the lead AspectJ maintainer on the mailing list, let's see what he says.
– kriegaex
Jan 20 at 14:55
Good to hear that this question lead to something good. In the meantime, I will accept your answer. I think at the moment my problem can indeed be solved with annotation processing.
– Koldar
Jan 21 at 8:55
add a comment |
If you want to declare static methods or members via ITD you need to know the class name, which is not the case for you. So you are stuck with doing things similar to what you already found, see also my answers here:
- https://stackoverflow.com/a/29060099/1082681
- https://stackoverflow.com/a/12127220/1082681
- https://stackoverflow.com/a/7393364/1082681
These examples also show how to log directly from another aspect because usually logging is also a cross-cutting concern. So if you can avoid logging manually, just use my approach.
But if you definitely do want to have a static logger for each target class and indeed use it the way your sample code shows, use the AspectJ integration with annotation processing briefly described and linked to from my first answer on the above list. Feel free to ask follow-up questions in case you do not understand the sample code from both links I provided there.
If you want to declare static methods or members via ITD you need to know the class name, which is not the case for you. So you are stuck with doing things similar to what you already found, see also my answers here:
- https://stackoverflow.com/a/29060099/1082681
- https://stackoverflow.com/a/12127220/1082681
- https://stackoverflow.com/a/7393364/1082681
These examples also show how to log directly from another aspect because usually logging is also a cross-cutting concern. So if you can avoid logging manually, just use my approach.
But if you definitely do want to have a static logger for each target class and indeed use it the way your sample code shows, use the AspectJ integration with annotation processing briefly described and linked to from my first answer on the above list. Feel free to ask follow-up questions in case you do not understand the sample code from both links I provided there.
answered Jan 20 at 3:12
kriegaexkriegaex
31.1k365101
31.1k365101
Hello @kriegaex, I was actually hoping you would answer me! Together with the aspect documentation, I read several answers of yours while learning aspectj (included the second and the third link you've provided). That being said, I'm intrigued on the first link, in particular on the Annotation processing: basically it allows the dynamically generates aspects given a specific annotation? in the readmeajcseems to require an annotation, aside as processor. Is my intuition correct?
– Koldar
Jan 20 at 10:18
Yes, if I understand your question correctly. You annotate the target classes with an (probably a self-defined) annotation, something like@Log. Then your processor is triggered whenever an annotated class would is found during the build's annotation processing phase. The processor creates the corresponding aspect source code files doing the ITD, one per target class. Then compilation proceeds normally and all your classes and aspects (including the APT-generated ones) get compiled and woven. You can do that manually or via AspectJ Maven plugin.
– kriegaex
Jan 20 at 14:52
1
The only thing I do not like so much about this approach is that you actually have to "pollute" your own application classes with annotations serving the sole purpose of being picked up by the AOP-related annotation processor. I like to keep my own aspect out of my application source code as much as possible. But currently ITD cannot use type name patterns like normal pointcuts, which is an AspectJ limitation. I just started discussing this issue with a the lead AspectJ maintainer on the mailing list, let's see what he says.
– kriegaex
Jan 20 at 14:55
Good to hear that this question lead to something good. In the meantime, I will accept your answer. I think at the moment my problem can indeed be solved with annotation processing.
– Koldar
Jan 21 at 8:55
add a comment |
Hello @kriegaex, I was actually hoping you would answer me! Together with the aspect documentation, I read several answers of yours while learning aspectj (included the second and the third link you've provided). That being said, I'm intrigued on the first link, in particular on the Annotation processing: basically it allows the dynamically generates aspects given a specific annotation? in the readmeajcseems to require an annotation, aside as processor. Is my intuition correct?
– Koldar
Jan 20 at 10:18
Yes, if I understand your question correctly. You annotate the target classes with an (probably a self-defined) annotation, something like@Log. Then your processor is triggered whenever an annotated class would is found during the build's annotation processing phase. The processor creates the corresponding aspect source code files doing the ITD, one per target class. Then compilation proceeds normally and all your classes and aspects (including the APT-generated ones) get compiled and woven. You can do that manually or via AspectJ Maven plugin.
– kriegaex
Jan 20 at 14:52
1
The only thing I do not like so much about this approach is that you actually have to "pollute" your own application classes with annotations serving the sole purpose of being picked up by the AOP-related annotation processor. I like to keep my own aspect out of my application source code as much as possible. But currently ITD cannot use type name patterns like normal pointcuts, which is an AspectJ limitation. I just started discussing this issue with a the lead AspectJ maintainer on the mailing list, let's see what he says.
– kriegaex
Jan 20 at 14:55
Good to hear that this question lead to something good. In the meantime, I will accept your answer. I think at the moment my problem can indeed be solved with annotation processing.
– Koldar
Jan 21 at 8:55
Hello @kriegaex, I was actually hoping you would answer me! Together with the aspect documentation, I read several answers of yours while learning aspectj (included the second and the third link you've provided). That being said, I'm intrigued on the first link, in particular on the Annotation processing: basically it allows the dynamically generates aspects given a specific annotation? in the readme
ajc seems to require an annotation, aside as processor. Is my intuition correct?– Koldar
Jan 20 at 10:18
Hello @kriegaex, I was actually hoping you would answer me! Together with the aspect documentation, I read several answers of yours while learning aspectj (included the second and the third link you've provided). That being said, I'm intrigued on the first link, in particular on the Annotation processing: basically it allows the dynamically generates aspects given a specific annotation? in the readme
ajc seems to require an annotation, aside as processor. Is my intuition correct?– Koldar
Jan 20 at 10:18
Yes, if I understand your question correctly. You annotate the target classes with an (probably a self-defined) annotation, something like
@Log. Then your processor is triggered whenever an annotated class would is found during the build's annotation processing phase. The processor creates the corresponding aspect source code files doing the ITD, one per target class. Then compilation proceeds normally and all your classes and aspects (including the APT-generated ones) get compiled and woven. You can do that manually or via AspectJ Maven plugin.– kriegaex
Jan 20 at 14:52
Yes, if I understand your question correctly. You annotate the target classes with an (probably a self-defined) annotation, something like
@Log. Then your processor is triggered whenever an annotated class would is found during the build's annotation processing phase. The processor creates the corresponding aspect source code files doing the ITD, one per target class. Then compilation proceeds normally and all your classes and aspects (including the APT-generated ones) get compiled and woven. You can do that manually or via AspectJ Maven plugin.– kriegaex
Jan 20 at 14:52
1
1
The only thing I do not like so much about this approach is that you actually have to "pollute" your own application classes with annotations serving the sole purpose of being picked up by the AOP-related annotation processor. I like to keep my own aspect out of my application source code as much as possible. But currently ITD cannot use type name patterns like normal pointcuts, which is an AspectJ limitation. I just started discussing this issue with a the lead AspectJ maintainer on the mailing list, let's see what he says.
– kriegaex
Jan 20 at 14:55
The only thing I do not like so much about this approach is that you actually have to "pollute" your own application classes with annotations serving the sole purpose of being picked up by the AOP-related annotation processor. I like to keep my own aspect out of my application source code as much as possible. But currently ITD cannot use type name patterns like normal pointcuts, which is an AspectJ limitation. I just started discussing this issue with a the lead AspectJ maintainer on the mailing list, let's see what he says.
– kriegaex
Jan 20 at 14:55
Good to hear that this question lead to something good. In the meantime, I will accept your answer. I think at the moment my problem can indeed be solved with annotation processing.
– Koldar
Jan 21 at 8:55
Good to hear that this question lead to something good. In the meantime, I will accept your answer. I think at the moment my problem can indeed be solved with annotation processing.
– Koldar
Jan 21 at 8:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54266555%2fadd-inter-type-public-static-method-to-classes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown