| 1 | /* |
| 2 | * Copyright (C) 2012-2015 DataStax Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.datastax.driver.mapping; |
| 17 | |
| 18 | import com.datastax.driver.core.CCMTestsSupport; |
| 19 | import com.datastax.driver.mapping.annotations.PartitionKey; |
| 20 | import com.datastax.driver.mapping.annotations.Table; |
| 21 | import com.google.common.io.Closeables; |
| 22 | import org.objectweb.asm.*; |
| 23 | import org.testng.annotations.Test; |
| 24 | |
| 25 | import java.io.IOException; |
| 26 | import java.io.InputStream; |
| 27 | import java.lang.reflect.Constructor; |
| 28 | |
| 29 | import static org.testng.Assert.assertEquals; |
| 30 | |
| 31 | @SuppressWarnings("unused") |
| 32 | public class SyntheticFieldsMapperTest extends CCMTestsSupport { |
| 33 | |
| 34 | @Override |
| 35 | public void onTestContextInitialized() { |
| 36 | execute("CREATE TABLE synthetic_fields (id int PRIMARY KEY)"); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Test that synthetic fields are ignored by the {@code AnnotationParser} (JAVA-465). |
| 41 | * <p/> |
| 42 | * This test creates a modified version of this class where {@code futureSynthetic} is marked as synthetic, and |
| 43 | * therefore ignored by the {@code AnnotationParser}. If the test throws an error "Cannot find matching getter and |
| 44 | * setter for field 'futureSynthetic'", it means that the field is not ignored by the mapped. However, |
| 45 | * if it succeed, we know that the field was ignored and that the fix does the right job. |
| 46 | * <p/> |
| 47 | * If the class {@code ClassWithSyntheticField} was used as-is, the {@code Mapper} would complain that there are |
| 48 | * no getter and setter for the field {@code futureSynthetic}. |
| 49 | * <p/> |
| 50 | * Note that we could also have used not-static inner classes, but the {@code Mapper} is never able to instantiate |
| 51 | * those as they require a reference to their enclosing class that is not provided at instantiation time. |
| 52 | */ |
| 53 | @Test(groups = "short") |
| 54 | public void should_ignore_synthetic_fields() { |
| 55 | Class<?> classWithSyntheticFields = makeTestClassWithSyntheticFields(); |
| 56 | Object instance = instantiateNewClass(classWithSyntheticFields, 42); |
| 57 | |
| 58 | // Here we cannot use ClassWithSyntheticField since it is not the one the source file was compiled against |
| 59 | // Instead, it is a different Class (identity + ClassLoader), a modified version of ClassWithSyntheticField |
| 60 | // Nice ClassCastException will be thrown if we try to cast it to ClassWithSyntheticField |
| 61 | @SuppressWarnings("unchecked") |
| 62 | Mapper<Object> m = (Mapper<Object>) new MappingManager(session()).mapper(classWithSyntheticFields); |
| 63 | m.save(instance); |
| 64 | |
| 65 | assertEquals(m.get(42), instance); |
| 66 | } |
| 67 | |
| 68 | private Class<?> makeTestClassWithSyntheticFields() { |
| 69 | InputStream stream = null; |
| 70 | try { |
| 71 | // Get class bytes |
| 72 | Class<ClassWithSyntheticField> c = ClassWithSyntheticField.class; |
| 73 | String classAsPath = c.getName().replace('.', '/') + ".class"; |
| 74 | stream = c.getClassLoader().getResourceAsStream(classAsPath); |
| 75 | |
| 76 | // Make "futureSynthetic" field actually synthetic |
| 77 | ClassWriter cw = new ClassWriter(0); |
| 78 | ClassVisitor cv = new SyntheticFieldCreator(Opcodes.ASM5, cw); |
| 79 | ClassReader cr = new ClassReader(stream); |
| 80 | cr.accept(cv, 0); |
| 81 | byte[] updatedClassBytes = cw.toByteArray(); |
| 82 | |
| 83 | // Build the new class |
| 84 | return new InterceptingClassLoader().defineClass( |
| 85 | ClassWithSyntheticField.class.getName(), |
| 86 | updatedClassBytes); |
| 87 | } catch (IOException e) { |
| 88 | throw new RuntimeException("Could not read Class bytes", e); |
| 89 | } finally { |
| 90 | try { |
| 91 | Closeables.close(stream, true); |
| 92 | } catch (IOException ignored) { |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private Object instantiateNewClass(Class<?> classWithSyntheticFields, int id) { |
| 98 | try { |
| 99 | Constructor<?> declaredConstructor = classWithSyntheticFields.getDeclaredConstructor(int.class); |
| 100 | return declaredConstructor.newInstance(id); |
| 101 | } catch (Exception e) { |
| 102 | throw new RuntimeException("Could not instantiate Class", e); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | @Table(name = "synthetic_fields") |
| 107 | public static class ClassWithSyntheticField { |
| 108 | @PartitionKey |
| 109 | private int id; |
| 110 | |
| 111 | // Intentionally broken class: there is no getter/setter for this field |
| 112 | private int futureSynthetic; |
| 113 | |
| 114 | // This constructor will be used by the Mapper |
| 115 | @SuppressWarnings("unused") |
| 116 | public ClassWithSyntheticField() { |
| 117 | } |
| 118 | |
| 119 | // This constructor is invoked using reflection |
| 120 | @SuppressWarnings("unused") |
| 121 | public ClassWithSyntheticField(int id) { |
| 122 | this.id = id; |
| 123 | } |
| 124 | |
| 125 | // Getters & Setters used by the Mapper |
| 126 | @SuppressWarnings("unused") |
| 127 | public int getId() { |
| 128 | return id; |
| 129 | } |
| 130 | |
| 131 | // Getters & Setters used by the Mapper |
| 132 | @SuppressWarnings("unused") |
| 133 | public void setId(int id) { |
| 134 | this.id = id; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public boolean equals(Object o) { |
| 139 | if (this == o) return true; |
| 140 | if (o == null || getClass() != o.getClass()) return false; |
| 141 | |
| 142 | ClassWithSyntheticField that = (ClassWithSyntheticField) o; |
| 143 | |
| 144 | return id == that.id; |
| 145 | |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public int hashCode() { |
| 150 | return id; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | private static class SyntheticFieldCreator extends ClassVisitor { |
| 155 | public SyntheticFieldCreator(int api, ClassVisitor cv) { |
| 156 | super(api, cv); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { |
| 161 | int newAccesses = access; |
| 162 | if ("futureSynthetic".equals(name)) { |
| 163 | newAccesses = access + Opcodes.ACC_SYNTHETIC; |
| 164 | } |
| 165 | return super.visitField(newAccesses, name, desc, signature, value); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | private static class InterceptingClassLoader extends ClassLoader { |
| 170 | public Class defineClass(String name, byte[] b) { |
| 171 | return defineClass(name, b, 0, b.length); |
| 172 | } |
| 173 | } |
| 174 | } |