Pointcut with a class name pattern - java

test.core and I want an aspect around every class in that or a sub-package with the name pattern Service.
sth like this: "execution(public de.test.core..Service.*(..)" but it doesn t seem to work.
Is aspectJ even able to match to a class pattern?

Match all methods defined in beans whose name ends with ‘Service’.
bean(*Service)
Match by Service pattern
#Pointcut("within(*..*Service)")
public void inServiceClass() {}

Related

Spring cannot include/exclude beans using filters of type REGEX with an asterisk in #ComponentScan

I am trying to manage the beans my spring application loads using IncludeFilters property of #ComponentScan annotation. I use filter of type FilterType.REGEX. I would like to match anything as the last part of my pattern using but I seems not to work that way at all.
I have a bean definitions:
package org.example.child;
public class ChildDao {}
...
public class ChildService{}
...
public class ChildComponent{}
and configuration class definition:
#ComponentScan(
value = "com.example",
includeFilters = {#ComponentScan.Filter(type = FilterType.REGEX,
pattern = "com.example.*.Child*")})
With such a configuration, Spring does not find any bean at all.
When asterisk is used to match not the very last part of the pattern but is used somewhere in between, then it seems to work without a problem.
For example, following configuration matches all the Services without a problem:
#ComponentScan(
value = "com.example",
includeFilters = {#ComponentScan.Filter(type = FilterType.REGEX,
pattern = "com.example.*.*Service")})
Is this a designed behaviour of the framework or should it be possible to match the last part of the pattern using such an 'ant like' regex pattern?
Filters of type FilterType.REGEX are matched using standard java Pattern and Matcher, so no ant-like patterns like "com.example.*.Child*" would match. The only reason why "com.example.*.*Service" is because of the .* which matches any sequence of characters. In order to include/exlude using regex use valid regex.
Edit:
So in this case, one possible option would be to use pattern like com.example.child.Child.* to match any classes from com.example.child package starting with the Child in their names.

Eclipse RCP - Injecting interfaces marked with #Singleton

I have the following code:
public interface DummyInterface {
}
and
#Singleton
#Creatable
public class DummyInterfaceImpl1 implements DummyInterface {
}
And when I want I can simply inject this, and it works just fine, (see below):
#Inject
DummyInterfaceImpl1
However I can't do
#Inject
DummyInterface
Because I get an
Unable to process "ClassWhereIInject.dummyInterface": no actual value was found for the argument "DummyInterface".
So, I am trying to understand, if I use the combination of #Creatable and #Singleon, without adding the instance that I want to inject in the IEclipseContext, then I can only inject implementation classes and not interfaces?
I can see how this can get problematic, especially when one has multiple implementation classes for the same interface, and the dependency injection framework doesn't know which to inject...that is if you don't use the #Named annotation to specify...
The injection system only looks for something with the name you specify. It does not try and find a class that happens to implement that interface. So no you can't use an #Creatable class with a different name to the interface.
An alternative is to use a 'ContextFunction'. This is a function which is called when the injection system is looking for a name. The context function can create an instance of something suitable and put it in the context for the injector. Full details on context function are here

Compiling Spring's MVC url pattern regex

I need to be able to turn a list of Spring MVC URL pattern matching expressions into actual Regex Patterns so I can match strings against them.
Example Spring pattern :
/actionGroups/{action-group-id:\d+}
At the moment if I compile the above string I get a PatternSyntaxException because of illegal repetition due to the { characters.
Does Spring expose the mechanism it uses to compile/match these string into actual Regex Patterns? It would be cool if I could reuse their functionality.
Else I suppose I'd need to escape the { characters manually and strip out the pattern name.. action-group-id in this example.
There is nothing wrong with what you are trying to do, check your pattern syntax :
#RequestMapping("/actionGroups/{action-group-id:\\d+}")
public void test(#PathVariable(value = "action-group-id") Integer id) {
// you can use id ...
}
Looks like it was the AntPathMatcher class i was looking for as per Sotirios Delimanolis's comment above.
This class has a matches method which takes a URL Pattern like the one above and an URL string and returns true/false depending on if the string matches.

Pointcut not well Formatted

What is the format problem with this pointcut?
#Around("execution(* #myPackage.SafetyCritical.*(*))&& #annotation(deny)")
.i forgot to add: exception is "Pointcut is not well-formed: expecting 'name pattern' (last closing bracket before &&)
for an example the pointcut should work with this class:
#SafetyCritical
public class SecureClass
{
public SecureClass(){
}
#Deny
public void isNotAllowed(){
System.out.println("This should not happen");
}
#Allow
public void isAllowed(){
System.out.println("Allowed");
}
}
EDIT:
I think the pointcut expression you are looking for would be more like this:
#Around("#target(myPackage.SafetyCritical) && #annotation(denyPackage.Deny)")
The #target designator is used to match classes that are marked with the given annoation, and the #annotation designator will filter to the methods annotated with the denyPackage.Deny annotation.
Again, a look through the Spring Documentation regarding AspectJ support would be helpful.
ORIGINAL:
To match on any number of arguments, the parameters definition passed to the execution pointcut designator should be '..'
#Around("execution(* myPackage.SafetyCritical.*(..)) && #annotation(deny)")
The Spring documentation has some examples of using this to denote accepting any number of arguments.
Also, I would venture to guess that that having the '#' symbol in front of your package name is not acceptable. You should remove it.
I've used a pointcut definition like this to match annotated methods:
#Around("execution(#myPackage.SafetyCritical * *(..)) && #annotation(deny)")
The last part #annotation(deny) (like you already know, but some others may not) is to bind the annotation to the advice method argument named "deny".
Edit: As per your update, I was not aware that SafetyCritical was an annotation on the class. I suppose that would be witin the target() goal then:
#Around("execution(* *(..)) && #target(myPackage.SafetyCritical) && #annotation(deny)")

How to match a method with an annotated argument in AspectJ

I'd like to match a method like this:
#Foo
public void boo(#Baz Bar bar) { ... }
Basically:
the method has a #Foo annotation (which I match with execution(#Foo * *(..)) && #annotation(foo)),
can have a variable amount of parameters,
and one of them should have a #Baz annotation,
I need to further work with that annotated argument (bar).
If a method has a #Foo annotation but is missing a #Baz annotation, I want to get an error as early as possible, if possible when weaving and not at runtime.
How can I do that?
public pointcut annArg(): execution(#Foo * *(.., #Baz (*),..));
declare error :execution(#Foo * *(..))&&!annArg() :"error";
Unfortunatly it is impossible to grab matched argument by args(..,arg,..). But you can use thisJoinPoint.getArgs() and reflection API to get the annotated argument. Or if you know the position of the argument you can use something like args(..,arg);

Categories