1 17 18 package org.objectweb.jac.core.rtti; 19 20 21 import java.lang.NoSuchMethodException ; 22 import java.lang.reflect.Constructor ; 23 import java.lang.reflect.InvocationTargetException ; 24 import org.apache.log4j.Logger; 25 import org.objectweb.jac.util.WrappedThrowableException; 26 27 34 35 public class ConstructorItem extends AbstractMethodItem { 36 static Logger logger = Logger.getLogger("rtti.method"); 37 38 45 46 public static Constructor [] toConstructors( ConstructorItem[] constructorItems ) { 47 Constructor [] res = new Constructor [constructorItems.length]; 48 for ( int i = 0; i < constructorItems.length; i++ ) { 49 if ( constructorItems[i] == null ) { 50 res[i] = null; 51 } else { 52 res[i] = constructorItems[i].getActualConstructor(); 53 } 54 } 55 return res; 56 } 57 58 59 64 65 public ConstructorItem(Constructor delegate, ClassItem parent) 66 throws InvalidDelegateException 67 { 68 super(delegate,parent); 69 try { 70 delegate.getDeclaringClass().getDeclaredMethod( 71 "_org_"+NamingConventions.getShortClassName(delegate.getDeclaringClass()), 72 delegate.getParameterTypes()); 73 } catch(NoSuchMethodException e) { 74 } 76 } 77 78 83 84 public Constructor getActualConstructor() { 85 return (Constructor ) delegate; 86 } 87 88 public String getName() { 89 return NamingConventions.getShortConstructorName(this); 90 } 91 92 public Class getType() { 93 return getParent().getType(); 94 } 95 96 103 104 public Object newInstance(Object [] params) 105 throws InstantiationException , IllegalAccessException , InvocationTargetException 106 { 107 return getActualConstructor().newInstance(params); 108 } 109 110 116 117 public Object newInstance() 118 throws InstantiationException , IllegalAccessException , InvocationTargetException 119 { 120 if (getClassItem().isAbstract()) 121 throw new InstantiationException ("Cannot instantiate abstract class "+getClassItem()); 122 return getType().newInstance(); 123 } 124 125 public Object invoke(Object substance, Object [] params) { 126 try { 127 return newInstance(params); 128 } catch (Exception e) { 129 logger.info("Failed to invoke "+this,e); 130 throw new WrappedThrowableException(e); 131 } 132 } 133 134 public Class [] getParameterTypes() { 135 return ((Constructor )delegate).getParameterTypes(); 136 } 137 138 public String toString() { 139 if (delegate!=null) 140 return getFullName(); 141 else 142 return "???"; 143 } 144 145 public String getFullName() { 146 String ret = NamingConventions.getShortConstructorName(this) + "("; 147 Class [] pts = getParameterTypes(); 148 for ( int i = 0; i < pts.length; i++ ) { 149 ret = ret + NamingConventions.getStandardClassName(pts[i]); 150 if ( i < pts.length - 1 ) ret = ret + ","; 151 } 152 ret = ret + ")"; 153 return ret; 154 } 155 156 public final boolean isAdder() { 157 return false; 158 } 159 public final CollectionItem[] getAddedCollections() { 160 return CollectionItem.emptyArray; 161 } 162 public final CollectionItem[] getRemovedCollections() { 163 return CollectionItem.emptyArray; 164 } 165 166 public final boolean isSetter() { return false; } 167 public final boolean isRemover() { return false; } 168 public final boolean isAccessor() { return false; } 169 public final boolean isWriter() { return false; } 170 public final boolean isGetter() { return false; } 171 172 public final boolean isFieldGetter() { return false; } 173 public final boolean isFieldSetter() { return false; } 174 175 public final boolean isReferenceGetter() { return false; } 176 public final boolean isReferenceSetter() { return false; } 177 public final boolean isReferenceAccessor() { return false; } 178 179 public final boolean isCollectionGetter() { return false; } 180 public final boolean isCollectionSetter() { return false; } 181 public final boolean isCollectionAccessor() { return false; } 182 183 public final FieldItem getSetField() { 184 return null; 185 } 186 187 } | Popular Tags |