2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
21 import java.lang.reflect.Field;
22 import java.lang.reflect.Member;
23 import java.lang.reflect.Method;
24 import java.lang.reflect.Modifier;
25 import java.util.Arrays;
26 import java.util.List;
33 public class IntrospectionUtil
36 public static boolean isJavaBeanCompliantSetter (Method method)
41 if (method.getReturnType() != Void.TYPE)
44 if (!method.getName().startsWith("set"))
47 if (method.getParameterTypes().length != 1)
53 public static Method findMethod (Class<?> clazz, String methodName, Class<?>[] args, boolean checkInheritance, boolean strictArgs)
54 throws NoSuchMethodException
57 throw new NoSuchMethodException("No class");
58 if (methodName==null || methodName.trim().equals(""))
59 throw new NoSuchMethodException("No method name");
62 Method[] methods = clazz.getDeclaredMethods();
63 for (int i=0;i<methods.length && method==null;i++)
65 if (methods[i].getName().equals(methodName) && checkParams(methods[i].getParameterTypes(), (args==null?new Class[] {}:args), strictArgs))
75 else if (checkInheritance)
76 return findInheritedMethod(clazz.getPackage(), clazz.getSuperclass(), methodName, args, strictArgs);
78 throw new NoSuchMethodException("No such method "+methodName+" on class "+clazz.getName());
86 public static Field findField (Class<?> clazz, String targetName, Class<?> targetType, boolean checkInheritance, boolean strictType)
87 throws NoSuchFieldException
90 throw new NoSuchFieldException("No class");
92 throw new NoSuchFieldException("No field name");
96 Field field = clazz.getDeclaredField(targetName);
99 if (field.getType().equals(targetType))
104 if (field.getType().isAssignableFrom(targetType))
107 if (checkInheritance)
109 return findInheritedField(clazz.getPackage(), clazz.getSuperclass(), targetName, targetType, strictType);
112 throw new NoSuchFieldException("No field with name "+targetName+" in class "+clazz.getName()+" of type "+targetType);
114 catch (NoSuchFieldException e)
116 return findInheritedField(clazz.getPackage(),clazz.getSuperclass(), targetName,targetType,strictType);
124 public static boolean isInheritable (Package pack, Member member)
131 int modifiers = member.getModifiers();
132 if (Modifier.isPublic(modifiers))
134 if (Modifier.isProtected(modifiers))
136 if (!Modifier.isPrivate(modifiers) && pack.equals(member.getDeclaringClass().getPackage()))
145 public static boolean checkParams (Class<?>[] formalParams, Class<?>[] actualParams, boolean strict)
147 if (formalParams==null)
148 return actualParams==null;
149 if (actualParams==null)
152 if (formalParams.length!=actualParams.length)
155 if (formalParams.length==0)
161 while (j<formalParams.length && formalParams[j].equals(actualParams[j]))
166 while ((j<formalParams.length) && (formalParams[j].isAssignableFrom(actualParams[j])))
172 if (j!=formalParams.length)
181 public static boolean isSameSignature (Method methodA, Method methodB)
188 List<Class<?>> parameterTypesA = Arrays.asList(methodA.getParameterTypes());
189 List<Class<?>> parameterTypesB = Arrays.asList(methodB.getParameterTypes());
191 if (methodA.getName().equals(methodB.getName())
193 parameterTypesA.containsAll(parameterTypesB))
199 public static boolean isTypeCompatible (Class<?> formalType, Class<?> actualType, boolean strict)
201 if (formalType==null)
202 return actualType==null;
203 if (actualType==null)
207 return formalType.equals(actualType);
209 return formalType.isAssignableFrom(actualType);
215 public static boolean containsSameMethodSignature (Method method, Class<?> c, boolean checkPackage)
219 if (!c.getPackage().equals(method.getDeclaringClass().getPackage()))
223 boolean samesig = false;
224 Method[] methods = c.getDeclaredMethods();
225 for (int i=0; i<methods.length && !samesig; i++)
227 if (IntrospectionUtil.isSameSignature(method, methods[i]))
234 public static boolean containsSameFieldName(Field field, Class<?> c, boolean checkPackage)
238 if (!c.getPackage().equals(field.getDeclaringClass().getPackage()))
242 boolean sameName = false;
243 Field[] fields = c.getDeclaredFields();
244 for (int i=0;i<fields.length && !sameName; i++)
246 if (fields[i].getName().equals(field.getName()))
254 protected static Method findInheritedMethod (Package pack, Class<?> clazz, String methodName, Class<?>[] args, boolean strictArgs)
255 throws NoSuchMethodException
258 throw new NoSuchMethodException("No class");
259 if (methodName==null)
260 throw new NoSuchMethodException("No method name");
262 Method method = null;
263 Method[] methods = clazz.getDeclaredMethods();
264 for (int i=0;i<methods.length && method==null;i++)
266 if (methods[i].getName().equals(methodName)
267 && isInheritable(pack,methods[i])
268 && checkParams(methods[i].getParameterTypes(), args, strictArgs))
276 return findInheritedMethod(clazz.getPackage(), clazz.getSuperclass(), methodName, args, strictArgs);
279 protected static Field findInheritedField (Package pack, Class<?> clazz, String fieldName, Class<?> fieldType, boolean strictType)
280 throws NoSuchFieldException
283 throw new NoSuchFieldException ("No class");
285 throw new NoSuchFieldException ("No field name");
288 Field field = clazz.getDeclaredField(fieldName);
289 if (isInheritable(pack, field) && isTypeCompatible(fieldType, field.getType(), strictType))
292 return findInheritedField(clazz.getPackage(), clazz.getSuperclass(),fieldName, fieldType, strictType);
294 catch (NoSuchFieldException e)
296 return findInheritedField(clazz.getPackage(), clazz.getSuperclass(),fieldName, fieldType, strictType);