1 22 package org.jboss.reflect.plugins.javassist; 23 24 import java.lang.reflect.Modifier ; 25 import java.util.Arrays ; 26 27 import javassist.CtClass; 28 import javassist.CtConstructor; 29 import javassist.NotFoundException; 30 31 import org.jboss.reflect.plugins.AnnotationHelper; 32 import org.jboss.reflect.spi.AnnotationValue; 33 import org.jboss.reflect.spi.ClassInfo; 34 import org.jboss.reflect.spi.ConstructorInfo; 35 import org.jboss.reflect.spi.ParameterInfo; 36 import org.jboss.reflect.spi.TypeInfo; 37 import org.jboss.util.JBossStringBuilder; 38 39 45 public class JavassistConstructorInfo extends JavassistAnnotatedParameterInfo implements ConstructorInfo 46 { 47 48 private static final JavassistReflectionFactory reflectionFactory = new JavassistReflectionFactory(true); 49 50 51 private CtConstructor ctConstructor; 52 53 54 private transient JavassistConstructor constructor; 55 56 63 public JavassistConstructorInfo(AnnotationHelper annotationHelper, JavassistTypeInfo typeInfo, CtConstructor ctConstructor) 64 { 65 super(annotationHelper); 66 this.typeInfo = typeInfo; 67 this.ctConstructor = ctConstructor; 68 69 } 70 71 public int getModifiers() 72 { 73 return ctConstructor.getModifiers(); 74 } 75 76 public boolean isPublic() 77 { 78 return Modifier.isPublic(getModifiers()); 79 } 80 81 public boolean isStatic() 82 { 83 return Modifier.isStatic(getModifiers()); 84 } 85 86 public ClassInfo getDeclaringClass() 87 { 88 return typeInfo; 89 } 90 91 public ClassInfo[] getExceptionTypes() 92 { 93 if (exceptionTypes == null) 94 { 95 try 96 { 97 CtClass[] types = ctConstructor.getExceptionTypes(); 98 exceptionTypes = new ClassInfo[types.length]; 99 for (int i = 0; i < types.length; ++i) 100 exceptionTypes[i] = (ClassInfo) typeInfo.getFactory().getTypeInfo(types[i]); 101 } 102 catch (NotFoundException e) 103 { 104 throw JavassistTypeInfoFactoryImpl.raiseClassNotFound("for exception types of constructor", e); 105 } 106 } 107 return exceptionTypes; 108 } 109 110 public ParameterInfo[] getParameters() 111 { 112 if (parameters == null) 113 generateParameters(); 114 return parameters; 115 } 116 117 public TypeInfo[] getParameterTypes() 118 { 119 if (parameterTypes == null) 120 generateParameters(); 121 return parameterTypes; 122 } 123 124 public Object newInstance(Object [] args) throws Throwable 125 { 126 if (constructor == null) 127 constructor = reflectionFactory.createConstructor(ctConstructor); 128 return constructor.newInstance(args); 129 } 130 131 protected int getHashCode() 132 { 133 int result = getDeclaringClass().hashCode(); 134 generateParameters(); 135 if (parameterTypes != null) 136 { 137 for (int i = 0; i < parameterTypes.length; i++) 138 result = 29 * result + parameterTypes[i].hashCode(); 139 } 140 return result; 141 } 142 143 public boolean equals(Object obj) 144 { 145 if (this == obj) 146 return true; 147 if (obj == null || obj instanceof ConstructorInfo == false) 148 return false; 149 150 final ConstructorInfo other = (ConstructorInfo) obj; 151 152 if (getDeclaringClass().equals(other.getDeclaringClass()) == false) 153 return false; 154 return (Arrays.equals(getParameterTypes(), other.getParameterTypes())); 155 } 156 157 158 protected void toString(JBossStringBuilder buffer) 159 { 160 buffer.append(Arrays.asList(getParameterTypes())); 161 super.toString(buffer); 162 } 163 164 167 protected void generateParameters() 168 { 169 try 170 { 171 CtClass[] types = ctConstructor.getParameterTypes(); 172 parameterTypes = new TypeInfo[types.length]; 173 for (int i = 0; i < types.length; ++i) 174 parameterTypes[i] = typeInfo.getFactory().getTypeInfo(types[i]); 175 parameters = new ParameterInfo[types.length]; 176 for (int i = 0; i < types.length; ++i) 177 parameters[i] = new JavassistParameterInfo(annotationHelper, this, i, parameterTypes[i]); 178 } 179 catch (NotFoundException e) 180 { 181 throw JavassistTypeInfoFactoryImpl.raiseClassNotFound("for parameters of constructor", e); 182 } 183 } 184 185 public AnnotationValue[] getAnnotations() 186 { 187 return getAnnotations(ctConstructor); 188 } 189 190 protected void createParameterAnnotations() 191 { 192 try 193 { 194 Object [][] parameterAnnotations = ctConstructor.getParameterAnnotations(); 195 super.setupParameterAnnotations(parameterAnnotations); 196 } 197 catch (ClassNotFoundException e) 198 { 199 throw new RuntimeException (e); 201 } 202 } 203 } 204 | Popular Tags |