2014-08-24T14:20:39+02:00http://sevenlist.github.io/Octopress2014-08-24T12:40:00+02:00http://sevenlist.github.io/2014/08/24/a-groovy-java-puzzler-setting-a-property-when-its-setter-is-overloadedTrying out Spring’s email support, I wanted to
send a simple message to a single recipient using code like:

Not having the book at hand at that time but vaguely remembering a Java Puzzler
for passing null to an overloaded method, I searched the Java Language Specification for how methods are chosen in my case.
I found out and refreshed my memory that Java chooses the most specific method.

As an array parameter is more specific than its single reference counterpart, the setTo(String[]) method is called.

With the Groovy logic to transform a String to a List of that String’s characters when that String is passed to a method
with a String array parameter, this results figuratively in an invocation like:

As each of those array entries is handled like a recipient, each is passed within a loop to InternetAddress.checkAddress(..).
The looping terminates while its third iteration as the “recipient” @ is passed to checkAddress(..) - the AddressException is thrown, stating
Missing local name in string ``@''. Finally, this message makes more sense, doesn’t it?

By the way, to solve the problem I just wrote:

1

mailMessage.to=['me@trash-mail.com']

All in all, isn’t that a groovy Java Puzzler?

]]>2014-08-24T12:05:35+02:00http://sevenlist.github.io/2014/08/24/anonymous-class-field-initialized-with-an-enclosing-argument-results-in-missingpropertyexceptionVote for the GROOVY-6996 bug!
]]>2014-08-24T12:05:00+02:00http://sevenlist.github.io/2014/08/24/spring-at-transactional-with-aspectjIf we want to use the @Transactional annotation on private methods, we cannot use Spring’s default proxy mode -
it only works for public methods that are called from client code.

For self-invoked methods we need to use AspectJ.

An Example

This may be the case e.g. when we want to save incoming data and send a letter using that data within a transaction.
If saving the data fails we do not want to send the letter.
If sending the letter fails we do not want to loose the data that we received.
Hence, we want to save the data in a separate transaction, by annotating our saveData(..) method with
@Transactional(propagation = Propagation.REQUIRES_NEW).

Configuring Spring To Use AspectJ

On a @Configuration class we just need to set the AspectJ advice mode for the transaction management configuration:

Configuring Maven To Run The AspectJ Compiler

Next we need to use the AspectJ compiler, so let’s add its needed dependencies:

1234567891011121314151617181920

<dependencies><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.7.4</version></dependency><!-- In this example we use JPA for persistence. --><dependency><groupId>org.hibernate.javax.persistence</groupId><artifactId>hibernate-jpa-2.1-api</artifactId><version>1.0.0.Final</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>4.0.6.RELEASE</version></dependency></dependencies>

If we do not use <forceAjcCompile> and do not set the <weaveDirectory> AspectJ will not weave the transaction code into
our generated Groovy bytecode. Instead, it will warn us with No sources found skipping aspectJ compile when Maven runs.
That is as the aspectj-maven-plugin processes the src/main/java directory by default but not src/main/groovy.

How The AspectJ Generated Code Looks Like

Just for completeness, let’s look how AspectJ has wrapped the saveData(..) method with a transaction. Using the
Procyon decompiler we can see: