From: aclement Date: Fri, 9 May 2008 16:13:55 +0000 (+0000) Subject: big refactoring X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=08ee3d1bcad062fb9ee76cdf74c2589704a7bc16;p=aspectj.git big refactoring --- diff --git a/weaver5/java5-src/org/aspectj/weaver/reflect/Java15AnnotationFinder.java b/weaver5/java5-src/org/aspectj/weaver/reflect/Java15AnnotationFinder.java index 4a51e98da..fd8924b6a 100644 --- a/weaver5/java5-src/org/aspectj/weaver/reflect/Java15AnnotationFinder.java +++ b/weaver5/java5-src/org/aspectj/weaver/reflect/Java15AnnotationFinder.java @@ -21,11 +21,14 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; +import org.aspectj.apache.bcel.classfile.AnnotationDefault; +import org.aspectj.apache.bcel.classfile.Attribute; import org.aspectj.apache.bcel.classfile.JavaClass; import org.aspectj.apache.bcel.classfile.LocalVariable; import org.aspectj.apache.bcel.classfile.LocalVariableTable; import org.aspectj.apache.bcel.util.NonCachingClassLoaderRepository; import org.aspectj.apache.bcel.util.Repository; +import org.aspectj.weaver.AnnotationX; import org.aspectj.weaver.ResolvedType; import org.aspectj.weaver.UnresolvedType; import org.aspectj.weaver.World; @@ -96,6 +99,74 @@ public class Java15AnnotationFinder implements AnnotationFinder, ArgNameFinder { return null; } + public AnnotationX getAnnotationOfType(UnresolvedType ofType,Member onMember) { + if (!(onMember instanceof AccessibleObject)) return null; + // here we really want both the runtime visible AND the class visible annotations + // so we bail out to Bcel and then chuck away the JavaClass so that we don't hog + // memory. + try { + JavaClass jc = bcelRepository.loadClass(onMember.getDeclaringClass()); + org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[] anns = new org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[0]; + if (onMember instanceof Method) { + org.aspectj.apache.bcel.classfile.Method bcelMethod = jc.getMethod((Method)onMember); + if (bcelMethod == null) { + // pr220430 + //System.err.println("Unexpected problem in Java15AnnotationFinder: cannot retrieve annotations on method '"+onMember.getName()+"' in class '"+jc.getClassName()+"'"); + } else { + anns = bcelMethod.getAnnotations(); + } + } else if (onMember instanceof Constructor) { + org.aspectj.apache.bcel.classfile.Method bcelCons = jc.getMethod((Constructor)onMember); + anns = bcelCons.getAnnotations(); + } else if (onMember instanceof Field) { + org.aspectj.apache.bcel.classfile.Field bcelField = jc.getField((Field)onMember); + anns = bcelField.getAnnotations(); + } + // the answer is cached and we don't want to hold on to memory + bcelRepository.clear(); + if (anns == null) anns = new org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[0]; + // convert to our Annotation type + for (int i=0;i expected = new HashSet(); + expected.addAll(mpnv.getParameterTypeAnnotations()); + + StringTokenizer st = new StringTokenizer(expectedParameterTypeAnnotations==null?"":expectedParameterTypeAnnotations); + while (st.hasMoreTokens()) { + String nextToken = st.nextToken(); + if (!expected.contains(nextToken)) + fail("In pointcut expression "+pe.getPointcutExpression()+" parameter "+parameterNumber+". The annotation type pattern did not include parameter type annotation "+nextToken+". It's full set was "+mpnv.getParameterTypeAnnotations()); + expected.remove(nextToken); + } + if (expected.size()>0) { // we have excess ones! + StringBuffer excessTokens = new StringBuffer(); + for (Iterator iterator = expected.iterator(); iterator.hasNext();) { + String string = (String) iterator.next(); + excessTokens.append(string).append(" "); + } + fail("In pointcut expression "+pe.getPointcutExpression()+" parameter "+parameterNumber+". The annotation type pattern has these unexpected parameter type annotations "+excessTokens.toString()); + } + + // parameter annotation checking + expected = new HashSet(); + expected.addAll(mpnv.getParameterAnnotations()); + + st = new StringTokenizer(expectedParameterAnnotations==null?"":expectedParameterAnnotations); + while (st.hasMoreTokens()) { + String nextToken = st.nextToken(); + if (!expected.contains(nextToken)) + fail("In pointcut expression "+pe.getPointcutExpression()+" parameter "+parameterNumber+". The annotation type pattern did not include parameter annotation "+nextToken+". It's full set was "+mpnv.getParameterAnnotations()); + expected.remove(nextToken); + } + if (expected.size()>0) { // we have excess ones! + StringBuffer excessTokens = new StringBuffer(); + for (Iterator iterator = expected.iterator(); iterator.hasNext();) { + String string = (String) iterator.next(); + excessTokens.append(string).append(" "); + } + fail("In pointcut expression "+pe.getPointcutExpression()+" parameter "+parameterNumber+". The annotation type pattern has these unexpected parameter annotations "+excessTokens.toString()); + } + + } + + static class MyPatternNodeVisitor extends AbstractPatternNodeVisitor { + private StringBuffer stringRep = new StringBuffer(); + private List parameterAnnotations = new ArrayList(); + private List parameterTypeAnnotations = new ArrayList(); + + public String getStringRepresentation() { return stringRep.toString(); } + public List getParameterAnnotations() { return parameterAnnotations; } + public List getParameterTypeAnnotations() { return parameterTypeAnnotations; } + + public Object visit(AndAnnotationTypePattern node, Object data) { + stringRep.append("("); + node.getLeft().accept(this, data); + stringRep.append(" and "); + node.getRight().accept(this, data); + stringRep.append(")"); + return node; + } + public Object visit(AnyAnnotationTypePattern node, Object data) { + stringRep.append("any"); + return node; + } + public Object visit(ExactAnnotationTypePattern node, Object data) { + stringRep.append("exact["+stringify(node.getResolvedAnnotationType())+":"+(node.isForParameterAnnotationMatch()?"p":"t")+"]"); + if (node.isForParameterAnnotationMatch()) { + parameterAnnotations.add(stringify(node.getResolvedAnnotationType())); + } else { + parameterTypeAnnotations.add(stringify(node.getResolvedAnnotationType())); + } + return node; + } + private String stringify(ResolvedType resolvedAnnotationType) { + return "@"+resolvedAnnotationType.getSimpleName(); + } + + public Object visit(BindingAnnotationTypePattern node, Object data) { + stringRep.append("binding"); + + return node; + } + public Object visit(NotAnnotationTypePattern node, Object data) { + stringRep.append("not"); + return node; + } + public Object visit(OrAnnotationTypePattern node, Object data) { + stringRep.append("("); + node.getLeft().accept(this, data); + stringRep.append(" or "); + node.getRight().accept(this, data); + stringRep.append(")"); + return node; + } + public Object visit(WildAnnotationTypePattern node, Object data) { + stringRep.append("wild["); + stringRep.append(node.getTypePattern().toString()); + stringRep.append("]"); + return node; + } + public Object visit(AnnotationPatternList node, Object data) { + stringRep.append("list"); + + return node; + } + + + } + + + public void testAtThis() { PointcutExpression atThis = parser.parsePointcutExpression("@this(org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation)"); ShadowMatch sMatch1 = atThis.matchesMethodExecution(a); diff --git a/weaver5/java5-testsrc/test/A.java b/weaver5/java5-testsrc/test/A.java new file mode 100644 index 000000000..12959cd51 --- /dev/null +++ b/weaver5/java5-testsrc/test/A.java @@ -0,0 +1,27 @@ +/* ******************************************************************* + * Copyright (c) 2008 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://www.eclipse.org/legal/epl-v10.html + * + * Contributors + * Andy Clement + * ******************************************************************/ +package test; + +public class A { + public void a(String s) {} + public void b(@A1 String s) {} + public void c(@A1 @A2 String s) {} + public void d(@A1 String s,@A2 String t) {} + + public void e(A1AnnotatedType s) {} + public void f(A2AnnotatedType s) {} + public void g(@A2 A1AnnotatedType s) {} + public void h(@A1 A1AnnotatedType s) {} + public void i(A1AnnotatedType s,@A2 String t) {} + public void j(@A1 @A2 String s) {} + +} diff --git a/weaver5/java5-testsrc/test/A1.java b/weaver5/java5-testsrc/test/A1.java new file mode 100644 index 000000000..616708345 --- /dev/null +++ b/weaver5/java5-testsrc/test/A1.java @@ -0,0 +1,20 @@ +/* ******************************************************************* + * Copyright (c) 2008 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://www.eclipse.org/legal/epl-v10.html + * + * Contributors + * Andy Clement + * ******************************************************************/ +package test; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface A1 { + +} diff --git a/weaver5/java5-testsrc/test/A1AnnotatedType.java b/weaver5/java5-testsrc/test/A1AnnotatedType.java new file mode 100644 index 000000000..e40addbf5 --- /dev/null +++ b/weaver5/java5-testsrc/test/A1AnnotatedType.java @@ -0,0 +1,17 @@ +/* ******************************************************************* + * Copyright (c) 2008 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://www.eclipse.org/legal/epl-v10.html + * + * Contributors + * Andy Clement + * ******************************************************************/ +package test; + +@A1 +public class A1AnnotatedType { + +} diff --git a/weaver5/java5-testsrc/test/A2.java b/weaver5/java5-testsrc/test/A2.java new file mode 100644 index 000000000..48749a3d4 --- /dev/null +++ b/weaver5/java5-testsrc/test/A2.java @@ -0,0 +1,20 @@ +/* ******************************************************************* + * Copyright (c) 2008 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://www.eclipse.org/legal/epl-v10.html + * + * Contributors + * Andy Clement + * ******************************************************************/ +package test; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface A2 { + +} diff --git a/weaver5/java5-testsrc/test/A2AnnotatedType.java b/weaver5/java5-testsrc/test/A2AnnotatedType.java new file mode 100644 index 000000000..0fa3b5c8a --- /dev/null +++ b/weaver5/java5-testsrc/test/A2AnnotatedType.java @@ -0,0 +1,17 @@ +/* ******************************************************************* + * Copyright (c) 2008 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://www.eclipse.org/legal/epl-v10.html + * + * Contributors + * Andy Clement + * ******************************************************************/ +package test; + +@A2 +public class A2AnnotatedType { + +} diff --git a/weaver5/java5-testsrc/test/A3.java b/weaver5/java5-testsrc/test/A3.java new file mode 100644 index 000000000..ab54388ae --- /dev/null +++ b/weaver5/java5-testsrc/test/A3.java @@ -0,0 +1,19 @@ +/* ******************************************************************* + * Copyright (c) 2008 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://www.eclipse.org/legal/epl-v10.html + * + * Contributors + * Andy Clement + * ******************************************************************/ +package test; + +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) +public @interface A3 { + Color value() default Color.RED; +} diff --git a/weaver5/java5-testsrc/test/AnnoValues.java b/weaver5/java5-testsrc/test/AnnoValues.java new file mode 100644 index 000000000..08301d1a0 --- /dev/null +++ b/weaver5/java5-testsrc/test/AnnoValues.java @@ -0,0 +1,20 @@ +/* ******************************************************************* + * Copyright (c) 2008 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://www.eclipse.org/legal/epl-v10.html + * + * Contributors + * Andy Clement + * ******************************************************************/ +package test; + +public class AnnoValues { + public void none() {} + @A3 public void defaultMethod() {} + @A3(Color.GREEN) public void greenMethod() {} + @A3(Color.RED) public void redMethod() {} + @A3(Color.BLUE) public void blueMethod() {} +} diff --git a/weaver5/java5-testsrc/test/Color.java b/weaver5/java5-testsrc/test/Color.java new file mode 100644 index 000000000..dea2593ac --- /dev/null +++ b/weaver5/java5-testsrc/test/Color.java @@ -0,0 +1,14 @@ +package test; +/* ******************************************************************* + * Copyright (c) 2008 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://www.eclipse.org/legal/epl-v10.html + * + * Contributors + * Andy Clement + * ******************************************************************/ + +public enum Color { RED, GREEN, BLUE } \ No newline at end of file