Enum value matching using AspectJ

I wrote an Aspect to advise a particular constant-specific method.

What is a constant-specific method ?

The J2SE 5 documentation explains it.

“You can declare the method abstract in the enum type and override it with a concrete method in each constant. Such methods are known as constant-specific methods.”

There is an example here.

The enum is

package com.test.generics;

import java.util.Collection;
import java.util.List;
import java.util.Set;

public enum TestEnum {
        public <T> List<T> getValue(){ return null; }
        public <T> List<T> getSameValue(){ return null; }
        public <T> Set <T>getValue(){ return null; }
        public <T> List<T> getSameValue(){ return null; }

   abstract <T> Collection<T> getValue();

   abstract <T> Collection<T> getSameValue();

	public static void main(String[] args) {
		System.out.println( Value1.getSameValue() );
		System.out.println( Value2.getSameValue() );

package com.test;

import java.util.List;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import com.test.generics.TestEnum;

public class EnumAspect {

                  Matches a particular method of all enum values
	"execution(List<T> getValue())" )
	public void testPointcut(){};

	public void test() {
		System.out.println( "Generics aspect" );

                  Matches a method of a particular enum values

	"execution(List<T> getSameValue()) && target(testEnum) && if()")
	public static boolean testPointcut1( TestEnum testEnum ){
		return testEnum == TestEnum.Value2;

	public void test1( TestEnum testEnum ) {
		System.out.println( "Generics aspect [" + testEnum.ordinal() + "]" );


The if() pointcut expression with an empty body is used and the testPointcut1 methods tests for the value of the enum. There does not seem to be a JoinPoint matching pattern to pick out certain enum values automatically. It looks like a pattern would be useful.

Update : Raised an enhancement request.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: