I am trying to create a reusable organised class to override AppDelegate class in IOS. Is there anyway to create a class that can override the AppDelegate's DidEnterBackground method?
I have tried create a class and inherits global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate and override the DidEnterBackground method, but it is not working.
public class Testing : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override void DidEnterBackground(UIApplication application)
{
Console.WriteLine("App entering background state.");
}
Related
I have an abstract service class.
abstract class AbstractService<T> {
public void saveNew(T entity) {
}
}
And I have two more abstract classes extends AbstractService and implement a shared interface.
abstract class MoreAbstractService2<T extends Some2>
extends AbstractService<T>
implements SharedInterface {
}
abstract class MoreAbstractService3<T extends Some3>
extends AbstractService<T>
implements SharedInterface {
}
Now I want to validate the entity argument on these two extending services' saveNew(T) method.
How can I define a #Pointcut and (or) an #Around for following conditions?
extends the AbstractService class
implements the SharedInterface interface
you can use within as following:
within(com.somepackage.Super+)
where com.somepackage.Super is the fully qualified base class name and + means "all subclasses". Other pointcut is
execution(* com.somepackage.Super+.*(..))
R.G's solution has a few disadvantages:
The pointcut matches too many joinpoints.
Thus it needs reflection in order to filter out the unnecessary ones.
I am going to show you a stand-alone AspectJ solution (no Spring, I am not a Spring user), but the aspect would look just the same in Spring, you only need to make it a #Component or declare a #Bean factory in your configuration. But the same applies to all the classes you want to intercept, of course.
Because I prefer a full MCVE with all necessary dependency classes in order for you to be able to copy, compile and run it, and because I also added negative test cases (sub-classes only extending the abstract base class or only implementing the interface), this is a lot of code. So please bear with me:
Abstract classes, interface and helper classes:
package de.scrum_master.app;
public abstract class AbstractService<T> {
public void saveNew(T entity) {
System.out.println("Saving new entity " + entity);
}
}
package de.scrum_master.app;
public class Some2 {}
package de.scrum_master.app;
public class Some3 {}
package de.scrum_master.app;
public abstract class MoreAbstractService2<T extends Some2>
extends AbstractService<T>
implements SharedInterface {}
package de.scrum_master.app;
public abstract class MoreAbstractService3<T extends Some3>
extends AbstractService<T>
implements SharedInterface {}
package de.scrum_master.app;
public interface SharedInterface {
void doSomething();
}
Driver application (AspectJ + POJOs, not Spring):
This driver class contains some static inner classes subclassing the given base classes and/or implementing the shared interface. Two are used for positive tests (should be intercepted), two for negative tests (should not be intercepted). Each class also contains an additional method as another negative test case which should not be matched - better safe than sorry.
package de.scrum_master.app;
public class Application {
public static void main(String[] args) {
// Should be intercepted
InterceptMe1 interceptMe1 = new InterceptMe1();
interceptMe1.saveNew(new Some2());
interceptMe1.doSomething();
interceptMe1.additional();
printSeparator();
// Should be intercepted
InterceptMe2 interceptMe2 = new InterceptMe2();
interceptMe2.saveNew(new Some3());
interceptMe2.doSomething();
interceptMe2.additional();
printSeparator();
// Should NOT be intercepted
DontInterceptMe1 dontInterceptMe1 = new DontInterceptMe1();
dontInterceptMe1.saveNew(new Some2());
dontInterceptMe1.additional();
printSeparator();
// Should NOT be intercepted
DontInterceptMe2 dontInterceptMe2 = new DontInterceptMe2();
dontInterceptMe2.additional();
printSeparator();
}
private static void printSeparator() {
System.out.println("\n----------------------------------------\n");
}
static class InterceptMe1 extends MoreAbstractService2<Some2> {
#Override
public void doSomething() {
System.out.println("Doing something in MoreAbstractService2<Some2>");
}
public void additional() {
System.out.println("Additional method in MoreAbstractService2<Some2>");
}
}
static class InterceptMe2 extends MoreAbstractService3<Some3> {
#Override
public void doSomething() {
System.out.println("Doing something in MoreAbstractService3<Some3>");
}
public void additional() {
System.out.println("Additional method in MoreAbstractService3<Some3>");
}
}
static class DontInterceptMe1 extends AbstractService<Some2> {
public void additional() {
System.out.println("Additional method in AbstractService<Some2>");
}
}
static class DontInterceptMe2 implements SharedInterface {
#Override
public void doSomething() {
System.out.println("Doing something in SharedInterface"); }
public void additional() {
System.out.println("Additional method in SharedInterface");
}
}
}
Aspect:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
#Aspect
public class EntityValidationAspect {
#Before(
"execution(* saveNew(*)) && " +
"args(entity) && " +
"target(de.scrum_master.app.SharedInterface) && " +
"target(de.scrum_master.app.AbstractService)"
)
public void validateEntity(JoinPoint thisJoinPoint, Object entity) {
System.out.println("-> Pre-save entity validation: " + entity);
}
}
As you can see, the aspect uses two target() pointcuts which must both match. It also specifically targets any saveNew method with a single argument saveNew(*), binding that argument as an advice method parameter via args().
For demonstration's sake I do not validate anything (I don't know how you want to do that) but just print the entity. Thus, a #Before advice is sufficient. If in case of negative validation you want to throw an exception, this advice type is also okay. If you need to do more, such as manipulate the entity's state or replace it before passing it on to the target method, call an alternative target method instead or none at all, return a specific result (in case of non-void methods, here not applicable), handle exceptions from the target method etc., you ought to use an #Around advice instead.
Console log:
-> Pre-save entity validation: de.scrum_master.app.Some2#28a418fc
Saving new entity de.scrum_master.app.Some2#28a418fc
Doing something in MoreAbstractService2<Some2>
Additional method in MoreAbstractService2<Some2>
----------------------------------------
-> Pre-save entity validation: de.scrum_master.app.Some3#5305068a
Saving new entity de.scrum_master.app.Some3#5305068a
Doing something in MoreAbstractService3<Some3>
Additional method in MoreAbstractService3<Some3>
----------------------------------------
Saving new entity de.scrum_master.app.Some2#1f32e575
Additional method in AbstractService<Some2>
----------------------------------------
Additional method in SharedInterface
----------------------------------------
Et voilĂ - the aspect does exactly what you asked for, as far as I understand your requirement. :-) More specifically, it does not get triggered in the third case when saveNew is being called, but the class does not implement the interface.
Following code can be used for the validation mentioned.
The point cut is to intercept on the execution of a specific method for the subclasses of AbstractService and the code logic is to only validate if SharedInterface is a superinterface of the target bean.
The use of isAssignableFrom() is required as the interfaces proxied by the AOP proxy does not include SharedInterface. As per my understanding , a pointcut expression to match the second criteria will not be possible for the same reason and hence handled the requirement in the code logic.
Hope this helps
#Aspect
#Component
public class ValidationAspect {
#Pointcut("execution(* package.to.AbstractService+.saveNew(..))")
public void isAbstractServiceType() {
}
#Around("isAbstractServiceType() && args(entity) && target(bean)")
public void validateEntityArugments(ProceedingJoinPoint pjp, Object entity, Object bean) throws Throwable {
if (SharedInterface.class.isAssignableFrom(bean.getClass())) {
System.out.println(entity);
// ..validate
}
pjp.proceed();
}
}
I have an abstract class which are implementing some basic interface.
(attack method is not inherited from basic interface)
public abstract class AbstractClass implements BasicInterface {
public void attack(String attackerId, float attackerAttackSpeed) {
... // method body
}
}
I have also an class which are extending abstract class.
#Service
public class A extends AbstractClass {
// other methods...
}
Right now im trying to observe method attack(String attackerId, float attackerSpeedAttack)
#Aspect
#Component
public class AspectJ{
#After(value = "execution(* package.A.attack(attackerId, ..)) && args(attackerId)")
public void broadcastAttackMessage(String attackerId) {
... //method body
}
}
But unfortunately Intellij told me that This advise advises to no methods.
Also during starting the application i received error with a stack trace :
Caused by: java.lang.IllegalArgumentException: error at ::0 name binding only allowed in target, this, and args pcds
What i'm doing wrong? Is there any way to observe method from super class? Do i miss something?
I want to access a getter from a Class that uses Vaadins #Route annotation.
Instantiating the class does not work and autowiring neither
This is my entry point. From here Id like to call the getter from the button in the next class to add the actionlistener(clicklistener) to further utilize its data in a small swing window
#SpringBootApplication
public class App
{
#Autowired
Main main;
public static void main( String[] args )
{
SpringApplicationBuilder builder = new SpringApplicationBuilder(App.class);
builder.headless(false);
ConfigurableApplicationContext configurableApplicationContext = builder.run(args);
View view = new View();
}
}
This is the class which implements Vaadin. Here is the getter I want to call
#Route("index")
public class Main extends VerticalLayout {
VerticalLayout buttons = new VerticalLayout();
TextField field = new TextField();
Button button = new Button("commit change");
public Main() {
button.addClickListener(buttonClickEvent -> {
if (StringUtils.isNotEmpty(field.getValue())) {
Checkbox checkbox = new Checkbox(field.getValue());
buttons.add(checkbox);
field.setValue("");
}
});
add(
new H1("Control it!"),
buttons,
field,
button
);
}
public TextField getField() {
return field;
}
I figured out a way to avoid calling the getters
I made the Gui Application that should get data from the Vaadin a #Component too and #Autowired it into Main. This might be the wrong way, but it works.
Even though you can autowire in the Main class, if you want to use it as a bean you need to add the Spring #Component (or Vaadin's #SpringComponent) annotation, as well as #UIScope for it to return the right instance of the bean.
I am building a JavaFX app and i want it to implement Spring's SmartLifeCycle interface to perform tasks when the main class terminates. A JavaFX main class must extend the Application class which contains a stop() method. The SmartLifeCycle interface also contains a stop method. It looks like these 2 methods refuse to co-exist even if they have different method signatures. The JavaFX method extended from the Application class has no parameters and throws an exception while the implemented method from SmartLifeCycle takes a Runnable object as an argument.
Is it possible for both these methods to exist in the same class? Both are required to implement by subclasses therefore the compiler complains no matter what i do.
Thanks
The Application abstract class has the following method:
public void stop() throws Exception {}
And the SmartLifecycle interface has the following method, inherited from Lifecycle:
void stop();
As you can see, one can throw an Exception and the other cannot. If you want to extend Application and implement SmartLifecycle, then you can't have throws Exception in your overridden stop() method.
public class MySpringJavaFxApp extends Application implements SmartLifecycle {
#Override
public void start(Stage primaryStage) throws Exception {
// ...
}
#Override
public void stop() {
// ...
}
// other methods that need to be implemented...
}
But note you have to override stop() to remove the throws clause. Otherwise the methods clash (Application#stop is not abstract, thus tries to implement Lifecycle#stop in this situation).
In the first Version of my program, I load MainScreen without problems on this way:
public class MyUI extends UI {
public void showMainView() {
addStyleName(ValoTheme.UI_WITH_MENU);
setContent(new MainScreen(MyUI.this));
mainScreen.setUI(MyUI.this);
}
public static MyUI get() {
return (MyUI) UI.getCurrent();
}
}
But now, I have changed it to:
#SpringUI
#Viewport("user-scalable=no,initial-scale=1.0")
#Theme("mytheme")
public class MyUI extends UI {
#Autowired
MainScreen mainScreen;
public void showMainView() {
addStyleName(ValoTheme.UI_WITH_MENU);
setContent(mainScreen);
getNavigator().navigateTo(getNavigator().getState());
}
public static MyUI get() {
return (MyUI) UI.getCurrent();
}
}
To get MainScreen via #Autowired, I had to clear the Constructor of MainScreen, but I don't know how to get now the UI I need for navigator.
I just get null in both ways.
#Component
#UIScope
public class MainScreen extends HorizontalLayout {
public MainScreen() {
UI ui = UI.getCurrent(); // is null
MyUI ui2 = MyUI.get(); // is null
CssLayout viewContainer = new CssLayout();
viewContainer.addStyleName("valo-content");
viewContainer.setSizeFull();
final Navigator navigator = new Navigator(ui, viewContainer);
// ...
}
I'm not familiar with all the frameworks you tagged. I'm going to address what I believe to be a logical error or possibly an incorrect design.
If you #Autowire the MainScreen into the UI you're allowing spring to guarantee whatever that is to be there. If you look at it using the preferred constructor injection it might be more obvious.
public class MyUI extends UI {
private MainScreen mainScreen;
#Autowired
public MyUI (MainScreen mainScreen) {
this.mainScreen = mainScreen;
}
}
MainScreen has already been instantiated at that point and could very well be in other parts of the code already so the mainScreen constructor shouldn't be dependent on MyUI.
How you deal with it is depends on what the desired behavior and relationship is. If you're needing the UI class in mainScreen but can't depend on it being available then you'll have to pass it in from the UI class itself (mainScreen.foo(this);).