kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | ca32d97e3bd329acd55f57b6bc7157dd2ebb5b0e9ae374b331328c836ba7d7e7 |
|---|---|
| Author | Alexandre Dutra <adutra@users.noreply.github.com> · 2016-10-04 18:39 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-10-04 09:39 -07:00 |
| Parents | bcd09728a1db |
4 files changed
modified changelog/README.md +2 −1
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | ## Changelog |
| 2 | 2 | |
| 3 | -### 3.1.1 | |
| 3 | +### 3.1.1 (in progress) | |
| 4 | 4 | |
| 5 | 5 | - [bug] JAVA-1284: ClockFactory should check system property before attempting to load Native class. |
| 6 | 6 | - [bug] JAVA-1255: Allow nested UDTs to be used in Mapper. |
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | - [improvement] JAVA-1053: Add a metric for authentication errors |
| 23 | 23 | - [improvement] JAVA-1263: Eliminate unnecessary memory copies in FrameCompressor implementations. |
| 24 | 24 | - [improvement] JAVA-893: Make connection pool non-blocking |
| 25 | +- [bug] JAVA-1279: Mapper should exclude Groovy's "metaClass" property when looking for mapped properties | |
| 25 | 26 | |
| 26 | 27 | |
| 27 | 28 | ### 3.1.0 |
modified driver-mapping/pom.xml +57 −0
| @@ -30,6 +30,7 @@ | ||
| 30 | 30 | |
| 31 | 31 | <properties> |
| 32 | 32 | <main.basedir>${project.parent.basedir}</main.basedir> |
| 33 | + <groovy.version>2.4.7</groovy.version> | |
| 33 | 34 | </properties> |
| 34 | 35 | |
| 35 | 36 | <dependencies> |
| @@ -108,10 +109,36 @@ | ||
| 108 | 109 | <scope>test</scope> |
| 109 | 110 | </dependency> |
| 110 | 111 | |
| 112 | + <dependency> | |
| 113 | + <groupId>org.codehaus.groovy</groupId> | |
| 114 | + <artifactId>groovy-all</artifactId> | |
| 115 | + <version>${groovy.version}</version> | |
| 116 | + <scope>test</scope> | |
| 117 | + </dependency> | |
| 118 | + | |
| 111 | 119 | </dependencies> |
| 112 | 120 | |
| 113 | 121 | <build> |
| 114 | 122 | <plugins> |
| 123 | + <plugin> | |
| 124 | + <groupId>org.codehaus.mojo</groupId> | |
| 125 | + <artifactId>build-helper-maven-plugin</artifactId> | |
| 126 | + <version>1.12</version> | |
| 127 | + <executions> | |
| 128 | + <execution> | |
| 129 | + <id>add-source</id> | |
| 130 | + <phase>generate-test-sources</phase> | |
| 131 | + <goals> | |
| 132 | + <goal>add-test-source</goal> | |
| 133 | + </goals> | |
| 134 | + <configuration> | |
| 135 | + <sources> | |
| 136 | + <source>${pom.basedir}/src/test/groovy</source> | |
| 137 | + </sources> | |
| 138 | + </configuration> | |
| 139 | + </execution> | |
| 140 | + </executions> | |
| 141 | + </plugin> | |
| 115 | 142 | <plugin> |
| 116 | 143 | <groupId>org.apache.felix</groupId> |
| 117 | 144 | <artifactId>maven-bundle-plugin</artifactId> |
| @@ -140,6 +167,36 @@ | ||
| 140 | 167 | </supportedProjectTypes> |
| 141 | 168 | </configuration> |
| 142 | 169 | </plugin> |
| 170 | + <plugin> | |
| 171 | + <groupId>org.codehaus.gmaven</groupId> | |
| 172 | + <artifactId>gmaven-plugin</artifactId> | |
| 173 | + <version>1.5</version> | |
| 174 | + <extensions>true</extensions> | |
| 175 | + <executions> | |
| 176 | + <execution> | |
| 177 | + <goals> | |
| 178 | + <goal>testCompile</goal> | |
| 179 | + </goals> | |
| 180 | + <configuration> | |
| 181 | + <sources> | |
| 182 | + <fileset> | |
| 183 | + <directory>${pom.basedir}/src/test/groovy</directory> | |
| 184 | + <includes> | |
| 185 | + <include>**/*.groovy</include> | |
| 186 | + </includes> | |
| 187 | + </fileset> | |
| 188 | + </sources> | |
| 189 | + </configuration> | |
| 190 | + </execution> | |
| 191 | + </executions> | |
| 192 | + <dependencies> | |
| 193 | + <dependency> | |
| 194 | + <groupId>org.codehaus.groovy</groupId> | |
| 195 | + <artifactId>groovy-all</artifactId> | |
| 196 | + <version>${groovy.version}</version> | |
| 197 | + </dependency> | |
| 198 | + </dependencies> | |
| 199 | + </plugin> | |
| 143 | 200 | </plugins> |
| 144 | 201 | </build> |
| 145 | 202 |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/ReflectionUtils.java +14 −2
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | package com.datastax.driver.mapping; |
| 17 | 17 | |
| 18 | 18 | import com.google.common.base.Throwables; |
| 19 | +import com.google.common.collect.ImmutableSet; | |
| 19 | 20 | |
| 20 | 21 | import java.beans.BeanInfo; |
| 21 | 22 | import java.beans.IntrospectionException; |
| @@ -25,12 +26,19 @@ | ||
| 25 | 26 | import java.lang.reflect.*; |
| 26 | 27 | import java.util.HashMap; |
| 27 | 28 | import java.util.Map; |
| 29 | +import java.util.Set; | |
| 28 | 30 | |
| 29 | 31 | /** |
| 30 | 32 | * Utility methods related to reflection. |
| 31 | 33 | */ |
| 32 | 34 | class ReflectionUtils { |
| 33 | 35 | |
| 36 | + private static final Set<String> EXCLUDED_PROPERTIES = ImmutableSet.of( | |
| 37 | + "class", | |
| 38 | + // JAVA-1279: exclude Groovy's metaClass property | |
| 39 | + "metaClass" | |
| 40 | + ); | |
| 41 | + | |
| 34 | 42 | static <T> T newInstance(Class<T> clazz) { |
| 35 | 43 | Constructor<T> publicConstructor; |
| 36 | 44 | try { |
| @@ -75,7 +83,7 @@ | ||
| 75 | 83 | HashMap<String, Field> fields = new HashMap<String, Field>(); |
| 76 | 84 | for (Class<?> clazz = baseClass; !clazz.equals(Object.class); clazz = clazz.getSuperclass()) { |
| 77 | 85 | for (Field field : clazz.getDeclaredFields()) { |
| 78 | - if (field.getName().equals("class") || field.isSynthetic() || Modifier.isStatic(field.getModifiers())) | |
| 86 | + if (field.isSynthetic() || Modifier.isStatic(field.getModifiers()) || isPropertyExcluded(field.getName())) | |
| 79 | 87 | continue; |
| 80 | 88 | // never override a more specific field masking another one declared in a superclass |
| 81 | 89 | if (!fields.containsKey(field.getName())) |
| @@ -94,13 +102,17 @@ | ||
| 94 | 102 | } |
| 95 | 103 | Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>(); |
| 96 | 104 | for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) { |
| 97 | - if (property.getName().equals("class")) | |
| 105 | + if (isPropertyExcluded(property.getName())) | |
| 98 | 106 | continue; |
| 99 | 107 | properties.put(property.getName(), property); |
| 100 | 108 | } |
| 101 | 109 | return properties; |
| 102 | 110 | } |
| 103 | 111 | |
| 112 | + private static boolean isPropertyExcluded(String name) { | |
| 113 | + return EXCLUDED_PROPERTIES.contains(name); | |
| 114 | + } | |
| 115 | + | |
| 104 | 116 | static Map<Class<? extends Annotation>, Annotation> scanPropertyAnnotations(Field field, PropertyDescriptor property) { |
| 105 | 117 | Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>(); |
| 106 | 118 | // annotations on getters should have precedence over annotations on fields |
added driver-mapping/src/test/groovy/com/datastax/driver/mapping/MapperGroovyTest.groovy +47 −0
| @@ -0,0 +1,47 @@ | ||
| 1 | +package com.datastax.driver.mapping | |
| 2 | + | |
| 3 | +import com.datastax.driver.core.CCMTestsSupport | |
| 4 | +import com.datastax.driver.mapping.annotations.Column | |
| 5 | +import com.datastax.driver.mapping.annotations.PartitionKey | |
| 6 | +import com.datastax.driver.mapping.annotations.Table | |
| 7 | +import org.testng.annotations.Test | |
| 8 | + | |
| 9 | +import static org.assertj.core.api.Assertions.assertThat | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * A simple test to check that mapping a Groovy class works as expected. | |
| 13 | + * | |
| 14 | + * @jira_ticket JAVA-1279 | |
| 15 | + */ | |
| 16 | +public class MapperGroovyTest extends CCMTestsSupport { | |
| 17 | + | |
| 18 | + @Table(name = "users") | |
| 19 | + static class User { | |
| 20 | + | |
| 21 | + @PartitionKey | |
| 22 | + @Column(name = "user_id") | |
| 23 | + def UUID userId | |
| 24 | + | |
| 25 | + def String name | |
| 26 | + | |
| 27 | + } | |
| 28 | + | |
| 29 | + @Override | |
| 30 | + public void onTestContextInitialized() { | |
| 31 | + execute("CREATE TABLE users (user_id uuid PRIMARY KEY, name text)") | |
| 32 | + } | |
| 33 | + | |
| 34 | + @Test(groups = "short") | |
| 35 | + public void should_map_groovy_class() { | |
| 36 | + def mapper = new MappingManager(session()).mapper(User) | |
| 37 | + def user1 = new User() | |
| 38 | + user1.userId = UUID.randomUUID() | |
| 39 | + user1.name = "John Doe" | |
| 40 | + mapper.save user1 | |
| 41 | + def user2 = mapper.get user1.userId | |
| 42 | + assertThat(user2.userId).isEqualTo(user1.userId) | |
| 43 | + assertThat(user2.name).isEqualTo(user1.name) | |
| 44 | + } | |
| 45 | + | |
| 46 | +} | |
| 47 | + |