From: acolyer Date: Mon, 19 Dec 2005 16:54:36 +0000 (+0000) Subject: merge of RB_V1_5_0 changes back into HEAD X-Git-Tag: POST_MEMORY_CHANGES~209 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1cbece39b31c67aa9c8711319c979992b1976536;p=aspectj.git merge of RB_V1_5_0 changes back into HEAD --- diff --git a/aspectj5rt/java5-src/org/aspectj/lang/Aspects.java b/aspectj5rt/java5-src/org/aspectj/lang/Aspects.java new file mode 100644 index 000000000..e82320c2e --- /dev/null +++ b/aspectj5rt/java5-src/org/aspectj/lang/Aspects.java @@ -0,0 +1,190 @@ +/******************************************************************************* + * Copyright (c) 2005 Contributors. + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://eclipse.org/legal/epl-v10.html + * + * Contributors: + * initial implementation Alexandre Vasseur + * generic signature update Adrian Colyer + *******************************************************************************/ +package org.aspectj.lang; + + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.InvocationTargetException; + +/** + * Handles generic aspectOf method when those are not available in the aspects but added later on + * thru load time weaving. + *

+ * Aspects.aspectOf(..) is doing reflective calls to the aspect aspectOf, so for better performance + * consider using ajc compilation of the aspects and using them as a binary dependancies in your project. + * + * @author Alexandre Vasseur + */ +public class Aspects { + + private final static Class[] EMPTY_CLASS_ARRAY = new Class[0]; + private final static Class[] PEROBJECT_CLASS_ARRAY = new Class[]{Object.class}; + private final static Class[] PERTYPEWITHIN_CLASS_ARRAY = new Class[]{Class.class}; + private final static Object[] EMPTY_OBJECT_ARRAY = new Object[0]; + private final static String ASPECTOF = "aspectOf"; + private final static String HASASPECT = "hasAspect"; + + /** + * Returns the singleton aspect or the percflow / percflowbelow associated with the current thread + * + * @param aspectClass + * @return + * @throws NoAspectBoundException if no such aspect + */ + public static T aspectOf(Class aspectClass) throws NoAspectBoundException { + try { + return (T) getSingletonOrThreadAspectOf(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY); + } catch (InvocationTargetException e) { + //FIXME asc Highly temporary change to see what the build makes of it - dont use 1.4 APIs + throw new NoAspectBoundException(aspectClass.getName(), e);//e.getCause()); + } catch (Exception e) { + throw new NoAspectBoundException(aspectClass.getName(), e); + } + } + + /** + * Returns the perthis / pertarget aspect + * @param aspectClass + * @param perObject + * @return + * @throws NoAspectBoundException if no such aspect, or no aspect bound + */ + public static T aspectOf(Class aspectClass, Object perObject) throws NoAspectBoundException { + try { + return (T) getPerObjectAspectOf(aspectClass).invoke(null, new Object[]{perObject}); + } catch (InvocationTargetException e) { + //FIXME asc Highly temporary change to see what the build makes of it - dont use 1.4 APIs + throw new NoAspectBoundException(aspectClass.getName(), e);//e.getCause()); + } catch (Exception e) { + throw new NoAspectBoundException(aspectClass.getName(), e); + } + } + + /** + * Returns the pertypewithin aspect + * @param aspectClass + * @param perTypeWithin class + * @return + * @throws NoAspectBoundException if no such aspect, or no aspect bound + */ + public static T aspectOf(Class aspectClass, Class perTypeWithin) throws NoAspectBoundException { + try { + return (T) getPerTypeWithinAspectOf(aspectClass).invoke(null, new Object[]{perTypeWithin}); + } catch (InvocationTargetException e) { +// FIXME asc Highly temporary change to see what the build makes of it - dont use 1.4 APIs + throw new NoAspectBoundException(aspectClass.getName(), e);//e.getCause()); + } catch (Exception e) { + throw new NoAspectBoundException(aspectClass.getName(), e); + } + } + + /** + * Returns true if singleton aspect or percflow / percflowbelow aspect is bound + * + * @param aspectClass + * @return + * @throws NoAspectBoundException if not bound + */ + public static boolean hasAspect(Class aspectClass) throws NoAspectBoundException { + try { + return ((Boolean)getSingletonOrThreadHasAspect(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY)).booleanValue(); + } catch (Exception e) { + return false; + } + } + + /** + * Returns true if the perthis / pertarget aspect is bound + * @param aspectClass + * @param perObject + * @return + * @throws NoAspectBoundException if not bound + */ + public static boolean hasAspect(Class aspectClass, Object perObject) throws NoAspectBoundException { + try { + return ((Boolean)getPerObjectHasAspect(aspectClass).invoke(null, new Object[]{perObject})).booleanValue(); + } catch (Exception e) { + return false; + } + } + + /** + * Returns true if the pertypewithin aspect is bound + * @param aspectClass + * @param perTypeWithin class + * @return + * @throws NoAspectBoundException if not bound + */ + public static boolean hasAspect(Class aspectClass, Class perTypeWithin) throws NoAspectBoundException { + try { + return ((Boolean)getPerTypeWithinHasAspect(aspectClass).invoke(null, new Object[]{perTypeWithin})).booleanValue(); + } catch (Exception e) { + return false; + } + } + + // -- aspectOf + + private static Method getSingletonOrThreadAspectOf(Class aspectClass) throws NoSuchMethodException { + Method method = aspectClass.getDeclaredMethod(ASPECTOF, EMPTY_CLASS_ARRAY); + return checkAspectOf(method, aspectClass); + } + + private static Method getPerObjectAspectOf(Class aspectClass) throws NoSuchMethodException { + Method method = aspectClass.getDeclaredMethod(ASPECTOF, PEROBJECT_CLASS_ARRAY); + return checkAspectOf(method, aspectClass); + } + + private static Method getPerTypeWithinAspectOf(Class aspectClass) throws NoSuchMethodException { + Method method = aspectClass.getDeclaredMethod(ASPECTOF, PERTYPEWITHIN_CLASS_ARRAY); + return checkAspectOf(method, aspectClass); + } + + private static Method checkAspectOf(Method method, Class aspectClass) throws NoSuchMethodException { + method.setAccessible(true); + if (!method.isAccessible() + || !Modifier.isPublic(method.getModifiers()) + || !Modifier.isStatic(method.getModifiers())) { + throw new NoSuchMethodException(aspectClass.getName() + ".aspectOf(..) is not accessible public static"); + } + return method; + } + + // -- hasAspect + + private static Method getSingletonOrThreadHasAspect(Class aspectClass) throws NoSuchMethodException { + Method method = aspectClass.getDeclaredMethod(HASASPECT, EMPTY_CLASS_ARRAY); + return checkHasAspect(method, aspectClass); + } + + private static Method getPerObjectHasAspect(Class aspectClass) throws NoSuchMethodException { + Method method = aspectClass.getDeclaredMethod(HASASPECT, PEROBJECT_CLASS_ARRAY); + return checkHasAspect(method, aspectClass); + } + + private static Method getPerTypeWithinHasAspect(Class aspectClass) throws NoSuchMethodException { + Method method = aspectClass.getDeclaredMethod(HASASPECT, PERTYPEWITHIN_CLASS_ARRAY); + return checkHasAspect(method, aspectClass); + } + + private static Method checkHasAspect(Method method, Class aspectClass) throws NoSuchMethodException { + method.setAccessible(true); + if (!method.isAccessible() + || !Modifier.isPublic(method.getModifiers()) + || !Modifier.isStatic(method.getModifiers())) { + throw new NoSuchMethodException(aspectClass.getName() + ".hasAspect(..) is not accessible public static"); + } + return method; + } +} diff --git a/lib/aspectj/lib/aspectjrt.jar b/lib/aspectj/lib/aspectjrt.jar index 297c989ac..bca216381 100644 Binary files a/lib/aspectj/lib/aspectjrt.jar and b/lib/aspectj/lib/aspectjrt.jar differ diff --git a/lib/test/aspectjrt.jar b/lib/test/aspectjrt.jar index 297c989ac..bca216381 100644 Binary files a/lib/test/aspectjrt.jar and b/lib/test/aspectjrt.jar differ diff --git a/tests/java5/ataspectj/ataspectj/DeclareParentsInterfaceTest.java b/tests/java5/ataspectj/ataspectj/DeclareParentsInterfaceTest.java index 2823251e5..d21fc7b7d 100644 --- a/tests/java5/ataspectj/ataspectj/DeclareParentsInterfaceTest.java +++ b/tests/java5/ataspectj/ataspectj/DeclareParentsInterfaceTest.java @@ -14,7 +14,6 @@ package ataspectj; import junit.framework.TestCase; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; -import org.aspectj.lang.annotation.DeclareImplements; import org.aspectj.lang.annotation.DeclareParents; import java.util.Arrays; diff --git a/tests/java5/ataspectj/ataspectj/bugs/AspectOfWhenAspectNotInIncludeTest.java b/tests/java5/ataspectj/ataspectj/bugs/AspectOfWhenAspectNotInIncludeTest.java index 5d4437eef..0703215f3 100644 --- a/tests/java5/ataspectj/ataspectj/bugs/AspectOfWhenAspectNotInIncludeTest.java +++ b/tests/java5/ataspectj/ataspectj/bugs/AspectOfWhenAspectNotInIncludeTest.java @@ -15,9 +15,9 @@ import junit.framework.TestCase; import junit.framework.Test; import junit.framework.TestSuite; import ataspectj.TestHelper; +import org.aspectj.lang.annotation.DeclareParents; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.DeclareImplements; import org.aspectj.lang.Aspects; import java.io.Serializable; @@ -36,7 +36,7 @@ public class AspectOfWhenAspectNotInIncludeTest extends TestCase { @Aspect static class TestAspectForAspect { - @DeclareImplements("ataspectj.bugs.AspectOfWhenAspectNotInIncludeTest.TestAspect") + @DeclareParents("ataspectj.bugs.AspectOfWhenAspectNotInIncludeTest.TestAspect") Serializable shouldNotHappenDueToInclude; }