2

My question: is it possible to change method signature in AspectJ? For example, given class:

Class Foo {

     @Test
     public void shouldReturnOne(){} 
}

Change into:

Class Foo {

     @Test
     public void test_shouldReturnOne(){} 
}

I cannot find any information if it is possible. And if yes, how to do that? Thanks in advance.

Volker Stolz
  • 7,274
  • 1
  • 32
  • 50
wonsky
  • 503
  • 2
  • 7
  • Not sure I understand the question. Do you mean change the signature at runtime? Why do you want to do that? – Zutty May 23 '13 at 10:39
  • You can do that by byte code engineering tool like BCEL/javaAssist. – Jayan May 23 '13 at 10:41
  • 1
    @wonsky - It may not be directly supported by aspectj - see notes : http://www.eclipse.org/aspectj/doc/released/faq.php#q:methodsignatures. It says 'AspectJ does not enable you to change the signature of a method' – Jayan May 23 '13 at 10:47
  • @Zutty - Someone from management changed the policy of Sonar. For now every test method should contains word 'test'... – wonsky May 23 '13 at 11:29
  • Um, then you'd be doing it at runtime. I guess the policy's motivation was that LOOKING at the code it should be called 'test...'? So don't you want to refactor the existing source instead? – Volker Stolz May 23 '13 at 11:32
  • @ShiDoiSi - of course that's the best solution but I wondering if we can do it in the runtime. For now we've got 8k unit tests so the easiest way is create some script which transform all methods signature. Moreover we prefer use the should* convention of unit test naming. – wonsky May 23 '13 at 11:41

2 Answers2

1

I would change the source code by scripting an adequate refactoring, e.g. using Eclipse's LTK. As an advantage over scripts, that framework actually understands Java and annotations, so I guess it'll be less brittle than a "true" script.

(Or wait for management in the car park after dark.)

Community
  • 1
  • 1
Volker Stolz
  • 7,274
  • 1
  • 32
  • 50
0

No, you cannot do that with AspectJ because this is not AspectJ's purpose, just like AspectJ is not meant to cook tea for you. ;-)

AspectJ is meant to add or modify cross-cutting behaviour, not to refactor or obfuscate existing Java code and possibly break its contracts. There are other tools for those purposes.

kriegaex
  • 63,017
  • 15
  • 111
  • 202