From 891ccc625c6126e7d2b9a50be5d468927151b74b Mon Sep 17 00:00:00 2001 From: Michael Dick Date: Wed, 16 Feb 2011 02:12:28 +0000 Subject: [PATCH] OPENJPA-1912: Generate externalizable methods correctly for super and subclasses. Submitted By: Mark Struberg git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1071123 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/openjpa/enhance/PCEnhancer.java | 83 +++++++++++++++++-- .../src/main/ant/enhancer.xml | 12 +++ .../openjpa/enhance/EnhancedSubClass.java | 41 +++++++++ .../openjpa/enhance/EnhancedSuperClass.java | 57 +++++++++++++ .../apache/openjpa/enhance/persistence1.xml | 37 +++++++++ pom.xml | 2 +- 6 files changed, 223 insertions(+), 9 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/enhance/EnhancedSubClass.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/enhance/EnhancedSuperClass.java create mode 100644 openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/enhance/persistence1.xml diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java index f5278aff7..b16b83716 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java @@ -4037,7 +4037,10 @@ public class PCEnhancer { Code code = meth.getCode(true); // super.readExternal (in); - Class sup = _meta.getDescribedType().getSuperclass(); + // not sure if this works: this is depending on the order of the enhancement! + // if the subclass gets enhanced first, then the superclass misses + // the Externalizable at this point! + Class sup = _meta.getDescribedType().getSuperclass(); if (!parentDetachable && Externalizable.class.isAssignableFrom(sup)) { loadManagedInstance(code, false); code.aload().setParam(0); @@ -4070,12 +4073,44 @@ public class PCEnhancer { void.class, new Class[]{ StateManager.class }); } + addReadExternalFields(); + + // readExternalFields(in.readObject ()); + loadManagedInstance(code, false); + code.aload().setParam(0); + code.invokevirtual().setMethod("readExternalFields", + void.class, inargs); + + code.vreturn(); + code.calculateMaxStack(); + code.calculateMaxLocals(); + } + + private void addReadExternalFields() throws NoSuchMethodException { + Class[] inargs = new Class[]{ ObjectInput.class }; + BCMethod meth = _pc.declareMethod("readExternalFields", void.class, inargs); + meth.setAccessFlags(Constants.ACCESS_PROTECTED); + Exceptions exceps = meth.getExceptions(true); + exceps.addException(IOException.class); + exceps.addException(ClassNotFoundException.class); + Code code = meth.getCode(true); + + Class sup = _meta.getPCSuperclass(); + if (sup != null) { + //add a call to super.readExternalFields() + loadManagedInstance(code, false); + code.aload().setParam(0); + code.invokespecial().setMethod(sup, "readExternalFields", void.class, inargs); + } + // read managed fields - FieldMetaData[] fmds = _meta.getFields(); - for (int i = 0; i < fmds.length; i++) - if (!fmds[i].isTransient()) + FieldMetaData[] fmds = _meta.getDeclaredFields(); + for (int i = 0; i < fmds.length; i++) { + if (!fmds[i].isTransient()) { readExternal(code, fmds[i].getName(), fmds[i].getDeclaredType(), fmds[i]); + } + } code.vreturn(); code.calculateMaxStack(); @@ -4227,12 +4262,44 @@ public class PCEnhancer { if (go2 != null) go2.setTarget(code.nop()); - // write managed fields - FieldMetaData[] fmds = _meta.getFields(); - for (int i = 0; i < fmds.length; i++) - if (!fmds[i].isTransient()) + addWriteExternalFields(); + + loadManagedInstance(code, false); + code.aload().setParam(0); + code.invokevirtual().setMethod("writeExternalFields", + void.class, outargs); + + // return + code.vreturn(); + code.calculateMaxStack(); + code.calculateMaxLocals(); + } + + + private void addWriteExternalFields() + throws NoSuchMethodException { + Class[] outargs = new Class[]{ ObjectOutput.class }; + BCMethod meth = _pc.declareMethod("writeExternalFields", void.class, outargs); + meth.setAccessFlags(Constants.ACCESS_PROTECTED); + Exceptions exceps = meth.getExceptions(true); + exceps.addException(IOException.class); + Code code = meth.getCode(true); + + Class sup = _meta.getPCSuperclass(); + if (sup != null) { + // add a call to super.readExternalFields() + loadManagedInstance(code, false); + code.aload().setParam(0); + code.invokespecial().setMethod(sup, "writeExternalFields", void.class, outargs); + } + + FieldMetaData[] fmds = _meta.getDeclaredFields(); + for (int i = 0; i < fmds.length; i++) { + if (!fmds[i].isTransient()) { writeExternal(code, fmds[i].getName(), fmds[i].getDeclaredType(), fmds[i]); + } + } // return code.vreturn(); diff --git a/openjpa-persistence-jdbc/src/main/ant/enhancer.xml b/openjpa-persistence-jdbc/src/main/ant/enhancer.xml index 90f4c52f1..8f54170ae 100644 --- a/openjpa-persistence-jdbc/src/main/ant/enhancer.xml +++ b/openjpa-persistence-jdbc/src/main/ant/enhancer.xml @@ -79,6 +79,8 @@ + + @@ -119,6 +121,16 @@ + + + + + + + + + + diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/enhance/EnhancedSubClass.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/enhance/EnhancedSubClass.java new file mode 100644 index 000000000..ee7f8cc4b --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/enhance/EnhancedSubClass.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.openjpa.enhance; + + +import javax.persistence.Entity; +import java.io.Serializable; + +/** + * @see TestClassHierarchyEnhancement + */ +@Entity +public class EnhancedSubClass extends EnhancedSuperClass implements Serializable { + private static final long serialVersionUID = 2311337663046757789L; + private String valueInSubclass; + + public String getValueInSubclass() { + return valueInSubclass; + } + + public void setValueInSubclass(String valueInSubclass) { + this.valueInSubclass = valueInSubclass; + } +} + diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/enhance/EnhancedSuperClass.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/enhance/EnhancedSuperClass.java new file mode 100644 index 000000000..bf5bc4285 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/enhance/EnhancedSuperClass.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.openjpa.enhance; + + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import java.io.Serializable; + +/** + * @see TestClassHierarchyEnhancement + */ +@Entity +public class EnhancedSuperClass implements Serializable { + private static final long serialVersionUID = -4276267285828916940L; + + @Id + @GeneratedValue + private Long id; + + private String valueInSuperclass; + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getValueInSuperclass() { + return valueInSuperclass; + } + + public void setValueInSuperclass(String valueInSuperclass) { + this.valueInSuperclass = valueInSuperclass; + } +} + diff --git a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/enhance/persistence1.xml b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/enhance/persistence1.xml new file mode 100644 index 000000000..db1e92ae8 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/enhance/persistence1.xml @@ -0,0 +1,37 @@ + + + + + + org.apache.openjpa.enhance.EnhancedSuperClass + org.apache.openjpa.enhance.EnhancedSubClass + + + + + + + + diff --git a/pom.xml b/pom.xml index 9e54c4657..5064025c9 100644 --- a/pom.xml +++ b/pom.xml @@ -839,7 +839,7 @@ org.codehaus.mojo openjpa-maven-plugin - 1.1 + 1.2 org.apache.maven.plugins