Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@495 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/head
30 changed files with 364 additions and 535 deletions
@ -1,26 +0,0 @@
@@ -1,26 +0,0 @@
|
||||
|
||||
package org.springframework.aop.aspectj; |
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor; |
||||
import org.aopalliance.intercept.MethodInvocation; |
||||
|
||||
/** |
||||
* @author robh |
||||
*/ |
||||
class CallCountingInterceptor implements MethodInterceptor { |
||||
|
||||
private int count; |
||||
|
||||
public Object invoke(MethodInvocation methodInvocation) throws Throwable { |
||||
count++; |
||||
return methodInvocation.proceed(); |
||||
} |
||||
|
||||
public int getCount() { |
||||
return count; |
||||
} |
||||
|
||||
public void reset() { |
||||
this.count = 0; |
||||
} |
||||
} |
||||
@ -1,109 +0,0 @@
@@ -1,109 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2006 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.aop.aspectj.annotation; |
||||
|
||||
import java.lang.reflect.Method; |
||||
|
||||
import org.aspectj.lang.JoinPoint; |
||||
import org.aspectj.lang.annotation.Aspect; |
||||
import org.aspectj.lang.annotation.Before; |
||||
import org.aspectj.lang.reflect.MethodSignature; |
||||
|
||||
import org.springframework.util.ObjectUtils; |
||||
|
||||
/** |
||||
* Add a DeclareParents field in concrete subclasses, to identify |
||||
* the type pattern to apply the introduction to. |
||||
* |
||||
* @author Rod Johnson |
||||
* @since 2.0 |
||||
*/ |
||||
@Aspect |
||||
public abstract class AbstractMakeModifiable { |
||||
|
||||
public interface MutableModifable extends Modifiable { |
||||
void markDirty(); |
||||
} |
||||
|
||||
public static class ModifiableImpl implements MutableModifable { |
||||
private boolean modified; |
||||
|
||||
public void acceptChanges() { |
||||
modified = false; |
||||
} |
||||
|
||||
public boolean isModified() { |
||||
return modified; |
||||
} |
||||
|
||||
public void markDirty() { |
||||
this.modified = true; |
||||
} |
||||
} |
||||
|
||||
@Before(value="execution(void set*(*)) && this(modifiable) && args(newValue)", |
||||
argNames="modifiable,newValue") |
||||
public void recordModificationIfSetterArgumentDiffersFromOldValue(JoinPoint jp, |
||||
MutableModifable mixin, Object newValue) { |
||||
|
||||
/* |
||||
* We use the mixin to check and, if necessary, change, |
||||
* modification status. We need the JoinPoint to get the |
||||
* setter method. We use newValue for comparison. |
||||
* We try to invoke the getter if possible. |
||||
*/ |
||||
|
||||
if (mixin.isModified()) { |
||||
// Already changed, don't need to change again
|
||||
//System.out.println("changed");
|
||||
return; |
||||
} |
||||
|
||||
// Find the current raw value, by invoking the corresponding setter
|
||||
Method correspondingGetter = getGetterFromSetter(((MethodSignature) jp.getSignature()).getMethod()); |
||||
boolean modified = true; |
||||
if (correspondingGetter != null) { |
||||
try { |
||||
Object oldValue = correspondingGetter.invoke(jp.getTarget()); |
||||
//System.out.println("Old value=" + oldValue + "; new=" + newValue);
|
||||
modified = !ObjectUtils.nullSafeEquals(oldValue, newValue); |
||||
} |
||||
catch (Exception ex) { |
||||
ex.printStackTrace(); |
||||
// Don't sweat on exceptions; assume value was modified
|
||||
} |
||||
} |
||||
else { |
||||
//System.out.println("cannot get getter for " + jp);
|
||||
} |
||||
if (modified) { |
||||
mixin.markDirty(); |
||||
} |
||||
} |
||||
|
||||
private Method getGetterFromSetter(Method setter) { |
||||
String getterName = setter.getName().replaceFirst("set", "get"); |
||||
try { |
||||
return setter.getDeclaringClass().getMethod(getterName, (Class[]) null); |
||||
} |
||||
catch (NoSuchMethodException ex) { |
||||
// must be write only
|
||||
return null; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,41 +0,0 @@
@@ -1,41 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2005 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.aop.aspectj.annotation; |
||||
|
||||
import org.springframework.aop.framework.Lockable; |
||||
|
||||
/** |
||||
* @author Rod Johnson |
||||
*/ |
||||
public class CannotBeUnlocked implements Lockable, Comparable<Object> { |
||||
|
||||
public void lock() { |
||||
} |
||||
|
||||
public void unlock() { |
||||
throw new UnsupportedOperationException(); |
||||
} |
||||
|
||||
public boolean locked() { |
||||
return true; |
||||
} |
||||
|
||||
public int compareTo(Object arg0) { |
||||
throw new UnsupportedOperationException(); |
||||
} |
||||
|
||||
} |
||||
@ -1,28 +0,0 @@
@@ -1,28 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2006 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.aop.aspectj.annotation; |
||||
|
||||
import org.aspectj.lang.annotation.Aspect; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
* @since 2.o |
||||
*/ |
||||
@Aspect |
||||
public class FooAspect { |
||||
|
||||
} |
||||
@ -1,34 +0,0 @@
@@ -1,34 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2006 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.aop.aspectj.annotation; |
||||
|
||||
import org.aspectj.lang.annotation.Aspect; |
||||
import org.aspectj.lang.annotation.DeclareParents; |
||||
|
||||
/** |
||||
* Adds a declare parents pointcut. |
||||
* @author Rod Johnson |
||||
* @since 2.0 |
||||
*/ |
||||
@Aspect |
||||
public class MakeITestBeanModifiable extends AbstractMakeModifiable { |
||||
|
||||
@DeclareParents(value = "org.springframework.beans.ITestBean+", |
||||
defaultImpl=ModifiableImpl.class) |
||||
public static MutableModifable mixin; |
||||
|
||||
} |
||||
@ -1,73 +0,0 @@
@@ -1,73 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2006 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.aop.aspectj.annotation; |
||||
|
||||
import org.aspectj.lang.annotation.Aspect; |
||||
import org.aspectj.lang.annotation.Before; |
||||
import org.aspectj.lang.annotation.DeclareParents; |
||||
import org.springframework.aop.framework.DefaultLockable; |
||||
import org.springframework.aop.framework.Lockable; |
||||
|
||||
/** |
||||
* Demonstrates introductions, AspectJ annotation style. |
||||
* <p> |
||||
* @author Rod Johnson |
||||
* @since 2.0 |
||||
*/ |
||||
@Aspect |
||||
public class MakeLockable { |
||||
|
||||
@DeclareParents(value = "org.springframework..*", |
||||
defaultImpl=DefaultLockable.class) |
||||
public static Lockable mixin; |
||||
|
||||
@Before(value="execution(void set*(*)) && this(mixin)", argNames="mixin") |
||||
public void checkNotLocked( |
||||
Lockable mixin) // Bind to arg
|
||||
{ |
||||
// Can also obtain the mixin (this) this way
|
||||
//Lockable mixin = (Lockable) jp.getThis();
|
||||
if (mixin.locked()) { |
||||
throw new IllegalStateException(); |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
/* |
||||
* |
||||
* public aspect MakeLockable { |
||||
* |
||||
* declare parents org....* implements Lockable; |
||||
* |
||||
* private boolean Lockable.locked; |
||||
|
||||
* public void Lockable.lock() { |
||||
this.locked = true; |
||||
} |
||||
|
||||
* public void Lockable.unlock() { |
||||
this.locked = false; |
||||
} |
||||
|
||||
* public boolean Lockable.locked() { |
||||
return this.locked; |
||||
} |
||||
* |
||||
* |
||||
* } |
||||
*/ |
||||
@ -1,30 +0,0 @@
@@ -1,30 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2006 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.aop.aspectj.annotation; |
||||
|
||||
/** |
||||
* Used as a mixin. |
||||
* |
||||
* @author Rod Johnson |
||||
*/ |
||||
public interface Modifiable { |
||||
|
||||
boolean isModified(); |
||||
|
||||
void acceptChanges(); |
||||
|
||||
} |
||||
@ -1,39 +0,0 @@
@@ -1,39 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2007 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.aop.aspectj.annotation; |
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint; |
||||
import org.aspectj.lang.annotation.Around; |
||||
import org.aspectj.lang.annotation.Aspect; |
||||
import org.aspectj.lang.annotation.Pointcut; |
||||
|
||||
/** |
||||
* @author Adrian Colyer |
||||
*/ |
||||
@Aspect |
||||
public class NamedPointcutWithArgs { |
||||
|
||||
@Pointcut("execution(* *(..)) && args(s,..)") |
||||
public void pointcutWithArgs(String s) {} |
||||
|
||||
@Around("pointcutWithArgs(aString)") |
||||
public Object doAround(ProceedingJoinPoint pjp, String aString) throws Throwable { |
||||
System.out.println("got '" + aString + "' at '" + pjp + "'"); |
||||
throw new IllegalArgumentException(aString); |
||||
} |
||||
|
||||
} |
||||
@ -1,15 +0,0 @@
@@ -1,15 +0,0 @@
|
||||
package org.springframework.aop.aspectj.annotation; |
||||
|
||||
public class NotLockable { |
||||
|
||||
private int intValue; |
||||
|
||||
public int getIntValue() { |
||||
return intValue; |
||||
} |
||||
|
||||
public void setIntValue(int intValue) { |
||||
this.intValue = intValue; |
||||
} |
||||
|
||||
} |
||||
@ -1,31 +0,0 @@
@@ -1,31 +0,0 @@
|
||||
/** |
||||
* |
||||
*/ |
||||
package org.springframework.aop.aspectj.annotation; |
||||
|
||||
import org.aspectj.lang.annotation.Around; |
||||
import org.aspectj.lang.annotation.Aspect; |
||||
import org.aspectj.lang.annotation.Before; |
||||
import org.springframework.beans.ITestBean; |
||||
import org.springframework.beans.TestBean; |
||||
|
||||
@Aspect("perthis(execution(* *.getSpouse()))") |
||||
public class PerThisAspect { |
||||
|
||||
public int count; |
||||
|
||||
/** |
||||
* Just to check that this doesn't cause problems with introduction processing |
||||
*/ |
||||
private ITestBean fieldThatShouldBeIgnoredBySpringAtAspectJProcessing = new TestBean(); |
||||
|
||||
@Around("execution(int *.getAge())") |
||||
public int returnCountAsAge() { |
||||
return count++; |
||||
} |
||||
|
||||
@Before("execution(void *.set*(int))") |
||||
public void countSetter() { |
||||
++count; |
||||
} |
||||
} |
||||
@ -1,48 +0,0 @@
@@ -1,48 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2006 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.aop.aspectj.autoproxy; |
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint; |
||||
import org.aspectj.lang.annotation.Around; |
||||
import org.aspectj.lang.annotation.Aspect; |
||||
|
||||
/** |
||||
* @author Rod Johnson |
||||
*/ |
||||
@Aspect |
||||
public class MultiplyReturnValue { |
||||
|
||||
private int multiple = 2; |
||||
|
||||
public int invocations; |
||||
|
||||
public void setMultiple(int multiple) { |
||||
this.multiple = multiple; |
||||
} |
||||
|
||||
public int getMultiple() { |
||||
return this.multiple; |
||||
} |
||||
|
||||
@Around("execution(int *.getAge())") |
||||
public Object doubleReturnValue(ProceedingJoinPoint pjp) throws Throwable { |
||||
++this.invocations; |
||||
int result = (Integer) pjp.proceed(); |
||||
return result * this.multiple; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue