I'm using AspectJ annotations instead of writing actual aspect files. I want to expose an annotation value to my advice.
I currently have this but it it doesn't expose the values inside MyAnnotation
#Before("execution(#MyAnnotation * * (..))")
public void intercept(JoinPoint jp) {
...
}
What I was thinking was something like this:
#Before("execution(#MyAnnotation * * (..)) && #this(MyAnnotation)")
public void intercept(JoinPoint jp, MyAnnotation myAnnotation) {
...
}
This clearly has a syntax error but was wondering if I was close. I can't seem to find an example syntax when using AspectJ annotations to do this.
You are using type, when you should be using an identifier. The correct code is:
#Before("execution(#MyAnnotation * * (..)) && #this(myAnnotation)")
public void intercept(JoinPoint jp, MyAnnotation myAnnotation) {
...
}
Related
I'm trying to add a custom annotation for JPA repository methods to have a advice on #Query value.
Below is the piece of code I tried
MyFilter class
#Aspect
#Component
public class MyFilter {
#Pointcut("execution(* *(..)) && #within(org.springframework.data.jpa.repository.Query)")
private void createQuery(){}
#Around("createQuery()")
public void invoke(JointPoint jp) {
}
}
The Respository code
#MyFilter
#Query(Select * ...)
MyObject findByNameAndClass(...)
So I keep getting error
createQuery() is never called At MyFilter
I'm trying to update the Query value using the advice.
What am I doing wrong?
To capture the annotation I often use this pattern:
"execution(#AnnotationToCapture * *(..)) && #annotation(annotationParam)"
Then in the proceeding method, you can have the annotation as parameter:
(..., AnnotationToCapture annotationParam, ...)
You should not use the #within annotation for this purpose. Instead, you should use #annotation, as follows:
#Pointcut("execution(* *(..)) && #annotation(org.springframework.data.jpa.repository.Query)")
private void createQuery(){}
Also, you should use JoinPoint to access the method signature and then you can extract the annotation from the signature.
In a my view I have an HBox
#FXML
private HBox hboxWarning;
and I want hide/show it according to the value of
private ObjectProperty<Integer> maxClientCount;
If maxClientCount > 10 then hboxWarning is visible else it's hide.
I bound the two elements in this way
hboxWarning.visibleProperty().bind(IntegerProperty.integerProperty(maxClientCount).greaterThan(10));
and works well. My problem is that
IntegerProperty.integerProperty(maxClientCount)
sets to zero the current value of maxClientCount. Is it a JavaFx bug or I'm using IntegerProperty.integerProperty improperly? And
how can I achieve my goal?
Turned out to be not as easy as assumed: the core fix needs additional methods in BidirectionalBinding to cope with the swapped sequence of number types. The actual number bindings are private, so no way to access in workaround code.
// method in u5, binds the wrong way round
// (for usage in IntegerProperty.integerProperty)
public static BidirectionalBinding bindNumber(Property<Integer> property1,
IntegerProperty property2)
// calls
private static <T extends Number> BidirectionalBinding bindNumber(Property<T> property1,
Property<Number> property2) {
The sequence is crucial because we need a type-cast from Number to T when setting the value of p1 (which is safe because we know that the number-type property copes with conversion from Number -> concrete type). Core fix simply adds all those methods with switched parameter sequence.
For a custom hack until the release of JDK 8u20, the only way I see is to not use the special number binding methods but the generic object binding:
public static IntegerProperty integerProperty(final Property<Integer> property) {
if (property == null) {
throw new NullPointerException("Property cannot be null");
}
return new IntegerPropertyBase() {
{
bindBidirectional(cast(property));
// original:
//BidirectionalBinding.bindNumber(property, this);
}
#Override
public Object getBean() {
return null; // Virtual property, no bean
}
#Override
public String getName() {
return property.getName();
}
#Override
protected void finalize() throws Throwable {
try {
unbindBidirectional(cast(property));
// original
// BidirectionalBinding.unbindNumber(property, this);
} finally {
super.finalize();
}
}
};
}
/**
* Type cast to allow bidi binding with a concrete XXProperty (with
* XX = Integer, Double ...). This is (?) safe because the XXProperty
* internally copes with type conversions from Number to the concrete
* type on setting its own value and exports the concrete type as
* needed by the object property.
*
*/
private static <T extends Number> Property<Number> cast(Property<T> p) {
return (Property<Number>) p;
}
Take it with a grain of salt - while rudimentarily tested, there might be side-effects I overlooked.
As rightly said by #kleopatra this is a JavaFx bug fixed in JDK 8u20.
Meanwhile I used the following workaround:
int maxClients = maxClientCount.get();
hboxWarning.visibleProperty().bind(IntegerProperty.integerProperty(maxClientCount).greaterThan(10));
maxClientCount.setValue(maxClients);
I hope this can help someone.
I was wondering if it is possible to change the auto-formatting in Android Studio in such a way that braces for anonymous classes are placed on the same line while still putting braces for regular classes, methods and blocks on a new line.
Currently, IntelliJ is giving me this result:
class TestClass
{
public void test()
{
FooBar foo = new FooBar(new Runnable() // I want the brace here...
{ // ...not here.
#Override
public void run()
{
//
}
});
}
}
However, I would like the code to be formatted like this:
class TestClass
{
public void test()
{
FooBar foo = new FooBar(new Runnable() { // <- Same Line
#Override
public void run()
{
//
}
});
}
}
Everything is fine, except the one detail that I cannot get the brace to be formatted like in the second example. Is this not possible or did I just overlook the setting?
I've wanted this for a long time now as well, but unfortunately it is not possible in Intellij.
The closest you can come is setting Wrapping and Braces/Braces Placement/In class declaration to "Next line if wrapped" or "End of line" (what I use). This of course modifies the way the brace is wrapped for not only anonymous inner classes (the desired result), but also for top level and inner classes; however, methods/if/else/for/while/do-while/try/catch/finally/switch etc are unaffected.
I really wish IntelliJ would add a Wrapping and Braces/Braces Placement/In anonymous class declaration option like Eclipse has.
I have a class that looks like this:
public class MyClass {
private class MyInnerClass {
public void someFunc() { }
}
public void usefulMethod() {
... some stuff...
nativeUsefulMethod();
}
private native void nativeUsefulMethod();
}
I need to keep the method names of all native functions, as well as the classes containing them, in order for the native code to work properly. Seems like no problem:
-keepnames class * {
native <methods>;
}
When I look at the resulting jar, I see that MyClass and nativeUsefulMethod() remain, while usefulMethod() has been obfuscated. Good. However, the inner class is still named "MyClass$MyInnerClass". It contains no native methods, so I would expect it to be called "MyClass$a" or just "a".
I tested changing "-keepnames" to "-keepclassmembernames", and the class names of both get obfuscated. It's definitely this directive that's keeping the inner class name. Is there a way to obfuscate the outer, but not the inner class name?
The proper configuration for native methods is:
-keepclasseswithmembernames class * {
native <methods>;
}
See the ProGuard manual > Examples > Processing native methods
I know with Castle Windsor, you can register aspects (when using method interception in Windsor as AOP) using code instead of applying attributes to classes. Is the same possible in Postsharp? It's a preference things, but prefer to have aspects matched to interfaces/objects in one place, as opposed to attributes all over.
Update:
Curious if I can assign aspects to interfaces/objects similiar to this:
container.Register(
Component
.For<IService>()
.ImplementedBy<Service>()
.Interceptors(InterceptorReference.ForType<LoggingAspect>()).Anywhere
);
If you could do this, you would have the option of NOT having to place attributes on assemblies/class/methods to apply aspects. I can then have one code file/class that contains which aspects are applied to which class/methods/etc.
Yes. You can either use multicasting (http://www.sharpcrafters.com/blog/post/Day-2-Applying-Aspects-with-Multicasting-Part-1.aspx , http://www.sharpcrafters.com/blog/post/Day-3-Applying-Aspects-with-Multicasting-Part-2.aspx) or you can use aspect providers (http://www.sharpcrafters.com/blog/post/PostSharp-Principals-Day-12-e28093-Aspect-Providers-e28093-Part-1.aspx , http://www.sharpcrafters.com/blog/post/PostSharp-Principals-Day-13-e28093-Aspect-Providers-e28093-Part-2.aspx).
Example:
using System;
using PostSharp.Aspects;
using PostSharp.Extensibility;
[assembly: PostSharpInterfaceTest.MyAspect(AttributeTargetTypes = "PostSharpInterfaceTest.Interface1", AttributeInheritance = MulticastInheritance.Multicast)]
namespace PostSharpInterfaceTest
{
class Program
{
static void Main(string[] args)
{
Example e = new Example();
Example2 e2 = new Example2();
e.DoSomething();
e2.DoSomething();
Console.ReadKey();
}
}
class Example : Interface1
{
public void DoSomething()
{
Console.WriteLine("Doing something");
}
}
class Example2 : Interface1
{
public void DoSomething()
{
Console.WriteLine("Doing something else");
}
}
interface Interface1
{
void DoSomething();
}
[Serializable]
class MyAspect : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
Console.WriteLine("Entered " + args.Method.Name);
}
}
}
I recommend that if you have complex requirements for determining which types get certain aspects that you consider creating an aspect provider instead.
Have a look at LOOM.NET, there you have a post compiler and a runtime weaver. With the later one you are able to archive exactly what you want.
It should be possible to use the PostSharp XML configuration. The XML configuration is the unification of the Plug-in and Project models in the project loader.
Description of .psproj could be found at http://www.sharpcrafters.com/blog/post/Configuring-PostSharp-Diagnostics-Toolkits.aspx.
Note, that I've only seen examples how PostSharp Toolkits use this XML configuration.
But it should work for custom aspects the same way.
Warning: I've noticed that installation of a PostSharp Toolkit from Nuget overwrites existing psproj file. So do not forget to back up it.