Saturday, March 31, 2012

Endpoint documentation controller for Spring MVC 3.1

I saw a demo on new Spring MVC 3.1 features by Rossen Stoyanchev a while back and found one particular demo item incredibly useful.
The demonstration was for an Endpoint Controller - a page to display all the uri's supported by the application, their corresponding handler methods and related patterns(method, params etc).

The RequestMappingHandlerMapping component of Spring MVC maps the uri's to different @RequestMapped methods, and the demo item shows a way to expose these uri's and handler methods using a controller - the Endpoint documentation Controller, which looks like this:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

public class EndpointDocController {
 private final RequestMappingHandlerMapping handlerMapping;

 public EndpointDocController(RequestMappingHandlerMapping handlerMapping) {
  this.handlerMapping = handlerMapping;
 @RequestMapping(value="/endpointdoc", method=RequestMethod.GET)
 public void show(Model model) {
  model.addAttribute("handlerMethods", this.handlerMapping.getHandlerMethods());

and a jsp page to display this information cleanly:
<div class="container">
  <div class="container">
    <h1>Spring MVC 3.1 Demo Endpoints</h1>
    <c:forEach items="${handlerMethods}" var="entry">
      <div class="span-3 colborder">
          <span class="alt">Patterns:</span><br> 
          <c:if test="${not empty entry.key.patternsCondition.patterns}">

This would be something along the lines of what is displayed in the UI:

More information is available at the demo link and at Rossen Stoyanchev's github location:

Saturday, March 24, 2012

Some Bit manipulation tricks

1. Let's start with a simple one:
n<<1 is equivalent to n*2
More generally:
n<<i is equivalent to n*2i

Along the same lines:
n>>1 is equivalent to n/2
n>>i is equivalent to n/2i

2. To find the index of the most significant bit of a number or -1 if 0:
A few examples:
for 3 (11) it is 1
for 121(1111001) it is 6

An algorithm for this involves iterating the bits of a number and looks something like this in java - essentially keep right shifting until the value is 0:
int findIndexOfMostSignificantBit(int n){
       int s=0;
            n = n >> 1;
        return s>0?s-1:-1;

3. Multiply two numbers using bit manipulation:
I look at it this way:
a*b with b represented in binary can be thought of as a*(x*2^0 + x*2^1....x*2^n),
int multiply(int a, int b){
     int sum = 0;
     for (int i=0;b!=0;i++){
       sum += (a<<i);
      b = b>>1;
     return sum;

4. What does (n &(n-1))==0 mean?
Well, it is a way to check if a number can be represented as a power of 2 -
eg 8&7 becomes 1000 & 0111 which is 0, 7 & 6 becomes 0111 & 0110 which is NOT 0

5. Getting a bit at index i:

(n & (1<<i))!=0

6. Setting a bit at index i:

7. Clearing the bits between index i and j:
A sample for this is 121(1111001) clear bits between 4 and 2 yields in binary 1100001

The trick here is to create a mask which is all 1's except between i and j:
this mask can be created in two parts, the right part of the mask is created by left shifting 1 j places and subtracting 1 which results in all the bits from 0 to j-1 being set with 1.

the left part of the mask is created by negating 0 which yields all 1's, followed by shifting left i+1 places which puts 0's upto ith place.
int clearBits(int n, int i, int j){
    int left = (~0)<<i+1;
    int right = (1<<(j))-1;
    return n&(left|right);

Saturday, March 10, 2012

Code Policy Enforcement using AspectJ

AspectJ has a feature using which some of the architecture policies can be enforced in the codebase. This is the domain of static code analysis tools like PMD and Checkstyle, however AspectJ also can be a very good fit here, in providing a compile time feedback on any standard violations and if AJDT is integrated with Eclipse in providing an immediate feedback on violations to the developer .

Consider a simple rule that I had for one of my projects. I wanted to prevent @Cachable annotation to be used in the Controllers. A policy aspect enforcing this rule will look like this:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
public aspect NoCachingInController {
    declare error: execution(@RequestMapping @Cacheable * (@Controller *).*(..)) : "Contoller methods should not have the Cacheable annotation";

Now if anybody happens to add @Cacheable to the Controller methods would see the following in the Eclipse IDE with AJDT integrated:

And would see the following at the point of compilation:

Thursday, March 1, 2012