### Eclipse Workspace Patch 1.0 #P cglib Index: src/test/net/sf/cglib/TestAll.java =================================================================== RCS file: /cvsroot/cglib/cglib/src/test/net/sf/cglib/TestAll.java,v retrieving revision 1.66 diff -u -r1.66 TestAll.java --- src/test/net/sf/cglib/TestAll.java 23 Dec 2004 03:46:25 -0000 1.66 +++ src/test/net/sf/cglib/TestAll.java 8 Apr 2010 04:22:34 -0000 @@ -81,6 +81,7 @@ suite.addTest(TestProvideFields.suite()); suite.addTest(TestAddDelegate.suite()); suite.addTest(TestInterceptFields.suite()); + suite.addTest(TestInterceptFieldsWithAnnotations.suite()); suite.addTest(TestDemo.suite()); // performance Index: src/proxy/net/sf/cglib/core/ClassEmitter.java =================================================================== RCS file: /cvsroot/cglib/cglib/src/proxy/net/sf/cglib/core/ClassEmitter.java,v retrieving revision 1.14 diff -u -r1.14 ClassEmitter.java --- src/proxy/net/sf/cglib/core/ClassEmitter.java 5 Mar 2006 02:43:19 -0000 1.14 +++ src/proxy/net/sf/cglib/core/ClassEmitter.java 8 Apr 2010 04:22:33 -0000 @@ -176,16 +176,18 @@ } public void declare_field(int access, String name, Type type, Object value) { + declaredFieldVisitor = null; //clear old references FieldInfo existing = (FieldInfo)fieldInfo.get(name); FieldInfo info = new FieldInfo(access, name, type, value); + if (existing != null) { if (!info.equals(existing)) { throw new IllegalArgumentException("Field \"" + name + "\" has been declared differently"); } } else { fieldInfo.put(name, info); - cv.visitField(access, name, type.getDescriptor(), null, value); - } + declaredFieldVisitor = cv.visitField(access, name, type.getDescriptor(), null, value); + } } // TODO: make public? @@ -254,14 +256,18 @@ public void visitEnd() { end_class(); } + + public transient FieldVisitor declaredFieldVisitor; public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { - declare_field(access, name, Type.getType(desc), value); - return null; // TODO + declare_field(access, name, Type.getType( desc ), value); + final FieldVisitor result = declaredFieldVisitor; + declaredFieldVisitor = null; //clear reference to be safe + return result; } public MethodVisitor visitMethod(int access, Index: src/test/net/sf/cglib/transform/impl/IsPresent.java =================================================================== RCS file: src/test/net/sf/cglib/transform/impl/IsPresent.java diff -N src/test/net/sf/cglib/transform/impl/IsPresent.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/test/net/sf/cglib/transform/impl/IsPresent.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,15 @@ +package net.sf.cglib.transform.impl; + +import java.lang.annotation.Target; +import java.lang.annotation.Retention; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.FIELD; + +/** + * @author Emmanuel Bernard + */ +@Target({METHOD, FIELD}) +@Retention(RUNTIME) +public @interface IsPresent { +} Index: src/test/net/sf/cglib/transform/impl/TestInterceptFieldsWithAnnotations.java =================================================================== RCS file: src/test/net/sf/cglib/transform/impl/TestInterceptFieldsWithAnnotations.java diff -N src/test/net/sf/cglib/transform/impl/TestInterceptFieldsWithAnnotations.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/test/net/sf/cglib/transform/impl/TestInterceptFieldsWithAnnotations.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,215 @@ +/* + * Copyright 2003 The Apache Software Foundation + * + * 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 net.sf.cglib.transform.impl; + +import java.lang.reflect.Method; +import java.lang.reflect.Field; + +import net.sf.cglib.transform.*; +import junit.framework.*; +import org.objectweb.asm.*; + +/** + * + * @author baliuka + */ +public class TestInterceptFieldsWithAnnotations extends AbstractTransformTest implements InterceptFieldCallback{ + + static Object TEST_VALUE = "test1"; + + @IsPresent + String field; + + @IsPresent + String getField() { return null; } + + /** Creates a new instance of TestInterceptFields */ + public TestInterceptFieldsWithAnnotations() { + } + + /** Creates a new instance of TestInterceptFields */ + public TestInterceptFieldsWithAnnotations(String name) { + super(name); + } + + + public void test(){ + + + InterceptFieldEnabled e = (InterceptFieldEnabled)this; + e.setInterceptFieldCallback( this ); + field = "test"; + assertEquals(TEST_VALUE,field); + + try { + Class aClass = this.getClass(); + + + System.out.println("Method getField annotations:"); + Method method = aClass.getDeclaredMethod( "getField" ); + Assert.assertEquals( "cannot read annotations on methods", 1, method.getAnnotations().length ); + + System.out.println("Field field annotations:"); + Field field = aClass.getDeclaredField( "field" ); + Assert.assertEquals( "cannot read annotations on fields", 1, field.getAnnotations().length ); + + } + catch ( NoSuchMethodException ee ) { + ee.printStackTrace(); + } + catch ( NoSuchFieldException ee ) { + ee.printStackTrace(); + } + + } + + protected ClassTransformerFactory getTransformer() throws Exception { + + return new ClassTransformerFactory(){ + + public ClassTransformer newInstance(){ + + return new InterceptFieldTransformer( + + new InterceptFieldFilter(){ + + public boolean acceptRead(Type owner, String name){ + return true; + } + public boolean acceptWrite(Type owner, String name){ + return true; + } + + } + + ); + + } + + }; + + + + } + + + + + + public boolean readBoolean(Object _this, String name, boolean oldValue) { + + return oldValue; + } + + public byte readByte(Object _this, String name, byte oldValue) { + + return oldValue; + } + + public char readChar(Object _this, String name, char oldValue) { + + return oldValue; + } + + public double readDouble(Object _this, String name, double oldValue) { + + return oldValue; + } + + public float readFloat(Object _this, String name, float oldValue) { + + return oldValue; + } + + public int readInt(Object _this, String name, int oldValue) { + + return oldValue; + } + + public long readLong(Object _this, String name, long oldValue) { + + return oldValue; + } + + public Object readObject(Object _this, String name, Object oldValue) { + + return TEST_VALUE; + } + + public short readShort(Object _this, String name, short oldValue) { + + return oldValue; + } + + public boolean writeBoolean(Object _this, String name, boolean oldValue, boolean newValue) { + + return newValue; + } + + public byte writeByte(Object _this, String name, byte oldValue, byte newValue) { + + return newValue; + } + + public char writeChar(Object _this, String name, char oldValue, char newValue) { + + return newValue; + } + + public double writeDouble(Object _this, String name, double oldValue, double newValue) { + + return newValue; + } + + public float writeFloat(Object _this, String name, float oldValue, float newValue) { + + return newValue; + } + + public int writeInt(Object _this, String name, int oldValue, int newValue) { + + return newValue; + } + + public long writeLong(Object _this, String name, long oldValue, long newValue) { + + return newValue; + } + + public Object writeObject(Object _this, String name, Object oldValue, Object newValue) { + + return newValue; + } + + public short writeShort(Object _this, String name, short oldValue, short newValue) { + + return newValue; + } + + + + + public static void main(String[] args) throws Exception{ + junit.textui.TestRunner.run(suite()); + } + + public static Test suite() throws Exception{ + return new TestSuite( new TestInterceptFieldsWithAnnotations( ).transform() ); + } + + +} \ No newline at end of file