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 {
    Value1{
        public <T> List<T> getValue(){ return null; }
        public <T> List<T> getSameValue(){ return null; }
   },
   Value2{
        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;

@Aspect()
public class EnumAspect {

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

	@Before("testPointcut()")
	public void test() {
		System.out.println( "Generics aspect" );
	}

                /*
                  Matches a method of a particular enum values
               */   

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

	@Before("testPointcut1(testEnum)")
	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.

Meaning of Story points

I spent a few months on an agile project but did not understand the meaning of User Story Points until I read “Hours vs. Story Points”

Ruby mixin

Like many other programmers I wish I had started learning Ruby when it was created, all the more so for the demand for broad programming skills now. I started using NetBeans 6.1 which made JRuby programming easier. I started to look at some example code written in other languages and write equivalent Ruby code. If you have good programming experience ( Updated : Hope it is not the Kruger-Dunning effect :-)then you can start with fairly complex code when learning a new language.

The example is JavaScript event code that can be mixed-in with other code. Actually I came across many powerful features of JavaScript when I programmed using RhinoScript recently.

I hope to update the code but as of now I have the skeleton below. I wouldn’t be able to explain everything because this is my first serious piece of Ruby code. I will try to use this “mixin” code along with other code.

    Updated : Used a module. Exploring how to write proper test code.

event_mixin_module.rb

module EventMixinModule
  $listeners

  EventManager   = { "notify" => proc {
                                    puts "notify"
                                    if ( nil == defined?($listeners) )
                                      puts "No listeners registered"
                                    end
                                    puts $listeners
                                  },
                     "listen" => proc {
                                    |listen|
                                    puts "listen"
                                    $listeners = listen || [];
                                 }
                  }
  puts EventManager["listen"].call( "listener" )
  #puts EventManager["notify"].call
    
end

domain_object.rb


require "event_mixin_module"

class DomainObject
  include EventMixinModule
  def initialize
    puts "DomainObject"
  end
  
  def setvalue
  puts "setvalue"
  EventManager["notify"].call
  end

end

domain_object_test.rb


require "domain_object"

class DomainObjectTest
  def initialize
    
  end
  domainobject = DomainObject.new
  domainobject.setvalue
  
end