1 22 package org.jboss.reflect.plugins.javassist; 23 24 import java.lang.reflect.Modifier ; 25 import java.util.Arrays ; 26 27 import javassist.CtBehavior; 28 import javassist.CtClass; 29 import javassist.CtMethod; 30 import javassist.NotFoundException; 31 32 import org.jboss.reflect.plugins.AnnotationHelper; 33 import org.jboss.reflect.spi.AnnotationValue; 34 import org.jboss.reflect.spi.ClassInfo; 35 import org.jboss.reflect.spi.MethodInfo; 36 import org.jboss.reflect.spi.ParameterInfo; 37 import org.jboss.reflect.spi.TypeInfo; 38 import org.jboss.util.JBossStringBuilder; 39 40 public class JavassistMethodInfo extends JavassistAnnotatedParameterInfo implements MethodInfo 41 { 42 43 private static final JavassistReflectionFactory reflectionFactory = new JavassistReflectionFactory(true); 44 45 46 private SignatureKey key; 47 48 49 private CtMethod ctMethod; 50 51 52 private transient JavassistMethod method; 53 54 55 private transient TypeInfo returnType; 56 57 65 public JavassistMethodInfo(AnnotationHelper annotationHelper, JavassistTypeInfo typeInfo, SignatureKey key, CtMethod ctMethod) 66 { 67 super(annotationHelper); 68 this.typeInfo = typeInfo; 69 this.key = key; 70 this.ctMethod = ctMethod; 71 } 72 73 public String getName() 74 { 75 return key.name; 76 } 77 78 public ClassInfo getDeclaringClass() 79 { 80 return typeInfo; 81 } 82 83 public int getModifiers() 84 { 85 return ctMethod.getModifiers(); 86 } 87 88 public boolean isPublic() 89 { 90 return Modifier.isPublic(getModifiers()); 91 } 92 93 public boolean isStatic() 94 { 95 return Modifier.isStatic(getModifiers()); 96 } 97 98 public ClassInfo[] getExceptionTypes() 99 { 100 if (exceptionTypes == null) 101 { 102 try 103 { 104 CtClass[] types = ctMethod.getExceptionTypes(); 105 exceptionTypes = new ClassInfo[types.length]; 106 for (int i = 0; i < types.length; ++i) 107 exceptionTypes[i] = (ClassInfo) typeInfo.getFactory().getTypeInfo(types[i]); 108 } 109 catch (NotFoundException e) 110 { 111 throw JavassistTypeInfoFactoryImpl.raiseClassNotFound("for exception types of method " + getName(), e); 112 } 113 } 114 return exceptionTypes; 115 } 116 117 public TypeInfo getReturnType() 118 { 119 if (returnType != null) 120 return returnType; 121 try 122 { 123 CtClass clazz = ctMethod.getReturnType(); 124 returnType = typeInfo.getFactory().getTypeInfo(clazz); 125 return returnType; 126 } 127 catch (NotFoundException e) 128 { 129 throw JavassistTypeInfoFactoryImpl.raiseClassNotFound("for return type of method " + getName(), e); 130 } 131 } 132 133 public ParameterInfo[] getParameters() 134 { 135 if (parameters == null) 136 generateParameters(); 137 return parameters; 138 } 139 140 public TypeInfo[] getParameterTypes() 141 { 142 if (parameterTypes == null) 143 generateParameters(); 144 return parameterTypes; 145 } 146 147 public Object invoke(Object target, Object [] args) throws Throwable 148 { 149 if (method == null) 150 method = reflectionFactory.createMethod(ctMethod); 151 return method.invoke(target, args); 152 } 153 154 protected int getHashCode() 155 { 156 return getName().hashCode(); 157 } 158 159 public boolean equals(Object obj) 160 { 161 if (obj == this) 162 return true; 163 if (obj == null || obj instanceof MethodInfo == false) 164 return false; 165 166 final MethodInfo other = (MethodInfo) obj; 167 168 if (getName().equals(other.getName()) == false) 169 return false; 170 if (getDeclaringClass().equals(other.getDeclaringClass()) == false) 171 return false; 172 if (getReturnType().equals(other.getReturnType()) == false) 173 return false; 174 return Arrays.equals(getParameterTypes(), other.getParameterTypes()); 175 } 176 177 public void toShortString(JBossStringBuilder buffer) 178 { 179 buffer.append(getName()); 180 } 181 182 protected void toString(JBossStringBuilder buffer) 183 { 184 buffer.append("name=").append(getName()); 185 super.toString(buffer); 186 } 187 188 191 protected void generateParameters() 192 { 193 try 194 { 195 CtClass[] types = ctMethod.getParameterTypes(); 196 parameterTypes = new TypeInfo[types.length]; 197 for (int i = 0; i < types.length; ++i) 198 parameterTypes[i] = typeInfo.getFactory().getTypeInfo(types[i]); 199 parameters = new ParameterInfo[types.length]; 200 for (int i = 0; i < types.length; ++i) 201 parameters[i] = new JavassistParameterInfo(annotationHelper, this, i, parameterTypes[i]); 202 } 203 catch (NotFoundException e) 204 { 205 throw JavassistTypeInfoFactoryImpl.raiseClassNotFound("for parameters of " + getName(), e); 206 } 207 } 208 209 public AnnotationValue[] getAnnotations() 210 { 211 return getAnnotations(ctMethod); 212 } 213 214 protected CtBehavior getParameterizedObject() 215 { 216 return ctMethod; 217 } 218 219 protected void createParameterAnnotations() 220 { 221 try 222 { 223 Object [][] parameterAnnotations = ctMethod.getParameterAnnotations(); 224 super.setupParameterAnnotations(parameterAnnotations); 225 } 226 catch (ClassNotFoundException e) 227 { 228 throw new RuntimeException (e); 230 } 231 } 232 233 } 234 | Popular Tags |