[jboss-cvs] JBossAS SVN: r59362 - in projects/aop/trunk/aop/src: main/org/jboss/aop/advice/annotation main/org/jboss/aop/instrument test/org/jboss/test/aop/beforeafterArgs
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu Jan 4 18:17:50 EST 2007
Author: flavia.rainone
Date: 2007-01-04 18:17:21 -0500 (Thu, 04 Jan 2007)
New Revision: 59362
Modified:
projects/aop/trunk/aop/src/main/org/jboss/aop/advice/annotation/AdviceInfo.java
projects/aop/trunk/aop/src/main/org/jboss/aop/advice/annotation/AdviceMethodFactory.java
projects/aop/trunk/aop/src/main/org/jboss/aop/advice/annotation/AnnotatedParameterAdviceInfo.java
projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/FieldJoinPointGenerator.java
projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/JoinPointGenerator.java
projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodByConJoinPointGenerator.java
projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodByMethodJoinPointGenerator.java
projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodJoinPointGenerator.java
projects/aop/trunk/aop/src/test/org/jboss/test/aop/beforeafterArgs/TargetTestCase.java
Log:
[JBAOP-326] Pass null target when join point is static
Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/advice/annotation/AdviceInfo.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/advice/annotation/AdviceInfo.java 2007-01-04 21:49:18 UTC (rev 59361)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/advice/annotation/AdviceInfo.java 2007-01-04 23:17:21 UTC (rev 59362)
@@ -53,6 +53,10 @@
*/
protected short getAssignabilityDegree(Class<?> fromType, Class<?> toType)
{
+ if (fromType == null || toType == null)
+ {
+ return AdviceMethodFactory.MAX_DEGREE;
+ }
// they're the same
if (fromType == toType)
{
@@ -139,7 +143,7 @@
* the assignability degree from <code>fromInterfaceType
* </code> to <code>toInterfaceType</code>.
*/
- public short getInterfaceInheritanceAD(Class<?> fromInterfaceType,
+ private short getInterfaceInheritanceAD(Class<?> fromInterfaceType,
Class<?> toInterfaceType, short currentDegree)
{
Class[] interfaces = fromInterfaceType.getInterfaces();
Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/advice/annotation/AdviceMethodFactory.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/advice/annotation/AdviceMethodFactory.java 2007-01-04 21:49:18 UTC (rev 59361)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/advice/annotation/AdviceMethodFactory.java 2007-01-04 23:17:21 UTC (rev 59362)
@@ -295,8 +295,8 @@
try
{
// advice applies to annotated parameter rules
- rankedAdvices.add(new AnnotatedParameterAdviceInfo(methods[i], rules,
- contextRules, mutuallyExclusive));
+ rankedAdvices.add(new AnnotatedParameterAdviceInfo(properties,
+ methods[i], rules, contextRules, mutuallyExclusive));
}catch (ParameterAnnotationRuleException pare)
{
// no need to print messages -> exception prints automatically on verbose
Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/advice/annotation/AnnotatedParameterAdviceInfo.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/advice/annotation/AnnotatedParameterAdviceInfo.java 2007-01-04 21:49:18 UTC (rev 59361)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/advice/annotation/AnnotatedParameterAdviceInfo.java 2007-01-04 23:17:21 UTC (rev 59362)
@@ -25,6 +25,8 @@
/**
* Creates an annotated parameter advice info.
*
+ * @param properties the properties to which <code>method</code> must
+ * comply with
* @param method the advice method
* @param rules the annnotated parameter rules this method should
* comply with
@@ -34,7 +36,8 @@
* @throws ParameterAnnotationRuleException thrown when the advice method does not
* comply with a parameter annotation rule.
*/
- public AnnotatedParameterAdviceInfo(Method method, ParameterAnnotationRule[] rules,
+ public AnnotatedParameterAdviceInfo(AdviceMethodProperties properties,
+ Method method, ParameterAnnotationRule[] rules,
ParameterAnnotationRule[] contextRules, int[][] mutuallyExclusive)
throws ParameterAnnotationRuleException
{
@@ -42,7 +45,7 @@
this.paramTypes = createParameterAnnotationTypes(rules);
this.contextParamTypes = createParameterAnnotationTypes(contextRules);
this.mutuallyExclusive = mutuallyExclusive;
- this.applyRules();
+ this.applyRules(properties);
}
public boolean validate(AdviceMethodProperties properties, ReturnType returnType)
@@ -169,26 +172,30 @@
/**
* Applies all parameter annotation rules to the advice method parameters.
*
+ * @param properties the properties which the searched advice method must comply
+ * with
+ *
* @throws ParameterAnnotationRuleException thrown when the advice method does not
* comply with a parameter annotation rule.
*/
- private void applyRules() throws ParameterAnnotationRuleException
+ private void applyRules(AdviceMethodProperties properties) throws ParameterAnnotationRuleException
{
Annotation[][] paramAnnotations = method.getParameterAnnotations();
- boolean ruleFound;
+ ParameterAnnotationType typeFound;
+ boolean nullifyRank = false;
for (int i = 0; i < paramAnnotations.length; i++)
{
- ruleFound = false;
+ typeFound = null;
for (Annotation annotation: paramAnnotations[i])
{
// no valid annotation found for parameter i yet
- if (!ruleFound)
+ if (typeFound == null)
{
- ruleFound = findAnnotationRule(annotation, i);
+ typeFound = findAnnotationType(annotation, i);
}
else
{
- if (findAnnotationRule(annotation, i))
+ if (findAnnotationType(annotation, i) != null)
{
if (AspectManager.verbose)
{
@@ -202,7 +209,7 @@
}
}
}
- if (!ruleFound)
+ if (typeFound == null)
{
if (AspectManager.verbose)
{
@@ -219,7 +226,15 @@
// no need to say the reason a rule's been broken
throw new ParameterAnnotationRuleException(null);
}
- }
+ // this happens when target or caller are nulls
+ // in this case, this advice should have the smallest rank, since
+ // any other advice is preferable (in case of overloaded advices)
+ nullifyRank = nullifyRank || (typeFound.rule.getAssignableFrom(properties) == null);
+ }
+ if (nullifyRank)
+ {
+ rank = 0;
+ }
}
private String getDescription(ParameterAnnotationType[] types)
@@ -242,14 +257,14 @@
* @param i the number of the advice parameter that is annotated with
* <code>annotation</code>
*
- * @return <code>true</code> if there is a rule correspondent to
- * <code>annotation</code>
+ * @return the parameter type if there is a rule correspondent to
+ * <code>annotation</code>; <code>null</code> otherwise
*
* @throws ParameterAnnotationRuleException if a parameter annotation is found
* more than once and the annotation rule forbides multiple occurences
*/
- private final boolean findAnnotationRule(Annotation annotation, int i)
- throws ParameterAnnotationRuleException
+ private final ParameterAnnotationType findAnnotationType(Annotation annotation,
+ int i) throws ParameterAnnotationRuleException
{
for (int j = 0; j < paramTypes.length; j++)
@@ -257,7 +272,7 @@
// found
if (paramTypes[j].applies(annotation, i))
{
- return true;
+ return paramTypes[j];
}
}
for (int j = 0; j < contextParamTypes.length; j++)
@@ -265,10 +280,10 @@
// found
if (contextParamTypes[j].applies(annotation, i))
{
- return true;
+ return contextParamTypes[j];
}
}
- return false;
+ return null;
}
/**
@@ -421,8 +436,13 @@
public final boolean internalValidate(AdviceMethodProperties properties)
{
+ Class assignableFrom = (Class) rule.getAssignableFrom(properties);
+ if (assignableFrom == null) // targets and callers can be null
+ {
+ return true;
+ }
if (index != -1 && !method.getParameterTypes()[index].isAssignableFrom(
- (Class) rule.getAssignableFrom(properties)))
+ assignableFrom))
{
if (AspectManager.verbose)
{
Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/FieldJoinPointGenerator.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/FieldJoinPointGenerator.java 2007-01-04 21:49:18 UTC (rev 59361)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/FieldJoinPointGenerator.java 2007-01-04 23:17:21 UTC (rev 59362)
@@ -145,7 +145,7 @@
(read()) ? new Class[] {} : new Class[] {field.getType()},
null,
null,
- Context.STATIC);
+ Context.TARGET_AVAILABLE);
}
protected CtClass[] getJoinpointParameters() throws NotFoundException
Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/JoinPointGenerator.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/JoinPointGenerator.java 2007-01-04 21:49:18 UTC (rev 59361)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/JoinPointGenerator.java 2007-01-04 23:17:21 UTC (rev 59362)
@@ -1656,12 +1656,12 @@
{
final Class[] adviceParams = properties.getAdviceMethod().getParameterTypes();
argsFound = appendParameters(code, args[0], adviceParams[0],
- isAround, generator);
+ isAround, properties, generator);
for (int i = 1 ; i < args.length ; i++)
{
code.append(", ");
argsFound = appendParameters(code, args[i], adviceParams[i],
- isAround, generator) || argsFound;
+ isAround, properties, generator) || argsFound;
}
}
@@ -1670,7 +1670,8 @@
}
private final boolean appendParameters(StringBuffer code, final int arg,
- final Class adviceParam, boolean isAround, JoinPointGenerator generator)
+ final Class adviceParam, boolean isAround,
+ AdviceMethodProperties properties, JoinPointGenerator generator)
{
code.append("(");
// In case of overloaded methods javassist sometimes seems to pick up the wrong method - use explicit casts to get hold of the parameters
@@ -1691,8 +1692,12 @@
code.append(THROWABLE);
break;
case AdviceMethodProperties.TARGET_ARG:
- if (isAround)
+ if (properties.getTargetType() == null)
{
+ code.append("null");
+ }
+ else if (isAround)
+ {
code.append(TARGET_FIELD);
}
else
@@ -1812,7 +1817,8 @@
invokeNextBody.append(" ");
invokeNextBody.append(returnStr);
invokeNextBody.append(" ");
- boolean result = super.appendAdviceCall(setup, invokeNextBody, true, generator);
+ boolean result = super.appendAdviceCall(setup, invokeNextBody, true,
+ generator);
if (!firstParamIsInvocation)
{
Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodByConJoinPointGenerator.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodByConJoinPointGenerator.java 2007-01-04 21:49:18 UTC (rev 59361)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodByConJoinPointGenerator.java 2007-01-04 23:17:21 UTC (rev 59362)
@@ -139,7 +139,7 @@
method.getParameterTypes(),
method.getExceptionTypes(),
null,
- Context.STATIC);
+ Context.TARGET_AVAILABLE);
}
protected boolean isCaller()
Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodByMethodJoinPointGenerator.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodByMethodJoinPointGenerator.java 2007-01-04 21:49:18 UTC (rev 59361)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodByMethodJoinPointGenerator.java 2007-01-04 23:17:21 UTC (rev 59362)
@@ -148,7 +148,9 @@
method.getParameterTypes(),
method.getExceptionTypes(),
null,
- hasCallingObject()? Context.CALLER_AVAILABLE: Context.STATIC);
+ hasCallingObject()?
+ Context.TARGET_CALLER_AVAILABLE:
+ Context.TARGET_AVAILABLE);
}
protected boolean isCaller()
Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodJoinPointGenerator.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodJoinPointGenerator.java 2007-01-04 21:49:18 UTC (rev 59361)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodJoinPointGenerator.java 2007-01-04 23:17:21 UTC (rev 59362)
@@ -132,7 +132,7 @@
method.getParameterTypes(),
method.getExceptionTypes(),
null,
- Context.STATIC);
+ Context.TARGET_AVAILABLE);
}
Modified: projects/aop/trunk/aop/src/test/org/jboss/test/aop/beforeafterArgs/TargetTestCase.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/beforeafterArgs/TargetTestCase.java 2007-01-04 21:49:18 UTC (rev 59361)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/beforeafterArgs/TargetTestCase.java 2007-01-04 23:17:21 UTC (rev 59362)
@@ -71,39 +71,42 @@
{
pojo.field1 = 0;
assertAllAdvices();
- assertSame(pojo, TargetAspect.before2Target);
+ assertTarget(pojo);
}
public void test3()
{
int test = pojo.field1;
assertAllAdvices();
- assertSame(pojo, TargetAspect.before2Target);
+ assertTarget(pojo);
}
public void test4()
{
TargetPOJO.field2 = 5;
- assertStaticAdvices();
+ assertAllAdvices();
+ assertTarget(null);
}
public void test5()
{
int test = TargetPOJO.field2;
- assertStaticAdvices();
+ assertAllAdvices();
+ assertTarget(null);
}
public void test6()
{
pojo.method1();
assertAllAdvices();
- assertSame(pojo, TargetAspect.before2Target);
+ assertTarget(pojo);
}
public void test7()
{
TargetPOJO.method2();
- assertStaticAdvices();
+ assertAllAdvices();
+ assertTarget(null);
}
public void test8()
@@ -116,12 +119,14 @@
{
pojo.method4();
assertAllAdvices();
+ assertTarget();
}
public void test10()
{
pojo.method5();
- assertStaticAdvices();
+ assertAllAdvices();
+ assertTarget(null);
}
public void test11()
@@ -134,12 +139,14 @@
{
TargetPOJO.method7();
assertAllAdvices();
+ assertTarget();
}
public void test13()
{
TargetPOJO.method8();
- assertStaticAdvices();
+ assertAllAdvices();
+ assertTarget(null);
}
private void assertAllAdvices()
@@ -150,11 +157,22 @@
assertTrue(TargetAspect.around2);
assertTrue(TargetAspect.after1);
assertTrue(TargetAspect.after2);
+ }
+
+ private void assertTarget()
+ {
assertNotNull(TargetAspect.before2Target);
assertSame(TargetAspect.before2Target, TargetAspect.around2Target);
assertSame(TargetAspect.around2Target, TargetAspect.after2Target);
}
+ private void assertTarget(Object target)
+ {
+ assertSame(target, TargetAspect.before2Target);
+ assertSame(TargetAspect.before2Target, TargetAspect.around2Target);
+ assertSame(TargetAspect.around2Target, TargetAspect.after2Target);
+ }
+
private void assertStaticAdvices()
{
assertTrue(TargetAspect.before1);
More information about the jboss-cvs-commits
mailing list