You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2006/10/10 06:19:42 UTC
svn commit: r454607 - in /incubator/cayenne/sandbox/asm-enhancer/src:
main/java/org/apache/cayenne/enhancer/ test/java/org/apache/cayenne/enhancer/
Author: aadamchik
Date: Mon Oct 9 21:19:35 2006
New Revision: 454607
URL: http://svn.apache.org/viewvc?view=rev&rev=454607
Log:
adding Apache headers and class docs
Modified:
incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/CayenneEnhancer.java
incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/ClassVisitorHelper.java
incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/EnhancerUtil.java
incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/PersistentClassVisitor.java
incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/PersistentGetterVisitor.java
incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/PersistentSetterVisitor.java
incubator/cayenne/sandbox/asm-enhancer/src/test/java/org/apache/cayenne/enhancer/CayenneEnhancerTest.java
incubator/cayenne/sandbox/asm-enhancer/src/test/java/org/apache/cayenne/enhancer/MockPojo1.java
Modified: incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/CayenneEnhancer.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/CayenneEnhancer.java?view=diff&rev=454607&r1=454606&r2=454607
==============================================================================
--- incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/CayenneEnhancer.java (original)
+++ incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/CayenneEnhancer.java Mon Oct 9 21:19:35 2006
@@ -1,3 +1,21 @@
+/*****************************************************************
+ * 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.cayenne.enhancer;
import java.lang.instrument.ClassFileTransformer;
@@ -6,46 +24,56 @@
import java.util.Collection;
import java.util.Map;
+import org.apache.cayenne.Persistent;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
+/**
+ * A ClassFileTransformer that enhances a POJO into a persistent object that can be used
+ * with Cayenne. More specifically, it ensures that the object implements
+ * {@link Persistent} interface and invokes callbacks from the accessor methods.
+ *
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
public class CayenneEnhancer implements ClassFileTransformer {
- protected Map<String, Collection<String>> persistentPropertiesByClass;
+ protected Map<String, Collection<String>> persistentPropertiesByClass;
- public CayenneEnhancer(
- Map<String, Collection<String>> persistentPropertiesByClass) {
- this.persistentPropertiesByClass = persistentPropertiesByClass;
- }
-
- public byte[] transform(ClassLoader loader, String className,
- Class<?> classBeingRedefined, ProtectionDomain protectionDomain,
- byte[] classfileBuffer) throws IllegalClassFormatException {
-
- ClassReader reader = new ClassReader(classfileBuffer);
- ClassWriter writer = new ClassWriter(reader, true);
-
- ClassVisitor visitor = createVisitor(className, writer);
- if (visitor == null) {
- // per JPA spec if no transformation occured, we must return null
- return null;
- }
-
- reader.accept(visitor, true);
- return writer.toByteArray();
- }
-
- /**
- * Builds a chain of ASM visitors.
- */
- protected ClassVisitor createVisitor(String className, ClassWriter writer) {
- Collection<String> properties = persistentPropertiesByClass
- .get(className);
- if (properties == null || properties.isEmpty()) {
- return null;
- }
+ public CayenneEnhancer(Map<String, Collection<String>> persistentPropertiesByClass) {
+ this.persistentPropertiesByClass = persistentPropertiesByClass;
+ }
+
+ public byte[] transform(
+ ClassLoader loader,
+ String className,
+ Class<?> classBeingRedefined,
+ ProtectionDomain protectionDomain,
+ byte[] classfileBuffer) throws IllegalClassFormatException {
+
+ ClassReader reader = new ClassReader(classfileBuffer);
+ ClassWriter writer = new ClassWriter(reader, true);
+
+ ClassVisitor visitor = createVisitor(className, writer);
+ if (visitor == null) {
+ // per JPA spec if no transformation occured, we must return null
+ return null;
+ }
+
+ reader.accept(visitor, true);
+ return writer.toByteArray();
+ }
+
+ /**
+ * Builds a chain of ASM visitors.
+ */
+ protected ClassVisitor createVisitor(String className, ClassWriter writer) {
+ Collection<String> properties = persistentPropertiesByClass.get(className);
+ if (properties == null || properties.isEmpty()) {
+ return null;
+ }
- return new PersistentClassVisitor(writer, properties);
- }
+ return new PersistentClassVisitor(writer, properties);
+ }
}
Modified: incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/ClassVisitorHelper.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/ClassVisitorHelper.java?view=diff&rev=454607&r1=454606&r2=454607
==============================================================================
--- incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/ClassVisitorHelper.java (original)
+++ incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/ClassVisitorHelper.java Mon Oct 9 21:19:35 2006
@@ -29,6 +29,7 @@
* A helper for the ASM ClassVisitor that encapsulates common class enhancement
* operations.
*
+ * @since 3.0
* @author Andrus Adamchik
*/
class ClassVisitorHelper {
Modified: incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/EnhancerUtil.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/EnhancerUtil.java?view=diff&rev=454607&r1=454606&r2=454607
==============================================================================
--- incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/EnhancerUtil.java (original)
+++ incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/EnhancerUtil.java Mon Oct 9 21:19:35 2006
@@ -1,32 +1,56 @@
+/*****************************************************************
+ * 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.cayenne.enhancer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+/**
+ * A helper for converting method names to property names.
+ *
+ * @author Andrus Adamchik
+ * @since 3.0
+ */
// duplicated from JpaClassDescriptor.
class EnhancerUtil {
- private static final Pattern GETTER_PATTERN = Pattern
- .compile("^(is|get)([A-Z])(.*)$");
- private static final Pattern SETTER_PATTERN = Pattern
- .compile("^set([A-Z])(.*)$");
+ private static final Pattern GETTER_PATTERN = Pattern
+ .compile("^(is|get)([A-Z])(.*)$");
- static String propertyNameForGetter(String getterName) {
- Matcher getMatch = GETTER_PATTERN.matcher(getterName);
- if (getMatch.matches()) {
- return getMatch.group(2).toLowerCase() + getMatch.group(3);
- }
-
- return null;
- }
-
- static String propertyNameForSetter(String setterName) {
- Matcher setMatch = SETTER_PATTERN.matcher(setterName);
-
- if (setMatch.matches()) {
- return setMatch.group(1).toLowerCase() + setMatch.group(2);
- }
+ private static final Pattern SETTER_PATTERN = Pattern.compile("^set([A-Z])(.*)$");
- return null;
- }
+ static String propertyNameForGetter(String getterName) {
+ Matcher getMatch = GETTER_PATTERN.matcher(getterName);
+ if (getMatch.matches()) {
+ return getMatch.group(2).toLowerCase() + getMatch.group(3);
+ }
+
+ return null;
+ }
+
+ static String propertyNameForSetter(String setterName) {
+ Matcher setMatch = SETTER_PATTERN.matcher(setterName);
+
+ if (setMatch.matches()) {
+ return setMatch.group(1).toLowerCase() + setMatch.group(2);
+ }
+
+ return null;
+ }
}
Modified: incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/PersistentClassVisitor.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/PersistentClassVisitor.java?view=diff&rev=454607&r1=454606&r2=454607
==============================================================================
--- incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/PersistentClassVisitor.java (original)
+++ incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/PersistentClassVisitor.java Mon Oct 9 21:19:35 2006
@@ -31,6 +31,7 @@
* ASM-based visitor that turns a pojo class into enahnced persistent object.
*
* @author Andrus Adamchik
+ * @since 3.0
*/
class PersistentClassVisitor extends ClassAdapter {
Modified: incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/PersistentGetterVisitor.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/PersistentGetterVisitor.java?view=diff&rev=454607&r1=454606&r2=454607
==============================================================================
--- incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/PersistentGetterVisitor.java (original)
+++ incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/PersistentGetterVisitor.java Mon Oct 9 21:19:35 2006
@@ -25,6 +25,10 @@
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
+/**
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
class PersistentGetterVisitor extends MethodAdapter {
private ClassVisitorHelper helper;
Modified: incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/PersistentSetterVisitor.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/PersistentSetterVisitor.java?view=diff&rev=454607&r1=454606&r2=454607
==============================================================================
--- incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/PersistentSetterVisitor.java (original)
+++ incubator/cayenne/sandbox/asm-enhancer/src/main/java/org/apache/cayenne/enhancer/PersistentSetterVisitor.java Mon Oct 9 21:19:35 2006
@@ -25,6 +25,10 @@
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
+/**
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
class PersistentSetterVisitor extends MethodAdapter {
private ClassVisitorHelper helper;
Modified: incubator/cayenne/sandbox/asm-enhancer/src/test/java/org/apache/cayenne/enhancer/CayenneEnhancerTest.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/asm-enhancer/src/test/java/org/apache/cayenne/enhancer/CayenneEnhancerTest.java?view=diff&rev=454607&r1=454606&r2=454607
==============================================================================
--- incubator/cayenne/sandbox/asm-enhancer/src/test/java/org/apache/cayenne/enhancer/CayenneEnhancerTest.java (original)
+++ incubator/cayenne/sandbox/asm-enhancer/src/test/java/org/apache/cayenne/enhancer/CayenneEnhancerTest.java Mon Oct 9 21:19:35 2006
@@ -1,3 +1,21 @@
+/*****************************************************************
+ * 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.cayenne.enhancer;
import java.lang.reflect.Field;
@@ -205,8 +223,8 @@
});
setAttribute1.invoke(o, new Object[] {
- "y"
- });
+ "y"
+ });
assertEquals("y", getAttribute1.invoke(o, (Object[]) null));
assertSame(o, change[0]);
assertEquals("attribute1", change[1]);
Modified: incubator/cayenne/sandbox/asm-enhancer/src/test/java/org/apache/cayenne/enhancer/MockPojo1.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/asm-enhancer/src/test/java/org/apache/cayenne/enhancer/MockPojo1.java?view=diff&rev=454607&r1=454606&r2=454607
==============================================================================
--- incubator/cayenne/sandbox/asm-enhancer/src/test/java/org/apache/cayenne/enhancer/MockPojo1.java (original)
+++ incubator/cayenne/sandbox/asm-enhancer/src/test/java/org/apache/cayenne/enhancer/MockPojo1.java Mon Oct 9 21:19:35 2006
@@ -1,13 +1,32 @@
+/*****************************************************************
+ * 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.cayenne.enhancer;
public class MockPojo1 {
- protected String attribute1;
- public String getAttribute1() {
- return attribute1;
- }
+ protected String attribute1;
- public void setAttribute1(String attribute1) {
- this.attribute1 = attribute1;
- }
+ public String getAttribute1() {
+ return attribute1;
+ }
+
+ public void setAttribute1(String attribute1) {
+ this.attribute1 = attribute1;
+ }
}