package application;

public aspect MethodSubstitionAspect {
	pointcut allMethodCallsPointcut(SMTPServer t, String msg):
		call(void SMTPServer.sendMail(..))
		&& target(t)
		&& args(msg); // here we can capture method arguments;
	
	pointcut specificMethodCallsPointcut(SMTPServer t, String msg):
		call(void SMTPServer.sendMail(..))
		&& cflowbelow(call (void  Proxy.*(..)))
		&& target(t)
		&& args(msg); // here we can capture method arguments;
	
	
//	void around(SMTPServer t, String msg) throws RuntimeException : specificMethodCallsPointcut(t, msg) {
//		if (0 == 1) {
//			check();
//		}
//		System.out.println("DEMO - fake mail ("+msg+") send on SMTP "+t);
//	}
	
//	after(SMTPServer t, String msg) : allMethodCallsPointcut(t, msg) {
//		performAction();
//	}
	
	private void check() throws RuntimeException {
		System.out.println("exception");
		throw new RuntimeException("parameter check");
	}
	
	private void performAction() {
		System.out.println("after mail send");
	}
	
	
	pointcut prohibitedRegion():
		(call(void *.*(..)) && within(application.prohibited.*))
		|| (call(void *.*(..)) && within(application.prohibited.*))
		;
	
	
	void around() : prohibitedRegion() {
		System.out.println("EEE");
	}
}
