/** * Returns a method that matches given name and parameter types. * Returns <code>null</code> if method is not found. */ public MethodDescriptor getMethodDescriptor(String name, Class[] paramTypes) { MethodDescriptor[] methodDescriptors = methodsMap.get(name); if (methodDescriptors == null) { return null; } for (MethodDescriptor methodDescriptor : methodDescriptors) { Method m = methodDescriptor.getMethod(); if (ReflectUtil.compareParameters(m.getParameterTypes(), paramTypes)) { return methodDescriptor; } } return null; }
/** * Returns a method that matches given name and parameter types. * Returns <code>null</code> if method is not found. */ public MethodDescriptor getMethodDescriptor(final String name, final Class[] paramTypes) { MethodDescriptor[] methodDescriptors = methodsMap.get(name); if (methodDescriptors == null) { return null; } for (MethodDescriptor methodDescriptor : methodDescriptors) { Method m = methodDescriptor.getMethod(); if (ClassUtil.compareParameters(m.getParameterTypes(), paramTypes)) { return methodDescriptor; } } return null; }
public DestroyMethodPoint[] resolve(final Class<?> type) { // lookup methods List<DestroyMethodPoint> list = new ArrayList<>(); ClassDescriptor cd = new ClassDescriptor(type, false, false, false, null); MethodDescriptor[] allMethods = cd.getAllMethodDescriptors(); for (MethodDescriptor methodDescriptor : allMethods) { Method method = methodDescriptor.getMethod(); PetiteDestroyMethod petiteDestroyMethod = method.getAnnotation(PetiteDestroyMethod.class); if (petiteDestroyMethod == null) { continue; } if (method.getParameterTypes().length > 0) { throw new PetiteException("Arguments are not allowed for Petite destroy method: " + type.getName() + '#' + method.getName()); } list.add(new DestroyMethodPoint(method)); } DestroyMethodPoint[] methods; if (list.isEmpty()) { methods = DestroyMethodPoint.EMPTY; } else { methods = list.toArray(new DestroyMethodPoint[0]); } return methods; }
/** * Resolves action method for given action class ane method name. */ public Method resolveActionMethod(final Class<?> actionClass, final String methodName) { MethodDescriptor methodDescriptor = ClassIntrospector.get().lookup(actionClass).getMethodDescriptor(methodName, false); if (methodDescriptor == null) { throw new MadvocException("Public method not found: " + actionClass.getSimpleName() + "#" + methodName); } return methodDescriptor.getMethod(); }
public InitMethodPoint[] resolve(final Class<?> type) { // lookup methods List<InitMethodPoint> list = new ArrayList<>(); ClassDescriptor cd = new ClassDescriptor(type, false, false, false, null); MethodDescriptor[] allMethods = cd.getAllMethodDescriptors(); for (MethodDescriptor methodDescriptor : allMethods) { Method method = methodDescriptor.getMethod(); PetiteInitMethod petiteInitMethod = method.getAnnotation(PetiteInitMethod.class); if (petiteInitMethod == null) { continue; } if (method.getParameterTypes().length > 0) { throw new PetiteException("Arguments are not allowed for Petite init method: " + type.getName() + '#' + method.getName()); } int order = petiteInitMethod.order(); list.add(new InitMethodPoint(method, order, petiteInitMethod.invoke())); } InitMethodPoint[] methods; if (list.isEmpty()) { methods = InitMethodPoint.EMPTY; } else { Collections.sort(list); methods = list.toArray(new InitMethodPoint[0]); } return methods; }
Method method = methodDescriptor.getMethod();
/** * Extracts references for given property. Returns {@code null} if property is not marked with an * annotation. */ public BeanReferences readReferenceFromAnnotation(final PropertyDescriptor propertyDescriptor) { final MethodDescriptor writeMethodDescriptor = propertyDescriptor.getWriteMethodDescriptor(); final FieldDescriptor fieldDescriptor = propertyDescriptor.getFieldDescriptor(); PetiteInject ref = null; if (writeMethodDescriptor != null) { ref = writeMethodDescriptor.getMethod().getAnnotation(PetiteInject.class); } if (ref == null && fieldDescriptor != null) { ref = fieldDescriptor.getField().getAnnotation(PetiteInject.class); } if (ref == null) { return null; } BeanReferences reference = null; String name = ref.value().trim(); if (name.length() != 0) { reference = BeanReferences.of(name); } reference = updateReferencesWithDefaultsIfNeeded(propertyDescriptor, reference); reference = reference.removeDuplicateNames(); return reference; }
/** * Registers static method provider. * * @param providerName provider name * @param type class type * @param staticMethodName static method name * @param arguments method argument types */ public void registerPetiteProvider(final String providerName, final Class type, final String staticMethodName, final Class[] arguments) { ClassDescriptor cd = ClassIntrospector.get().lookup(type); MethodDescriptor md = cd.getMethodDescriptor(staticMethodName, arguments, true); if (md == null) { throw new PetiteException("Provider method not found: " + staticMethodName); } ProviderDefinition providerDefinition = new ProviderDefinition(providerName, md.getMethod()); providers.put(providerName, providerDefinition); }
public ValueInjectionPoint[] resolveParamInjectionPoints(final Class type) { final ClassDescriptor cd = ClassIntrospector.get().lookup(type); final List<ValueInjectionPoint> valueInjectionPointList = new ArrayList<>(); for (final PropertyDescriptor pd : cd.getAllPropertyDescriptors()) { final FieldDescriptor fd = pd.getFieldDescriptor(); if (fd != null) { final PetiteValue petiteValue = fd.getField().getAnnotation(PetiteValue.class); if (petiteValue != null) { valueInjectionPointList.add(new ValueInjectionPoint(pd.getName(), petiteValue.value())); continue; } } MethodDescriptor md = pd.getWriteMethodDescriptor(); if (md != null) { final PetiteValue petiteValue = md.getMethod().getAnnotation(PetiteValue.class); if (petiteValue != null) { valueInjectionPointList.add(new ValueInjectionPoint(pd.getName(), petiteValue.value())); continue; } } } return valueInjectionPointList.toArray(new ValueInjectionPoint[0]); }
/** * Process all annotations of provided properties. */ protected void collectPropertyAnnotationChecks(final List<Check> annChecks, final PropertyDescriptor propertyDescriptor) { FieldDescriptor fd = propertyDescriptor.getFieldDescriptor(); if (fd != null) { Annotation[] annotations = fd.getField().getAnnotations(); collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations); } MethodDescriptor md = propertyDescriptor.getReadMethodDescriptor(); if (md != null) { Annotation[] annotations = md.getMethod().getAnnotations(); collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations); } md = propertyDescriptor.getWriteMethodDescriptor(); if (md != null) { Annotation[] annotations = md.getMethod().getAnnotations(); collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations); } }
/** * Builds action runtime configuration on founded action class. * Action classes are annotated with {@link jodd.madvoc.meta.MadvocAction} annotation. */ @SuppressWarnings("NonConstantStringShouldBeStringBuffer") protected void acceptActionClass(final Class<?> actionClass) { if (actionClass == null) { return; } if (!checkClass(actionClass)) { return; } if (actionClass.getAnnotation(MadvocAction.class) == null) { return; } ClassDescriptor cd = ClassIntrospector.get().lookup(actionClass); MethodDescriptor[] allMethodDescriptors = cd.getAllMethodDescriptors(); for (MethodDescriptor methodDescriptor : allMethodDescriptors) { if (!methodDescriptor.isPublic()) { continue; } // just public methods final Method method = methodDescriptor.getMethod(); final boolean hasAnnotation = actionConfigManager.hasActionAnnotationOn(method); if (!hasAnnotation) { continue; } webappConfigurations.add(() -> actionsManager.registerAction(actionClass, method, null)); } }
/** * Registers instance method provider. * * @param providerName provider name * @param beanName bean name * @param methodName instance method name * @param arguments method argument types */ public void registerPetiteProvider(final String providerName, final String beanName, final String methodName, final Class[] arguments) { BeanDefinition beanDefinition = lookupBeanDefinition(beanName); if (beanDefinition == null) { throw new PetiteException("Bean not found: " + beanName); } Class beanType = beanDefinition.type; ClassDescriptor cd = ClassIntrospector.get().lookup(beanType); MethodDescriptor md = cd.getMethodDescriptor(methodName, arguments, true); if (md == null) { throw new PetiteException("Provider method not found: " + methodName); } ProviderDefinition providerDefinition = new ProviderDefinition(providerName, beanName, md.getMethod()); providers.put(providerName, providerDefinition); }
/** * Registers destroy method. * * @param beanName bean name * @param destroyMethodNames destroy method names */ public void registerPetiteDestroyMethods(final String beanName, String... destroyMethodNames) { BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName); ClassDescriptor cd = ClassIntrospector.get().lookup(beanDefinition.type); if (destroyMethodNames == null) { destroyMethodNames = StringPool.EMPTY_ARRAY; } int total = destroyMethodNames.length; DestroyMethodPoint[] destroyMethodPoints = new DestroyMethodPoint[total]; int i; for (i = 0; i < destroyMethodNames.length; i++) { MethodDescriptor md = cd.getMethodDescriptor(destroyMethodNames[i], ClassUtil.EMPTY_CLASS_ARRAY, true); if (md == null) { throw new PetiteException("Destroy method not found: " + beanDefinition.type.getName() + '#' + destroyMethodNames[i]); } destroyMethodPoints[i] = new DestroyMethodPoint(md.getMethod()); } beanDefinition.addDestroyMethodPoints(destroyMethodPoints); }
/** * Registers init method. * * @param beanName bean name * @param invocationStrategy moment of invocation * @param initMethodNames init method names */ public void registerPetiteInitMethods(final String beanName, final InitMethodInvocationStrategy invocationStrategy, String... initMethodNames) { BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName); ClassDescriptor cd = ClassIntrospector.get().lookup(beanDefinition.type); if (initMethodNames == null) { initMethodNames = StringPool.EMPTY_ARRAY; } int total = initMethodNames.length; InitMethodPoint[] initMethodPoints = new InitMethodPoint[total]; int i; for (i = 0; i < initMethodNames.length; i++) { MethodDescriptor md = cd.getMethodDescriptor(initMethodNames[i], ClassUtil.EMPTY_CLASS_ARRAY, true); if (md == null) { throw new PetiteException("Init method not found: " + beanDefinition.type.getName() + '#' + initMethodNames[i]); } initMethodPoints[i] = new InitMethodPoint(md.getMethod(), i, invocationStrategy); } beanDefinition.addInitMethodPoints(initMethodPoints); }
assertEquals("isFlag", propertyDescriptors[0].getReadMethodDescriptor().getMethod().getName()); MethodDescriptor[] mds = cd.getAllMethodDescriptors(); int c = 0; assertEquals("isFlag", propertyDescriptors[0].getReadMethodDescriptor().getMethod().getName()); mds = cd.getAllMethodDescriptors(); c = 0;