Java Code Examples for org.reflections.ReflectionUtils#getMethods()
The following examples show how to use
org.reflections.ReflectionUtils#getMethods() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractHandlerProvider.java From enode with MIT License | 6 votes |
private void registerHandler(Class<?> handlerType) { Set<Method> handleMethods = ReflectionUtils.getMethods(handlerType, this::isHandleMethodMatch); handleMethods.forEach(method -> { try { //反射Method转换为MethodHandle,提高效率 MethodHandle handleMethod = lookup.findVirtual(handlerType, method.getName(), MethodType.methodType(method.getReturnType(), method.getParameterTypes())); TKey key = getKey(method); List<THandlerProxyInterface> handlers = handlerDict.computeIfAbsent(key, k -> new ArrayList<>()); IObjectContainer objectContainer = getObjectContainer(); if (objectContainer == null) { throw new IllegalArgumentException("IObjectContainer is null"); } // prototype THandlerProxyInterface handlerProxy = objectContainer.resolve(getHandlerProxyImplementationType()); if (handlerProxy == null) { throw new EnodeRuntimeException("THandlerProxyInterface is null, " + getHandlerProxyImplementationType().getName()); } handlerProxy.setHandlerType(handlerType); handlerProxy.setMethod(method); handlerProxy.setMethodHandle(handleMethod); handlers.add(handlerProxy); } catch (Exception e) { throw new RegisterComponentException(e); } }); }
Example 2
Source File: AppMetadata.java From chassis with Apache License 2.0 | 6 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) private Method findLifecycleMethod(final Class lifecycleAnnotationClass) { Set<Method> methods = ReflectionUtils.getMethods(declaringClass, new Predicate<Method>() { @Override public boolean apply(Method input) { return input != null && input.getAnnotation(lifecycleAnnotationClass) != null; } }); if (methods.isEmpty()) { return null; } if (methods.size() > 1) { throw new BootstrapException("Found multiple " + lifecycleAnnotationClass.getSimpleName() + " methods in class " + declaringClass.getSimpleName() + ". Only 1 is allowed."); } return methods.iterator().next(); }
Example 3
Source File: PolicyTest.java From gravitee-gateway with Apache License 2.0 | 5 votes |
private Method resolvePolicyMethod(Class<?> clazz, Class<? extends Annotation> annotationClass) { Set<Method> methods = ReflectionUtils.getMethods( clazz, withModifier(Modifier.PUBLIC), withAnnotation(annotationClass)); if (methods.isEmpty()) { return null; } return methods.iterator().next(); }
Example 4
Source File: ReflectionBasedFieldPresenceTest.java From slack-client with Apache License 2.0 | 4 votes |
static <T> Object buildTestInstance(Class<? extends T> base) throws Exception { Optional<Method> builderFactoryMethodMaybe = ReflectionUtils.getMethods(base, ReflectionUtils.withName("builder")).stream() .filter(method -> Modifier.isStatic(method.getModifiers())) .findFirst(); if (!builderFactoryMethodMaybe.isPresent()) { throw new IllegalStateException( String.format( "Couldn't find a method with name 'builder' on class %s", base.getClass().getName() ) ); } Object builder = builderFactoryMethodMaybe.get().invoke(null); // static methods ignore object params here Set<Method> builderMethods = ReflectionUtils.getMethods( builder.getClass(), ReflectionUtils.withPrefix("set"), ReflectionUtils.withParametersCount(1), method -> !Iterable.class.isAssignableFrom(method.getParameterTypes()[0]), method -> !(Optional.class.isAssignableFrom(method.getParameterTypes()[0]) && !OPTIONAL_STRING_METHODS_TO_BUILD.contains(method.getName())), method -> !ReflectionUtils.withAnyParameterAnnotation(Nullable.class).apply(method) ); for (Method builderMethod : builderMethods) { if (builderMethod.getParameterCount() != 1) { LOG.debug("Skipping non-monadic {}", builderMethod.getName()); continue; } Class<?> singletonParamType = builderMethod.getParameterTypes()[0]; if (isNumeric(singletonParamType)) { LOG.debug("Calling {} with default of {}", builderMethod.getName(), DEFAULT_NUMBER); builderMethod.invoke(builder, DEFAULT_NUMBER); } else if (String.class.isAssignableFrom(singletonParamType)) { LOG.debug("Calling {} with default of {}", builderMethod.getName(), DEFAULT_STRING); builderMethod.invoke(builder, DEFAULT_STRING); } else if (OPTIONAL_STRING_METHODS_TO_BUILD.contains(builderMethod.getName())) { LOG.debug("Calling {} with default optional of {}", builderMethod.getName(), DEFAULT_OPTIONAL_STRING); builderMethod.invoke(builder, DEFAULT_OPTIONAL_STRING); } else if (Boolean.class.isAssignableFrom(singletonParamType) || boolean.class.isAssignableFrom(singletonParamType)) { LOG.debug("Calling {} with default of {}", builderMethod.getName(), DEFAULT_BOOLEAN); builderMethod.invoke(builder, DEFAULT_BOOLEAN); } else if (Enum.class.isAssignableFrom(singletonParamType)) { Object firstEnumConstant = singletonParamType.getEnumConstants()[0]; LOG.debug("Calling {} with default of {}", builderMethod.getName(), firstEnumConstant); builderMethod.invoke(builder, firstEnumConstant); } else if (Map.class.isAssignableFrom(singletonParamType)) { LOG.debug("Calling {} with default of {}", builderMethod.getName(), DEFAULT_MAP); builderMethod.invoke(builder, DEFAULT_MAP); } else { LOG.debug("Recursively constructing test instance for {}", builderMethod.getName()); builderMethod.invoke(builder, buildTestInstance(singletonParamType)); } } Optional<Method> buildMethodMaybe = ReflectionUtils.getMethods(builder.getClass(), ReflectionUtils.withName("build")) .stream() .findFirst(); if (!buildMethodMaybe.isPresent()) { throw new IllegalStateException( String.format( "Couldn't find builder 'build' method on %s", builder.getClass() ) ); } Method buildMethod = buildMethodMaybe.get(); return buildMethod.invoke(builder); }