Aspect questions?

N

Novice

Is this a reasonable place to ask questions about AspectJ?

I'm just getting started with it and it's not going really well so far. I'm
imitating simple examples from the manual and they just don't work. I'm not
sure what I'm messing up....

I don't recall ever seeing anyone ask aspect-oriented questions but I
certainly don't read every thread or even every subject line so I could
have missed some....
 
L

Lew

Novice said:
Is this a reasonable place to ask questions about AspectJ?

Isn't that one right there?

Does it seem reasonable to you?
I'm just getting started with it and it's not going really well so far. I'm
imitating simple examples from the manual and they just don't work. I'm not
sure what I'm messing up....

I don't recall ever seeing anyone ask aspect-oriented questions but I
certainly don't read every thread or even every subject line so I could
have missed some....

Since this is a newsgroup devoted to the Java programmer and AspectJ is a Java
programming tool, yes, of course this is the right forum.

Aspect programming is an advanced, hairy and special-purpose discipline. Are
you just learning it for the sake of learning it, or are you contemplating a
project use for it?

"Doctor, I have a problem," is a very limited request. We aren't sure what
you're messing up either.

There are some areas of Java programming that are almost never needed at the
application level, but are quite useful for framework development. They
include advance JavaBeans stuff like BeanInfo properties descriptors,
reflection beyond 'instanceof' and 'Class.newInstance()', class loader magic
and aspect programming.

Given your handle in this group, you might want to learn other things in Java
first before tacking aspect programming.

What is your use case for aspect programming?
 
M

markspace

I don't recall ever seeing anyone ask aspect-oriented questions but I
certainly don't read every thread or even every subject line so I could
have missed some....


I'm not an expert on Aspects or CDI, but I feel that AoP has largely
been replaced in the Java world by CDI (Context and Dependency
Injection). Which is perhaps why you haven't seen much discussion of AoP.

AoP is probably important in some fields of research and with other
languages, or with specialized systems. But most of us here are
generalists, and AoP doesn't seem to be used much, if at all, in general
Java programming.
 
A

Arne Vajhøj

Is this a reasonable place to ask questions about AspectJ?

I would say so.

AspectJ is almost entirely used to weave Java code into Java code,
so pretty much on topic I would say.
I'm just getting started with it and it's not going really well so far. I'm
imitating simple examples from the manual and they just don't work. I'm not
sure what I'm messing up....

You will need to post specific info.
I don't recall ever seeing anyone ask aspect-oriented questions but I
certainly don't read every thread or even every subject line so I could
have missed some....

Actually I do not remember an AspectJ question either.

But solutions based on AspectJ has been suggested for
hard problems occasionally.

(I know for sure, because I have done it)

Arne
 
T

Tom Anderson

Is this a reasonable place to ask questions about AspectJ?

I'm just getting started with it and it's not going really well so far.
I'm imitating simple examples from the manual and they just don't work.
I'm not sure what I'm messing up....

What does your java command line look like? Are you loading any necessary
agent?

tom
 
A

Arne Vajhøj

I'm not an expert on Aspects or CDI, but I feel that AoP has largely
been replaced in the Java world by CDI (Context and Dependency
Injection). Which is perhaps why you haven't seen much discussion of AoP.

I don't agree with that. I see very little overlap between what one
would use CDI for and what one would use AspectJ for.
AoP is probably important in some fields of research and with other
languages, or with specialized systems. But most of us here are
generalists, and AoP doesn't seem to be used much, if at all, in general
Java programming.

AOP is not as hot as it was 5 years ago.

But it is used in the Java world.

But mostly in frameworks and not so much in business code.

I believe several well known Java frameworks/apps uses
AspectJ including Spring and JBoss.

Arne
 
L

Lew

I don't agree with that. I see very little overlap between what one
would use CDI for and what one would use AspectJ for.


AOP is not as hot as it was 5 years ago.

But it is used in the Java world.

But mostly in frameworks and not so much in business code.

I believe several well known Java frameworks/apps uses
AspectJ including Spring and JBoss.

It is as you say. There are niches where bytecode weaving does wonders. JPA is
another such. But there's a Law of Demeter thing going on. When you use JPA,
or Spring, or JBoss, you don't normally think about the bytecode rewrites;
they exist in a dark layer hidden by the framework.

In a manner of speaking CDI and annotations generally are bytecode weavers,
but that isn't exactly right. Let's say they are cousins, in the same tribe as
reflection. Like all in that tribe, they share attributes of secrecy,
complication and the need to operate in isolation from more conventional
system components. In return they give you great magic.

In a well-designed framework those difficulties are covered for you. If you're
a framework developer, more power to you. You'll need it for the great
responsibility.
 
N

Novice

Lew said:
Isn't that one right there?

Does it seem reasonable to you?


Since this is a newsgroup devoted to the Java programmer and AspectJ
is a Java programming tool, yes, of course this is the right forum.

Aspect programming is an advanced, hairy and special-purpose
discipline. Are you just learning it for the sake of learning it, or
are you contemplating a project use for it?
Hey, I'm just making sure this is an appropriate place to ask before I
take the time to lay out my problem. This seems like a reasonable place
to ask but the rest of the group may not agree....

"Doctor, I have a problem," is a very limited request. We aren't sure
what you're messing up either.
Obviously. I haven't asked a specific question yet. I wanted to be sure
there was no major objection before laying out the issue.
There are some areas of Java programming that are almost never needed
at the application level, but are quite useful for framework
development. They include advance JavaBeans stuff like BeanInfo
properties descriptors, reflection beyond 'instanceof' and
'Class.newInstance()', class loader magic and aspect programming.

Given your handle in this group, you might want to learn other things
in Java first before tacking aspect programming.
There are dozens of things I'd like to learn about Java and I probably
need to learn several of them first.
What is your use case for aspect programming?
Based on other answers to my question, maybe I _don't_ want to learn
AspectJ!

I saw a seminar about aspects a couple of years back and thought it
sounded quite intriguing. It struck me that aspects might be a very good
way to handle what I am currently trying to do. But maybe there are
better ways. So let me lay out what I'm trying to accomplish and maybe
you can suggest a better approach.

I'd like to make both localization and logging a part of every class that
I write. The localization would allow each class to display text in any
(supported) language that the user requests and I'm also thinking of
localizing the messages in exceptions that get displayed or written to
logs. It seems like logging is the standard way to go for writing and/or
displaying messages to the user and to logs.

Now, I could go and add very similar code to lots and lots of classes to
accomplish those things but maybe writing aspects would enable me to
write a few lines of code that apply to EVERY class I've written. One
aspect could verify that a Locale had been passed to the class (assuming
it is invoked from another class) or throw an exception if it hadn't.
Another aspect could do something similar for the logger.

This is still just half-baked in my head but maybe it's clear enough that
you get the gist of it.

I'd be interested in the advice of the group on this issue.

By the way, I found another place to ask AspectJ questions and the issues
I had when I first posted here are now resolved. But the bigger issue
remains: are aspects a good approach for what I want to do? If not, what
would be a better strategy?
 
L

Lew

Hey, I'm just making sure this is an appropriate place to ask before I
take the time to lay out my problem. This seems like a reasonable place
to ask but the rest of the group may not agree....


Obviously. I haven't asked a specific question yet. I wanted to be sure
there was no major objection before laying out the issue.

There are dozens of things I'd like to learn about Java and I probably
need to learn several of them first.

Based on other answers to my question, maybe I _don't_ want to learn
AspectJ!

I wouldn't advise against learning it.

I do advise laying a good foundation. Certain advanced topics are
foundational, and you don't need to be linear and do nothing interesting.

For example, every Java programmer should understand serialization and have a
basic grasp of non-trivial reflection. More importantly, understand where the
various topics apply so you don't deploy them where they don't serve well.
I saw a seminar about aspects a couple of years back and thought it
sounded quite intriguing. It struck me that aspects might be a very good

They are and would be.
way to handle what I am currently trying to do. But maybe there are
better ways. So let me lay out what I'm trying to accomplish and maybe
you can suggest a better approach.

Very wise and balanced approach, that.
I'd like to make both localization and logging a part of every class that
I write. The localization would allow each class to display text in any
(supported) language that the user requests and I'm also thinking of
localizing the messages in exceptions that get displayed or written to
logs. It seems like logging is the standard way to go for writing and/or
displaying messages to the user and to logs.

The widespread practice for l10n (because you omit ten characters from
"localization") are

<http://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html>
[snip]
By the way, I found another place to ask AspectJ questions and the issues
I had when I first posted here are now resolved. But the bigger issue
remains: are aspects a good approach for what I want to do? If not, what
would be a better strategy?

You ask great questions.

I18n ("i[nternationalizatio]n") and l10n are most likely best handled with
resource bundles and properties. There is a lot of lore around that approach,
and it's just as flexible as aspects.

Logging is a great use case for aspects. However, there are alternatives to
lessening logging's overhead, too. You have to weigh costs and benefits.

It's not just in the code you weigh. Frameworks have a deployment cost. It's
harder to deploy than to write code. Framework bloat is a major problem in the
real world. Aspects also reduce visibility into areas, sometimes a good thing
and others not.

Locale management is best done in plain sight, IMO. You still have single code
points that translate automagically, so that's OK.

Aspects are to hide things from the code. Great, if you don't mind hunting for
them when you need them. XML configuration files can suffer this problem, too.
Too much externalization is bad just like too little.

I use code templates to handle logging. My editor autoinserts logging stuff
into new source files. That's enough for my laziness. Besides, the logging
statements help document the code, done right.

So where /should/ you use aspects? Hmm.

Like I say, you ask great questions.
 
A

Arved Sandstrom

Hey, I'm just making sure this is an appropriate place to ask before I
take the time to lay out my problem. This seems like a reasonable place
to ask but the rest of the group may not agree....


Obviously. I haven't asked a specific question yet. I wanted to be sure
there was no major objection before laying out the issue.

There are dozens of things I'd like to learn about Java and I probably
need to learn several of them first.

Based on other answers to my question, maybe I _don't_ want to learn
AspectJ!

I saw a seminar about aspects a couple of years back and thought it
sounded quite intriguing. It struck me that aspects might be a very good
way to handle what I am currently trying to do. But maybe there are
better ways. So let me lay out what I'm trying to accomplish and maybe
you can suggest a better approach.

I'd like to make both localization and logging a part of every class that
I write. The localization would allow each class to display text in any
(supported) language that the user requests and I'm also thinking of
localizing the messages in exceptions that get displayed or written to
logs. It seems like logging is the standard way to go for writing and/or
displaying messages to the user and to logs.

Now, I could go and add very similar code to lots and lots of classes to
accomplish those things but maybe writing aspects would enable me to
write a few lines of code that apply to EVERY class I've written. One
aspect could verify that a Locale had been passed to the class (assuming
it is invoked from another class) or throw an exception if it hadn't.
Another aspect could do something similar for the logger.

This is still just half-baked in my head but maybe it's clear enough that
you get the gist of it.

I'd be interested in the advice of the group on this issue.

By the way, I found another place to ask AspectJ questions and the issues
I had when I first posted here are now resolved. But the bigger issue
remains: are aspects a good approach for what I want to do? If not, what
would be a better strategy?
A bit of reasoning can go a long ways to telling you whether aspects
make sense for a particular job. This reasoning starts with an
examination of the join points that AspectJ makes available in its join
point model (JPM). You'll find this information tabulated in the AspectJ
programming guide.

You'll see that the join points available to us in AspectJ include
method calls/executions. The method call/execution join points have
state, one piece of which is the object array of method arguments. As
you might expect the other pieces of state are the objects involved
(only one for the method execution join point).

You also know, or should be starting to understand, that pointcuts are
the mechanism by which you choose *which* join points are in use. This
can be very powerful: for the method execution join point you could
easily select only the public methods for a given class, and that
becomes a certain pointcut that you use.

Dynamic pointcuts would even allow you to say that, for example, if you
have defined a pointcut, say Pointcut 1, that selects all the public
methods for class A (more precisely, selects the join points that
represent the executions for all the public methods in class A), that
you are also interested in the executions of methods that happen within
the context of those join points. In other words, if "public
A.someMethod(args)" runs, that's been identified by Pointcut 1. Pointcut
2 could include any methods that are *called by* A.someMethod(args).

Finally, with the _advice_, you have even more control on what code you
run at each selected join point, *when* you run it in temporal relation
to the join point (e.g. maybe you run some code if the method threw an
exception), and even whether you run the code at all. And as you might
expect, the advice knows about certain state - advice associated with a
pointcut has access to the state of the join points. So an advice
associated with our Pointcut 1 sees the method arguments.

Of all the other join points available in AspectJ, let's keep field
assignment and constructor calls/executions in mind as well, as we think
logging.

So let's consider logging, armed with all of this knowledge. If nothing
else, you know that you can log method calls & executions with great
flexibility. If you wanted to, you could be logging, using log4j say,
messages out at an error or warn level for exceptions thrown by any
methods which are called by public methods in package org.whatsit.*. At
the same time you might log the public calls in that package at debug level.

As an aside, you can see why this type of application of AOP is called
tracing. Very powerful tracing, but still execution tracing.

But, and this is one main "but", you don't have access to the guts of
interesting methods unless the internals are also identifiable by join
points. If there is an important conditional, the correct behaviour of
which is bedevilling you, and which relies on local variables computed
in a local chunk of code in that method, AspectJ has no eyes on it.
You're not going to be logging this kind of thing with aspects.

Another big "but", maybe not so much a "but" rather a general
consideration, is that if you start getting very fine-grained about
pointcuts, you're sort of defeating the purpose of AOP. We're, I expect,
considering a codebase that has no extensive logging, maybe none. If you
are going to spend inordinate amounts of time tailoring pointcuts and
advice for umpteen different types of logging, *you may as well bite the
bullet and go into the code and directly use the logging framework*,
rather than apply it through the agency of aspects.

In a nutshell, for logging, I myself have used AspectJ for emergency
maintenance: the codebase in question has no logging, or what it has is
useless, and there is limited time to add logging to source. So this is
execution tracing logging.

I hope this helps.

AHS
 
A

Arne Vajhøj

Based on other answers to my question, maybe I _don't_ want to learn
AspectJ!

I saw a seminar about aspects a couple of years back and thought it
sounded quite intriguing. It struck me that aspects might be a very good
way to handle what I am currently trying to do. But maybe there are
better ways. So let me lay out what I'm trying to accomplish and maybe
you can suggest a better approach.

I'd like to make both localization and logging a part of every class that
I write. The localization would allow each class to display text in any
(supported) language that the user requests and I'm also thinking of
localizing the messages in exceptions that get displayed or written to
logs. It seems like logging is the standard way to go for writing and/or
displaying messages to the user and to logs.

Now, I could go and add very similar code to lots and lots of classes to
accomplish those things but maybe writing aspects would enable me to
write a few lines of code that apply to EVERY class I've written. One
aspect could verify that a Locale had been passed to the class (assuming
it is invoked from another class) or throw an exception if it hadn't.
Another aspect could do something similar for the logger.

This is still just half-baked in my head but maybe it's clear enough that
you get the gist of it.

I'd be interested in the advice of the group on this issue.

By the way, I found another place to ask AspectJ questions and the issues
I had when I first posted here are now resolved. But the bigger issue
remains: are aspects a good approach for what I want to do? If not, what
would be a better strategy?

Those things seems perfectly valid to do using AOP.

Arne
 
A

Arne Vajhøj

It is as you say. There are niches where bytecode weaving does wonders.
JPA is another such. But there's a Law of Demeter thing going on. When
you use JPA, or Spring, or JBoss, you don't normally think about the
bytecode rewrites; they exist in a dark layer hidden by the framework.

In a manner of speaking CDI and annotations generally are bytecode
weavers, but that isn't exactly right. Let's say they are cousins, in
the same tribe as reflection.

Dynamic weaving may be in the same tribe as annotations/DI/reflection,
but static weaving is more in the tribe of another language.
Like all in that tribe, they share
attributes of secrecy, complication and the need to operate in isolation
from more conventional system components. In return they give you great
magic.

In a well-designed framework those difficulties are covered for you. If
you're a framework developer, more power to you. You'll need it for the
great responsibility.

The problems with AOP like AspectJ is that:
1) it is a more complex language than pure Java
2) it can be very difficult to read the code, because
the original Java class source code has no references
to the aspects weaved in, so it can be a problem finding
those

And the consequence of that combined with the skill sets of
the average Java business code developer is that AOP has
somewhat been delegated to frameworks.

But that was not the original intention with AOP. It was
expected to be used for ordinary business code.

Arne
 
A

Arved Sandstrom

It is as you say. There are niches where bytecode weaving does wonders.
JPA is another such. But there's a Law of Demeter thing going on. When
you use JPA, or Spring, or JBoss, you don't normally think about the
bytecode rewrites; they exist in a dark layer hidden by the framework.

In a manner of speaking CDI and annotations generally are bytecode
weavers, but that isn't exactly right. Let's say they are cousins, in
the same tribe as reflection. Like all in that tribe, they share
attributes of secrecy, complication and the need to operate in isolation
from more conventional system components. In return they give you great
magic.

In a well-designed framework those difficulties are covered for you. If
you're a framework developer, more power to you. You'll need it for the
great responsibility.
Another thing to consider is that every extra layer of bytecode
manipulation you add is another source of conflict. You might have
EclipseLink JPA doing weaving. AspectJ might be weaving. You might be
using AgitarOne for generating JUnit tests for legacy code. You might be
using Emma or Cobertura for code coverage, both of which instrument with
weaving. JRebel also does class rewriting.

I've run into situations many times - since I have used all of the above
- where one had to be careful about what order to have these bits weave.
Sometimes you can't control that, and you hope the combo works. In a
number of cases you simply cannot use two weavers together (I've found
it impossible to combine JRebel and EclipseLink with lazy weaving for
example).

I have no problem with bytecode rewriting, but the developer needs to be
aware of what's already there (maybe they are using EclipseLink JPA as
part of their Java EE) before they start adding extra layers of it.

AHS
 
M

markspace

You'll see that the join points available to us in AspectJ include
method calls/executions.


That was a great post on AoP, and I appreciate you taking the time to
type all that out. I have a couple of additional questions, if I may:

Does "method calls/execution" include static methods?

And, is all AoP in Java code weaving now, or does some of it involve
wrapper classes or reflection?
 
N

Novice

Lew said:
I wouldn't advise against learning it.

I do advise laying a good foundation. Certain advanced topics are
foundational, and you don't need to be linear and do nothing
interesting.

For example, every Java programmer should understand serialization and
have a basic grasp of non-trivial reflection. More importantly,
understand where the various topics apply so you don't deploy them
where they don't serve well.
One of these days, I'm going to have to get a comprehensive list of the
things I should know and then take a systematic approach to learning all
of them. But I don't think that's going to be today. Right now, I want to
focus on my logging and l10n....
They are and would be.


Very wise and balanced approach, that.
Thank you.
The widespread practice for l10n (because you omit ten characters from
"localization") are

<http://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html
I've already got that covered. I've been writing and using resource
bundles for some time. I'm just not sure I'm using them the best possible
way.
[snip]
By the way, I found another place to ask AspectJ questions and the
issues I had when I first posted here are now resolved. But the
bigger issue remains: are aspects a good approach for what I want to
do? If not, what would be a better strategy?

You ask great questions.

I18n ("i[nternationalizatio]n") and l10n are most likely best handled
with resource bundles and properties. There is a lot of lore around
that approach, and it's just as flexible as aspects.
As mentioned above, I already take advantage of resource bundles in
several projects, including the one that concerns me today.
Logging is a great use case for aspects. However, there are
alternatives to lessening logging's overhead, too. You have to weigh
costs and benefits.
What are other good ways to do logging?

Right now, the two main programs in my current project each create their
own logger and then pass it to the classes they call in their parameters.
The called classes write their messages to that logger. I've created a
method that does the logger creation and put it in a utility class in my
Common project so that my programs only need to execute that method
(passing in a few parameters of course) and get an appropriate logger
back. Each of the classes instantiated by the main programs gets a logger
passed among their parameters. The newly instantiated class then verifies
that the logger is not null and throws an IllegalArgumentException if it
is. Each class writes to the log when it seems appropriate. Sometimes,
that is to log an Exception. Other times, I log things that are more
diagnostic in nature. If a method isn't 100% right yet, I will log
calculations that it is doing, such as calculating the width of a column
in a JTable or whatever.

This works okay but it seems to me that it might be more elegant to put
the logging in aspects, especially the diagnostic stuff that I am using
to verify that the suspect methods are doing their jobs correctly.

I'm not sure yet if all the logging code, including the creation of the
log itself, should go into aspects though. It probably makes more sense
to create the logger the way I am doing it and continue to do the actual
writes to the logs in my existing classes when it involves Exception
handling. The diagnostic logging, though, seems like a prime candidate
for the aspects. I could write pointcuts that specify the methods that
are a little dubious and log them in as much detail as I need. Methods
that are already working to my satisfaction could be left alone with only
their Exception logging.

But if there is a better way to do things, I'm all ears.

I'm a one-man shop and if there are other Java programmers in this area,
I haven't found them yet. That makes newsgroups and forums my primary way
of finding out how the pros do things.
It's not just in the code you weigh. Frameworks have a deployment
cost. It's harder to deploy than to write code. Framework bloat is a
major problem in the real world. Aspects also reduce visibility into
areas, sometimes a good thing and others not.
What do you mean when you say "framework"? That term puts me in mind of
things like Spring - which I know of but have never used - but I'm not
using any formal frameworks like that and know very little about them.
I'm getting the impression that you are thinking of my work as being sort
of a homegrown framework. Maybe it is; I really don't know.
Locale management is best done in plain sight, IMO. You still have
single code points that translate automagically, so that's OK.

My code basically uses Locale.getDefault() to get the Locale implied by
the user.country and user.language JVM parameters (which can of course be
overridden within my projects JVM settings). I write ResourceBundles for
English and two other languages using Google Translate to the
translations. Not professional grade translations by any means but
sufficient for my purposes.
Aspects are to hide things from the code. Great, if you don't mind
hunting for them when you need them. XML configuration files can
suffer this problem, too. Too much externalization is bad just like
too little.
It shouldn't be very hard to find the aspects provided I name them well
and put them in the same package as the code it is monitoring. Aspects
that monitor every class I have are a bit more challenging but I'd put
them in my Common project in an appropriate package, assuming that's
possible. I haven't gotten far enough into the manual to find out how to
do an aspect that covers a lot of projects and classes but I'm assuming
it's possible given what I've already seen.
I use code templates to handle logging. My editor autoinserts logging
stuff into new source files. That's enough for my laziness. Besides,
the logging statements help document the code, done right.
So you use the code templates feature in Eclipse (or something equivalent
in a different IDE)?

I can't say I've done much with those. I mostly just copy and paste from
whatever project I most recently did that has the relevant code. And
every time I do that, I think seriously about making that code fragment a
method in a utility class. I even do it more often than not ;-)
So where /should/ you use aspects? Hmm.

Like I say, you ask great questions.
Thanks!
 
A

Arne Vajhøj

Does "method calls/execution" include static methods?

AspectJ can be used with static methods.
And, is all AoP in Java code weaving now, or does some of it involve
wrapper classes or reflection?

Static or dynamic weaving.

And weaving is more the how where wrapper is that what.

Arne
 
N

Novice

[snip]
A bit of reasoning can go a long ways to telling you whether aspects
make sense for a particular job. This reasoning starts with an
examination of the join points that AspectJ makes available in its
join point model (JPM). You'll find this information tabulated in the
AspectJ programming guide.

You'll see that the join points available to us in AspectJ include
method calls/executions. The method call/execution join points have
state, one piece of which is the object array of method arguments. As
you might expect the other pieces of state are the objects involved
(only one for the method execution join point).

You also know, or should be starting to understand, that pointcuts are
the mechanism by which you choose *which* join points are in use. This
can be very powerful: for the method execution join point you could
easily select only the public methods for a given class, and that
becomes a certain pointcut that you use.

Dynamic pointcuts would even allow you to say that, for example, if
you have defined a pointcut, say Pointcut 1, that selects all the
public methods for class A (more precisely, selects the join points
that represent the executions for all the public methods in class A),
that you are also interested in the executions of methods that happen
within the context of those join points. In other words, if "public
A.someMethod(args)" runs, that's been identified by Pointcut 1.
Pointcut 2 could include any methods that are *called by*
A.someMethod(args).

Finally, with the _advice_, you have even more control on what code
you run at each selected join point, *when* you run it in temporal
relation to the join point (e.g. maybe you run some code if the method
threw an exception), and even whether you run the code at all. And as
you might expect, the advice knows about certain state - advice
associated with a pointcut has access to the state of the join points.
So an advice associated with our Pointcut 1 sees the method arguments.

Of all the other join points available in AspectJ, let's keep field
assignment and constructor calls/executions in mind as well, as we
think logging.

So let's consider logging, armed with all of this knowledge. If
nothing else, you know that you can log method calls & executions with
great flexibility. If you wanted to, you could be logging, using log4j
say, messages out at an error or warn level for exceptions thrown by
any methods which are called by public methods in package
org.whatsit.*. At the same time you might log the public calls in that
package at debug level.

As an aside, you can see why this type of application of AOP is called
tracing. Very powerful tracing, but still execution tracing.

But, and this is one main "but", you don't have access to the guts of
interesting methods unless the internals are also identifiable by join
points. If there is an important conditional, the correct behaviour of
which is bedevilling you, and which relies on local variables computed
in a local chunk of code in that method, AspectJ has no eyes on it.
You're not going to be logging this kind of thing with aspects.
I think you just saved me a fair bit of time ;-) I was curious to see
whether I could use aspects to do what I'll call diagnostic logging. I
have some methods that do "calculations" of one kind or another, like the
best width for a column in a JTable given the width of the values in each
column, and it would be neat if I could remove all of that logging code
from the method itself and put it in aspects. Then, when the method is
working 100% to my satisfaction, I can discard that aspect and leave the
method uncluttered.

From what you're saying, I may not be able to capture the values of all
the variables that are being juggled in the suspect method. I had
wondered about that and was going to try it myself to see. I probably
still will but I'll be less surprised/disappointed if I can't access the
information I want.
Another big "but", maybe not so much a "but" rather a general
consideration, is that if you start getting very fine-grained about
pointcuts, you're sort of defeating the purpose of AOP. We're, I
expect, considering a codebase that has no extensive logging, maybe
none. If you are going to spend inordinate amounts of time tailoring
pointcuts and advice for umpteen different types of logging, *you may
as well bite the bullet and go into the code and directly use the
logging framework*, rather than apply it through the agency of
aspects.
Actually, I already have logging in place for most of classes and am
retrofitting it to the rest. I use the Java Logging classes, as opposed
to Log4j, but I'm not fanatical about it. I'm hearing more and more to
the effect that Log4j is better so maybe I'll make the switch. The better
I design my logging, the less painful that is going to be ;-)
In a nutshell, for logging, I myself have used AspectJ for emergency
maintenance: the codebase in question has no logging, or what it has
is useless, and there is limited time to add logging to source. So
this is execution tracing logging.

I hope this helps.
It does, Arved. Thanks!

So what do you use aspects for, besides the occasional need to add
logging into poorly-logged classes?
 
J

jlp

Le 25/02/2012 18:03, Novice a écrit :
Thanks, I'll probably use that in addition to this newsgroup.

and the main developper of AspectJ ( Andy CLEMENT) is very reactive in
the forum ( without flaming newbies ;-) )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top