diff options
author | acolyer <acolyer> | 2005-09-21 14:59:55 +0000 |
---|---|---|
committer | acolyer <acolyer> | 2005-09-21 14:59:55 +0000 |
commit | a39f595c0cdcddf8eac0b99e1918d0578f2dc501 (patch) | |
tree | 94865da1d5f277582d9d0720347693cd3b3c258a /bcel-builder | |
parent | fc2d08e2ae9d03fb377bd0ed0bd56983af4687a5 (diff) | |
download | aspectj-a39f595c0cdcddf8eac0b99e1918d0578f2dc501.tar.gz aspectj-a39f595c0cdcddf8eac0b99e1918d0578f2dc501.zip |
tests and implementation for 108120 - runtime pointcut parsing and matching.
Diffstat (limited to 'bcel-builder')
3 files changed, 96 insertions, 2 deletions
diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/JavaClass.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/JavaClass.java index 29676da59..e92f6a798 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/JavaClass.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/JavaClass.java @@ -77,7 +77,7 @@ import java.util.StringTokenizer; * class file. Those interested in programatically generating classes * should see the <a href="../generic/ClassGen.html">ClassGen</a> class. - * @version $Id: JavaClass.java,v 1.6 2005/07/08 15:17:23 aclement Exp $ + * @version $Id: JavaClass.java,v 1.7 2005/09/21 15:02:05 acolyer Exp $ * @see org.aspectj.apache.bcel.generic.ClassGen * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> */ @@ -454,6 +454,27 @@ public class JavaClass extends AccessFlags implements Cloneable, Node { return null; } + + public Method getMethod(java.lang.reflect.Constructor c) { + for (int i = 0; i < methods.length; i++) { + Method method = methods[i]; + + if (method.getName().equals("<init>") + && (c.getModifiers() == method.getModifiers()) + && Type.getSignature(c).equals(method.getSignature())) { + return method; + } + } + + return null; + } + + public Field getField(java.lang.reflect.Field field) { + for (int i = 0; i < fields.length; i++) { + if (fields[i].getName().equals(field.getName())) return fields[i]; + } + return null; + } /** * @return Minor number of class file version. diff --git a/bcel-builder/src/org/aspectj/apache/bcel/generic/Type.java b/bcel-builder/src/org/aspectj/apache/bcel/generic/Type.java index dee474795..c20a4ff04 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/generic/Type.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/generic/Type.java @@ -65,7 +65,7 @@ import org.aspectj.apache.bcel.classfile.Utility; * Abstract super class for all possible java types, namely basic types * such as int, object types like String and array types, e.g. int[] * - * @version $Id: Type.java,v 1.5 2005/06/01 14:57:23 aclement Exp $ + * @version $Id: Type.java,v 1.6 2005/09/21 15:02:04 acolyer Exp $ * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> * * modified: @@ -305,6 +305,18 @@ public abstract class Type implements java.io.Serializable { return sb.toString(); } + public static String getSignature(java.lang.reflect.Constructor cons) { + StringBuffer sb = new StringBuffer("("); + Class[] params = cons.getParameterTypes(); // avoid clone + + for(int j = 0; j < params.length; j++) { + sb.append(getType(params[j]).getSignature()); + } + + sb.append(")V"); + return sb.toString(); + } + public static class TypeHolder { private Type t; private int consumed; diff --git a/bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/GetReflectMembersTest.java b/bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/GetReflectMembersTest.java new file mode 100644 index 000000000..06bd9ccc8 --- /dev/null +++ b/bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/GetReflectMembersTest.java @@ -0,0 +1,61 @@ +/* ******************************************************************* + * 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: + * Adrian Colyer Initial implementation + * ******************************************************************/ +package org.aspectj.apache.bcel.classfile.tests; + +import org.aspectj.apache.bcel.classfile.JavaClass; +import org.aspectj.apache.bcel.util.ClassLoaderRepository; +import org.aspectj.apache.bcel.util.Repository; + +import junit.framework.TestCase; + +/** + * @author colyer + * + */ +public class GetReflectMembersTest extends TestCase { + + private Repository bcelRepository; + private JavaClass jc; + + public void testGetMethod() throws Exception { + assertNotNull(jc.getMethod(GetMe.class.getMethod("foo",new Class[] {String.class}))); + } + + public void testGetConstructor() throws Exception { + assertNotNull(jc.getMethod(GetMe.class.getConstructor(new Class[] {int.class}))); + } + + public void testGetField() throws Exception { + assertNotNull(jc.getField(GetMe.class.getDeclaredField("x"))); + } + + protected void setUp() throws Exception { + super.setUp(); + this.bcelRepository = new ClassLoaderRepository(getClass().getClassLoader()); + this.jc = bcelRepository.loadClass(GetMe.class); + } + + protected void tearDown() throws Exception { + super.tearDown(); + this.bcelRepository.clear(); + } + + private static class GetMe { + + private int x; + + public GetMe(int x) { this.x = x;} + + public void foo(String s) {}; + + } +} |