[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Creating a crosscut using ClassLoader.Loadclass using AspectJ

>> To do so, I planned to use a crosscut on the ClassLoader.LoadClass
function, so I can check each class which is loaded in order to list
it's methods (it's okay to check only the loaded classes).

Is that really necessary? Seems to me that you are making it harder
for yourself.

Here is an aspect I wrote It seems to do what you want

public aspect CodeCoverAspect pertypewithin(*){
  private static Set types = new HashSet();
  private Set ms = new HashSet();
  pointcut init() : staticinitialization(*) &&
!staticinitialization(CodeCoverAspect)
&&!staticinitialization(ControlAspect);
  pointcut mexec() : execution(* *(..)) && !within(CodeCoverAspect) &&
!within(ControlAspect);

  declare soft : ClassNotFoundException : within(CodeCoverAspect);
  declare soft : NoSuchMethodException : within(CodeCoverAspect);

  after() returning() : init() {
      String typeName = getWithinTypeName();
      Class clazz = Class.forName(typeName);
      Method[] m = clazz.getDeclaredMethods();
      ms.addAll(Arrays.asList(m));
      types.add(typeName);
  }

  before() : mexec() {
      String mName = thisJoinPointStaticPart.getSignature().getName();
      Object[] args = thisJoinPoint.getArgs();
      Class[] types = new Class[args.length];
      for(int i=0;i<args.length;i++){
          types[i] = args[i].getClass();
      }
      Class clazz = thisJoinPointStaticPart.getSignature().getDeclaringType();
      Method m = clazz.getDeclaredMethod(mName, types);
      ms.remove(m);
  }

  void report(){
      Iterator it = ms.iterator();
      while(it.hasNext()) {
          Method m = (Method)it.next();
          if(m.getName().indexOf('$') < 0)
          System.out.println(m.getName() + " in type " +
m.getDeclaringClass().getName() + " does not have code coverage.");
      }
  }

  static Set types(){
      return Collections.unmodifiableSet(types);
  }
}

and another aspect to capture main(...) to do the reporting

public aspect ControlAspect{
    pointcut mains() : execution(public static void *.main(String[]));

    after() returning() : mains() {
        Set types = CodeCoverAspect.types();
        Iterator it = types.iterator();
        while(it.hasNext()){
            try {
              Class clazz = Class.forName((String)it.next());
              if(CodeCoverAspect.hasAspect(clazz)) {
                  CodeCoverAspect cca = CodeCoverAspect.aspectOf(clazz);
                  cca.report();
              }
            }
            catch(ClassNotFoundException ex){
                ex.printStackTrace();
            }
        }
    }

}

On Dec 30, 2007 10:25 PM, Boudewijn Ector <boudewijn@xxxxxxxxxxxxxxxxx> wrote:
> Dear peole,
>
> For a university assignment, I had to do a number of assignments. One of
> these (the last one) is creating an aspect which checks for code
> coverage, specifically function coverage.
>
> To do so, I planned to use a crosscut on the ClassLoader.LoadClass
> function, so I can check each class which is loaded in order to list
> it's methods (it's okay to check only the loaded classes).
>
> But whatever I'm doing, the Aspect just won't get triggered.
> My current code :
>
> after() returning(Class <?> c):  target(ClassLoader) &&  call (Class<?>
> loadClass(..))  && !within(SWE35)
>
> SWE35 is the current aspect. Afaik, this should work but I've also
> tested while removing the <?> part (generics don't have to be
> explicitely mentioned afaik). Can someone tell me what I'm doing wrong?
> I've written succesfully about 25 aspects, but I just can't figure this
> one out.
>
>
> I'm using Sun Java 1.6 on a Linux box, and Eclipse-3.2 using it's
> respective AspectJ plugin (the newest one) without further security
> modifications.
>
>
>
> Cheers!
>
> Boudewijn Ector
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>