List of usage examples for java.lang.reflect Method getName
@Override
public String getName()
From source file:MyClass.java
public static ArrayList<String> getMethodsDesciption(Method[] methods) { ArrayList<String> methodList = new ArrayList<>(); for (Method m : methods) { String modifiers = getModifiers(m); Class returnType = m.getReturnType(); String returnTypeName = returnType.getSimpleName(); String methodName = m.getName(); String params = getParameters(m).toString(); String throwsClause = getExceptionList(m).toString(); methodList.add(/*from w w w.j av a 2 s . c o m*/ modifiers + " " + returnTypeName + " " + methodName + "(" + params + ") " + throwsClause); } return methodList; }
From source file:grails.plugin.springsecurity.web.access.GrailsWebInvocationPrivilegeEvaluator.java
static HttpServletRequest createInstance(final String contextPath, final String httpMethod, final String requestURI) { final Map<String, Object> attributes = new HashMap<String, Object>(); return (HttpServletRequest) Proxy.newProxyInstance(HttpServletRequest.class.getClassLoader(), new Class[] { HttpServletRequest.class }, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) { String methodName = method.getName(); if ("getContextPath".equals(methodName)) return contextPath; if ("getMethod".equals(methodName)) return httpMethod; if ("getRequestURI".equals(methodName)) return requestURI; if ("setAttribute".equals(methodName)) { attributes.put((String) args[0], args[1]); return null; }// www .ja v a 2 s . co m if ("getAttribute".equals(methodName)) { return attributes.get(args[0]); } if ("getProtocol".equals(methodName) || "getScheme".equals(methodName)) return "http"; if ("getServerName".equals(methodName)) return "localhost"; if ("getServerPort".equals(methodName)) return 8080; if (methodName.startsWith("is")) return false; if ("getParameterMap".equals(methodName)) return Collections.emptyMap(); if ("getAttributeNames".equals(methodName) || "getHeaderNames".equals(methodName) || "getHeaders".equals(methodName) || "getLocales".equals(methodName) || "getParameterNames".equals(methodName)) { return Collections.enumeration(Collections.emptySet()); } return null; } }); }
From source file:com.puppycrawl.tools.checkstyle.doclets.TokenTypesDocletTest.java
private static Method getMethodGetRootDocImplByReflection() throws ClassNotFoundException { Method result = null;//w ww . ja v a2 s.c om final Class<?> javadocToolClass = Class.forName("com.sun.tools.javadoc.JavadocTool"); final Method[] methods = javadocToolClass.getMethods(); for (Method method : methods) { if ("getRootDocImpl".equals(method.getName())) { result = method; } } return result; }
From source file:cz.jirutka.validator.collection.internal.AnnotationUtils.java
/** * Creates instance (proxy) of the specified annotation with the given * attributes.//from w ww .j a v a 2s . c o m * * @param annotationType The annotation's class. * @param attributes A map with attribute values for the annotation to be created. * @param <T> The type of the annotation. * * @return An instance of the annotation. * @throws IllegalArgumentException if some attribute has wrong type or * a required attribute is missing. */ public static <T extends Annotation> T createAnnotation(Class<T> annotationType, Map<String, Object> attributes) { // check if given attributes are defined in annotation for (Iterator<String> it = attributes.keySet().iterator(); it.hasNext();) { String name = it.next(); Object value = attributes.get(name); if (value == null) { LOG.warn("Attribute's value must not be null; will be ignored"); it.remove(); continue; } if (!hasAttribute(annotationType, name)) { LOG.warn("Annotation {} does not define attribute: {}; will be ignored", annotationType.getName(), name); it.remove(); continue; } Class<?> attrType = getAttributeType(annotationType, name); Validate.isTrue(isAssignable(value.getClass(), attrType), "Attribute '%s' expects %s, but given: %s (%s)", name, attrType.getName(), value, value.getClass().getName()); } // check if required attributes are given for (Method m : annotationType.getDeclaredMethods()) { Validate.isTrue(attributes.containsKey(m.getName()) || m.getDefaultValue() != null, "Missing required attribute: %s", m.getName()); } AnnotationDescriptor<T> descriptor = AnnotationDescriptor.getInstance(annotationType, attributes); return AnnotationFactory.create(descriptor); }
From source file:Util.java
private static void fillGetterMethods(Class<?> pojoClass, Map<String, Method> baseMap) { if (pojoClass.getSuperclass() != Object.class) fillGetterMethods(pojoClass.getSuperclass(), baseMap); Method[] methods = pojoClass.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { Method m = methods[i]; if (!Modifier.isStatic(m.getModifiers()) && m.getParameterTypes().length == 0 && m.getReturnType() != null && Modifier.isPublic(m.getModifiers())) { String name = m.getName(); if (name.startsWith(IS)) baseMap.put(toProperty(IS.length(), name), m); else if (name.startsWith(GET)) baseMap.put(toProperty(GET.length(), name), m); }// ww w . j a v a2 s.co m } }
From source file:ReflectionHelper.java
public static Object getValue(Member member, Object object) { Object value = null;/* w w w . j a va 2 s . c o m*/ if (member instanceof Method) { Method method = (Method) member; try { value = method.invoke(object); } catch (IllegalAccessException e) { throw new RuntimeException("Unable to access " + method.getName(), e); } catch (InvocationTargetException e) { throw new RuntimeException("Unable to access " + method.getName(), e); } } else if (member instanceof Field) { Field field = (Field) member; try { value = field.get(object); } catch (IllegalAccessException e) { throw new RuntimeException("Unable to access " + field.getName(), e); } } return value; }
From source file:com.relicum.ipsum.Reflection.ReflectionUtil.java
/** * Gets a {@link Method} in a given {@link Class} object. * * @param clazz Class object/*from w w w . j a v a 2 s .co m*/ * @param name Method name * @return The method, or null if none exists */ public static final Method getMethod(Class<?> clazz, String name) { Validate.notNull(clazz, "clazz cannot be null!"); Validate.notNull(name, "name cannot be null!"); for (Method method : clazz.getMethods()) { if (method.getName().equals(name)) return method; } return null; }
From source file:com.yahoo.sql4d.sql4ddriver.Util.java
public static <T extends DruidBaseBean> void printTable(List<T> baseAllRows) { if (baseAllRows == null || baseAllRows.isEmpty()) { return;//from w w w . jav a2s . co m } List<Method> setters = getAllSetters(baseAllRows.get(0).getClass()); for (Method setter : setters) { printf("%-10s |", setter.getName().substring(3)); } newLine(); List<Method> getters = getAllGetters(baseAllRows.get(0).getClass()); for (T row : baseAllRows) { for (Method getter : getters) { try { printf("%-10s |", getter.invoke(row)); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); } } newLine(); } }
From source file:com.evolveum.midpoint.util.ReflectionUtil.java
/** * Rough lookup of a compatible method. It takes first method with matching name, number of parameters and compatible * parameter values. It is not perfect, e.g. it cannot select foo(String) instead of foo(Object). But it is better than * nothing. And stock Java reflection has really nothing. *//*w w w.ja v a 2 s. c o m*/ private static Method findMethodCompatible(Object object, String methodName, List<?> argList) throws SecurityException { for (Method method : object.getClass().getMethods()) { if (method.getName().equals(methodName)) { Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length == argList.size()) { boolean wrong = false; for (int i = 0; i < parameterTypes.length; i++) { Object arg = argList.get(i); if (arg == null) { // null argument matches any parameter type } else { if (!parameterTypes[i].isAssignableFrom(arg.getClass())) { wrong = true; break; } } } if (!wrong) { // We got it. We have compatible signature here. return method; } } } } return null; }
From source file:com.relicum.ipsum.Reflection.ReflectionUtil.java
/** * Gets a {@link Method} in a given {@link Class} object with the specified * arguments.//from w ww.j ava 2 s . c o m * * @param clazz Class object * @param name Method name * @param args Arguments * @return The method, or null if none exists */ public static final Method getMethod(Class<?> clazz, String name, Class<?>... args) { Validate.notNull(clazz, "clazz cannot be null!"); Validate.notNull(name, "name cannot be null!"); if (args == null) args = new Class<?>[0]; for (Method method : clazz.getMethods()) { if (method.getName().equals(name) && Arrays.equals(args, method.getParameterTypes())) return method; } return null; }