List of usage examples for java.lang Class getMethods
@CallerSensitive public Method[] getMethods() throws SecurityException
From source file:com.freetmp.common.util.ClassUtils.java
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) { Assert.notNull(clazz, "Class must not be null"); Assert.notNull(methodName, "Method name must not be null"); if (paramTypes != null) { try {//w w w . j a va 2 s . co m return clazz.getMethod(methodName, paramTypes); } catch (NoSuchMethodException ex) { throw new IllegalStateException("Expected method not found: " + ex); } } else { Set<Method> candidates = new HashSet<Method>(1); Method[] methods = clazz.getMethods(); for (Method method : methods) { if (methodName.equals(method.getName())) { candidates.add(method); } } if (candidates.size() == 1) { return candidates.iterator().next(); } else if (candidates.isEmpty()) { throw new IllegalStateException("Expected method not found: " + clazz + "." + methodName); } else { throw new IllegalStateException("No unique method found: " + clazz + "." + methodName); } } }
From source file:pt.webdetails.cpf.InterPluginCall.java
public void run() { Class<?> classe = null; Method operation = null;/*from w w w . j av a 2s .c o m*/ Object o = null; // try { o = getBeanObject(); classe = o.getClass(); Method[] methods = classe.getMethods(); for (Method m : methods) { if (m.getName().equals(method)) { operation = m; break; } } Annotation[][] params = operation.getParameterAnnotations(); Class<?>[] paramTypes = operation.getParameterTypes(); List<Object> parameters = new ArrayList<Object>(); for (int i = 0; i < params.length; i++) { String paramName = ""; String paramDefaultValue = ""; for (Annotation annotation : params[i]) { String annotationClass = annotation.annotationType().getName(); if (annotationClass == "javax.ws.rs.QueryParam") { QueryParam param = (QueryParam) annotation; paramName = param.value(); } else if (annotationClass == "javax.ws.rs.DefaultValue") { DefaultValue param = (DefaultValue) annotation; paramDefaultValue = param.value(); } else if (annotationClass == "javax.ws.rs.core.Context") { if (paramTypes[i] == HttpServletRequest.class) { CpfHttpServletRequest cpfRequest = (CpfHttpServletRequest) getRequest(); for (Map.Entry<String, Object> entry : requestParameters.entrySet()) { String key = entry.getKey(); Object paramValue = entry.getValue(); if (paramValue instanceof String[]) { String[] lValues = (String[]) paramValue; if (lValues.length == 1) cpfRequest.setParameter(key, lValues[0]); else cpfRequest.setParameter(key, lValues); } else if (paramValue != null) { cpfRequest.setParameter(key, paramValue.toString()); } } parameters.add((HttpServletRequest) cpfRequest); } else if (paramTypes[i] == HttpServletResponse.class) { HttpServletResponse response = (HttpServletResponse) getParameterProviders().get("path") .getParameter("httpresponse"); if (response == null) { response = getResponse(); } parameters.add(response); } } } if (requestParameters.containsKey(paramName)) { Object paramValue = requestParameters.get(paramName); if (paramTypes[i] == int.class) { if (paramValue instanceof String[]) { String[] lValues = (String[]) paramValue; if (lValues.length > 0) paramValue = lValues[0]; else paramValue = null; } int val = Integer.parseInt((String) paramValue); parameters.add(val); } else if (paramTypes[i] == java.lang.Boolean.class || paramTypes[i] == boolean.class) { if (paramValue instanceof String[]) { String[] lValues = (String[]) paramValue; if (lValues.length > 0) paramValue = lValues[0]; else paramValue = null; } boolean val = Boolean.parseBoolean((String) paramValue); parameters.add(val); } else if (paramTypes[i] == java.util.List.class) { List<String> list = new ArrayList<String>(); String[] splittedValues; if (paramValue instanceof String[]) { splittedValues = (String[]) paramValue; } else { splittedValues = ((String) paramValue).split(","); } for (String s : splittedValues) { list.add(s); } parameters.add(list); } else if (paramTypes[i] == java.lang.String.class) { if (paramValue instanceof String[]) { String[] lValues = (String[]) paramValue; if (lValues.length > 0) paramValue = lValues[0]; else paramValue = null; } parameters.add(paramValue); } requestParameters.remove(paramName); } else { if (paramTypes[i] == int.class) { int val = Integer.parseInt(paramDefaultValue); parameters.add(val); } else if (paramTypes[i] == Boolean.class || paramTypes[i] == boolean.class) { boolean val = Boolean.parseBoolean(paramDefaultValue); parameters.add(val); } else if (paramTypes[i] == java.util.List.class) { List<String> list = new ArrayList<String>(); String values = paramDefaultValue; String[] splittedValues = values.split(","); for (String s : splittedValues) { list.add(s); } parameters.add(list); } else if (paramTypes[i] == java.lang.String.class) { parameters.add(paramDefaultValue); } } } try { objectResponse = operation.invoke(o, parameters.toArray()); } catch (IllegalAccessException ex) { logger.error("", ex); } catch (IllegalArgumentException ex) { logger.error("", ex); } catch (InvocationTargetException ex) { logger.error("", ex); } catch (Exception ex) { logger.error("", ex); } }
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 ww .j ava2s. c o m*/ 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:edu.cmu.tetrad.util.TetradSerializableUtils.java
/** * @return a reference to the public static serializableInstance() method of * clazz, if there is one; otherwise, returns null. *///from w w w. ja v a 2 s. c om private Method serializableInstanceMethod(Class clazz) { Method[] methods = clazz.getMethods(); for (Method method : methods) { if ("serializableInstance".equals(method.getName())) { Class[] parameterTypes = method.getParameterTypes(); if (!(parameterTypes.length == 0)) { continue; } if (!(Modifier.isStatic(method.getModifiers()))) { continue; } if (Modifier.isAbstract(method.getModifiers())) { continue; } return method; } } return null; }
From source file:com.evolveum.midpoint.prism.marshaller.PrismBeanInspector.java
private <T> Method findSetterUncached(Class<T> classType, String fieldName) { String setterName = getSetterName(fieldName); for (Method method : classType.getMethods()) { if (!method.getName().equals(setterName)) { continue; }/*from w ww . j av a 2s. c om*/ Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length != 1) { continue; } Class<?> setterType = parameterTypes[0]; if (setterType.equals(Object.class) || Node.class.isAssignableFrom(setterType)) { // Leave for second pass, let's try find a better setter continue; } return method; } // Second pass for (Method method : classType.getMethods()) { if (!method.getName().equals(setterName)) { continue; } Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length != 1) { continue; } return method; } return null; }
From source file:edu.cmu.tetradapp.util.TetradSerializableUtils.java
/** * Returns a reference to the public static serializableInstance() method of * clazz, if there is one; otherwise, returns null. *//*from w ww .j a v a2 s . c o m*/ public Method serializableInstanceMethod(Class clazz) { Method[] methods = clazz.getMethods(); for (Method method : methods) { if ("serializableInstance".equals(method.getName())) { Class[] parameterTypes = method.getParameterTypes(); if (!(parameterTypes.length == 0)) { continue; } if (!(Modifier.isStatic(method.getModifiers()))) { continue; } if (Modifier.isAbstract(method.getModifiers())) { continue; } return method; } } return null; }
From source file:freemarker.ext.dump.BaseDumpDirective.java
private Map<String, Object> getObjectDump(TemplateHashModelEx model, Object object) throws TemplateModelException { Map<String, Object> map = new HashMap<String, Object>(); map.put(Key.TYPE.toString(), object.getClass().getName()); if (object instanceof java.lang.reflect.Method) return map; // Compile the collections of properties and methods available to the template SortedMap<String, Object> properties = new TreeMap<String, Object>(); SortedMap<String, Object> methods = new TreeMap<String, Object>(); // keys() gets only values visible to template based on the BeansWrapper used. // Note: if the BeansWrapper exposure level > BeansWrapper.EXPOSE_PROPERTIES_ONLY, // keys() returns both method and property name for any available method with no // parameters: e.g., both name and getName(). We are going to eliminate the latter. TemplateCollectionModel keys = model.keys(); TemplateModelIterator iModel = keys.iterator(); // Create a Set from keys so we can use the Set API. Set<String> keySet = new HashSet<String>(); while (iModel.hasNext()) { String key = iModel.next().toString(); keySet.add(key);//from w w w. j a v a 2 s. co m } if (keySet.size() > 0) { Class<?> cls = object.getClass(); Method[] classMethods = cls.getMethods(); // Iterate through the methods rather than the keys, so that we can remove // some keys based on reflection on the methods. We also want to remove duplicates // like name/getName - we'll keep only the first form. for (Method method : classMethods) { if ("declaringClass".equals(method.getName())) continue; // Eliminate methods declared on Object // and other unusual places that can cause problems. Class<?> c = method.getDeclaringClass(); if (c == null || c.equals(java.lang.Object.class) || c.equals(java.lang.reflect.Constructor.class) || c.equals(java.lang.reflect.Field.class)) continue; if (c.getPackage().getName().startsWith("sun.") || c.getPackage().getName().startsWith("java.lang") || c.getPackage().getName().startsWith("java.security")) continue; // Eliminate deprecated methods if (method.isAnnotationPresent(Deprecated.class)) { continue; } // Include only methods included in keys(). This factors in visibility // defined by the model's BeansWrapper. String methodName = method.getName(); Matcher matcher = PROPERTY_NAME_PATTERN.matcher(methodName); // If the method name starts with "get" or "is", check if it's available // as a property if (matcher.find()) { String propertyName = getPropertyName(methodName); // The method is available as a property if (keySet.contains(propertyName)) { try { TemplateModel value = model.get(propertyName); properties.put(propertyName, getDump(value)); } catch (Throwable th) { log.error("problem dumping " + propertyName + " on " + object.getClass().getName() + " declared in " + c.getName(), th); } continue; } } // Else look for the entire methodName in the key set, to include // those that are exposed as methods rather than properties. if (keySet.contains(methodName)) { String methodDisplayName = getMethodDisplayName(method); // If no arguments, invoke the method to get the result if (methodDisplayName.endsWith("()")) { SimpleMethodModel methodModel = (SimpleMethodModel) model.get(methodName); try { Object result = methodModel.exec(null); ObjectWrapper wrapper = getWrapper(model); TemplateModel wrappedResult = wrapper.wrap(result); methods.put(methodDisplayName, getDump(wrappedResult)); } catch (Exception e) { log.error(e, e); } // Else display method name, parameter types, and return type } else { String returnTypeName = getReturnTypeName(method); Map<String, String> methodValue = new HashMap<String, String>(); if (!returnTypeName.equals("void")) { methodValue.put(Key.TYPE.toString(), returnTypeName); } methods.put(methodDisplayName, methodValue); } } } } Map<String, Object> objectValue = new HashMap<String, Object>(2); objectValue.put(Key.PROPERTIES.toString(), properties); objectValue.put(Key.METHODS.toString(), methods); map.put(Key.VALUE.toString(), objectValue); return map; }
From source file:com.google.gdt.eclipse.designer.uibinder.model.util.UiChildSupport.java
/** * @return the {@link Description}s for methods of given {@link WidgetInfo}. */// w w w .j a v a2 s .c om private List<Description> getDescriptions(WidgetInfo widget) throws Exception { Class<?> componentClass = widget.getDescription().getComponentClass(); List<Description> descriptions = m_descriptions.get(componentClass); if (descriptions == null) { descriptions = Lists.newArrayList(); for (Method method : componentClass.getMethods()) { Annotation[] annotations = method.getAnnotations(); for (Annotation annotation : annotations) { if (ReflectionUtils.isSuccessorOf(annotation, UI_CHILD)) { descriptions.add(new Description(method, annotation)); } } } m_descriptions.put(componentClass, descriptions); } return descriptions; }
From source file:com.bugvm.maven.surefire.BugVMSurefireProvider.java
private String[] testToRunToClassPatterns(Class<?> clazz) { List<String> result = new ArrayList<>(); if (!StringUtils.isBlank(this.requestedTestMethod)) { // Copied from JUnit4Provider String actualTestMethod = getMethod(clazz, this.requestedTestMethod); String[] testMethods = StringUtils.split(actualTestMethod, "+"); Method[] methods = clazz.getMethods(); for (Method method : methods) { for (String testMethod : testMethods) { if (SelectorUtils.match(testMethod, method.getName())) { result.add(clazz.getName() + "#" + method.getName()); }/*from w w w.j ava 2s.c om*/ } } } else { result.add(clazz.getName()); } return result.toArray(new String[result.size()]); }
From source file:org.crazydog.util.spring.ClassUtils.java
/** * Determine whether the given class has a public method with the given signature, * and return it if available (else throws an {@code IllegalStateException}). * <p>In case of any signature specified, only returns the method if there is a * unique candidate, i.e. a single public method with the specified name. * <p>Essentially translates {@code NoSuchMethodException} to {@code IllegalStateException}. * @param clazz the clazz to analyze// w w w .ja va2 s . c o m * @param methodName the name of the method * @param paramTypes the parameter types of the method * (may be {@code null} to indicate any signature) * @return the method (never {@code null}) * @throws IllegalStateException if the method has not been found * @see Class#getMethod */ public static Method getMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) { org.springframework.util.Assert.notNull(clazz, "Class must not be null"); org.springframework.util.Assert.notNull(methodName, "Method name must not be null"); if (paramTypes != null) { try { return clazz.getMethod(methodName, paramTypes); } catch (NoSuchMethodException ex) { throw new IllegalStateException("Expected method not found: " + ex); } } else { Set<Method> candidates = new HashSet<Method>(1); Method[] methods = clazz.getMethods(); for (Method method : methods) { if (methodName.equals(method.getName())) { candidates.add(method); } } if (candidates.size() == 1) { return candidates.iterator().next(); } else if (candidates.isEmpty()) { throw new IllegalStateException("Expected method not found: " + clazz + "." + methodName); } else { throw new IllegalStateException("No unique method found: " + clazz + "." + methodName); } } }