cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r606722 - /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/enhancer/EmbeddableVisitor.java
Date Mon, 24 Dec 2007 18:33:32 GMT
Author: aadamchik
Date: Mon Dec 24 10:33:30 2007
New Revision: 606722

URL: http://svn.apache.org/viewvc?rev=606722&view=rev
Log:
JPA: adding check for attempted double enhancement of embeddables

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/enhancer/EmbeddableVisitor.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/enhancer/EmbeddableVisitor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/enhancer/EmbeddableVisitor.java?rev=606722&r1=606721&r2=606722&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/enhancer/EmbeddableVisitor.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/enhancer/EmbeddableVisitor.java
Mon Dec 24 10:33:30 2007
@@ -18,9 +18,13 @@
  ****************************************************************/
 package org.apache.cayenne.enhancer;
 
+import java.util.ArrayList;
+import java.util.Collection;
+
 import org.apache.cayenne.Persistent;
 import org.objectweb.asm.ClassAdapter;
 import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.FieldVisitor;
 
 /**
  * Enhances classes passed through the visitor to add embeddable fields and methods needed
@@ -31,16 +35,56 @@
  */
 public class EmbeddableVisitor extends ClassAdapter {
 
+    private static String OWNER_FEILD = "owner";
+    private static String EMBEDDED_PROPERTY_FEILD = "embeddedProperty";
+
     protected EnhancementHelper helper;
+    protected Collection<String> reservedFieldNames;
+    protected boolean checkReserved;
 
     public EmbeddableVisitor(ClassVisitor visitor) {
         super(visitor);
         this.helper = new EnhancementHelper(this);
+        this.checkReserved = true;
+
+        this.reservedFieldNames = new ArrayList<String>(2);
+        reservedFieldNames.add(helper.getPropertyField(OWNER_FEILD));
+        reservedFieldNames.add(helper.getPropertyField(EMBEDDED_PROPERTY_FEILD));
+    }
+
+    /**
+     * Checks that no double enhancement happens.
+     */
+    @Override
+    public FieldVisitor visitField(
+            int access,
+            String name,
+            String desc,
+            String signature,
+            Object value) {
+
+        if (checkReserved && reservedFieldNames.contains(name)) {
+            throw new DoubleEnhanceException("Embeddable class already contains field "
+                    + name);
+        }
+
+        return super.visitField(access, name, desc, signature, value);
     }
 
     @Override
     public void visitEnd() {
-        helper.createField(Persistent.class, "owner");
-        helper.createField(String.class, "embeddedProperty");
+
+        // 'checkReserved' flipping makes the visitor thread-unsafe... pay attention if
+        // we are to ever reuse the visitor for multiple classes...
+
+        checkReserved = false;
+
+        try {
+            helper.createField(Persistent.class, OWNER_FEILD);
+            helper.createField(String.class, EMBEDDED_PROPERTY_FEILD);
+        }
+        finally {
+            checkReserved = true;
+        }
     }
 }



Mime
View raw message