1 21 package com.db4o.reflect.jdk; 22 23 import java.lang.reflect.*; 24 25 import com.db4o.*; 26 import com.db4o.reflect.*; 27 28 31 public class JdkClass implements ReflectClass{ 32 33 private final Reflector _reflector; 34 private final Class _clazz; 35 private ReflectConstructor _constructor; 36 private Object [] _constructorParams; 37 38 public JdkClass(Reflector reflector, Class clazz) { 39 if(reflector == null){ 40 throw new NullPointerException (); 41 } 42 _reflector = reflector; 43 _clazz = clazz; 44 } 45 46 public ReflectClass getComponentType() { 47 return _reflector.forClass(_clazz.getComponentType()); 48 } 49 50 public ReflectConstructor[] getDeclaredConstructors(){ 51 Constructor[] constructors = _clazz.getDeclaredConstructors(); 52 ReflectConstructor[] reflectors = new ReflectConstructor[constructors.length]; 53 for (int i = 0; i < constructors.length; i++) { 54 reflectors[i] = new JdkConstructor(_reflector, constructors[i]); 55 } 56 return reflectors; 57 } 58 59 public ReflectField getDeclaredField(String name){ 60 try { 61 return new JdkField(_reflector, _clazz.getDeclaredField(name)); 62 } catch (Exception e) { 63 return null; 64 } 65 } 66 67 public ReflectField[] getDeclaredFields(){ 68 Field[] fields = _clazz.getDeclaredFields(); 69 ReflectField[] reflectors = new ReflectField[fields.length]; 70 for (int i = 0; i < reflectors.length; i++) { 71 reflectors[i] = new JdkField(_reflector, fields[i]); 72 } 73 return reflectors; 74 } 75 76 public ReflectClass getDelegate(){ 77 return this; 78 } 79 80 public ReflectMethod getMethod(String methodName, ReflectClass[] paramClasses){ 81 try { 82 Method method = _clazz.getMethod(methodName, JdkReflector.toNative(paramClasses)); 83 if(method == null){ 84 return null; 85 } 86 return new JdkMethod(method, reflector()); 87 } catch (Exception e) { 88 return null; 89 } 90 } 91 92 public String getName(){ 93 return _clazz.getName(); 94 } 95 96 public ReflectClass getSuperclass() { 97 return _reflector.forClass(_clazz.getSuperclass()); 98 } 99 100 public boolean isAbstract(){ 101 return Modifier.isAbstract(_clazz.getModifiers()); 102 } 103 104 public boolean isArray() { 105 return _clazz.isArray(); 106 } 107 108 public boolean isAssignableFrom(ReflectClass type) { 109 if(!(type instanceof JdkClass)) { 110 return false; 111 } 112 return _clazz.isAssignableFrom(JdkReflector.toNative(type)); 113 } 114 115 public boolean isCollection() { 116 return _reflector.isCollection(this); 117 } 118 119 public boolean isInstance(Object obj) { 120 return _clazz.isInstance(obj); 121 } 122 123 public boolean isInterface(){ 124 return _clazz.isInterface(); 125 } 126 127 public boolean isPrimitive() { 128 return _clazz.isPrimitive(); 129 } 130 131 public boolean isSecondClass() { 132 133 return isPrimitive(); 134 135 138 } 140 141 public Object newInstance(){ 142 try { 143 if(_constructor == null){ 144 return _clazz.newInstance(); 145 } 146 return _constructor.newInstance(_constructorParams); 147 } catch (Throwable t) { 148 } 149 return null; 150 } 151 152 Class getJavaClass(){ 153 return _clazz; 154 } 155 156 public Reflector reflector() { 157 return _reflector; 158 } 159 160 public boolean skipConstructor(boolean flag){ 161 if(flag){ 162 Constructor constructor = Platform4.jdk().serializableConstructor(_clazz); 163 if(constructor != null){ 164 try{ 165 Object o = constructor.newInstance((Object [])null); 166 if(o != null){ 167 useConstructor(new JdkConstructor(_reflector, constructor), null); 168 return true; 169 } 170 }catch(Throwable t){ 171 172 } 173 } 174 } 175 useConstructor(null, null); 176 return false; 177 } 178 179 public void useConstructor(ReflectConstructor constructor, Object [] params){ 180 this._constructor = constructor; 181 _constructorParams = params; 182 } 183 184 public Object [] toArray(Object obj){ 185 return null; 186 } 187 } 188 | Popular Tags |