@Override public boolean apply(@Nullable Entity input) { if (input == null) return false; for (Class<?> cls : Reflections.getAllInterfaces(input.getClass())) { if (pattern.matcher(cls.getName()).matches()) { return true; } } return false; } }
/** Finds and invokes a suitable constructor, supporting varargs and primitives, boxing and looking at compatible supertypes in the constructor's signature */ public static <T> Maybe<T> invokeConstructorFromArgs(Class<? extends T> clazz, Object...argsArray) { return invokeConstructorFromArgs(clazz, argsArray, false); }
/** invokes the given method on the given clazz or instance, assuming that the method matches passed arguments * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ public static Object invokeMethodFromArgs(Object clazzOrInstance, Method m, List<?> args) throws IllegalAccessException, InvocationTargetException { return invokeMethodFromArgs(clazzOrInstance, m, args, false); }
/** @deprecated since 0.11.0, use {@link org.apache.brooklyn.util.core.ClassLoaderUtils} in a combination with {@link #invokeConstructorFromArgs(Class, Object...)} instead */ public static <T> Maybe<T> invokeConstructorFromArgsUntyped(ClassLoader classLoader, String className, Object...argsArray) { Reflections reflections = new Reflections(classLoader); @SuppressWarnings("unchecked") Class<T> clazz = (Class<T>)reflections.loadClass(className); return invokeConstructorFromArgs(reflections, clazz, argsArray, false); }
/** As {@link #invokeConstructorFromArgs(Class, Object...)} but allowing more configurable input; * in particular setAccessible allows private constructors to be used (not the default) */ public static <T> Maybe<T> invokeConstructorFromArgs(ClassLoader classLoader, Class<T> clazz, Object[] argsArray, boolean setAccessible) { Reflections reflections = new Reflections(classLoader); return invokeConstructorFromArgs(reflections, clazz, argsArray, setAccessible); }
public static Maybe<Object> getFieldValueMaybe(Object instance, String fieldName) { try { if (instance==null) return null; Field f = findField(instance.getClass(), fieldName); return getFieldValueMaybe(instance, f); } catch (Exception e) { Exceptions.propagateIfFatal(e); return Maybe.absent(e); } }
/** @deprecated since 0.11.0, use {@link org.apache.brooklyn.util.core.ClassLoaderUtils} in a combination with {@link #invokeConstructorFromArgs(Class, Object...)} instead */ public Object loadInstance(String classname, Object...argValues) throws ReflectionNotFoundException, ReflectionAccessException { Class<?> clazz = loadClass(classname); Maybe<?> v = null; try { v = invokeConstructorFromArgs(clazz, argValues); if (v.isPresent()) return v.get(); } catch (Exception e) { throw new IllegalStateException("Error invoking constructor for "+clazz+Arrays.toString(argValues) + ": " + Exceptions.collapseText(e)); } throw new IllegalStateException("No suitable constructor for "+clazz+Arrays.toString(argValues)); } /** @deprecated since 0.11.0, use {@link org.apache.brooklyn.util.core.ClassLoaderUtils} in a combination with {@link #invokeConstructorFromArgs(Class, Object...)} instead */
/** as {@link #invokeMethodFromArgs(Object, String, List)} but giving control over whether to set it accessible */ public static Maybe<Object> invokeMethodFromArgs(Object clazzOrInstance, String method, List<?> args, boolean setAccessible, Optional<? extends TypeCoercer> coercer) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Maybe<Method> maybeMethod = getMethodFromArgs(clazzOrInstance, method, args); if (coercer.isPresent() && maybeMethod.isAbsent()) { maybeMethod = getMethodFromArgs(clazzOrInstance, method, args, coercer); } if (maybeMethod.isAbsent()) { return Maybe.absent(Maybe.getException(maybeMethod)); } Method m = maybeMethod.get(); return Maybe.of(invokeMethodFromArgs(clazzOrInstance, m, args, setAccessible, coercer)); }
Reflections reflections = new Reflections(CatalogOsgiVersionMoreEntityRebindTest.class.getClassLoader()); Class<? extends Location> locationClazz = (Class<? extends Location>) new ClassLoaderUtils(reflections.getClassLoader(), mgmt()).loadClass(locationTypeWithBundlePrefix); Location loc = mgmt().getLocationManager().createLocation(LocationSpec.create(locationClazz)); ((EntityInternal)entity).addLocations(ImmutableList.of(loc));
public RebindIteration(RebindManagerImpl rebindManager, ManagementNodeState mode, ClassLoader classLoader, RebindExceptionHandler exceptionHandler, Semaphore rebindActive, AtomicInteger readOnlyRebindCount, PersistenceActivityMetrics rebindMetrics, BrooklynMementoPersister persistenceStoreAccess ) { // NB: there is no particularly deep meaning in what is passed in vs what is looked up from the RebindManager which calls us // (this is simply a refactoring of previous code to a new class) this.rebindManager = rebindManager; this.mode = mode; this.classLoader = checkNotNull(classLoader, "classLoader"); this.exceptionHandler = checkNotNull(exceptionHandler, "exceptionHandler"); this.rebindActive = rebindActive; this.readOnlyRebindCount = readOnlyRebindCount; this.rebindMetrics = rebindMetrics; this.persistenceStoreAccess = persistenceStoreAccess; managementContext = rebindManager.getManagementContext(); rebindContext = new RebindContextImpl(managementContext, exceptionHandler, classLoader); reflections = new Reflections(classLoader).applyClassRenames(DeserializingClassRenamesProvider.INSTANCE.loadDeserializingMapping()); instantiator = new BrooklynObjectInstantiator(classLoader, rebindContext, reflections); if (mode==ManagementNodeState.HOT_STANDBY || mode==ManagementNodeState.HOT_BACKUP) { rebindContext.setAllReadOnly(); } else { Preconditions.checkState(mode==ManagementNodeState.MASTER, "Must be either master or read only to rebind (mode "+mode+")"); } }
Map<String,Method> methodSources = Maps.newLinkedHashMap(); for (Field f : Reflections.findPublicFieldsOrderedBySuper(clazz)) { if (Effector.class.isAssignableFrom(f.getType())) { if (!Modifier.isStatic(f.getModifiers())) { for (Method m : Reflections.findPublicMethodsOrderedBySuper(clazz)) { org.apache.brooklyn.core.annotation.Effector effectorAnnotation = m.getAnnotation(org.apache.brooklyn.core.annotation.Effector.class); if (effectorAnnotation != null) {
/** @deprecated since 0.10.0 use {@link #findMappedNameMaybe(Map, String)} */ @Deprecated public static Optional<String> tryFindMappedName(Map<String, String> renames, String name) { return findMappedNameMaybe(renames, name).toOptional(); }
protected <T> Maybe<T> tryCoerceArray(Object value, TypeToken<T> targetTypeToken, Class<? super T> targetType) { List<?> listValue = Reflections.arrayToList(value); return tryCoerceIterable(listValue, targetTypeToken, targetType); }
/** * Tries to find a single-parameter method with a parameter compatible with (can be coerced to) the argument, and * invokes it. * * @param instance the object to invoke the method on * @param methodName the name of the method to invoke * @param argument the argument to the method's parameter. * @return the result of the method call, or {@link org.apache.brooklyn.util.guava.Maybe#absent()} if method could not be matched. */ public static Maybe<?> tryFindAndInvokeSingleParameterMethod(final Object instance, final String methodName, final Object argument) { Class<?> clazz = instance.getClass(); Iterable<Method> methods = Arrays.asList(clazz.getMethods()); Optional<Method> matchingMethod = Iterables.tryFind(methods, matchSingleParameterMethod(methodName, argument)); if (matchingMethod.isPresent()) { Method method = matchingMethod.get(); Method accessibleMethod = Reflections.findAccessibleMethod(method).get(); try { Type paramType = method.getGenericParameterTypes()[0]; Object coercedArgument = TypeCoercions.coerce(argument, TypeToken.of(paramType)); return Maybe.of(accessibleMethod.invoke(instance, coercedArgument)); } catch (IllegalAccessException | InvocationTargetException e) { throw Exceptions.propagate(e); } } else { return Maybe.absent(); } }
/** * Handles inner classes, where the outer class has been renamed. For example: * * {@code findMappedName("com.example.MyFoo$MySub")} will return {@code com.example.renamed.MyFoo$MySub}, if * the renamed contains {@code com.example.MyFoo: com.example.renamed.MyFoo}. */ @Beta public String findMappedName(String name) { return Reflections.findMappedNameAndLog(loadDeserializingMapping(), name); }
Map<String,Sensor<?>> result = Maps.newLinkedHashMap(); Map<String,Field> sources = Maps.newLinkedHashMap(); for (Field f : Reflections.findPublicFieldsOrderedBySuper((clazz))) { if (Sensor.class.isAssignableFrom(f.getType())) { if (!Modifier.isStatic(f.getModifiers())) {
managementContext.getStorage().getMap(LocalUsageManager.APPLICATION_USAGE_KEY).clear(); managementContext.getStorage().getMap(LocalUsageManager.LOCATION_USAGE_KEY).clear(); Field machineDetailsField = Reflections.findField(JcloudsSshMachineLocation.class, "machineDetails"); machineDetailsField.setAccessible(true); machineDetailsField.set(machine, null);
/** @deprecated since 0.11.0, use {@link org.apache.brooklyn.util.core.ClassLoaderUtils} in a combination with {@link #invokeConstructorFromArgs(Class, Object...)} instead */ @SuppressWarnings("unchecked") public static <T> Maybe<T> invokeConstructorFromArgs(ClassLoader classLoader, Class<T> optionalSupertype, String className, Object...argsArray) { Reflections reflections = new Reflections(classLoader); Class<?> clazz = reflections.loadClass(className); if (optionalSupertype!=null && !optionalSupertype.isAssignableFrom(clazz)) { return Maybe.absent("The type requested '"+className+"' is not assignable to "+optionalSupertype); } return invokeConstructorFromArgs(reflections, (Class<T>)clazz, argsArray, false); }
/** As {@link #invokeConstructorFromArgs(Class, Object...)} but will use private constructors (with setAccessible = true) */ public static <T> Maybe<T> invokeConstructorFromArgsIncludingPrivate(Class<? extends T> clazz, Object ...argsArray) { return Reflections.invokeConstructorFromArgs(new Reflections(clazz.getClassLoader()), clazz, argsArray, true); } /** As {@link #invokeConstructorFromArgs(Class, Object...)} but allowing more configurable input;
public static String findMappedNameAndLog(Map<String, String> renames, String name) { Maybe<String> rename = Reflections.findMappedNameMaybe(renames, name); if (rename.isPresent()) { LOG.debug("Mapping class '"+name+"' to '"+rename.get()+"'"); return rename.get(); } return name; }