List of usage examples for java.lang.reflect Method getParameterCount
public int getParameterCount()
From source file:com.phoenixnap.oss.ramlapisync.parser.ResourceParser.java
/** * Extracts the TOs and other parameters from a method and will convert into JsonSchema for inclusion in the body * TODO refactor this code structure//from ww w . j av a 2s . com * * @param apiAction The Verb of the action to be added * @param method The method to be inspected * @param parameterComments The parameter comments associated with these parameters * @return A map of supported mime types for the request */ protected Map<String, MimeType> extractRequestBodyFromMethod(ActionType apiAction, Method method, Map<String, String> parameterComments) { if (!(doesActionTypeSupportRequestBody(apiAction)) || method.getParameterCount() == 0) { return Collections.emptyMap(); } String comment = null; List<ApiParameterMetadata> apiParameters = getApiParameters(method, false, true); if (apiParameters.size() == 0) { //We only have url params it seems return Collections.emptyMap(); } Pair<String, MimeType> schemaAndMime = extractRequestBody(method, parameterComments, comment, apiParameters); return Collections.singletonMap(schemaAndMime.getFirst(), schemaAndMime.getSecond()); }
From source file:com.github.tddts.jet.view.fx.spring.DialogProvider.java
private void processInitMethods(Class<?> type, Dialog<?> dialog, Object[] args) { List<Method> initMethods = dialogInitCache.get(type); // Add dialog init methods to cache if (initMethods == null) { initMethods = new ArrayList<>(); for (Method method : type.getDeclaredMethods()) { if (method.isAnnotationPresent(FxDialogInit.class)) initMethods.add(method); }/*from w ww .j ava 2s . c o m*/ dialogInitCache.put(type, initMethods); } // Invoke init methods try { for (Method method : initMethods) { method.setAccessible(true); if (method.getParameterCount() == 0) { method.invoke(dialog, EMPTY_ARGS); } else { method.invoke(dialog, args); } } } catch (IllegalAccessException | InvocationTargetException e) { throw new DialogException("Could not initialize dialog!", e); } }
From source file:org.springframework.beans.ExtendedBeanInfo.java
private void handleCandidateWriteMethod(Method method) throws IntrospectionException { int nParams = method.getParameterCount(); String propertyName = propertyNameFor(method); Class<?> propertyType = method.getParameterTypes()[nParams - 1]; PropertyDescriptor existingPd = findExistingPropertyDescriptor(propertyName, propertyType); if (nParams == 1) { if (existingPd == null) { this.propertyDescriptors.add(new SimplePropertyDescriptor(propertyName, null, method)); } else {// w ww . ja v a2 s. c om existingPd.setWriteMethod(method); } } else if (nParams == 2) { if (existingPd == null) { this.propertyDescriptors .add(new SimpleIndexedPropertyDescriptor(propertyName, null, null, null, method)); } else if (existingPd instanceof IndexedPropertyDescriptor) { ((IndexedPropertyDescriptor) existingPd).setIndexedWriteMethod(method); } else { this.propertyDescriptors.remove(existingPd); this.propertyDescriptors.add(new SimpleIndexedPropertyDescriptor(propertyName, existingPd.getReadMethod(), existingPd.getWriteMethod(), null, method)); } } else { throw new IllegalArgumentException("Write method must have exactly 1 or 2 parameters: " + method); } }
From source file:com.phoenixnap.oss.ramlapisync.parser.ResourceParser.java
/** * Extracts parameters from a method call and attaches these with the comments extracted from the javadoc * //w ww.j av a 2 s . c om * @param apiAction The Verb of the action containing these parametes * @param method The method to inspect * @param parameterComments The parameter comments associated with these parameters * @return A collection of parameters keyed by name */ protected Map<String, QueryParameter> extractQueryParameters(ActionType apiAction, Method method, Map<String, String> parameterComments) { // Since POST requests have a body we choose to keep all request data in one place as much as possible if (apiAction.equals(ActionType.POST) || method.getParameterCount() == 0) { return Collections.emptyMap(); } Map<String, QueryParameter> queryParams = new LinkedHashMap<>(); for (Parameter param : method.getParameters()) { if (isQueryParameter(param)) { // Lets skip resourceIds since these are going to be going in the URL ParamType simpleType = SchemaHelper.mapSimpleType(param.getType()); if (simpleType == null) { queryParams.putAll(SchemaHelper.convertClassToQueryParameters(param, javaDocs.getJavaDoc(param.getType()))); } else { // Check if we have comments String paramComment = parameterComments.get(param.getName()); queryParams.putAll(SchemaHelper.convertParameterToQueryParameter(param, paramComment)); } } } return queryParams; }
From source file:org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsStreamListenerSetupMethodOrchestrator.java
private boolean methodParameterSupports(Method method) { boolean supports = false; for (int i = 0; i < method.getParameterCount(); i++) { MethodParameter methodParameter = MethodParameter.forExecutable(method, i); Class<?> parameterType = methodParameter.getParameterType(); if (parameterType.equals(KStream.class) || parameterType.equals(KTable.class) || parameterType.equals(GlobalKTable.class)) { supports = true;/*from w ww . j a v a 2 s. c o m*/ } } return supports; }
From source file:org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.java
protected void processScheduled(Scheduled scheduled, Method method, Object bean) { try {// ww w . java 2s . c om Assert.isTrue(method.getParameterCount() == 0, "Only no-arg methods may be annotated with @Scheduled"); Method invocableMethod = AopUtils.selectInvocableMethod(method, bean.getClass()); Runnable runnable = new ScheduledMethodRunnable(bean, invocableMethod); boolean processedSchedule = false; String errorMessage = "Exactly one of the 'cron', 'fixedDelay(String)', or 'fixedRate(String)' attributes is required"; Set<ScheduledTask> tasks = new LinkedHashSet<>(4); // Determine initial delay long initialDelay = scheduled.initialDelay(); String initialDelayString = scheduled.initialDelayString(); if (StringUtils.hasText(initialDelayString)) { Assert.isTrue(initialDelay < 0, "Specify 'initialDelay' or 'initialDelayString', not both"); if (this.embeddedValueResolver != null) { initialDelayString = this.embeddedValueResolver.resolveStringValue(initialDelayString); } if (StringUtils.hasLength(initialDelayString)) { try { initialDelay = parseDelayAsLong(initialDelayString); } catch (RuntimeException ex) { throw new IllegalArgumentException("Invalid initialDelayString value \"" + initialDelayString + "\" - cannot parse into long"); } } } // Check cron expression String cron = scheduled.cron(); if (StringUtils.hasText(cron)) { String zone = scheduled.zone(); if (this.embeddedValueResolver != null) { cron = this.embeddedValueResolver.resolveStringValue(cron); zone = this.embeddedValueResolver.resolveStringValue(zone); } if (StringUtils.hasLength(cron)) { Assert.isTrue(initialDelay == -1, "'initialDelay' not supported for cron triggers"); processedSchedule = true; TimeZone timeZone; if (StringUtils.hasText(zone)) { timeZone = StringUtils.parseTimeZoneString(zone); } else { timeZone = TimeZone.getDefault(); } tasks.add(this.registrar .scheduleCronTask(new CronTask(runnable, new CronTrigger(cron, timeZone)))); } } // At this point we don't need to differentiate between initial delay set or not anymore if (initialDelay < 0) { initialDelay = 0; } // Check fixed delay long fixedDelay = scheduled.fixedDelay(); if (fixedDelay >= 0) { Assert.isTrue(!processedSchedule, errorMessage); processedSchedule = true; tasks.add(this.registrar .scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay))); } String fixedDelayString = scheduled.fixedDelayString(); if (StringUtils.hasText(fixedDelayString)) { if (this.embeddedValueResolver != null) { fixedDelayString = this.embeddedValueResolver.resolveStringValue(fixedDelayString); } if (StringUtils.hasLength(fixedDelayString)) { Assert.isTrue(!processedSchedule, errorMessage); processedSchedule = true; try { fixedDelay = parseDelayAsLong(fixedDelayString); } catch (RuntimeException ex) { throw new IllegalArgumentException("Invalid fixedDelayString value \"" + fixedDelayString + "\" - cannot parse into long"); } tasks.add(this.registrar .scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay))); } } // Check fixed rate long fixedRate = scheduled.fixedRate(); if (fixedRate >= 0) { Assert.isTrue(!processedSchedule, errorMessage); processedSchedule = true; tasks.add( this.registrar.scheduleFixedRateTask(new FixedRateTask(runnable, fixedRate, initialDelay))); } String fixedRateString = scheduled.fixedRateString(); if (StringUtils.hasText(fixedRateString)) { if (this.embeddedValueResolver != null) { fixedRateString = this.embeddedValueResolver.resolveStringValue(fixedRateString); } if (StringUtils.hasLength(fixedRateString)) { Assert.isTrue(!processedSchedule, errorMessage); processedSchedule = true; try { fixedRate = parseDelayAsLong(fixedRateString); } catch (RuntimeException ex) { throw new IllegalArgumentException("Invalid fixedRateString value \"" + fixedRateString + "\" - cannot parse into long"); } tasks.add(this.registrar .scheduleFixedRateTask(new FixedRateTask(runnable, fixedRate, initialDelay))); } } // Check whether we had any attribute set Assert.isTrue(processedSchedule, errorMessage); // Finally register the scheduled tasks synchronized (this.scheduledTasks) { Set<ScheduledTask> registeredTasks = this.scheduledTasks.get(bean); if (registeredTasks == null) { registeredTasks = new LinkedHashSet<>(4); this.scheduledTasks.put(bean, registeredTasks); } registeredTasks.addAll(tasks); } } catch (IllegalArgumentException ex) { throw new IllegalStateException( "Encountered invalid @Scheduled method '" + method.getName() + "': " + ex.getMessage()); } }
From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java
/** * Calls a method with a set of arguments. After execution the stack may have an extra item pushed on it: the object that was returned * by this method (if any)./*w w w. j a v a 2s . c o m*/ * @param method method to call * @param args method argument instruction lists -- each instruction list must leave one item on the stack of the type expected * by the method (note that if this is a non-static method, the first argument must always evaluate to the "this" pointer/reference) * @return instructions to invoke a method * @throws NullPointerException if any argument is {@code null} or array contains {@code null} * @throws IllegalArgumentException if the length of {@code args} doesn't match the number of parameters in {@code method} */ public static InsnList call(Method method, InsnList... args) { Validate.notNull(method); Validate.notNull(args); Validate.noNullElements(args); InsnList ret = new InsnList(); for (InsnList arg : args) { ret.add(arg); } Type clsType = Type.getType(method.getDeclaringClass()); Type methodType = Type.getType(method); if ((method.getModifiers() & Modifier.STATIC) == Modifier.STATIC) { Validate.isTrue(method.getParameterCount() == args.length); ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, clsType.getInternalName(), method.getName(), methodType.getDescriptor(), false)); } else if (method.getDeclaringClass().isInterface()) { Validate.isTrue(method.getParameterCount() + 1 == args.length); ret.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, clsType.getInternalName(), method.getName(), methodType.getDescriptor(), true)); } else { Validate.isTrue(method.getParameterCount() + 1 == args.length); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, clsType.getInternalName(), method.getName(), methodType.getDescriptor(), false)); } return ret; }
From source file:sh.scrap.scrapper.functions.StringFunctionFactory.java
@Override public DataScrapperFunction create(String name, DataScrapperFunctionLibrary library, Object mainArgument, Map<String, Object> annotations) { return context -> subscription -> { Class<?> targetClass = stringUtils.contains(name) ? StringUtils.class : StringEscapeUtils.class; try {// w ww. j av a 2s .c o m Method method = findMethod(targetClass, name, mainArgument, annotations); if (method == null) { subscription.onError(new IllegalArgumentException("Unknown function [string:" + name + "]")); subscription.onComplete(); return; } String[] paramIndexes = discoverer.getParameterNames(method); subscription.onSubscribe(new Subscription() { @Override public void request(long n) { Object data = context.data(); context.objectProcessed(data); Object[] invokeArgs = new Object[method.getParameterCount()]; invokeArgs[0] = data; if (method.getParameterCount() > 1) invokeArgs[1] = mainArgument; for (int idx = method.getParameterCount() > 1 ? 2 : 1; idx < paramIndexes.length; idx++) invokeArgs[idx] = annotations.get(paramIndexes[idx]); try { try { data = method.invoke(method.getDeclaringClass(), invokeArgs); } catch (InvocationTargetException e) { throw e.getTargetException(); } } catch (Throwable e) { subscription.onError(e); subscription.onComplete(); return; } subscription.onNext(context.withData(data)); subscription.onComplete(); } @Override public void cancel() { } }); } catch (IllegalArgumentException e) { subscription.onError(e); subscription.onComplete(); } }; }
From source file:jp.ac.tohoku.ecei.sb.metabolome.lims.gui.MainWindowController.java
@SuppressWarnings("unchecked") private void initializeTable(TableView tableView, Class clazz) { ArrayList<TableColumn> columns = new ArrayList<>(); HashSet<String> methodNames = new HashSet<>(); method: for (Method one : clazz.getMethods()) { for (String black : new String[] { "getClass", "getAttributeKeySet" }) if (one.getName().equals(black)) continue method; if (!one.getName().startsWith("get") && !one.getName().startsWith("is")) continue; if (one.getParameterCount() != 0) continue; if (methodNames.contains(one.getName())) continue; methodNames.add(one.getName());//from w w w. j ava 2s . c om TableColumn oneColumn = new TableColumn(); String name = one.getName().substring(3); if (one.getName().startsWith("is")) { name = one.getName().substring(2); } oneColumn.setText(name); oneColumn.setCellValueFactory(new PropertyValueFactory(name)); if (one.getName().equals("getId")) columns.add(0, oneColumn); else columns.add(oneColumn); } tableView.getColumns().addAll(columns.toArray()); }
From source file:com.googlecode.jdbcproc.daofactory.impl.block.service.ParametersSetterBlockServiceImpl.java
private List<IParametersSetterBlock> addListBlocks(JdbcTemplate jdbcTemplate, ParameterConverterService converterService, Method method, StoredProcedureInfo procedureInfo, List<IParametersSetterBlock> blocks) { List<IParametersSetterBlock> result = new ArrayList<>(blocks); for (int i = 1; i < method.getParameterCount(); i++) { result.add(createParametersSetterBlockList(jdbcTemplate, converterService, method, i, procedureInfo)); }//from www. j ava 2 s .c o m // return Collections.unmodifiableList(result); return result; }