1 /* 2 * Copyright 2003 The Apache Software Foundation 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 org.mockito.cglib.beans; 17 18 import java.lang.reflect.Constructor; 19 import java.lang.reflect.Method; 20 import java.lang.reflect.Modifier; 21 import java.util.*; 22 23 import org.mockito.asm.ClassVisitor; 24 import org.mockito.cglib.core.*; 25 26 /** 27 * @author Juozas Baliuka 28 */ 29 abstract public class BulkBean 30 { 31 private static final BulkBeanKey KEY_FACTORY = 32 (BulkBeanKey)KeyFactory.create(BulkBeanKey.class); 33 34 interface BulkBeanKey { 35 public Object newInstance(String target, String[] getters, String[] setters, String[] types); 36 } 37 38 protected Class target; 39 protected String[] getters, setters; 40 protected Class[] types; 41 42 protected BulkBean() { } 43 44 abstract public void getPropertyValues(Object bean, Object[] values); 45 abstract public void setPropertyValues(Object bean, Object[] values); 46 47 public Object[] getPropertyValues(Object bean) { 48 Object[] values = new Object[getters.length]; 49 getPropertyValues(bean, values); 50 return values; 51 } 52 53 public Class[] getPropertyTypes() { 54 return (Class[])types.clone(); 55 } 56 57 public String[] getGetters() { 58 return (String[])getters.clone(); 59 } 60 61 public String[] getSetters() { 62 return (String[])setters.clone(); 63 } 64 65 public static BulkBean create(Class target, String[] getters, String[] setters, Class[] types) { 66 Generator gen = new Generator(); 67 gen.setTarget(target); 68 gen.setGetters(getters); 69 gen.setSetters(setters); 70 gen.setTypes(types); 71 return gen.create(); 72 } 73 74 public static class Generator extends AbstractClassGenerator { 75 private static final Source SOURCE = new Source(BulkBean.class.getName()); 76 private Class target; 77 private String[] getters; 78 private String[] setters; 79 private Class[] types; 80 81 public Generator() { 82 super(SOURCE); 83 } 84 85 public void setTarget(Class target) { 86 this.target = target; 87 } 88 89 public void setGetters(String[] getters) { 90 this.getters = getters; 91 } 92 93 public void setSetters(String[] setters) { 94 this.setters = setters; 95 } 96 97 public void setTypes(Class[] types) { 98 this.types = types; 99 } 100 101 protected ClassLoader getDefaultClassLoader() { 102 return target.getClassLoader(); 103 } 104 105 public BulkBean create() { 106 setNamePrefix(target.getName()); 107 String targetClassName = target.getName(); 108 String[] typeClassNames = ReflectUtils.getNames(types); 109 Object key = KEY_FACTORY.newInstance(targetClassName, getters, setters, typeClassNames); 110 return (BulkBean)super.create(key); 111 } 112 113 public void generateClass(ClassVisitor v) throws Exception { 114 new BulkBeanEmitter(v, getClassName(), target, getters, setters, types); 115 } 116 117 protected Object firstInstance(Class type) { 118 BulkBean instance = (BulkBean)ReflectUtils.newInstance(type); 119 instance.target = target; 120 121 int length = getters.length; 122 instance.getters = new String[length]; 123 System.arraycopy(getters, 0, instance.getters, 0, length); 124 125 instance.setters = new String[length]; 126 System.arraycopy(setters, 0, instance.setters, 0, length); 127 128 instance.types = new Class[types.length]; 129 System.arraycopy(types, 0, instance.types, 0, types.length); 130 131 return instance; 132 } 133 134 protected Object nextInstance(Object instance) { 135 return instance; 136 } 137 } 138 } 139