private static Field findFieldRecursively(Class<?> c, String fieldName) { Field f = null; try { f = c.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { if (!c.equals(Object.class)) f = findFieldRecursively(c.getSuperclass(), fieldName); } return f; }
public static Method findGetterForField(Class<?> c, String fieldName) { Method retval = findGetterForFieldUsingReflection(c, fieldName); if (retval == null) { if (!c.equals(Object.class)) { if (!c.isInterface()) { retval = findGetterForField(c.getSuperclass(), fieldName); if (retval == null) { for (Class<?> ifc : c.getInterfaces()) { retval = findGetterForField(ifc, fieldName); if (retval != null) break; } } } } } return retval; }
public static Method findMethod(Class<?> type, String methodName) { try { return type.getDeclaredMethod(methodName); } catch (NoSuchMethodException e) { if (type.equals(Object.class) || type.isInterface()) { throw new CacheException(e); } return findMethod(type.getSuperclass(), methodName); } }
public static void invokeLifecycle(Object component, Class<? extends Annotation> lifecycle) { List<Method> methods = ReflectionUtil.getAllMethods(component.getClass(), lifecycle); for (Method m : methods) { ReflectionUtil.invokeAccessibly(component, m, EMPTY_OBJECT_ARRAY); } }
@Override public void writeObject(ObjectOutput output, FuzzyQuery object) throws IOException { output.writeObject(object.getTerm()); output.writeInt(object.getPrefixLength()); output.writeBoolean(object.getTranspositions()); output.writeInt(object.getMaxEdits()); output.writeInt((int) getValue(object, MAX_EXPANSIONS_FIELD)); }
List<Field> fields = ReflectionUtil.getAllFields(instance.getClass(), Inject.class); Map<Object, Object> unmatchedComponents = new IdentityHashMap<>(); for (Object component : components) { ReflectionUtil.setAccessibly(instance, f, value); previousMatch = currentMatch; unmatchedComponents.remove(currentMatch);
public static List<Field> getAnnotatedFields(Class<?> c, Class<? extends Annotation> annotationType) { List<Field> fields = new ArrayList<>(4); // Class could be null in the case of an interface for (;c != null && !c.equals(Object.class); c = c.getSuperclass()) { getAnnotatedFieldHelper(fields, c, annotationType); } return fields; }
public static Method findSetterForField(Class<?> c, String fieldName) { if (c == Object.class) { return null; } for (Method m : c.getDeclaredMethods()) { String name = m.getName(); String s = null; if (name.startsWith("set")) { s = name.substring(3); } if (s != null && s.equalsIgnoreCase(fieldName)) { return m; } } // Try parent class until we run out return findSetterForField(c.getSuperclass(), fieldName); }
private void startProtocolServer(ProtocolServerConfiguration configuration) throws StartException { // Start the server and record it ProtocolServer server; try { server = serverClass.newInstance(); } catch (Exception e) { throw ROOT_LOGGER.failedConnectorInstantiation(e, serverName); } ROOT_LOGGER.connectorStarting(serverName); configuration.ignoredCaches().forEach(server::ignoreCache); SecurityActions.startProtocolServer(server, configuration, getCacheManager().getValue()); protocolServer = server; try { transport = (Transport) ReflectionUtil.getValue(protocolServer, "transport"); } catch (Exception e) { throw ROOT_LOGGER.failedTransportInstantiation(e.getCause(), serverName); } }
@BeforeClass public void createCacheStore() throws PersistenceException { ConfigurationBuilder builder = TestCacheManagerFactory.getDefaultCacheConfiguration(false); JdbcStringBasedStoreConfigurationBuilder storeBuilder = createJdbcConfig(builder); UnitTestDatabaseManager.buildTableManipulation(storeBuilder.table()); UnitTestDatabaseManager.configureUniqueConnectionFactory(storeBuilder); cacheStore = new JdbcStringBasedStore(); marshaller = new TestObjectStreamMarshaller(); cacheStore.init(PersistenceMockUtil.createContext(getClass().getSimpleName(), builder.build(), marshaller)); cacheStore.start(); tableManager = (TableManager) ReflectionUtil.getValue(cacheStore, "tableManager"); }
public static Method findMethod(Class<?> type, String methodName, Class<?>[] parameters) { try { return type.getDeclaredMethod(methodName, parameters); } catch (NoSuchMethodException e) { if (type.equals(Object.class) || type.isInterface()) { throw new CacheException(e); } return findMethod(type.getSuperclass(), methodName, parameters); } }
/** * Retrieves the value of a field of an object instance via reflection * * @param instance to inspect * @param fieldName name of field to retrieve * @return a value */ public static Object getValue(Object instance, String fieldName) { Field f = findFieldRecursively(instance.getClass(), fieldName); if (f == null) throw new CacheException("Could not find field named '" + fieldName + "' on instance " + instance); try { f.setAccessible(true); return f.get(instance); } catch (IllegalAccessException iae) { throw new CacheException("Cannot access field " + f, iae); } }