Author: aadamchik
Date: Thu May 4 14:57:06 2006
New Revision: 399868
URL: http://svn.apache.org/viewcvs?rev=399868&view=rev
Log:
refactoring property reflection code
Added:
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/enhancer/DataObjectAccessorInjector.java
Modified:
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaClassDescriptor.java
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaPropertyDescriptor.java
incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/map/JpaClassDescriptorTest.java
Added:
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/enhancer/DataObjectAccessorInjector.java
URL:
http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/enhancer/DataObjectAccessorInjector.java?rev=399868&view=auto
==============================================================================
---
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/enhancer/DataObjectAccessorInjector.java
(added)
+++
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/enhancer/DataObjectAccessorInjector.java
Thu May 4 14:57:06 2006
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2006 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 org.apache.cayenne.jpa.enhancer;
+
+import net.sf.cglib.asm.Attribute;
+import net.sf.cglib.asm.Type;
+import net.sf.cglib.core.CodeEmitter;
+import net.sf.cglib.core.Signature;
+import net.sf.cglib.transform.ClassEmitterTransformer;
+
+/**
+ * Injects persistence code to the property accessors.
+ *
+ * @author Andrus Adamchik
+ */
+public class DataObjectAccessorInjector extends ClassEmitterTransformer {
+
+ @Override
+ public CodeEmitter begin_method(
+ int access,
+ Signature sig,
+ Type[] exceptions,
+ Attribute attrs) {
+ return super.begin_method(access, sig, exceptions, attrs);
+ }
+}
Modified:
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaClassDescriptor.java
URL:
http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaClassDescriptor.java?rev=399868&r1=399867&r2=399868&view=diff
==============================================================================
---
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaClassDescriptor.java
(original)
+++
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaClassDescriptor.java
Thu May 4 14:57:06 2006
@@ -26,7 +26,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-
/**
* Provides the JPA information about a class, such potential persistence
fields, etc.
*
@@ -34,14 +33,34 @@
*/
public class JpaClassDescriptor {
- static final Pattern GETTER_PATTERN =
Pattern.compile("^(is|get)([A-Z].*)$");
- static final Pattern SETTER_PATTERN = Pattern.compile("^set([A-Z].*)$");
+ private static final Pattern GETTER_PATTERN = Pattern
+ .compile("^(is|get)([A-Z])(.*)$");
+ private static final Pattern SETTER_PATTERN =
Pattern.compile("^set([A-Z])(.*)$");
protected Collection<JpaPropertyDescriptor> fieldDescriptors;
protected Collection<JpaPropertyDescriptor> propertyDescriptors;
protected Class managedClass;
protected JpaEntityMap entityMap;
+ public static String propertyNameForGetter(String getterName) {
+ Matcher getMatch = GETTER_PATTERN.matcher(getterName);
+ if (getMatch.matches()) {
+ return getMatch.group(2).toLowerCase() + getMatch.group(3);
+ }
+
+ return null;
+ }
+
+ public static String propertyNameForSetter(String setterName) {
+ Matcher setMatch = SETTER_PATTERN.matcher(setterName);
+
+ if (setMatch.matches()) {
+ return setMatch.group(1).toLowerCase() + setMatch.group(2);
+ }
+
+ return null;
+ }
+
public JpaClassDescriptor(JpaEntityMap entityMap, Class managedClass) {
this.managedClass = managedClass;
this.entityMap = entityMap;
@@ -175,10 +194,9 @@
boolean isVoid = Void.TYPE.equals(returnType);
if (!isVoid && parameters.length == 0) {
- Matcher getMatch = GETTER_PATTERN.matcher(name);
- if (getMatch.matches()) {
+ String propertyName = propertyNameForGetter(name);
- String propertyName = getMatch.group(2);
+ if (propertyName != null) {
String key = propertyName + ":" + returnType.getName();
PropertyTuple t = properties.get(key);
if (t == null) {
@@ -193,10 +211,11 @@
}
if (isVoid && parameters.length == 1) {
- Matcher setMatch = SETTER_PATTERN.matcher(name);
- if (setMatch.matches()) {
+ String propertyName = propertyNameForSetter(name);
+
+ if (propertyName != null) {
- String key = setMatch.group(1) + ":" +
parameters[0].getName();
+ String key = propertyName + ":" + parameters[0].getName();
PropertyTuple t = properties.get(key);
if (t == null) {
Modified:
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaPropertyDescriptor.java
URL:
http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaPropertyDescriptor.java?rev=399868&r1=399867&r2=399868&view=diff
==============================================================================
---
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaPropertyDescriptor.java
(original)
+++
incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaPropertyDescriptor.java
Thu May 4 14:57:06 2006
@@ -23,7 +23,6 @@
import java.lang.reflect.Type;
import java.sql.Types;
import java.util.Collection;
-import java.util.regex.Matcher;
import javax.persistence.TemporalType;
@@ -51,10 +50,8 @@
}
public JpaPropertyDescriptor(Method getter, String name) {
- Matcher getterMatcher =
JpaClassDescriptor.GETTER_PATTERN.matcher(getter
- .getName());
- if (!getterMatcher.matches()) {
+ if (JpaClassDescriptor.propertyNameForGetter(getter.getName()) ==
null) {
throw new JpaProviderException("Invalid property getter name: "
+ getter.getName());
}
@@ -109,7 +106,7 @@
public int getJdbcType(TemporalType temporalHint) {
// per JPA spec only date types are mapped explicitly... everything
else is guess
// per JDBC default...
-
+
if (temporalHint != null) {
if (TemporalType.TIMESTAMP == temporalHint) {
Modified:
incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/map/JpaClassDescriptorTest.java
URL:
http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/map/JpaClassDescriptorTest.java?rev=399868&r1=399867&r2=399868&view=diff
==============================================================================
---
incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/map/JpaClassDescriptorTest.java
(original)
+++
incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/map/JpaClassDescriptorTest.java
Thu May 4 14:57:06 2006
@@ -15,40 +15,23 @@
*/
package org.apache.cayenne.jpa.map;
-import java.util.regex.Matcher;
+import junit.framework.TestCase;
import org.apache.cayenne.jpa.conf.MockPropertyRegressionBean;
-import org.apache.cayenne.jpa.map.AccessType;
-import org.apache.cayenne.jpa.map.JpaClassDescriptor;
-import org.apache.cayenne.jpa.map.JpaEntityMap;
-
-import junit.framework.TestCase;
public class JpaClassDescriptorTest extends TestCase {
- public void testGetterPattern() {
- Matcher m1 = JpaClassDescriptor.GETTER_PATTERN.matcher("xxx");
- assertFalse(m1.matches());
+ public void testPropertyNameForGetter() {
- Matcher m2 = JpaClassDescriptor.GETTER_PATTERN.matcher("isA");
- assertTrue(m2.matches());
- assertEquals("A", m2.group(2));
-
- Matcher m3 = JpaClassDescriptor.GETTER_PATTERN.matcher("getThis");
- assertTrue(m3.matches());
- assertEquals("This", m3.group(2));
+ assertNull(JpaClassDescriptor.propertyNameForGetter("xxx"));
+ assertEquals("a", JpaClassDescriptor.propertyNameForGetter("isA"));
+ assertEquals("this",
JpaClassDescriptor.propertyNameForGetter("getThis"));
}
- public void testSetterPattern() {
- Matcher m1 = JpaClassDescriptor.SETTER_PATTERN.matcher("xxx");
- assertFalse(m1.matches());
-
- Matcher m2 = JpaClassDescriptor.SETTER_PATTERN.matcher("set");
- assertFalse(m2.matches());
-
- Matcher m3 = JpaClassDescriptor.SETTER_PATTERN.matcher("setThis");
- assertTrue(m3.matches());
- assertEquals("This", m3.group(1));
+ public void testPropertyNameForSetter() {
+ assertNull(JpaClassDescriptor.propertyNameForSetter("xxx"));
+ assertNull(JpaClassDescriptor.propertyNameForSetter("set"));
+ assertEquals("this",
JpaClassDescriptor.propertyNameForSetter("setThis"));
}
public void testGetMemberDescriptors() throws Exception {