title: AspectJ pointcut for constructor using java.lang.reflection
body: <p>The following example is a reduction of the real problem in that it tries to simplify is as much as possible.
  <p>
  I have a java interface, and several objects that implement that interface, like:</p>
  
  <pre><code>public interface Shape{
      public void draw();
      public void erase();
      public boolean isDrawn();
  }
  
  public class Square implements Shape{
      @Override
      public void draw(){
          //TODO: method implementation
      }
  
      @Override
      public void erase(){
          //TODO: method implementation
      } 
  
      Override
      public boolean isDrawn(){
          //TODO: method implementation
          return false;
      }
  }
  
  public Triangle implements Shape{
      //same as above
  }
  
  public Circle implements Shape{
      //same as above
  }
  </code></pre>
  
  <p>This is the structure of my program. By using AspectJ I want to have a map that holds each object that implements the interface. To do so I was trying to capture the constructors by using the following aspect:</p>
  
  <pre><code>public aspect ShapeHolderAspect{
      private Map&lt;Integer, Shape&gt; map = new HashMap&lt;&gt;();
      private int count = 0;    
  
      pointcut shapeInit(): call((Shape+).new(..));
  
      Object around(): shapeInit() {
          System.out.println("capturing new");
  
          Shape shapeType = (Shape)proceed();
          map.put(++count, shapeType);
          return shapeType;
      }
  }
  </code></pre>
  
  <p>This code will work if I create a Shape using the following scenario:</p>
  
  <pre><code>public static void main(String[] args){
      Shape myShape = new Circle();
  }
  </code></pre>
  
  <p>However, I am using java language reflection, and so technically I don't call the "new" constructor. Instead I locate the path of the package, and create the object passing a string with the name of the class:</p>
  
  <pre><code>public static void main(String[] args){
      String shapeClassName = args[0];
      Class&lt;?&gt; classType = Class.forName("myPackage.figures" + "." + shapeClassName);
      Shape myShape =(Shape)classType.getConstructor().newInstance();
  }
  </code></pre>
  
  <p>By doing this way, AspectJ cannot detect that I am creating shapes. How do I fix this?</p>