001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.util; 018 019import java.lang.reflect.Field; 020import java.lang.reflect.Method; 021import java.lang.reflect.Modifier; 022import java.util.Arrays; 023 024/** 025 * Helper for working with reflection on classes. 026 * <p/> 027 * This code is based on org.apache.camel.spring.util.ReflectionUtils class. 028 */ 029public final class ReflectionHelper { 030 031 private ReflectionHelper() { 032 // utility class 033 } 034 035 /** 036 * Callback interface invoked on each field in the hierarchy. 037 */ 038 public interface FieldCallback { 039 040 /** 041 * Perform an operation using the given field. 042 * 043 * @param field the field to operate on 044 */ 045 void doWith(Field field) throws IllegalArgumentException, IllegalAccessException; 046 } 047 048 /** 049 * Action to take on each method. 050 */ 051 public interface MethodCallback { 052 053 /** 054 * Perform an operation using the given method. 055 * 056 * @param method the method to operate on 057 */ 058 void doWith(Method method) throws IllegalArgumentException, IllegalAccessException; 059 } 060 061 /** 062 * Invoke the given callback on all fields in the target class, going up the 063 * class hierarchy to get all declared fields. 064 * @param clazz the target class to analyze 065 * @param fc the callback to invoke for each field 066 */ 067 public static void doWithFields(Class<?> clazz, FieldCallback fc) throws IllegalArgumentException { 068 // Keep backing up the inheritance hierarchy. 069 Class<?> targetClass = clazz; 070 do { 071 Field[] fields = targetClass.getDeclaredFields(); 072 for (Field field : fields) { 073 try { 074 fc.doWith(field); 075 } catch (IllegalAccessException ex) { 076 throw new IllegalStateException("Shouldn't be illegal to access field '" + field.getName() + "': " + ex); 077 } 078 } 079 targetClass = targetClass.getSuperclass(); 080 } 081 while (targetClass != null && targetClass != Object.class); 082 } 083 084 /** 085 * Perform the given callback operation on all matching methods of the given 086 * class and superclasses (or given interface and super-interfaces). 087 * <p/> 088 * <b>Important:</b> This method does not take the 089 * {@link java.lang.reflect.Method#isBridge() bridge methods} into account. 090 * 091 * @param clazz class to start looking at 092 * @param mc the callback to invoke for each method 093 */ 094 public static void doWithMethods(Class<?> clazz, MethodCallback mc) throws IllegalArgumentException { 095 // Keep backing up the inheritance hierarchy. 096 Method[] methods = clazz.getDeclaredMethods(); 097 for (Method method : methods) { 098 if (method.isBridge()) { 099 // skip the bridge methods which in Java 8 leads to problems with inheritance 100 // see https://bugs.openjdk.java.net/browse/JDK-6695379 101 continue; 102 } 103 try { 104 mc.doWith(method); 105 } catch (IllegalAccessException ex) { 106 throw new IllegalStateException("Shouldn't be illegal to access method '" + method.getName() + "': " + ex); 107 } 108 } 109 if (clazz.getSuperclass() != null) { 110 doWithMethods(clazz.getSuperclass(), mc); 111 } else if (clazz.isInterface()) { 112 for (Class<?> superIfc : clazz.getInterfaces()) { 113 doWithMethods(superIfc, mc); 114 } 115 } 116 } 117 118 /** 119 * Attempt to find a {@link Method} on the supplied class with the supplied name 120 * and parameter types. Searches all superclasses up to {@code Object}. 121 * <p>Returns {@code null} if no {@link Method} can be found. 122 * @param clazz the class to introspect 123 * @param name the name of the method 124 * @param paramTypes the parameter types of the method 125 * (may be {@code null} to indicate any signature) 126 * @return the Method object, or {@code null} if none found 127 */ 128 public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) { 129 ObjectHelper.notNull(clazz, "Class must not be null"); 130 ObjectHelper.notNull(name, "Method name must not be null"); 131 Class<?> searchType = clazz; 132 while (searchType != null) { 133 Method[] methods = searchType.isInterface() ? searchType.getMethods() : searchType.getDeclaredMethods(); 134 for (Method method : methods) { 135 if (name.equals(method.getName()) && (paramTypes == null || Arrays.equals(paramTypes, method.getParameterTypes()))) { 136 return method; 137 } 138 } 139 searchType = searchType.getSuperclass(); 140 } 141 return null; 142 } 143 144 public static void setField(Field f, Object instance, Object value) { 145 try { 146 boolean oldAccessible = f.isAccessible(); 147 boolean shouldSetAccessible = !Modifier.isPublic(f.getModifiers()) && !oldAccessible; 148 if (shouldSetAccessible) { 149 f.setAccessible(true); 150 } 151 f.set(instance, value); 152 if (shouldSetAccessible) { 153 f.setAccessible(oldAccessible); 154 } 155 } catch (Exception ex) { 156 throw new UnsupportedOperationException("Cannot inject value of class: " + value.getClass() + " into: " + f); 157 } 158 } 159 160}